Module mqtt implemented
This commit is contained in:
parent
e8334669e8
commit
70600ba2e7
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
|
47
powerplug.py
47
powerplug.py
@ -1,6 +1,6 @@
|
|||||||
import config
|
import config
|
||||||
import logging
|
import logging
|
||||||
import paho.mqtt.client as paho
|
import mqtt
|
||||||
import report
|
import report
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -72,53 +72,32 @@ class sispmctl(object):
|
|||||||
topic = config.MQTT_TOPIC + "/status/" + str(output)
|
topic = config.MQTT_TOPIC + "/status/" + str(output)
|
||||||
logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
|
logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
|
||||||
try:
|
try:
|
||||||
self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
|
self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false")
|
||||||
except (socket.timeout, OSError) as e:
|
except (socket.timeout, OSError) as e:
|
||||||
logger.warning("Erro while sending state information information")
|
logger.warning("Erro while sending state information information")
|
||||||
|
|
||||||
|
|
||||||
class mqtt_powerplug(object):
|
class mqtt_powerplug(mqtt.mqtt_client):
|
||||||
SUBTOPICS = [
|
|
||||||
"set/1",
|
|
||||||
"set/2",
|
|
||||||
"set/3",
|
|
||||||
"set/4",
|
|
||||||
"set/all",
|
|
||||||
"toggle/1",
|
|
||||||
"toggle/2",
|
|
||||||
"toggle/3",
|
|
||||||
"toggle/4",
|
|
||||||
"toggle/all",
|
|
||||||
]
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
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
|
for target in ["1", "2", "3", "4", "all"]:
|
||||||
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
self.add_callback(config.MQTT_TOPIC + "/set/" + target, self.__set__)
|
||||||
try:
|
self.add_callback(config.MQTT_TOPIC + "/toggle/" + target, self.__toggle__)
|
||||||
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
#
|
||||||
self.__client__.loop_start() # start the loop
|
self.__sc__ = sispmctl(self)
|
||||||
self.__topics__ = []
|
|
||||||
for subtopic in self.SUBTOPICS:
|
|
||||||
self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
|
|
||||||
self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
|
|
||||||
except (socket.timeout, OSError) as e:
|
|
||||||
logger.warning("Erro while setting up mqtt instance and listener")
|
|
||||||
self.__sc__ = sispmctl(self.__client__)
|
|
||||||
|
|
||||||
def __receive__(self, client, userdata, message):
|
def __set__(self, client, userdata, message):
|
||||||
if message.topic in self.__topics__:
|
|
||||||
output = message.topic.split("/")[-1]
|
output = message.topic.split("/")[-1]
|
||||||
if message.topic.find("set") >= 0:
|
if message.topic.find("set") >= 0:
|
||||||
state = message.payload == b"true"
|
state = message.payload == b"true"
|
||||||
logger.info("Received request to set output channel %s to state %s", output, str(state))
|
logger.info("Received request to set output channel %s to state %s", output, str(state))
|
||||||
self.__sc__.set_out_state(output, state)
|
self.__sc__.set_out_state(output, state)
|
||||||
elif message.topic.find("toggle") >= 0:
|
|
||||||
|
def __toggle__(self, client, userdata, message):
|
||||||
|
output = message.topic.split("/")[-1]
|
||||||
logger.info("Received request to toggle output channel %s", output)
|
logger.info("Received request to toggle output channel %s", output)
|
||||||
if message.payload != b"false":
|
if message.payload != b"false":
|
||||||
self.__sc__.toggle_out_state(output)
|
self.__sc__.toggle_out_state(output)
|
||||||
else:
|
|
||||||
logger.warning("Ignoring unknown mqtt topic %s", message.topic)
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.__client__.loop_stop() # stop the loop
|
self.__client__.loop_stop() # stop the loop
|
||||||
|
Loading…
x
Reference in New Issue
Block a user