@@ -69,10 +69,7 @@ def on_connect(self, client, userdata, flags, rc):
69
69
log .error (flags )
70
70
71
71
def on_message (self , client , userdata , message ):
72
- log .debug ("message received '" , str (message .payload .decode ("utf-8" )),
73
- "' via topic '" , message .topic , "' (retained=" , message .retain , ")." )
74
-
75
- subscriber = self .subscribers .get (message .topic , None )
72
+ subscriber = self .subscribers .get (message .topic .lower (), None )
76
73
if subscriber is not None :
77
74
data = None
78
75
try :
@@ -99,17 +96,19 @@ def publish(self, topic: str, value: str):
99
96
def rpc_subscribe (self , src ):
100
97
t = src .topic_prefix
101
98
if self .topic_prefix is not None :
102
- t = self .topic_prefix + '/' + t + '/rpc'
103
- self .subscribers [t ] = src
104
- self .client .subscribe (t , 0 )
99
+ t = self .topic_prefix + '/' + t
100
+ self .subscribers [t .lower () + '/rpc' ] = src
101
+ log .info (f"Subscribing to rpc topic { t } /rpc." )
102
+ self .client .subscribe (t + '/rpc' , 0 )
105
103
106
104
def rpc_unsubscribe (self , src ):
107
105
t = src .topic_prefix
108
106
if self .topic_prefix is not None :
109
- t = self .topic_prefix + '/' + t + '/rpc'
110
- if self .subscribers .get (t , None ) is not None :
111
- self .client .unsubscribe (t )
112
- del self .subscribers [t ]
107
+ t = self .topic_prefix + '/' + t
108
+ if self .subscribers .get (t .lower () + '/rpc' , None ) is not None :
109
+ log .info (f"Unsubscribing from rpc topic { t } /rpc." )
110
+ self .client .unsubscribe (t + '/rpc' )
111
+ del self .subscribers [t .lower () + '/rpc' ]
113
112
114
113
115
114
class Register :
@@ -206,21 +205,24 @@ def set_value(self, src, params):
206
205
for c in self .coils :
207
206
name = re .sub (r'/\s\s+/g' , '_' , str (c ["name" ]).strip ())
208
207
if cname == name or cname == str (c ["name" ]):
209
- if 'w' in c ["mode" ]:
208
+ if 'w' not in c ["mode" ]:
210
209
# Can't write to this coil
210
+ log .info ("Could not write becaue coil mode is set to read-only." )
211
211
return False
212
212
coil = c
213
213
break
214
214
215
215
if coil is None :
216
216
return False
217
217
218
- if value == c ["on_value" ] or bool (value ):
218
+ if value == c ["on_value" ] or ( not isinstance ( value , str ) and bool (value ) is True ):
219
219
value = True
220
220
else :
221
221
value = False
222
222
223
- rr = src .client .write_coil (self .start + int (coil ["bit" ]), value , slave = unitid )
223
+ addr = self .start + int (coil ["bit" ]) - 1
224
+ log .debug (f"Writing coil at address { addr } with value { value } ." )
225
+ rr = src .client .write_coil (addr , value , slave = unitid )
224
226
if not rr :
225
227
raise ModbusException ("Received empty modbus respone." )
226
228
if rr .isError ():
@@ -349,8 +351,8 @@ def poller_thread(self):
349
351
if not msg :
350
352
continue
351
353
352
- name = msg .get ("target" , " None" )
353
- if not name :
354
+ name = msg .get ("target" , None )
355
+ if name is None :
354
356
continue
355
357
356
358
# Find the target
@@ -360,6 +362,10 @@ def poller_thread(self):
360
362
target = r
361
363
break
362
364
365
+ if target is None :
366
+ log .info (f"Could not find rpc message target { name } " )
367
+ continue
368
+
363
369
match msg .get ("method" , "invalid" ):
364
370
365
371
case "set" :
0 commit comments