def main(): gate = Ubigate('marmitek-gw', default_file='resources/conf.json.default') logger.info("Starting application") logger.debug('Timezone: %s' % gate.timezone) for data in mochad_reader.run(gate.timezone): if data['type'] != 'error': try: data['house'] = gate.find_house(data['sensor']) except KeyError: logger.warning("Unknown sensor: %s" % data['sensor']) else: # Appending house prefix if dealing with door sensor if data['sensorKind'] == 'door': for house in gate.config['houses']: if house['id'] == data['house']: prefix = house['prefix'] break else: prefix = '' data['sensor'] = prefix.lower() + data['sensor'] topic = "/marmitek/sensor/%s" % data['sensor'] gate.push(data['house'], topic, data)
def initialize_APImode(ser_init): """ Initialize the Xbee module in mode API/level2 API mode gives the ability to get the emitter address, to choose the recipient, and to deal with the bad interpretation of binary data. """ response = None ser_init.write("+++") time.sleep(2) for iteration in range(NB_ITERATION): response = ser_init.readlines(None) logger.debug("response 1: %s" % response) if response == AT_OK: ser_init.write("ATAP 02\r") response = ser_init.readlines(None) logger.debug("response 2: %s" % response) if response == AT_OK: logger.info('The Xbee shield is passing into API mode, ' 'level 2') return True else: logger.debug('Bad signal received when passing into API ' 'mode %s' % response) else: logger.debug('Bad signal received when passing into AT command ' 'mode %s' % response) return False
def matches(signal, timezone): # sample matching input: 11/08 14:42:15 Rx RFSEC Addr: 7E:2D:00 \r # Func: Contact_normal_max_tamper_DS12A logger.debug('Checking door for signal "%s"' % signal) pattern = ( r"^(?P<month>\d{2})/(?P<day>\d{2})\s" "(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})\s" "Rx\sRFSEC\sAddr:\s" "(?P<sensor1>[0-9A-F]{2}):(?P<sensor2>[0-9A-F]{2}):" "(?P<sensor3>[0-9A-F]{2})\s" "Func:\sContact_(?P<value>normal|alert)" "_(?:max|min)_(?:tamper_)?\w{2}\d+\w$\n*" ) regexp = re.compile(pattern) match = regexp.match(signal) if not match: return None logger.info("Door activity detected:") tz = pytz.timezone(str(timezone)) try: date = tz.localize( datetime( datetime.now().year, int(match.group("month")), int(match.group("day")), int(match.group("hour")), int(match.group("minute")), int(match.group("second")), ) ) except ValueError: logger.warn( "Invalid date: %s-%s-%s %s:%s:%s, event skipped" % ( datetime.now().year, match.group("month"), match.group("day"), match.group("hour"), match.group("minute"), match.group("second"), ) ) return None sensor = "%s%s%s" % (match.group("sensor1"), match.group("sensor2"), match.group("sensor3")) logger.info("%s: The door sensor %s sent %s" % (date.isoformat(), sensor, match.group("value"))) return { "type": "event", "sensor": sensor, "sensorKind": "door", "value": match.group("value"), "date": date.isoformat(), }
def initialize_APImode(): """ Method to initialise the Xbee module in mode API/level2 => ability to get the emitter address and to choose the destinatory/ and to deal with the bad interpretation of binary data. """ iteration = 0 response = None ser_init.write("+++") time.sleep(2) while iteration < NB_ITERATION: iteration = iteration + 1 response = ser_init.readlines(None) logger.debug("response 1: %s" %response) if response == AT_OK: ser_init.write("ATAP 02\r") response = ser_init.readlines(None) logger.debug("response 2: %s" %response) if response == AT_OK: logger.info('The Xbee shield is passing into API mode, level 2') return True else: logger.debug('Bad signal received when passing into API mode %s' % response) else: logger.debug('Bad signal received when passing into AT command mode %s' % response) return False
def matches(signal, timezone): """@todo: Docstring for matches. :signal: @todo :returns: @todo """ # sample matching input: 11/08 14:42:15 Rx RFSEC Addr: 7E:2D:00 \r # Func: Contact_normal_max_tamper_DS12A logger.debug('Checking door for signal "%s"' % signal) pattern = (r'^(?P<month>\d{2})/(?P<day>\d{2})\s' '(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})\s' 'Rx\sRFSEC\sAddr:\s' '(?P<sensor1>[0-9A-F]{2}):(?P<sensor2>[0-9A-F]{2}):' '(?P<sensor3>[0-9A-F]{2})\s' 'Func:\sContact_(?P<value>normal|alert)' '_(?:max|min)_(?:tamper_)?\w{2}\d+\w$\n*') regexp = re.compile(pattern) match = regexp.match(signal) if match: logger.info('Door activity detected:') tz = pytz.timezone(str(timezone)) try: date = tz.localize(datetime(datetime.now().year, int(match.group('month')), int(match.group('day')), int(match.group('hour')), int(match.group('minute')), int(match.group('second')))) except ValueError: logger.warn('Invalid date: %s-%s-%s %s:%s:%s, event skipped' % (datetime.now().year, match.group('month'), match.group('day'), match.group('hour'), match.group('minute'), match.group('second'))) return None sensor = 'd%s%s%s' % (match.group('sensor1'), match.group('sensor2'), match.group('sensor3')) # Next line is an ugly tweak, due to ugly sensor input # sensor = 'E7C300' logger.info('value: "%s", sensor: "%s", date: "%s"' % (match.group('value'), sensor, date.isoformat())) data = {'sensor': sensor, 'value': match.group('value'), 'date': date.isoformat()} return sensor, data return None
def initialize(self, sample_bits): """ Initialization signal TODO: Add a memory to keep this informations """ logger.info('The signal matched with the bedsensor initialization patern') self.nb_FSR = int(sample_bits[1]) self.nb_FSC = int(sample_bits[2]) logger.info('The bedsensor is equiped with %s FSR sensors and %s FSC sensors' % (self.nb_FSR, self.nb_FSC))
def main(): gate = Ubigate('zigbee-gw', default_file='resources/conf.json.default') logger.info("Starting application") logger.debug('Timezone: %s' % gate.timezone) for data in zigbee_IO.run(gate): if data['type'] != 'error': try: data['house'] = gate.find_house(data['sensor']) except KeyError: logger.warning("Unknown sensor: %s" % data['sensor']) else: topic = "/zigbee/sensor/%s/%s" % (data['sensor'], data['type']) gate.push(data['house'], topic, data)
def matches(signal, timezone): # sample matching input: 11/08 14:42:15 Rx RFSEC Addr: 7E:2D:00 \r # Func: Contact_normal_max_tamper_DS12A logger.debug('Checking door for signal "%s"' % signal) pattern = (r'^(?P<month>\d{2})/(?P<day>\d{2})\s' '(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})\s' 'Rx\sRFSEC\sAddr:\s' '(?P<sensor1>[0-9A-F]{2}):(?P<sensor2>[0-9A-F]{2}):' '(?P<sensor3>[0-9A-F]{2})\s' 'Func:\sContact_(?P<value>normal|alert)' '_(?:max|min)_(?:tamper_)?\w{2}\d+\w$\n*') regexp = re.compile(pattern) match = regexp.match(signal) if not match: return None logger.info('Door activity detected:') tz = pytz.timezone(str(timezone)) try: date = tz.localize( datetime(datetime.now().year, int(match.group('month')), int(match.group('day')), int(match.group('hour')), int(match.group('minute')), int(match.group('second')))) except ValueError: logger.warn('Invalid date: %s-%s-%s %s:%s:%s, event skipped' % (datetime.now().year, match.group('month'), match.group('day'), match.group('hour'), match.group('minute'), match.group('second'))) return None sensor = '%s%s%s' % (match.group('sensor1'), match.group('sensor2'), match.group('sensor3')) logger.info("%s: The door sensor %s sent %s" % (date.isoformat(), sensor, match.group('value'))) return { 'type': 'event', 'sensor': sensor, 'sensorKind': 'door', 'value': match.group('value'), 'date': date.isoformat() }
def matches(signal, timezone): """@todo: Docstring for matches. :signal: @todo :returns: @todo """ # sample matching input: 01/01 00:14:31 Rx RF HouseUnit: A1 Func: On logger.debug('Checking motion for signal "%s"' % signal) pattern = (r'^(?P<month>\d{2})/(?P<day>\d{2})\s' '(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})\s' 'Rx\sRF\sHouseUnit:\s(?P<sensor>\w\d+)\s' 'Func:\s(?P<value>On|Off)$\n*') regexp = re.compile(pattern) match = regexp.match(signal) if match: logger.info('Motion detected:') tz = pytz.timezone(str(timezone)) try: date = tz.localize(datetime(datetime.now().year, int(match.group('month')), int(match.group('day')), int(match.group('hour')), int(match.group('minute')), int(match.group('second')))) except ValueError: logger.warn('Invalid date: %s-%s-%s %s:%s:%s, event skipped' % (datetime.datetime.now().year, match.group('month'), match.group('day'), match.group('hour'), match.group('minute'), match.group('second'))) return None logger.info('value: "%s", sensor: "%s", date: "%s"' % (match.group('value'), match.group('sensor'), date.isoformat())) sensor = match.group('sensor') data = {'sensor': sensor, 'value': match.group('value'), 'date': date.isoformat()} return sensor, data return None
def matches(signal, timezone): # sample matching input: 01/01 00:14:31 Rx RF HouseUnit: A1 Func: On logger.debug('Checking motion for signal "%s"' % signal) pattern = (r'^(?P<month>\d{2})/(?P<day>\d{2})\s' '(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})\s' 'Rx\sRF\sHouseUnit:\s(?P<sensor>\w+\d+)\s' 'Func:\s(?P<value>On|Off)$\n*') regexp = re.compile(pattern) match = regexp.match(signal) if not match: return None logger.info('Motion detected:') tz = pytz.timezone(str(timezone)) try: date = tz.localize(datetime(datetime.now().year, int(match.group('month')), int(match.group('day')), int(match.group('hour')), int(match.group('minute')), int(match.group('second')))) except ValueError: logger.warn('Invalid date: %s-%s-%s %s:%s:%s, event skipped' % (datetime.datetime.now().year, match.group('month'), match.group('day'), match.group('hour'), match.group('minute'), match.group('second'))) return None logger.info("%s: The motion sensor %s sent %s" % (date.isoformat(), match.group('sensor'), match.group('value'))) return {'type': 'event', 'sensor': match.group('sensor'), 'sensorKind': 'motion', 'value': match.group('value').lower(), 'date': date.isoformat()}
def matches(signal, timezone): # sample matching input: 01/01 00:14:31 Rx RF HouseUnit: A1 Func: On logger.debug('Checking motion for signal "%s"' % signal) pattern = (r'^(?P<month>\d{2})/(?P<day>\d{2})\s' '(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})\s' 'Rx\sRF\sHouseUnit:\s(?P<sensor>\w+\d+)\s' 'Func:\s(?P<value>On|Off)$\n*') regexp = re.compile(pattern) match = regexp.match(signal) if not match: return None logger.info('Motion detected:') tz = pytz.timezone(str(timezone)) try: date = tz.localize( datetime(datetime.now().year, int(match.group('month')), int(match.group('day')), int(match.group('hour')), int(match.group('minute')), int(match.group('second')))) except ValueError: logger.warn('Invalid date: %s-%s-%s %s:%s:%s, event skipped' % (datetime.datetime.now().year, match.group('month'), match.group('day'), match.group('hour'), match.group('minute'), match.group('second'))) return None logger.info( "%s: The motion sensor %s sent %s" % (date.isoformat(), match.group('sensor'), match.group('value'))) return { 'type': 'event', 'sensor': match.group('sensor'), 'sensorKind': 'motion', 'value': match.group('value').lower(), 'date': date.isoformat() }
def main(): gate = Ubigate('resources/conf.ini') log.add_logger_file('data.log', logging.WARN) logger.setLevel(logging.INFO) logger.info("Starting application") logger.info('Server: %s\n' 'Port: %s\n' 'Password: %s\n' 'House: %s\n' 'Username: %s\n' 'Timezone: %s' % (gate.config.server, gate.config.port, gate.config.password, gate.config.house, gate.config.username, gate.timezone)) for sensor, data in mochad_reader.run(gate.timezone): topic = "/marmitek/sensor/%s" % sensor data['house'] = gate.config.house gate.push(topic, data)
def main(): gate = Ubigate('resources/conf.ini') log.add_logger_file('data.log', logging.DEBUG) logger.setLevel(logging.INFO) logger.info("Starting application") logger.info('Server: %s\n' 'Port: %s\n' 'Password: %s\n' 'House: %s\n' 'Username: %s\n' 'Timezone: %s' % (gate.config.server, gate.config.port, gate.config.password, gate.config.house, gate.config.username, gate.timezone)) for meta_data, data in zigbee_reader.run(gate.timezone): if meta_data['type'] == 'error': break topic = "/zigbee/sensor/%s/%s" % (meta_data['sensor'], meta_data['type']) data['house'] = gate.config.house gate.push(topic, data)
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