def run(timezone): bed_signal = Bedsensor(timezone) i = 0 while i < NB_TRY: logger.debug('initialization of API, test %i' % (i+1)) if initialize_APImode(): break i = i + 1 if i == NB_TRY: logger.error('The program fail to convert the Xbee module in API mode, try to restart') meta_data = {'type': 'error'} yield meta_data, None while True: signal = read_zigbee() logger.debug('Data received: %s' % signal) try: meta_data, data = gather_data(signal, bed_signal) logger.debug('data received: %s' % data) if data is not None: yield meta_data, data except TypeError: pass
def get_DR1(sample_data): DR1 = {} iterator = iter(sample_data) sensors = ['R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8'] for sensor in sensors: try: utf8 = next(iterator) + next(iterator) except: logger.error('There are less than 8 values received') return None DR1[sensor] = sum([ord(octet) * pow(256, i) for i, octet in enumerate(reversed(utf8))]) return DR1
def _init(): # TODO Put the config in the config file MOCHADHOST = "127.0.0.1" MOCHADPORT = 1099 while True: try: Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Sock.connect((MOCHADHOST, MOCHADPORT)) Sock.settimeout(10) logger.debug("Successfully connected to Mochad") return Sock except socket.error as e: Sock.close() logger.error("Unable to listen from Mochad : %s" % e) time.sleep(5)
def matches(self, signal): signal_data = signal['rf_data'] signal_addr = signal['source_addr'] """ The method to check if the signal received is corresponding with the bedsensor patern. If the signal is corresponding, we extract the information corresponding """ #Data FSR 1spl $ DR1 , ID , R0 R1 R2 R3 R4 R5 R6 R7 \n #example: $DR1,\x00\x13\xa2\x00@\xa1N\xba,\x00\x00\x06\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n #The other simple possible is $YOP,nb_FSR,nbFSC\n #example: $YOP,08,02\n #$DR1,\x00\x10\x02\r,\n\x90\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\n logger.debug('Checking the signal "%s" in bedsensor program' % signal_data) pattern = (r'^\$(?P<data_type>YOP|DR1).*$\n') regexp = re.compile(pattern) match = regexp.match(signal_data) if match: date = self.get_date() sample_bits = signal_data.split(",") if match.group('data_type') == 'YOP': self.initialize(sample_bits) return None, None else: """ FSR data signal """ # if integrity(signal) == False: # return None, None logger.info('The signal matchs with the bedsensor DR1 data patern') logger.debug('The DR1 sample is: %s' %sample_bits) if len(sample_bits) < FRAGMENT_NUMBER: # It is necessary to put a condition because the Arduino sending binary values can send unexpected '/n'. logger.error('There are %s fragments, this is less than the %s fragments expected in a sample' % (len(sample_bits), FRAGMENT_NUMBER)) return None, None sample_ID = sample_bits[1] bed_time = 0 for octet in sample_ID: bed_time = (bed_time*256)+ord(octet) bed_addr = 0 for octet in signal_addr: bed_addr = (bed_addr*256)+ord(octet) # bed_addr = int(signal_addr, 16) bed_ID = 'BED-'+str(bed_addr) sample_data = sample_bits[2] DR1 = {} i = 0 for val in ['R1','R2','R3','R4','R5','R6','R7','R8']: try: utf8 = sample_data[i]+sample_data[i+1] except: logger.error('There are less than 8 values received') return None, None DR1[val] = 0 for octet in utf8: DR1[val] = (DR1[val]*256 )+ord(octet) i = i+2 logger.debug('Measures of the FSR: %s, date: %s' % (DR1, date.isoformat())) return self.formalize(DR1, bed_ID, bed_time, date, match.group('data_type')) else: logger.debug('The signal is not matching with the bedsensor patern') return None, None
def integrity(self, signal): if sys.getsizeof(signal) == SIZEOF_DR1: logger.debug('The size of the data received is corresponding') return True logger.error('The size of the data received is not corresponding') return False