14
14
import paho .mqtt .publish as publish
15
15
import paho .mqtt .client as mqtt
16
16
17
+
17
18
def msg (text ):
18
- print ("%s : %s" % (datetime .now ().strftime ("%Y-%m-%d %H:%M:%S.%f" ),text ))
19
+ print ("%s : %s" % (datetime .now ().strftime ("%Y-%m-%d %H:%M:%S.%f" ), text ))
20
+
19
21
20
22
def getenv ():
21
23
conf = {
@@ -54,7 +56,7 @@ def getenv():
54
56
55
57
class Ec133 :
56
58
57
- def __init__ (self , serconf , ecconf , callback = False ):
59
+ def __init__ (self , serconf , ecconf , callback = False ):
58
60
self .serconf = serconf
59
61
self .ecconf = ecconf
60
62
self .ser = False
@@ -64,15 +66,15 @@ def __init__(self, serconf, ecconf , callback=False):
64
66
self .brightness = [255 , 255 , 255 ]
65
67
self .register = [255 , 255 , 255 ]
66
68
self .lock = threading .Lock ()
67
-
69
+
68
70
def __del__ (self ):
69
71
msg ("Closing serial device" )
70
72
if bool (self .rtu ):
71
73
del self .rtu
72
74
if bool (self .ser ):
73
75
del self .ser
74
76
75
- def set_callback (self ,callback ):
77
+ def set_callback (self , callback ):
76
78
self .callback = callback
77
79
78
80
def connect (self ):
@@ -102,28 +104,29 @@ def connect(self):
102
104
raise e
103
105
104
106
def set_channel (self , client , userdata , message ):
107
+
105
108
ch = int (userdata ['channel' ])
106
109
107
- payload_str = str (message .payload .decode ("utf-8" ))
108
-
109
110
try :
110
- payload = json .loads (payload_str )
111
+ payload = json .loads (message . payload . decode ( "utf-8" ) )
111
112
except Exception as e :
112
- msg ("Channel%s : Malformed json message : %s" % (ch ,e ))
113
+ msg ("Channel%s : Malformed json message : %s" % (ch , e ))
113
114
return
114
115
115
116
if type (payload ) is not dict :
116
- msg ("Channel%s : mqtt_json format expected , got %s!" % (ch ,type (payload )))
117
+ msg ("Channel%s : mqtt_json format expected , got %s!" % (ch , type (payload )))
117
118
return
118
119
119
120
self .lock .acquire (blocking = True , timeout = - 1 )
120
121
121
- msg ("Channel%s: %s" % (ch ,payload ))
122
-
123
- if payload .get ("brightness" ,False ):
122
+ msg ("Channel%s command : %s" % (ch , payload ))
123
+
124
+ if payload .get ("brightness" , False ):
124
125
self .brightness [ch ] = int (payload ['brightness' ])
126
+ else :
127
+ payload ['brightness' ] = int (self .brightness [ch ])
125
128
126
- if payload .get ('state' ,'ON' ) == 'ON' :
129
+ if payload .get ('state' , 'ON' ) == 'ON' :
127
130
self .register [ch ] = int (self .brightness [ch ])
128
131
else :
129
132
self .register [ch ] = int (0 )
@@ -143,7 +146,8 @@ def set_channel(self, client, userdata, message):
143
146
else :
144
147
time .sleep (0.02 )
145
148
if bool (self .callback ):
146
- self .callback (ch ,payload_str )
149
+
150
+ self .callback (ch , json .dumps (payload ))
147
151
self .lock .release ()
148
152
149
153
@@ -177,33 +181,37 @@ def consume_all(self):
177
181
for ch , topic in self .ctopics .items ():
178
182
self ._consume_topic (ch )
179
183
180
- def postback (self ,ch ,payload ):
184
+ def postback (self , ch , payload ):
181
185
auth = None
182
186
if self .mqconf ['username' ] != None :
183
- auth = { 'username' : self .mqconf ['username' ],
187
+ auth = {'username' : self .mqconf ['username' ],
184
188
'password' : self .mqconf ['password' ]
185
- }
189
+ }
186
190
187
191
try :
188
192
publish .single (self .stopics [str (ch )],
189
- hostname = self .mqconf ['address' ],
190
- port = self .mqconf ['port' ],
191
- auth = auth ,
192
- payload = payload ,
193
- qos = self .mqconf ['qos' ],
194
- keepalive = 15 ,
195
- retain = True )
193
+ hostname = self .mqconf ['address' ],
194
+ port = self .mqconf ['port' ],
195
+ auth = auth ,
196
+ payload = payload ,
197
+ qos = self .mqconf ['qos' ],
198
+ keepalive = 15 ,
199
+ retain = True )
196
200
except Exception as e :
197
- msg ("Unable to send channel%s state update : %s" % (ch ,e ))
201
+ msg ("Unable to send channel%s state update : %s" % (ch , e ))
198
202
else :
199
- msg ("Channel%s state update sent " % ch )
203
+ msg ("Channel%s state: %s " % ( ch , payload ) )
200
204
201
205
202
206
def main ():
203
207
"""
204
208
Main routine
205
209
"""
206
-
210
+
211
+ # signals are only used to break out of signal.pause()
212
+ signal .signal (signal .SIGINT , (lambda signum , frame : None ))
213
+ signal .signal (signal .SIGTERM , (lambda signum , frame : None ))
214
+
207
215
msg ("Start ..." )
208
216
conf = getenv ()
209
217
@@ -212,7 +220,8 @@ def main():
212
220
ec .connect ()
213
221
214
222
msg ("Consume mqtt topics" )
215
- mq = Mqtt (conf ['mqtt' ], conf ['ec133' ]['command_topics' ], conf ['ec133' ]['state_topics' ], ec .set_channel )
223
+ mq = Mqtt (conf ['mqtt' ], conf ['ec133' ]['command_topics' ],
224
+ conf ['ec133' ]['state_topics' ], ec .set_channel )
216
225
mq .consume_all ()
217
226
ec .set_callback (mq .postback )
218
227
@@ -225,4 +234,3 @@ def main():
225
234
226
235
if __name__ == "__main__" :
227
236
main ()
228
-
0 commit comments