-
Notifications
You must be signed in to change notification settings - Fork 5
/
RFXcom-MQTT.py
executable file
·105 lines (82 loc) · 2.53 KB
/
RFXcom-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
#!/usr/bin/python
from RFXtrx.pyserial import PySerialTransport
from RFXtrx import LightingDevice
import mosquitto,sys
import json
import thread
import time
from RFXtrx.lowlevel import Lighting2
PORT = '/dev/ttyUSB0'
LISTEN = True
PREFIX = "rfxcom"
MQTT_HOST = "localhost"
def on_connect(mosq, rc,a):
mosq.subscribe(PREFIX+"/#", 0)
def on_message(a,mosq, msg):
global transport
try:
print("RECIEVED MQTT MESSAGE: "+msg.topic + " " + str(msg.payload))
topics = msg.topic.split("/")
name = topics[-2]
if topics[-1] == "set":
value = msg.payload.upper()
if value == "ON":
value = 100
elif value == "OFF":
value = 0
value = int(value)
#print "Set command"
#Implemented support for Lightening2 only
if topics[-4] == "17":
print "Seting Lighting2 level"
print topics
print value
pkt = Lighting2()
#pkt.parse_id(topics[-3],topics[-2])
code = topics[-2].split(":")
pkt.id_combined = int(code[0],16)
pkt.unitcode = int(code[1])
pkt.subtype = int(topics[-3])
pkt.packettype = int(topics[-4])
device = LightingDevice(pkt)
if value == 0:
device.send_off(transport)
elif value == 100:
device.send_on(transport)
else:
device.send_dim(transport,value)
except:
print "Error when parsing incomming message."
return
def ControlLoop():
# schedule the client loop to handle messages, etc.
print "Starting MQTT listener"
client.loop_forever()
print "Closing connection to MQTT"
time.sleep(1)
transport = PySerialTransport(PORT, debug=True)
#transport.reset()
client = mosquitto.Mosquitto("RFXcom-client")
client.username_pw_set("driver","1234")
client.will_set(topic = "system/RFXcom", payload="Offline", qos=1, retain=True)
#Connect and notify others of our presence.
client.connect(MQTT_HOST, keepalive=10)
client.publish("system/" + PREFIX, "Online",1,True)
client.on_connect = on_connect
client.on_message = on_message
#Start tread...
#thread.start_new_thread(ControlLoop,())
client.loop_start()
while True:
event = transport.receive_blocking()
if event == None:
continue
for value in event.values:
topic = PREFIX +"/"+ str(event.device.packettype) + "/" + str(event.device.subtype) + "/" + event.device.id_string+"/"+value
print topic + " " + str(event.values[value])
#print "DEBUG"
#print event.device.id_combined
#print event.device.unitcode
client.publish(topic , event.values[value], 1)
print event
client.disconnect()