def _messagetests(self, d): ''' Run tests on a message to see if a specific one has been passed. If so, mark the test passed. :param bytes d: a data packet from the queue ''' if 'TERM' in str(d): printM('Got TERM message...', sender=self.sender) t.TEST['c_TERM'][1] = True self.alive = False elif 'ALARM' in str(d): printM('Got ALARM message with time %s' % (helpers.fsec(helpers.get_msg_time(d))), sender=self.sender) t.TEST['c_ALARM'][1] = True elif 'RESET' in str(d): printM('Got RESET message with time %s' % (helpers.fsec(helpers.get_msg_time(d))), sender=self.sender) t.TEST['c_RESET'][1] = True elif 'IMGPATH' in str(d): printM('Got IMGPATH message with time %s' % (helpers.fsec(helpers.get_msg_time(d))), sender=self.sender) printM('and path %s' % (helpers.get_msg_path(d)), sender=self.sender) t.TEST['c_IMGPATH'][1] = True
def _when_alarm(self, d): ''' Send a telegram in an alert scenario. :param bytes d: queue message ''' event_time = helpers.fsec(helpers.get_msg_time(d)) self.last_event_str = '%s' % (event_time.strftime(self.fmt)[:22]) message = '%s %s UTC - %s' % (self.message0, self.last_event_str, self.livelink) response = None try: printM('Sending alert...', sender=self.sender) response = self.telegram.sendMessage(chat_id=self.chat_id, text=message) printM('Sent Telegram: %s' % (message), sender=self.sender) except Exception as e: printE('Could not send alert - %s' % (e)) try: printE('Waiting 5 seconds and trying to send again...', sender=self.sender, spaces=True) time.sleep(5) self.auth() response = self.telegram.sendMessage(chat_id=self.chat_id, text=message) printM('Sent Telegram: %s' % (message), sender=self.sender) except Exception as e: printE('Could not send alert - %s' % (e)) response = None
def _is_trigger(self): ''' Figures out it there's a trigger active. ''' if self.stalta.max() > self.thresh: if not self.exceed: # raise a flag that the Producer can read and modify self.alarm = helpers.fsec(self.stream[0].stats.starttime + timedelta(seconds= trigger_onset(self.stalta, self.thresh, self.reset)[-1][0] * self.stream[0].stats.delta)) self.exceed = True # the state machine; this one should not be touched from the outside, otherwise bad things will happen print() printM('Trigger threshold of %s exceeded at %s' % (self.thresh, self.alarm.strftime('%Y-%m-%d %H:%M:%S.%f')[:22]), self.sender) printM('Trigger will reset when STA/LTA goes below %s...' % self.reset, sender=self.sender) COLOR['current'] = COLOR['purple'] if self.testing: TEST['c_alerton'][1] = True else: pass if self.stalta.max() > self.maxstalta: self.maxstalta = self.stalta.max() else: if self.exceed: if self.stalta[-1] < self.reset: self.alarm_reset = helpers.fsec(self.stream[0].stats.endtime) # lazy; effective self.exceed = False print() printM('Max STA/LTA ratio reached in alarm state: %s' % (round(self.maxstalta, 3)), self.sender) printM('Earthquake trigger reset and active again at %s' % ( self.alarm_reset.strftime('%Y-%m-%d %H:%M:%S.%f')[:22]), self.sender) self.maxstalta = 0 COLOR['current'] = COLOR['green'] if self.testing: TEST['c_alertoff'][1] = True else: pass
def _when_alarm(self, d): ''' Send a tweet when you get an ``ALARM`` message. :param bytes d: queue message ''' event_time = helpers.fsec(helpers.get_msg_time(d)) self.last_event_str = '%s' % (event_time.strftime(self.fmt)[:22]) message = '%s %s UTC%s - %s' % (self.message0, self.last_event_str, self.extra_text, self.livelink) response = None try: printM('Tweet: %s' % (message), sender=self.sender) if not self.testing: response = self.twitter.update_status( status=message, lat=rs.inv[0][0].latitude, long=rs.inv[0][0].longitude, geo_enabled=True, display_coordinates=True) # location will only stick to tweets on accounts that have location enabled in Settings url = 'https://twitter.com/%s/status/%s' % ( response['user']['screen_name'], response['id_str']) printM('Tweet URL: %s' % url) if self.testing: TEST['c_tweet'][1] = True except Exception as e: printE('could not send alert tweet - %s' % (e)) try: printE('Waiting 5 seconds and trying to send tweet again...', sender=self.sender, spaces=True) time.sleep(5.1) printM('Tweet: %s' % (message), sender=self.sender) if not self.testing: self.auth() response = self.twitter.update_status( status=message, lat=rs.inv[0][0].latitude, long=rs.inv[0][0].longitude, geo_enabled=True, display_coordinates=True) # location will only stick to tweets on accounts that have location enabled in Settings url = 'https://twitter.com/%s/status/%s' % ( response['user']['screen_name'], response['id_str']) printM('Tweet URL: %s' % url) except Exception as e: printE('could not send alert tweet - %s' % (e)) response = None self.last_message = message
def getq(self): ''' Get data from the queue and test for whether it has certain strings. ALARM and TERM both trigger specific behavior. ALARM messages cause the event counter to increment, and if :py:data:`screencap==True` then aplot image will be saved when the event is :py:data:`self.save_pct` of the way across the plot. ''' d = self.queue.get() self.queue.task_done() if 'TERM' in str(d): plt.close() if 'SELF' in str(d): printM('Plot has been closed, plot thread will exit.', self.sender) self.alive = False rs.producer = False elif 'ALARM' in str(d): self.events += 1 # add event to count self.save_timer -= 1 # don't push the save time forward if there are a large number of alarm events event = [ self.save_timer + int(self.save_pct * self.pkts_in_period), helpers.fsec(helpers.get_msg_time(d)) ] # event = [save after count, datetime] self.last_event_str = '%s UTC' % ( event[1].strftime('%Y-%m-%d %H:%M:%S.%f')[:22]) printM('Event time: %s' % (self.last_event_str), sender=self.sender) # show event time in the logs if self.screencap: printM( 'Saving png in about %i seconds' % (self.save_pct * (self.seconds)), self.sender) self.save.append(event) # append self.fig.suptitle( '%s.%s live output - detected events: %s' # title % (self.net, self.stn, self.events), fontsize=14, color=self.fgcolor, x=0.52) self.fig.canvas.set_window_title( '(%s) %s.%s - Raspberry Shake Monitor' % (self.events, self.net, self.stn)) if rs.getCHN(d) in self.chans: self.raw = rs.update_stream(stream=self.raw, d=d, fill_value='latest') return True else: return False
def _when_img(self, d): ''' Send a tweet with an image in when you get an ``IMGPATH`` message. :param bytes d: queue message ''' if self.tweet_images: imgpath = helpers.get_msg_path(d) imgtime = helpers.fsec(helpers.get_msg_time(d)) message = '%s %s UTC' % (self.message1, imgtime.strftime(self.fmt)[:22]) response = None if os.path.exists(imgpath): with open(imgpath, 'rb') as image: try: printM('Uploading image to Twitter %s' % (imgpath), self.sender) response = self.twitter.upload_media(media=image) time.sleep(5.1) printM('Sending tweet...', sender=self.sender) response = self.twitter.update_status(status=message, media_ids=response['media_id'], lat=rs.inv[0][0].latitude, long=rs.inv[0][0].longitude, geo_enabled=True, display_coordinates=True) # location will only stick to tweets on accounts that have location enabled in Settings printM('Tweeted with image: %s' % (message), sender=self.sender) url = 'https://twitter.com/%s/status/%s' % (response['user']['screen_name'], response['id_str']) printM('Tweet URL: %s' % url) except Exception as e: printE('could not send multimedia tweet - %s' % (e)) try: printM('Waiting 5 seconds and trying to send tweet again...', sender=self.sender) time.sleep(5.1) self.auth() printM('Uploading image to Twitter (2nd try) %s' % (imgpath), self.sender) response = self.twitter.upload_media(media=image) time.sleep(5.1) printM('Sending tweet...', sender=self.sender) response = self.twitter.update_status(status=message, media_ids=response['media_id'], lat=rs.inv[0][0].latitude, long=rs.inv[0][0].longitude, geo_enabled=True, display_coordinates=True) # location will only stick to tweets on accounts that have location enabled in Settings printM('Tweeted with image: %s' % (message), sender=self.sender) url = 'https://twitter.com/%s/status/%s' % (response['user']['screen_name'], response['id_str']) printM('Tweet URL: %s' % url) except Exception as e: printE('could not send multimedia tweet (2nd try) - %s' % (e)) response = None else: printM('Could not find image: %s' % (imgpath), sender=self.sender)
def _when_alarm(self, d): ''' Send a telegram in an alert scenario. :param bytes d: queue message ''' event_time = helpers.fsec(helpers.get_msg_time(d)) self.last_event_str = '%s' % (event_time.strftime(self.fmt)[:22]) message = '%s %s UTC%s - %s' % (self.message0, self.last_event_str, self.extra_text, self.livelink) response = None try: printM('Sending alert...', sender=self.sender) printM('Telegram message: %s' % (message), sender=self.sender) if not self.testing: response = self.telegram.sendMessage(chat_id=self.chat_id, text=message) else: TEST['c_telegram'][1] = True except Exception as e: printE('Could not send alert - %s' % (e)) try: printE('Waiting 5 seconds and trying to send again...', sender=self.sender, spaces=True) time.sleep(5) self.auth() printM('Telegram message: %s' % (message), sender=self.sender) if not self.testing: response = self.telegram.sendMessage(chat_id=self.chat_id, text=message) else: # if you are here in testing mode, there is a problem TEST['c_telegram'][1] = False except Exception as e: printE('Could not send alert - %s' % (e)) response = None self.last_message = message