Пример #1
0
    class PlugEntity(Agent):
        def __init__(self, **kwargs):
            super(PlugEntity, self).__init__(**kwargs)
            self.boss_client = BossClient(BOSS_EMAIL, BOSS_PASSWORD, BOSS_DEVICE_NAME)

        @Core.periodic(settings.HEARTBEAT_PERIOD)
        def status_push(self):
            data = {
              'status': self.boss_client.status(),
              'power': self.boss_client.power()
            }
            prefix = settings.TYPE_PREFIX + '/' + AGENT_ID + '/data'
            headers = {
              headers_mod.FROM: AGENT_ID,
              headers_mod.CONTENT_TYPE: headers_mod.CONTENT_TYPE.JSON,
              headers_mod.DATE: datetime.datetime.today().isoformat(),
              "Location": config.get("location")
            }
            self.publish_all(data, prefix, headers)
            _log.debug("publishing heartbeat: %s", str(data))

        def publish_all(self, data, prefix, headers):
            for item in data.keys():
                topic = prefix + '/' + item
                self.vip.pubsub.publish('pubsub', topic, headers, jsonapi.dumps(data[item]))

        @PubSub.subscribe('pubsub', settings.TYPE_PREFIX + '/' + AGENT_ID + '/operations/status')
        def on_switch(self, peer, sender, bus, topic, headers, message):
            _log.debug('Plug got\nTopic: {topic}, {headers}, Message: {message}'.format(topic=topic, headers=headers, message=message))
            self.boss_client.switch(jsonapi.loads(message))
Пример #2
0
 def __init__(self, **kwargs):
     super(PlugEntity, self).__init__(**kwargs)
     self.boss_client = BossClient(BOSS_EMAIL, BOSS_PASSWORD, BOSS_DEVICE_NAME)