def onEveryMessageBeforeHandler(context, raw_message): team = context['team'] if team.name is None: message = formatter.parse(raw_message, False) if message['sentence_id'] == 'RBHRB': team.name = message['data'][6] elif message['sentence_id'] == 'RBDOK' or message[ 'sentence_id'] == 'RBFLG': team.name = message['data'][2] else: logger.debug( "Received unknown sentence_id in onEveryMessageBeforeHandler()" ) return raw_message with team_dict_lock: team_dict[team.name] = team log_folder = LOGS_PATH + str(date.today()) + '/' + team.name context['logfile'] = open(log_folder + '_STORY.txt', 'a') context['rawlog'] = open(log_folder + '_RAW.txt', 'a') logger.info('Team ' + team.name + ' connected.') # write out to raw log file try: context['rawlog'].write(timeutil.rn_timestamp() + ' | ' + context['client_address'] + ' | ' + raw_message + '\n') except BaseException: logger.warn('error. NMEA message not logged.') return raw_message
def run(self): logger.info("Pinglistener connecting to {}:{}".format( \ self.pinger_ip, self.pinger_port)) sock = None while not self.shutdown_flag: # try to connect to the pinger box if not self.Connected: try: # create a new socket and try to connect sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(3) sock.connect((self.pinger_ip, self.pinger_port)) logger.info('Pinger TCP connected') self.Connected = True except BaseException: # if it doesn't work, try again in two seconds time.sleep(2) # once we are connected to the pingers: else: try: response = sock.recv(1024) message = formatter.parse(response) # update the field docs with buoy and pinger info. name = message['sentence_type'] if name == 'PNS': self.Field = message['data'][0] # print 'Field:', self.Field self.Active = message['data'][1] # print 'Active pinger:', self.Active self.Sync = message['data'][2] # print 'Pinger Sync Mode:', self.Sync self.Voltage = float(message['data'][3]) / 1000 # print 'Pinger voltage:', self.Voltage self.Time = self.timeutil.aslocaltimestr( datetime.utcnow()) # print 'Time:', str(self.Time) folder = 'Field_' + str(message['data'][0]) else: name = 'PINGlog' folder = 'ERRORS' log_folder = self.logs_path + \ str(date.today()) + '/' + folder log_file = log_folder + '/' + name + '.txt' try: with open(log_file, 'a') as f: f.write(str(self.Time) + ' | ' + response) except BaseException: os.mkdir(log_folder) with open(log_file, 'a') as f: f.write(str(self.Time) + ' | ' + response) except BaseException: logger.exception("Exc:") logger.error('Pinger TCP disconnected') time.sleep(2) sock.close() self.Connected = False
def run(self): logger.info("BUOYlistener connecting to {}:{}".format( \ self.buoy_ip, self.buoy_port)) sock = None while not self.shutdown_flag: if not self.Connected: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(3) sock.connect((self.buoy_ip, self.buoy_port)) self.Connected = True logger.info('Buoy TCP connected.') except BaseException: time.sleep(2) else: try: response = sock.recv(1024) message = formatter.parse(response) name = message['sentence_type'] if name == 'BYS': self.Field = message['data'][0] # print 'Field:', self.Field self.State = message['data'][1] # print 'Buoy state:', self.State self.Voltage = float(message['data'][2]) / 1000 # print 'Buoy voltage:', self.Voltage self.Time = self.timeutil.rn_timestamp() folder = 'Field_' + str(message['data'][0]) else: name = 'BUOYlog' folder = 'ERRORS' log_folder = self.logs_path + \ str(date.today()) + '/' + folder log_file = log_folder + '/' + name + '.txt' try: with open(log_file, 'a') as f: f.write(str(self.Time) + ' | ' + response) except BaseException: os.mkdir(log_folder) with open(log_file, 'a') as f: f.write(str(self.Time) + ' | ' + response) except BaseException: logger.info('Buoy TCP disconnected') sock.close() self.Connected = False logger.info("BUOYlistener done")