Exemple #1
0
 def onAdaptorService(self, msg):
     self.cbLog("debug", "Service msg: " + str(msg))
     (services, serviceid) = self._parseServices(msg)
     self.cbLog("debug",
                "Service Id: " + str(serviceid) + " " + str(services))
     if services.has_key('binary_sensor'):
         if services.has_key('switch'):
             self.switchId = serviceid
             self._requestData(serviceid, ['connected'], [0])
             self.cbLog("info", "SWITCH FOUND: " + str(self.switchId))
         elif not services.has_key("gpio"):
             self.smokeId = serviceid
             self._requestData(serviceid,
                               ['binary_sensor', 'battery', 'connected'],
                               [0, 0, 0])
             self.cbLog("info",
                        "SMOKE DETECTOR FOUND: " + str(self.smokeId))
     if services.has_key('gpio'):
         self.gpioId = serviceid
         self._requestData(serviceid, ['gpio'], [0])
         self.cbLog("info", "GPIO FOUND: " + str(self.gpioId))
     if self.smokeId and self.switchId and self.gpioId and not self.km:
         # Only start KM after all the adaptors are connected
         self.km = CbKitchenMinder(self)
         # km.update not implemented yet & LoopingCall has been problematic. Commented-out.
         #t = task.LoopingCall(self.km.update)
         #t.start(1.0)
         self._setState('running')
         self.cbLog("info",
                    " ------- All devices connected. Started KM ---------")
Exemple #2
0
 def onAdaptorService(self, msg):
     self.cbLog("debug", "Service msg: " + str(msg))
     (services, serviceid) = self._parseServices(msg)
     self.cbLog("debug", "Service Id: " + str(serviceid) + " " + str(services))
     if services.has_key('binary_sensor'):
         if services.has_key('switch'):
             self.switchId = serviceid
             self._requestData(serviceid, ['connected'], [0])
             self.cbLog("info", "SWITCH FOUND: " + str(self.switchId))
         elif not services.has_key("gpio"):
             self.smokeId = serviceid
             self._requestData(serviceid, ['binary_sensor', 'battery', 'connected'], [0, 0, 0])
             self.cbLog("info", "SMOKE DETECTOR FOUND: " + str(self.smokeId))
     if services.has_key('gpio'):
         self.gpioId = serviceid
         self._requestData(serviceid, ['gpio'], [0])
         self.cbLog("info", "GPIO FOUND: " + str(self.gpioId))
     if self.smokeId and self.switchId and self.gpioId and not self.km:
         # Only start KM after all the adaptors are connected
         self.km = CbKitchenMinder(self)
         # km.update not implemented yet & LoopingCall has been problematic. Commented-out.
         #t = task.LoopingCall(self.km.update)
         #t.start(1.0)
         self._setState('running')
         self.cbLog("info", " ------- All devices connected. Started KM ---------")
Exemple #3
0
class App(CbApp):
    def __init__(self, argv):
        self.smokeId = None
        self.switchId = None
        self.gpioId = None
        self.smokeConnected = True
        self.switchConnected = True
        self.km = None
        self.dm = None
        CbApp.__init__(self, argv)

    # Local helper functions
    def _setState(self, action):
        self.state = action
        msg = {"id": self.id,
               "status": "state",
               "state": self.state}
        self.sendManagerMessage(msg)

    def _parseServices(self, msg):
        services = {}
        for s in msg['service']:
            services[s['characteristic']] = s
        return (services, self._getId(msg))

    def _requestData(self, serviceid, services, intervals):
        if serviceid == None:
            self.cbLog("debug", "requestData, serviceid not set")
            return
        self.cbLog("debug", "<----- requestData: " + str(services) + " @ " + str(intervals))
        sreqs = []
        assert len(services) == len(intervals)
        for s, i in zip(services, intervals):
            sreqs.append({'characteristic': s, 'interval': i})
        req = {'id': self.id, 'request': 'service', 'service': sreqs}
        self.cbLog("debug", "requestData sending: " + json.dumps(req, indent=4))
        self.sendMessage(req, serviceid)

    def _getId(self, msg):
        return msg['id']

    def _getTS(self, msg):
        return msg['timeStamp']

    def _getData(self, msg, characteristic):
        if msg['characteristic'] == characteristic:
            return msg['data']
        else:
            return None

    def _sendData(self, serviceid, data):
        if serviceid == None:
            self.cbLog("debug", "sendData, serviceid not set")
            return
        self.cbLog("debug", "-----> sendData: " + str(data))
        cmd = {'id': self.id, 'request': 'command', 'data': data}
        self.sendMessage(cmd, serviceid)

    # Functions called automagically by CB framework
    def onAdaptorService(self, msg):
        self.cbLog("debug", "Service msg: " + str(msg))
        (services, serviceid) = self._parseServices(msg)
        self.cbLog("debug", "Service Id: " + str(serviceid) + " " + str(services))
        if services.has_key('binary_sensor'):
            if services.has_key('switch'):
                self.switchId = serviceid
                self._requestData(serviceid, ['connected'], [0])
                self.cbLog("info", "SWITCH FOUND: " + str(self.switchId))
            elif not services.has_key("gpio"):
                self.smokeId = serviceid
                self._requestData(serviceid, ['binary_sensor', 'battery', 'connected'], [0, 0, 0])
                self.cbLog("info", "SMOKE DETECTOR FOUND: " + str(self.smokeId))
        if services.has_key('gpio'):
            self.gpioId = serviceid
            self._requestData(serviceid, ['gpio'], [0])
            self.cbLog("info", "GPIO FOUND: " + str(self.gpioId))
        if self.smokeId and self.switchId and self.gpioId and not self.km:
            # Only start KM after all the adaptors are connected
            self.km = CbKitchenMinder(self)
            # km.update not implemented yet & LoopingCall has been problematic. Commented-out.
            #t = task.LoopingCall(self.km.update)
            #t.start(1.0)
            self._setState('running')
            self.cbLog("info", " ------- All devices connected. Started KM ---------")

    def onAdaptorData(self, msg):
        self.cbLog("debug", "onAdaptorData,  message: " + str(msg))
        event = "Smoke"
        if self.km:
            event = None
            if self._getId(msg) == self.smokeId:
                timeStamp = self._getTS(msg)
                sensor = self._getData(msg, 'binary_sensor')
                if sensor != None:
                    if sensor == 'on':
                        event = 'Smoke'
                    else:
                        event = 'NoSmoke'
                    self.km.addEvent(event)
                    self.dm.storeEvent(timeStamp, event)
                level = self._getData(msg, 'battery')
                if level != None:
                    self.dm.storeBattery(timeStamp, level)
                connected = self._getData(msg, 'connected')
                if connected != None:
                    changed = (self.smokeConnected != connected)
                    self.smokeConnected = connected
                    if changed:
                        if self.smokeConnected and self.switchConnected:
                            event = 'Connected'
                        else:
                            event = 'NotConnected'
                        self.km.addEvent(event)
                        self.dm.storeEvent(timeStamp, event)
            elif self._getId(msg) == self.switchId:
                timeStamp = self._getTS(msg)
                connected = self._getData(msg, 'connected')
                if connected != None:
                    changed = (self.switchConnected != connected)
                    self.switchConnected = connected
                    if changed:
                        if self.smokeConnected and self.switchConnected:
                            event = 'Connected'
                        else:
                            event = 'NotConnected'
                        self.km.addEvent(event)
                        self.dm.storeEvent(timeStamp, event)
            elif self._getId(msg) == self.gpioId:
                timeStamp = self._getTS(msg)
                gpio = self._getData(msg, 'gpio')
                if gpio != None:
                    if gpio == 'button':
                        event = 'SwitchPressed'
                    elif self._getData(msg, 'gpio') == 'movement':
                        event = 'Movement'
                    assert event != None
                    self.km.addEvent(event)
                    self.dm.storeEvent(timeStamp, event)
            else:
                self.cbLog("debug", "Unhandled data: " + str(msg))

    def onConfigureMessage(self, config):
        self.cbLog("debug", "onConfigureMessage, config: " + str(config))
        self.client = Client(self.id)
        self.client.sendMessage = self.sendMessage
        self.client.cbLog = self.cbLog
        self.dm = DataManager(self.bridge_id)
        self.dm.cbLog = self.cbLog
        self.dm.client = self.client
        self.dm.storeEvent(time.time(), 'Boot')
Exemple #4
0
class App(CbApp):
    def __init__(self, argv):
        self.smokeId = None
        self.switchId = None
        self.gpioId = None
        self.smokeConnected = True
        self.switchConnected = True
        self.km = None
        self.dm = None
        CbApp.__init__(self, argv)

    # Local helper functions
    def _setState(self, action):
        self.state = action
        msg = {"id": self.id, "status": "state", "state": self.state}
        self.sendManagerMessage(msg)

    def _parseServices(self, msg):
        services = {}
        for s in msg['service']:
            services[s['characteristic']] = s
        return (services, self._getId(msg))

    def _requestData(self, serviceid, services, intervals):
        if serviceid == None:
            self.cbLog("debug", "requestData, serviceid not set")
            return
        self.cbLog(
            "debug",
            "<----- requestData: " + str(services) + " @ " + str(intervals))
        sreqs = []
        assert len(services) == len(intervals)
        for s, i in zip(services, intervals):
            sreqs.append({'characteristic': s, 'interval': i})
        req = {'id': self.id, 'request': 'service', 'service': sreqs}
        self.cbLog("debug",
                   "requestData sending: " + json.dumps(req, indent=4))
        self.sendMessage(req, serviceid)

    def _getId(self, msg):
        return msg['id']

    def _getTS(self, msg):
        return msg['timeStamp']

    def _getData(self, msg, characteristic):
        if msg['characteristic'] == characteristic:
            return msg['data']
        else:
            return None

    def _sendData(self, serviceid, data):
        if serviceid == None:
            self.cbLog("debug", "sendData, serviceid not set")
            return
        self.cbLog("debug", "-----> sendData: " + str(data))
        cmd = {'id': self.id, 'request': 'command', 'data': data}
        self.sendMessage(cmd, serviceid)

    # Functions called automagically by CB framework
    def onAdaptorService(self, msg):
        self.cbLog("debug", "Service msg: " + str(msg))
        (services, serviceid) = self._parseServices(msg)
        self.cbLog("debug",
                   "Service Id: " + str(serviceid) + " " + str(services))
        if services.has_key('binary_sensor'):
            if services.has_key('switch'):
                self.switchId = serviceid
                self._requestData(serviceid, ['connected'], [0])
                self.cbLog("info", "SWITCH FOUND: " + str(self.switchId))
            elif not services.has_key("gpio"):
                self.smokeId = serviceid
                self._requestData(serviceid,
                                  ['binary_sensor', 'battery', 'connected'],
                                  [0, 0, 0])
                self.cbLog("info",
                           "SMOKE DETECTOR FOUND: " + str(self.smokeId))
        if services.has_key('gpio'):
            self.gpioId = serviceid
            self._requestData(serviceid, ['gpio'], [0])
            self.cbLog("info", "GPIO FOUND: " + str(self.gpioId))
        if self.smokeId and self.switchId and self.gpioId and not self.km:
            # Only start KM after all the adaptors are connected
            self.km = CbKitchenMinder(self)
            # km.update not implemented yet & LoopingCall has been problematic. Commented-out.
            #t = task.LoopingCall(self.km.update)
            #t.start(1.0)
            self._setState('running')
            self.cbLog("info",
                       " ------- All devices connected. Started KM ---------")

    def onAdaptorData(self, msg):
        self.cbLog("debug", "onAdaptorData,  message: " + str(msg))
        event = "Smoke"
        if self.km:
            event = None
            if self._getId(msg) == self.smokeId:
                timeStamp = self._getTS(msg)
                sensor = self._getData(msg, 'binary_sensor')
                if sensor != None:
                    if sensor == 'on':
                        event = 'Smoke'
                    else:
                        event = 'NoSmoke'
                    self.km.addEvent(event)
                    self.dm.storeEvent(timeStamp, event)
                level = self._getData(msg, 'battery')
                if level != None:
                    self.dm.storeBattery(timeStamp, level)
                connected = self._getData(msg, 'connected')
                if connected != None:
                    changed = (self.smokeConnected != connected)
                    self.smokeConnected = connected
                    if changed:
                        if self.smokeConnected and self.switchConnected:
                            event = 'Connected'
                        else:
                            event = 'NotConnected'
                        self.km.addEvent(event)
                        self.dm.storeEvent(timeStamp, event)
            elif self._getId(msg) == self.switchId:
                timeStamp = self._getTS(msg)
                connected = self._getData(msg, 'connected')
                if connected != None:
                    changed = (self.switchConnected != connected)
                    self.switchConnected = connected
                    if changed:
                        if self.smokeConnected and self.switchConnected:
                            event = 'Connected'
                        else:
                            event = 'NotConnected'
                        self.km.addEvent(event)
                        self.dm.storeEvent(timeStamp, event)
            elif self._getId(msg) == self.gpioId:
                timeStamp = self._getTS(msg)
                gpio = self._getData(msg, 'gpio')
                if gpio != None:
                    if gpio == 'button':
                        event = 'SwitchPressed'
                    elif self._getData(msg, 'gpio') == 'movement':
                        event = 'Movement'
                    assert event != None
                    self.km.addEvent(event)
                    self.dm.storeEvent(timeStamp, event)
            else:
                self.cbLog("debug", "Unhandled data: " + str(msg))

    def onConfigureMessage(self, config):
        self.cbLog("debug", "onConfigureMessage, config: " + str(config))
        self.client = Client(self.id)
        self.client.sendMessage = self.sendMessage
        self.client.cbLog = self.cbLog
        self.dm = DataManager(self.bridge_id)
        self.dm.cbLog = self.cbLog
        self.dm.client = self.client
        self.dm.storeEvent(time.time(), 'Boot')