Esempio n. 1
0
 def delete(self, name):
     """Delete the named class instance"""
     device = self.get(name)
     if device.enabled is True:
         self.remove_device_routing(device)
     HandlerRegistry.singleton().device_removed(device.uuid)
     del (self.devices[device.uuid])
Esempio n. 2
0
    def add(self, device):
        """Add a device class instance to the registry, with a friendly name"""
        self.devices[device.uuid] = device
        self.device_ids[device.device_id] = device.uuid

        if device.enabled is True:
            self.setup_device_routing(device)
        HandlerRegistry.singleton().device_added(device)
Esempio n. 3
0
 def handle_message(self, payload):
     for rec in payload["recs"]:
         paramid = rec["paramid"]
         # TODO: consider making this table driven and allowing our base class to fill our readings in for us
         # TODO: consider using @OpenThings.parameter as a decorator to the receive function
         # it will then register a handler for that message for itself as a handler
         # we still need Readings() defined too as a cache. The decorator could add
         # an entry into the cache too for us perhaps?
         if "value" in rec:
             value = rec["value"]
             if paramid == OpenThings.PARAM_VOLTAGE:
                 self.readings.battery_voltage = value
                 HandlerRegistry.handle_reading(self.uuid,
                                                'battery_voltage', value)
             elif paramid == OpenThings.PARAM_CURRENT:
                 self.readings.current = value
                 HandlerRegistry.handle_reading(self.uuid, 'current', value)
             elif paramid == OpenThings.PARAM_APPARENT_POWER:
                 self.readings.apparent_power = value
                 HandlerRegistry.handle_reading(self.uuid, 'apparent_power',
                                                value)
             else:
                 try:
                     param_name = OpenThings.param_info[paramid][
                         'n']  # name
                 except:
                     param_name = "UNKNOWN_%s" % str(hex(paramid))
                 print("unwanted paramid: %s" % param_name)
     pass
Esempio n. 4
0
 def handle_message(self, payload):
     # print("MIHO033 new data %s %s" % (self.device_id, payload))
     for rec in payload["recs"]:
         paramid = rec["paramid"]
         # TODO: consider making this table driven and allowing our base class to fill our readings in for us
         # TODO: consider using @OpenThings.parameter as a decorator to the receive function
         # it will then register a handler for that message for itself as a handler
         # we still need Readings() defined too as a cache. The decorator could add
         # an entry into the cache too for us perhaps?
         if "value" in rec:
             value = rec["value"]
             if paramid == OpenThings.PARAM_DOOR_SENSOR:
                 self.readings.switch_state = ((value is True)
                                               or (value != 0))
                 HandlerRegistry.handle_reading(self.uuid, 'switch_state',
                                                self.readings.switch_state)
             else:
                 try:
                     param_name = OpenThings.param_info[paramid][
                         'n']  # name
                 except:
                     param_name = "UNKNOWN_%s" % str(hex(paramid))
                 print("unwanted paramid: %s" % param_name)
Esempio n. 5
0
def handlers(name=None):
    hr = HandlerRegistry.singleton()
    if request.method == 'POST':
        try:
            hr.add(request.get_json())
        except KeyError as e:
            raise BadRequest("Invalid POST data")
    elif request.method == 'DELETE':
        if name is None:
            raise BadRequest("Bad Request")
        try:
            hr.remove(name)
        except KeyError as e:
            raise NotFound(name + " Not found")
    elif request.method == 'GET':
        if name is None:
            return {k: hr.get(k).serialise() for k in hr.list()}
        return hr.get(name).serialise()
    return jsonify({'status': 'ok'})
Esempio n. 6
0
 def incoming_message(self, payload):
     """Entry point for a message to be processed"""
     # This is the base-class entry point, don't  override this, but override handle_message
     self.rxseq += 1
     self.handle_message(payload)
     if self.updated_cb is not None:
         self.updated_cb(self, payload)
     l = time.time()
     if self.last_receive_time > 0:
         interval = l - self.last_receive_time
         self.__last_receive_intervals.append(interval)
     if len(self.__last_receive_intervals) > 60:
         self.__last_receive_intervals = self.__last_receive_intervals[-60:]
     self.last_receive_time = l
     HandlerRegistry.handle_reading(self.uuid, 'last_receive_time', self.last_receive_time)
     HandlerRegistry.handle_reading(self.uuid, 'next_receive_time', self.get_next_receive_time())
     HandlerRegistry.handle_reading(self.uuid, 'receive_count', self.get_receive_count())
Esempio n. 7
0
 def handle_message(self, payload):
     # print("MIHO032 new data %s %s" % (self.device_id, payload))
     # sys.stdout.flush()
     for rec in payload["recs"]:
         paramid = rec["paramid"]
         # TODO: consider making this table driven and allowing our base class to fill our readings in for us
         # TODO: consider using @OpenThings.parameter as a decorator to the receive function
         # it will then register a handler for that message for itself as a handler
         # we still need Readings() defined too as a cache. The decorator could add
         # an entry into the cache too for us perhaps?
         if "value" in rec:
             value = rec["value"]
             if paramid == OpenThings.PARAM_MOTION_DETECTOR:
                 state = ((value is True) or (value != 0))
                 if self.readings.switch_state != state:
                     self.readings.switch_state = state
                     HandlerRegistry.handle_reading(self.uuid,
                                                    'switch_state', state)
                     # print("MIHO032 new data %s %s" % (self.device_id, payload))
                     if self.callback is not None:
                         self.callback(self, state)
             elif paramid == OpenThings.PARAM_ALARM:
                 if value == 0x42:  # battery alarming
                     self.readings.battery_alarm = True
                     HandlerRegistry.handle_reading(self.uuid,
                                                    'battery_alarm', True)
                 elif value == 0x62:  # battery not alarming
                     self.readings.battery_alarm = False
                     HandlerRegistry.handle_reading(self.uuid,
                                                    'battery_alarm', False)
             else:
                 try:
                     param_name = OpenThings.param_info[paramid][
                         'n']  # name
                 except:
                     param_name = "UNKNOWN_%s" % str(hex(paramid))
                 print("unwanted paramid: %s" % param_name)
Esempio n. 8
0
    def handle_message(self, payload):
        # send a message whilst receive window is open, this MUST be done first
        if len(self.send_queue) > 0:
            message = self.send_queue.pop(0)
            self.send_message(message)
            # logging.debug("Sent message %s" % str(message))
            # logging.debug("MIHO013 send %s (%s remaining)" % (self.device_id, len(self.send_queue)))

        # check if it's time to refresh readings
        now = time.time()
        if self.voltageReadingPeriod is not None and (
                self.lastVoltageReading is None
                or now - self.lastVoltageReading > self.voltageReadingPeriod):
            self.queue_message(
                OpenThings.Message(MIHO013_BATTERY_LEVEL,
                                   header=self.__class__.header()))
            self.lastVoltageReading = now

        if self.diagnosticsReadingPeriod is not None and (
                self.lastDiagnosticsReading is None or now -
                self.lastDiagnosticsReading > self.diagnosticsReadingPeriod):
            self.queue_message(
                OpenThings.Message(MIHO013_DIAGNOSTICS,
                                   header=self.__class__.header()))
            self.lastDiagnosticsReading = now

        # extract data from message
        for rec in payload["recs"]:
            paramid = rec["paramid"]
            if "value" in rec:
                value = rec["value"]
                # logging.debug("MIHO013 new data %s %s %s" % (self.device_id, OpenThings.paramid_to_paramname(paramid), value))
                if paramid == OpenThings.PARAM_TEMPERATURE:
                    self.readings.ambient_temperature = value
                    HandlerRegistry.handle_reading(self.uuid,
                                                   'ambient_temperature',
                                                   value)
                    if value < self._temperature_alarm_low:
                        HandlerRegistry.alarm(self.uuid, 'ambient_temperature',
                                              value, 'Temperature Too Low')
                    elif value > self._temperature_alarm_high:
                        HandlerRegistry.alarm(self.uuid, 'ambient_temperature',
                                              value, 'Temperature Too High')

                elif paramid == OpenThings.PARAM_VOLTAGE:
                    self.readings.battery_voltage = value
                    HandlerRegistry.handle_reading(self.uuid,
                                                   'battery_voltage', value)
                    if value < self._battery_alarm_low:
                        HandlerRegistry.alarm(self.uuid, 'battery_voltage',
                                              value, 'Battery Running Low')

                elif paramid == OpenThings.PARAM_DIAGNOSTICS:
                    self.readings.diagnostic_flags = value
                    HandlerRegistry.handle_reading(self.uuid,
                                                   'diagnostic_flags', value)
                else:
                    logging.debug("Unhandled param id %s" % str(paramid))
Esempio n. 9
0
 def unknown_device(self, address, message):
     logging.debug("Message from unknown device:%s" % str(address))
     HandlerRegistry.singleton().handle_unknown(address, message)
Esempio n. 10
0
 def handle_message(self, payload):
     # print("MIHO005 new data %s %s" % (self.device_id, payload))
     for rec in payload["recs"]:
         paramid = rec["paramid"]
         # TODO: consider making this table driven and allowing our base class to fill our readings in for us
         #  then just define the mapping table in __init__ (i.e. paramid->Readings field name)
         value = rec["value"]
         if paramid == OpenThings.PARAM_VOLTAGE:
             self.readings.voltage = value
             HandlerRegistry.handle_reading(self.uuid, 'voltage', value)
         elif paramid == OpenThings.PARAM_CURRENT:
             self.readings.current = value
             HandlerRegistry.handle_reading(self.uuid, 'current', value)
         elif paramid == OpenThings.PARAM_REAL_POWER:
             self.readings.real_power = value
             HandlerRegistry.handle_reading(self.uuid, 'real_power', value)
         elif paramid == OpenThings.PARAM_APPARENT_POWER:
             self.readings.apparent_power = value
             HandlerRegistry.handle_reading(self.uuid, 'apparent_power', value)
         elif paramid == OpenThings.PARAM_REACTIVE_POWER:
             self.readings.reactive_power = value
             HandlerRegistry.handle_reading(self.uuid, 'reactive_power', value)
         elif paramid == OpenThings.PARAM_FREQUENCY:
             self.readings.frequency = value
             HandlerRegistry.handle_reading(self.uuid, 'frequency', value)
         else:
             try:
                 param_name = OpenThings.param_info[paramid]['n']  # name
             except:
                 param_name = "UNKNOWN_%s" % str(hex(paramid))
             print("unwanted paramid: %s" % param_name)