Compare commits
3 Commits
cd9ab64d54
...
5425af984b
Author | SHA1 | Date | |
---|---|---|---|
5425af984b | |||
eb24a3ee5d | |||
33a68f3c0e |
@ -6,7 +6,8 @@ import sys
|
||||
SERVICE_FILE = """
|
||||
[Unit]
|
||||
Description=Powerplug Service
|
||||
After=mosquitto.target
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
[Service]
|
||||
User=%(UID)d
|
||||
Group=%(GID)d
|
||||
|
@ -6,17 +6,17 @@ import report
|
||||
MQTT_USER = "user"
|
||||
MQTT_PASS = "pass"
|
||||
MQTT_SERVER = "localhost"
|
||||
MQTT_TOPIC = "hifi/powerplug"
|
||||
MQTT_TOPIC = "dirk/powerplug"
|
||||
|
||||
#
|
||||
# Logging
|
||||
#
|
||||
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
|
||||
APP_NAME = "powerplug"
|
||||
LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout'
|
||||
LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout'
|
||||
LOGLVL = 'DEBUG'
|
||||
|
||||
LOGHOST = 'cutelog'
|
||||
LOGPORT = 19996
|
||||
|
||||
formatter = report.LONG_FMT
|
||||
formatter = report.SHORT_FMT
|
||||
|
45
powerplug.py
45
powerplug.py
@ -51,6 +51,13 @@ class sispmctl(object):
|
||||
logger.info('sispmctl channel %s changed to %s', output, state)
|
||||
self.publish_states()
|
||||
|
||||
def toggle_out_state(self, output):
|
||||
if output == "all":
|
||||
for i in range(1,5):
|
||||
self.toggle_out_state(str(i))
|
||||
else:
|
||||
self.set_out_state(output, not self.__state__[int(output) - 1])
|
||||
|
||||
def get_out_states(self):
|
||||
try:
|
||||
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
|
||||
@ -70,7 +77,10 @@ class sispmctl(object):
|
||||
def send_state(self, 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]))
|
||||
self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
|
||||
try:
|
||||
self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
|
||||
except (socket.timeout, OSError) as e:
|
||||
logger.warning("Erro while sending state information information")
|
||||
|
||||
|
||||
class mqtt_powerplug(object):
|
||||
@ -79,27 +89,40 @@ class mqtt_powerplug(object):
|
||||
"set/2",
|
||||
"set/3",
|
||||
"set/4",
|
||||
"set/all"
|
||||
"set/all",
|
||||
"toggle/1",
|
||||
"toggle/2",
|
||||
"toggle/3",
|
||||
"toggle/4",
|
||||
"toggle/all",
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
self.__client__ = mqtt.Client("mqtt_powerplug") # create client object
|
||||
self.__client__.on_message = self.__receive__ # attach function to callback
|
||||
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
||||
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
||||
self.__client__.loop_start() # start the loop
|
||||
self.__topics__ = []
|
||||
for subtopic in self.SUBTOPICS:
|
||||
self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
|
||||
self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
|
||||
try:
|
||||
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
||||
self.__client__.loop_start() # start the loop
|
||||
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):
|
||||
if message.topic in self.__topics__:
|
||||
output = message.topic.split("/")[-1]
|
||||
state = message.payload == b"true"
|
||||
logger.info("Received request to set output channel %s to state %s", output, str(state))
|
||||
self.__sc__.set_out_state(output, state)
|
||||
if message.topic.find("set") >= 0:
|
||||
state = message.payload == b"true"
|
||||
logger.info("Received request to set output channel %s to state %s", output, str(state))
|
||||
self.__sc__.set_out_state(output, state)
|
||||
elif message.topic.find("toggle") >= 0:
|
||||
logger.info("Received request to toggle output channel %s", output)
|
||||
if message.payload != b"false":
|
||||
self.__sc__.toggle_out_state(output)
|
||||
else:
|
||||
logger.warning("Ignoring unknown mqtt topic %s", message.topic)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user