Esempio n. 1
0
    def publish(self, msg, info=None):

        self._data = tools.checkKeys(msg, ('nodeid', 'childid', 'payload'))
        if self._data and self.connected:
            self._log.debug(
                'Try to publish values to the MQTT Broker on {0}: {1}'.format(
                    config.MQTTBroker, msg))

            for mqttTopic in config.MQTTTopics:
                topic = "/" + mqttTopic
                try:
                    topic = topic.replace('%nodeid',
                                          self._data['nodeid']).replace(
                                              '%childid',
                                              self._data['childid']).replace(
                                                  '%sensorid',
                                                  self._data['childid'])

                    if not (info is None):
                        topic = topic.replace('%childdescription',
                                              info.description).replace(
                                                  '%sensordescription',
                                                  info.description)

                    self.__publish(topic, self._data['payload'])
                except Exception, e:
                    self._log.error(
                        'MQTT Publish failed: Failed to create topic')
Esempio n. 2
0
    def publish(self, msg):
        self._ok = False
        self._log.debug('Try to push value to openhab: {0}'.format(msg))
        self._data = tools.checkKeys(msg, ('payload', 'nodeid', 'childid'))
        if self._data:
            self._data['openhab'] = self._db.openhab(node=self._data['nodeid'],
                                                     sensor=self._data['childid'])
            if self._data['openhab'] and self._data['openhab'] in self.Items():
                otype = self.Items()[self._data['openhab']]
                if otype == 'ContactItem':
                    if self._data['payload'] == '0':
                        self._payload = 'CLOSED'
                    elif self._data['payload'] == '1':
                        self._payload = 'OPEN'
                else:
                    self._payload = self._data['payload']
                if self._payload is not None:
                    self._url = '{0}/{1}/state'.format(config.OpenhabAPI,
                                                       self._data['openhab'])
                    self._log.debug('Openhab put url {0} with data {1}'.format(self._url,\
                                                                               self._payload))
                    self.__requestPut()
                    if self._ok:
                        self._log.info('Openhab updated successfully:\n\
                                        Node: {nodeid},\n\
                                        Sensor: {childid}\n\
                                        Openhab: {openhab}\n\
                                        Value: {payload}'.format(**self._data))

                    else:
                        self._log.error('Openhab update failed:\n\
                                         Node: {nodeid},\n\
                                         Sensor: {childid}\n\
                                         Openhab: {openhab}\n\
                                         Value: {payload}'.format(**self._data))

                else:
                    self._log.error('Openhab cant parse value: {0} for item: {1}'.format(self._data['payload'],\
                                                                                         self._data['openhab']))

            else:
                self._log.error('Openhab entry missing: {0}'.format(self._data))
Esempio n. 3
0
 def publish(self, msg, info = None):
 
     self._data = tools.checkKeys(msg, ('nodeid', 'childid', 'payload'))
     if self._data and self.connected:
         self._log.debug('Try to publish values to the MQTT Broker on {0}: {1}'.format(config.MQTTBroker,
                                                                                        msg))
         topic = "/" + config.MQTTTopic
         try:
             topic = topic.replace(
                 '%nodeid', self._data['nodeid']).replace(
                 '%childid', self._data['childid']).replace(
                 '%sensorid', self._data['childid'])
            
             if not (info is None):
                 topic=topic.replace(
                     '%childdescription', info.description).replace(
                     '%sensordescription', info.description)
        
             self.__publish(topic, self._data['payload'])
         except Exception, e:
          self._log.error('MQTT Publish failed: Failed to create topic');