Compare commits

..

No commits in common. "5425af984b01f56ba4b4530d75e103cfdff0b585" and "cd9ab64d545f2664450a6d24811c4ae75e48a3db" have entirely different histories.

3 changed files with 15 additions and 39 deletions

View File

@ -6,8 +6,7 @@ import sys
SERVICE_FILE = """ SERVICE_FILE = """
[Unit] [Unit]
Description=Powerplug Service Description=Powerplug Service
After=network-online.target After=mosquitto.target
Wants=network-online.target
[Service] [Service]
User=%(UID)d User=%(UID)d
Group=%(GID)d Group=%(GID)d

View File

@ -6,17 +6,17 @@ import report
MQTT_USER = "user" MQTT_USER = "user"
MQTT_PASS = "pass" MQTT_PASS = "pass"
MQTT_SERVER = "localhost" MQTT_SERVER = "localhost"
MQTT_TOPIC = "dirk/powerplug" MQTT_TOPIC = "hifi/powerplug"
# #
# Logging # Logging
# #
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__)) __BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
APP_NAME = "powerplug" APP_NAME = "powerplug"
LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout' LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout'
LOGLVL = 'DEBUG' LOGLVL = 'DEBUG'
LOGHOST = 'cutelog' LOGHOST = 'cutelog'
LOGPORT = 19996 LOGPORT = 19996
formatter = report.SHORT_FMT formatter = report.LONG_FMT

View File

@ -51,13 +51,6 @@ class sispmctl(object):
logger.info('sispmctl channel %s changed to %s', output, state) logger.info('sispmctl channel %s changed to %s', output, state)
self.publish_states() 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): def get_out_states(self):
try: try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"]) out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
@ -77,10 +70,7 @@ class sispmctl(object):
def send_state(self, output): def send_state(self, output):
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: self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
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): class mqtt_powerplug(object):
@ -89,40 +79,27 @@ class mqtt_powerplug(object):
"set/2", "set/2",
"set/3", "set/3",
"set/4", "set/4",
"set/all", "set/all"
"toggle/1",
"toggle/2",
"toggle/3",
"toggle/4",
"toggle/all",
] ]
def __init__(self): def __init__(self):
self.__client__ = mqtt.Client("mqtt_powerplug") # create client object self.__client__ = mqtt.Client("mqtt_powerplug") # create client object
self.__client__.on_message = self.__receive__ # attach function to callback 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__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
try: self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection self.__client__.loop_start() # start the loop
self.__client__.loop_start() # start the loop self.__topics__ = []
self.__topics__ = [] for subtopic in self.SUBTOPICS:
for subtopic in self.SUBTOPICS: self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic) self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
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__) self.__sc__ = sispmctl(self.__client__)
def __receive__(self, client, userdata, message): def __receive__(self, client, userdata, message):
if message.topic in self.__topics__: if message.topic in self.__topics__:
output = message.topic.split("/")[-1] output = message.topic.split("/")[-1]
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:
logger.info("Received request to toggle output channel %s", output)
if message.payload != b"false":
self.__sc__.toggle_out_state(output)
else: else:
logger.warning("Ignoring unknown mqtt topic %s", message.topic) logger.warning("Ignoring unknown mqtt topic %s", message.topic)