forked from Anton04/sispmctrl-MQTT-bridge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsispmctl-to-MQTT.py
executable file
·183 lines (157 loc) · 5.1 KB
/
sispmctl-to-MQTT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
import ConfigParser
import json
import os
import sys
import thread
import time
import paho.mqtt.client as mqtt
# The MQTT format for setting outlets are cmnd/<prefix>/<device_id>/POWER<outlet>
POLL_TIME = 5 # For checking state changes on outlets.
QOS = 1
def socket_off(device, socket):
global states
#print("turn socket", socket, "off")
os.system("/usr/bin/sispmctl -q -D %s -f %i" % (device, socket))
states[device]=socket_get_state(device)
publish_state(device)
def socket_on(device, socket):
global states
#print("turn socket", socket, "on")
os.system("/usr/bin/sispmctl -q -D %s -o %i" % (device, socket))
states[device]=socket_get_state(device)
publish_state(device)
def socket_get_state(device):
#print("get state of all socket",device)
result = {}
for line in os.popen("/usr/bin/sispmctl -q -D %s -n -g all" % device).readlines():
if line[0] == "0":
result["POWER"+str(len(result)+1)]="OFF"
elif line[0] == "1":
result["POWER"+str(len(result)+1)]="ON"
return result
def devices_get_serialnumbers():
#generate device list: {'01:02:03:04:05': 0}
discovered_devices = {}
discovered_states = {}
for line in os.popen("/usr/bin/sispmctl -s ").readlines():
if line.find("serial number") != -1:
serial = line.strip("\n").split(" ")[1]
discovered_devices[serial] = len(discovered_devices)
discovered_states[serial] = socket_get_state(serial)
return (discovered_devices,discovered_states)
def publish_state(device):
global states
topic = "tele/"+ prefix +"/"+ device +"/STATE"
#message = str(states[device])
message = json.dumps(states[device], sort_keys=True)
client.publish(topic, message , QOS)
print("SENT MQTT MESSAGE: "+topic+ " " + message)
def on_connect(mqtt, rc, a):
mqtt.subscribe("cmnd/"+prefix+"/#", 0)
def on_message(a, mqtt, msg):
global devices
# try:
if True:
print("RECEIVED MQTT MESSAGE: "+msg.topic + " " + str(msg.payload))
#cmnd/<prefix>/<serial>/POWER<socket+1> = <ON|OFF|0|1>
topics = msg.topic.split("/")
socket = int(topics[-1][-1])
#print("socket: "+str(socket))
value = msg.payload
device = topics[-2]
if not device in devices:
return
if value.upper() == "ON" or value == "1":
# Turn on
socket_on(device, socket)
else:
# Turn off
socket_off(device, socket)
# except:
# print "Error occured while processing command"
return
def ControlLoop():
# schedule the client loop to handle messages, etc.
print("Starting MQTT listener")
while True:
client.loop()
time.sleep(0.1)
# Get all devices.
if __name__ == '__main__':
# Where am I
path = os.path.abspath(os.path.dirname(sys.argv[0]))
# Load config file...
try:
ConfigFile = sys.argv[1]
except:
ConfigFile = path + "/sispmctl.conf"
try:
f = open(ConfigFile, "r")
f.close()
except:
try:
ConfigFile = path + "/sispmctl.conf"
f = open(ConfigFile, "r")
f.close()
except:
print("Please provide a valid config file! By argument or as default sispmctl.conf file.")
exit(1)
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.read(ConfigFile)
# Load basic config.
ip = config.get("MQTTServer", "Address")
port = config.get("MQTTServer", "Port")
user = config.get("MQTTServer", "User")
password = config.get("MQTTServer", "Password")
prefix = config.get("MQTTServer", "Prefix")
instance = config.get("MQTTServer", "Instance")
client = mqtt.Client("sispmctl-to-MQTT-client")
if user != None:
client.username_pw_set(user, password)
client.will_set(topic="tele/" + prefix+"/"+instance+"/LWT",
payload="Offline", qos=1, retain=True)
# Connect and notify others of our presence.
client.connect(ip)
client.publish("tele/"+prefix+"/"+instance+"/LWT", "Online", 1, retain=True)
client.on_connect = on_connect
client.on_message = on_message
client.subscribe("cmnd/"+prefix+"/#", 0)
# Init
devices = {}
states = {}
# Start tread...
thread.start_new_thread(ControlLoop, ())
while True:
# Check if anything changed
# Send update if it did.
# Detect devices
old_devices = devices
old_states = states
devices,states = devices_get_serialnumbers()
# Look for disconnections..
for device in old_devices:
if not device in devices:
print("device disconnected: "+device)
topic = "tele/"+prefix +"/"+ device + "/LWT"
client.publish(topic , False, QOS)
# Look for devices being connected..
for device in devices:
if not device in old_devices:
print("device connected: "+device)
topic = "tele/"+prefix +"/"+ device + "/LWT"
client.publish(topic , True, QOS)
# Detect states changes on outlets
#print(devices)states[device] != old_states[device]:
for device in devices:
if not device in old_states:
#no old state, publish state
publish_state(device)
else:
# old state exists, only publish if changed
# compare stateclient.publish(topic, str(states[device]), QOS)
#print("newstate: "+str(states[device])+" oldstate: "+str(old_states[device]))
if states[device] != old_states[device]:
publish_state(device)
time.sleep(POLL_TIME)
client.disconnect()