Module mqtt implemented
This commit is contained in:
parent
934992d275
commit
01fc7408ff
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +1,6 @@
|
|||||||
[submodule "report"]
|
[submodule "report"]
|
||||||
path = report
|
path = report
|
||||||
url = https://git.mount-mockery.de/pylib/report.git
|
url = https://git.mount-mockery.de/pylib/report.git
|
||||||
|
[submodule "mqtt"]
|
||||||
|
path = mqtt
|
||||||
|
url = https://git.mount-mockery.de/pylib/mqtt.git
|
||||||
|
1
mqtt
Submodule
1
mqtt
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 3d9fc164b4a2cca4a528bd71f04b87a50cd5a078
|
@ -2,7 +2,7 @@ import config
|
|||||||
import json
|
import json
|
||||||
import lirc
|
import lirc
|
||||||
import logging
|
import logging
|
||||||
import paho.mqtt.client as paho
|
import mqtt
|
||||||
import report
|
import report
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
@ -14,26 +14,19 @@ except ImportError:
|
|||||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
||||||
|
|
||||||
|
|
||||||
class remote_control(object):
|
class remote_control(mqtt.mqtt_client):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__lirc_client__ = None
|
self.__lirc_client__ = None
|
||||||
self.__lirc_init__()
|
self.__lirc_init__()
|
||||||
#
|
#
|
||||||
self.__start_data__ = None
|
self.__start_data__ = None
|
||||||
#
|
#
|
||||||
self.__client__ = paho.Client(config.APP_NAME) # create client object
|
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
||||||
self.__client__.on_message = self.__receive__ # attach function to callback
|
#
|
||||||
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
for remote in config.SUPPORTED_REMOTES:
|
||||||
try:
|
for command in config.SUPPORTED_REMOTES[remote]:
|
||||||
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
topic = config.MQTT_TOPIC + "/" + remote + "/" + command
|
||||||
self.__client__.loop_start() # start the loop
|
self.add_callback(topic, self.mqtt_to_lirc)
|
||||||
for remote in config.SUPPORTED_REMOTES:
|
|
||||||
for command in config.SUPPORTED_REMOTES[remote]:
|
|
||||||
topic = config.MQTT_TOPIC + "/" + remote + "/" + command
|
|
||||||
logger.debug("Subscribing \"%s\"", topic)
|
|
||||||
self.__client__.subscribe(topic) # subscibe a topic
|
|
||||||
except (socket.timeout, OSError) as e:
|
|
||||||
logger.warning("Erro while setting up mqtt instance and listener")
|
|
||||||
|
|
||||||
def __lirc_init__(self):
|
def __lirc_init__(self):
|
||||||
if self.__lirc_client__ is not None:
|
if self.__lirc_client__ is not None:
|
||||||
@ -42,7 +35,7 @@ class remote_control(object):
|
|||||||
logger.info("Initiating lirc connection")
|
logger.info("Initiating lirc connection")
|
||||||
self.__lirc_client__ = lirc.Client()
|
self.__lirc_client__ = lirc.Client()
|
||||||
|
|
||||||
def __receive__(self, client, userdata, message):
|
def mqtt_to_lirc(self, client, userdata, message):
|
||||||
try:
|
try:
|
||||||
payload = json.loads(message.payload)
|
payload = json.loads(message.payload)
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user