def read_serial_data(self): Msg = Message(self.signature) bytes_in_waiting = self.serial.inWaiting() if bytes_in_waiting: new_data = self.serial.read(bytes_in_waiting) self.buffer = self.buffer + new_data else: sleep(0.1) crlf_index = self.buffer.find('\r\n') if crlf_index > -1: self.last_read_line = self.buffer[0:crlf_index] temp = self.re_data.findall(self.last_read_line) self.log.debug('read self.last_read_line: ' + self.last_read_line) if len(temp): final_data = dict() timestamp = datetime.now().strftime('%Y-%m-%d-%H:%M:%S') final_data['timestamp'] = timestamp final_data['raw'] = self.last_read_line try: final_data.update({'cmd_number' : sjson.loads(temp[0][0])}) final_data.update(sjson.loads(temp[0][1])) self.log.debug('.....updated final_data') except Exception as E: final_data.update({'cmd_number' : -1}) error_msg = {'timestamp' : timestamp, 'from': self.signature, 'source' : 'ComPort', 'function' : 'def run() - inner', 'error' : E.message} Msg.msg = error_msg self.log.error(Msg.msg) Msg.msg = final_data self.log.debug("final_data={}".format(final_data)) self.redis.publish(self.redis_pub_channel, Msg.as_jsno()) #self.log.debug('.....publish to :' + self.redis_pub_channel) self.redis.set(self.redis_read_key,Msg.as_jsno()) #self.log.debug('.....empty buffer') self.buffer = self.buffer[crlf_index+2:] else: self.buffer = '' self.send('Z') self.log.debug('.....reseting command number') pass
def StarDigitempSubmit(channel, host='0.0.0.0', submit_to='192.168.1.10'): """ :param channel: - publish channel :param host: - redis host :param submit_to: - server running sensoredweb :return: - no ret value data_set = [[0, '1030B8D2010800BC', 20.1875], [1, '1002BDD2010800ED', 22.375], [2, '10F237C0010800D6', 23.5625], [3, '10EAB6D201080015', 22.9375], [4, '109A3FD30108003A', 23.9375], [5, '10F3F1D201080060', 22.875], [6, '101BBFD2010800A3', 22.75], [7, '109729C001080020', 24.875]] """ print"StartIqrSubmit(%s, %s, %s)" % (channel, host, submit_to) D = dt.Digitemp() R = Redis() Q = Queue(connection=R) threshold = 0.5; try: not_done = True while not_done: timestamp = datetime.datetime.now() data_set = D.GetData() print data_set M = Message() M.msg = {'data' : data_set} R.publish(channel, M.as_jsno()) last_enqueue = Q.enqueue(submit, data_set,\ timestamp=timestamp,\ submit_to=submit_to,\ threshold=threshold) #not_done = False except KeyboardInterrupt: pass print "Exiting " + __name__
def reader(self): ''' Run is the function that runs in the new thread and is called by ''' try: self.log.debug('Starting the listner thread') Msg = Message(self.signature) while self.alive and self._reader_alive: bytes_in_waiting = self.serial.inWaiting() if bytes_in_waiting: new_data = self.serial.read(bytes_in_waiting) self.buffer = self.buffer + new_data else: sleep(0.1) crlf_index = self.buffer.find('\r\n') if crlf_index > -1: line = self.buffer[0:crlf_index] temp = self.re_data.findall(line) self.log.debug('read line: ' + line) if len(temp): final_data = dict() timestamp = datetime.now().strftime('%Y-%m-%d-%H:%M:%S') final_data['timestamp'] = timestamp final_data['raw'] = line try: final_data.update({'cmd_number' : sjson.loads(temp[0][0])}) final_data.update(sjson.loads(temp[0][1])) self.log.debug('.....updated final_data') except Exception as E: final_data.update({'cmd_number' : -1}) error_msg = {'timestamp' : timestamp, 'from': self.signature, 'source' : 'ComPort', 'function' : 'def run() - inner', 'error' : E.message} Msg.msg = error_msg self.log.error(Msg.msg) Msg.msg = final_data self.log.debug("final_data={}".format(final_data)) self.redis.publish(self.redis_pub_channel, Msg.as_jsno()) #self.log.debug('.....publish to :' + self.redis_pub_channel) self.redis.set(self.redis_read_key,Msg.as_jsno()) self.buffer = self.buffer[crlf_index+2:] #self.log.debug('.....empty buffer') else: #self.buffer = '' #self.send('Z') #self.log.debug('.....reseting command number') pass except Exception as E: error_msg = {'source' : 'ComPort', 'function' : 'def run() - outter', 'error' : E.message} self.log.error("Exception occured, within the run function: %s" % E.message) self.log.debug('Exiting run() function')