Ejemplo n.º 1
0
def run(printFREQ=60, port=8888):
    '''
	Initialize stream and print constants, then process data for packet loss.

	:param int printFREQ: Value in seconds denoting the frequency with which this program will report packets lost
	:param int port: Local port to listen on

	'''
    global DPtime, DPttlLoss
    printM("Initializing...")
    raspberryshake.initRSlib(
        dport=port, rsstn='Z0000'
    )  # runs in quiet mode; suppresses needless output but shows errors
    add_debug_handler()  # now start console output
    # initialize data stream constants
    printM('Opened data port successfully.')
    DP = raspberryshake.getDATA()
    CHAN = raspberryshake.getCHN(
        DP)  # first channel - doesn't matter which, used to stop looping
    TR = raspberryshake.tf  # transmission rate - in milliseconds
    TRE = (TR +
           TR * .5) / 1000.  # time diff / error to identify a missed packet
    SR = raspberryshake.sps  # sample / second
    ttlCHN = raspberryshake.getTTLCHN()  # total number of channels
    printM("	Total Channels: %s" % ttlCHN)
    printM("	   Sample Rate: %s samples / second" % SR)
    printM("	       TX Rate: Every %s milliseconds" % TR)

    # start processing data packets for packet loss detection
    # initialize
    chnNum = 0
    while chnNum < ttlCHN:
        DP = raspberryshake.getDATA()
        CHAN = raspberryshake.getCHN(DP)
        DPtime[CHAN] = raspberryshake.getTIME(DP)
        timeStart[CHAN] = DPtime[CHAN]
        DPttlLoss[CHAN] = 0
        chnNum += 1

    printM('Data Packet reading begun.')
    printM(
        'Will report any DP loss as it happens and totals every %s seconds.' %
        printFREQ)

    while 1:  # loop forever
        DP = raspberryshake.getDATA()
        CHAN = raspberryshake.getCHN(DP)
        timeS = raspberryshake.getTIME(DP)
        timeD = timeS - DPtime[CHAN]
        if abs(timeD) > TRE:
            printM("DP loss of %s second(s) Current TS: %s, Previous TS: %s" %
                   (round(timeD, 3), timeS, DPtime[CHAN]))
            DPttlLoss[CHAN] += abs(int(timeD * TR))
        DPtime[CHAN] = timeS

        if int(timeS) % printFREQ == 0:
            if printTTLS(CHAN, TR):
                timeStart[CHAN] = timeS
                DPttlLoss[CHAN] = 0
Ejemplo n.º 2
0
	def run(self):
		"""
		Distributes queue objects to another address and port on the network.
		"""
		printM('Opening socket...', sender=self.sender)
		socket_type = s.SOCK_DGRAM if os.name in 'nt' else s.SOCK_DGRAM | s.SO_REUSEADDR
		sock = s.socket(s.AF_INET, socket_type)

		printM('Forwarding %s data to %s:%s' % (self.chans, self.addr, self.port), sender=self.sender)

		try:
			while self.running:
				p = self.queue.get()	# get a packet
				self.queue.task_done()	# close the queue

				if 'TERM' in str(p):	# shutdown if there's a TERM message on the queue
					self.alive = False
					printM('Exiting.', self.sender)
					sys.exit()

				if RS.getCHN(p) in self.chans:
					sock.sendto(p, (self.addr, self.port))

		except Exception as e:
			self.alive = False
			printM('ERROR: %s' % e, sender=self.sender)
			sys.exit(2)
Ejemplo n.º 3
0
    def getq(self):
        '''
		Reads data from the queue and updates the stream.

		:rtype: bool
		:return: Returns ``True`` if stream is updated, otherwise ``False``.
		'''
        d = self.queue.get(True, timeout=None)
        self.queue.task_done()
        if 'TERM' in str(d):
            self.alive = False
            printM('Exiting.', self.sender)
            sys.exit()
        elif str(d.decode('UTF-8')).split(' ')[0] in [
                'ALARM', 'RESET', 'IMGPATH'
        ]:
            pass
        else:
            if rs.getCHN(d) in self.chans:
                self.stream = rs.update_stream(stream=self.stream,
                                               d=d,
                                               fill_value=None)
                return True
            else:
                return False
Ejemplo n.º 4
0
    def _getd(self):
        '''
		Reads data from the queue and updates the stream.
		'''
        d = self._getq()

        if rs.getCHN(d) in self.cha:
            self.stream = rs.update_stream(stream=self.stream,
                                           d=d,
                                           fill_value='latest')
        else:
            self._messagetests(d)
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    def _datatests(self, d):
        '''
		Run tests on a data packet to see if it can be processed into a stream object.
		If so, mark the data and processing tests passed.

		:param bytes d: a data packet from the queue

		'''

        if rs.getCHN(d) in self.cha:
            t.TEST['c_data'][1] = True
            self.stream = rs.update_stream(stream=self.stream,
                                           d=d,
                                           fill_value='latest')
            t.TEST['c_processing'][1] = True
Ejemplo n.º 7
0
    def run(self):
        """
		Gets and distributes queue objects to another address and port on the network.
		"""
        printM('Opening socket...', sender=self.sender)
        socket_type = s.SOCK_DGRAM if os.name in 'nt' else s.SOCK_DGRAM | s.SO_REUSEADDR
        sock = s.socket(s.AF_INET, socket_type)

        msg_data = '%s data' % (self.chans) if self.fwd_data else ''
        msg_and = ' and ' if (self.fwd_data and self.fwd_alarms) else ''
        msg_alarms = 'ALARM / RESET messages' if self.fwd_alarms else ''

        printM('Forwarding %s%s%s to %s:%s' %
               (msg_data, msg_and, msg_alarms, self.addr, self.port),
               sender=self.sender)

        try:
            while self.running:
                p = self.queue.get()  # get a packet
                self.queue.task_done()  # close the queue

                if 'TERM' in str(
                        p):  # shutdown if there's a TERM message on the queue
                    self._exit()

                if 'IMGPATH' in str(p):
                    continue

                if ('ALARM' in str(p)) or ('RESET' in str(p)):
                    if self.fwd_alarms:
                        sock.sendto(p, (self.addr, self.port))
                    continue

                if "{'" in str(p):
                    if (self.fwd_data) and (rs.getCHN(p) in self.chans):
                        sock.sendto(p, (self.addr, self.port))

                if self.testing:
                    TEST['c_forward'][1] = True

        except Exception as e:
            self.alive = False
            printE('%s' % e, sender=self.sender)
            if self.testing:
                TEST['c_forward'][1] = False
            sys.exit(2)
Ejemplo n.º 8
0
    def getq(self):
        d = self.queue.get()
        self.queue.task_done()
        if 'TERM' in str(d):
            plt.close()
            if 'SELF' in str(d):
                print()
                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),
                RS.UTCDateTime.strptime(d.decode('utf-8'),
                                        'ALARM %Y-%m-%dT%H:%M:%S.%fZ')
            ]  # event = [save after count, datetime]
            self.last_event_str = event[1].strftime('%Y-%m-%d %H:%M:%S UTC')
            printM('Event time: %s' % (self.last_event_str),
                   self.sender)  # show event time in the logs
            if self.screencap:
                print()
                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
Ejemplo n.º 9
0
 def getq(self):
     d = self.queue.get(True, timeout=None)
     self.queue.task_done()
     if 'TERM' in str(d):
         self.alive = False
         printM('Exiting.', self.sender)
         sys.exit()
     elif 'ALARM' in str(d):
         pass
     else:
         if RS.getCHN(d) in self.chans:
             self.stream = RS.update_stream(stream=self.stream,
                                            d=d,
                                            fill_value=None)
             return True
         else:
             return False