Skip to content

Commit 895c2e2

Browse files
Merge pull request sixfab#76 from sixfab/fix/f-string-concatation-bug
fix: Fix f-sting concatenation bug on mqtt.py set_will_config function
2 parents 28a110f + ae1ec81 commit 895c2e2

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

core/modules/mqtt.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class MQTT:
1010
"""
1111
Class for including functions of MQTT related operations of picocell module.
1212
"""
13-
CTRL_Z = '\x1A'
13+
14+
CTRL_Z = "\x1A"
1415

1516
def __init__(self, atcom):
1617
"""
@@ -154,7 +155,7 @@ def set_timeout_config(self, cid=0, timeout=5, retry_count=3, timeout_notice=0):
154155

155156
def set_will_config(
156157
self, will_topic, will_message, cid=0, will_flag=0, will_qos=0, will_retain=0
157-
):
158+
):
158159
"""
159160
Function for setting modem MQTT will configuration
160161
@@ -190,8 +191,10 @@ def set_will_config(
190191
Result that includes "status" and "response" keys
191192
"""
192193

193-
command = f'AT+QMTCFG="will",{cid},{will_flag},{will_qos},'\
194-
f'{will_retain},"{will_topic}","{will_message}"'
194+
command = (
195+
f'AT+QMTCFG="will",{cid},{will_flag},{will_qos},'
196+
+ f'{will_retain},"{will_topic}","{will_message}"'
197+
)
195198
return self.atcom.send_at_comm(command)
196199

197200
def set_message_recieve_mode_config(self, cid=0, message_recieve_mode=0):
@@ -234,10 +237,10 @@ def open_connection(self, host=None, port=None, cid=0):
234237
Result that includes "status" and "response" keys
235238
"""
236239
if host is None:
237-
host = get_parameter(["mqtts","host"])
240+
host = get_parameter(["mqtts", "host"])
238241

239242
if port is None:
240-
port = get_parameter(["mqtts","port"], 8883) # default port is 8883
243+
port = get_parameter(["mqtts", "port"], 8883) # default port is 8883
241244

242245
if host and port:
243246
command = f'AT+QMTOPEN={cid},"{host}",{port}'
@@ -251,10 +254,12 @@ def open_connection(self, host=None, port=None, cid=0):
251254
f"+QMTOPEN: {cid},3",
252255
f"+QMTOPEN: {cid},4",
253256
f"+QMTOPEN: {cid},5",
254-
]
257+
]
255258

256259
if result["status"] == Status.SUCCESS:
257-
result = self.atcom.get_urc_response(desired_response, fault_responses, timeout=60)
260+
result = self.atcom.get_urc_response(
261+
desired_response, fault_responses, timeout=60
262+
)
258263
return result
259264
return {"status": Status.ERROR, "response": "Missing parameters : host"}
260265

@@ -290,15 +295,17 @@ def close_connection(self, cid=0):
290295
dict
291296
Result that includes "status" and "response" keys
292297
"""
293-
command = f'AT+QMTCLOSE={cid}'
294-
result = self.atcom.send_at_comm(command)
298+
command = f"AT+QMTCLOSE={cid}"
299+
result = self.atcom.send_at_comm(command)
295300

296301
if result["status"] == Status.SUCCESS:
297302
desired_response = f"+QMTCLOSE: {cid},0"
298303
result = self.atcom.get_urc_response(desired_response, timeout=60)
299304
return result
300305

301-
def connect_broker(self, client_id_string="Picocell", username=None, password=None, cid=0):
306+
def connect_broker(
307+
self, client_id_string="Picocell", username=None, password=None, cid=0
308+
):
302309
"""
303310
Function for connecting to MQTT broker. This function is used when a client requests a
304311
connection to the MQTT server. When a TCP/IP socket connection is established between
@@ -321,8 +328,8 @@ def connect_broker(self, client_id_string="Picocell", username=None, password=No
321328
Result that includes "status" and "response" keys
322329
"""
323330
if username is None and password is None:
324-
username = get_parameter(["mqtts","username"])
325-
password = get_parameter(["mqtts","password"])
331+
username = get_parameter(["mqtts", "username"])
332+
password = get_parameter(["mqtts", "password"])
326333

327334
if username and password:
328335
command = f'AT+QMTCONN={cid},"{client_id_string}","{username}","{password}"'
@@ -334,7 +341,9 @@ def connect_broker(self, client_id_string="Picocell", username=None, password=No
334341
if result["status"] == Status.SUCCESS:
335342
desired_response = f"+QMTCONN: {cid},0,0"
336343
fault_responses = [f"QMTSTAT: 0,{err_code}" for err_code in range(1, 8)]
337-
result = self.atcom.get_urc_response(desired_response, fault_responses, timeout=60)
344+
result = self.atcom.get_urc_response(
345+
desired_response, fault_responses, timeout=60
346+
)
338347
return result
339348

340349
def is_connected_to_broker(self, cid=0):
@@ -369,7 +378,7 @@ def disconnect_broker(self, cid=0):
369378
dict
370379
Result that includes "status" and "response" keys
371380
"""
372-
command = f'AT+QMTDISC={cid}'
381+
command = f"AT+QMTDISC={cid}"
373382
return self.atcom.send_at_comm(command)
374383

375384
def subscribe_topics(self, topics=None, cid=0, message_id=1):
@@ -398,10 +407,10 @@ def subscribe_topics(self, topics=None, cid=0, message_id=1):
398407
Result that includes "status" and "response" keys
399408
"""
400409
if topics is None:
401-
topics = get_parameter(["mqtts","sub_topics"])
410+
topics = get_parameter(["mqtts", "sub_topics"])
402411

403412
if topics:
404-
prefix = f'AT+QMTSUB={cid},{message_id},'
413+
prefix = f"AT+QMTSUB={cid},{message_id},"
405414
command = prefix + ",".join(f'"{topic}",{qos}' for topic, qos in topics)
406415
result = self.atcom.send_at_comm(command)
407416

@@ -433,7 +442,9 @@ def unsubscribe_topic(self, topic, cid=0, message_id=1):
433442
command = f'AT+QMTUNS={cid},{message_id},"{topic}"'
434443
return self.atcom.send_at_comm(command)
435444

436-
def publish_message(self, payload, topic=None, qos=1, retain=0, message_id=1, cid=0):
445+
def publish_message(
446+
self, payload, topic=None, qos=1, retain=0, message_id=1, cid=0
447+
):
437448
"""
438449
Function for publishing MQTT message. This function is used when a client requests
439450
a message to be published. This method uses data mode of the modem to send the message.
@@ -468,15 +479,17 @@ def publish_message(self, payload, topic=None, qos=1, retain=0, message_id=1, ci
468479
Result that includes "status" and "response" keys
469480
"""
470481
if topic is None:
471-
topic = get_parameter(["mqtts","pub_topic"])
482+
topic = get_parameter(["mqtts", "pub_topic"])
472483

473484
if payload and topic:
474485
command = f'AT+QMTPUB={cid},{message_id},{qos},{retain},"{topic}"'
475486
result = self.atcom.send_at_comm(command, ">", urc=True)
476487

477488
if result["status"] == Status.SUCCESS:
478-
self.atcom.send_at_comm_once(payload, line_end=False) # Send message
479-
result = self.atcom.send_at_comm(self.CTRL_Z) # Send end char --> CTRL+Z
489+
self.atcom.send_at_comm_once(payload, line_end=False) # Send message
490+
result = self.atcom.send_at_comm(
491+
self.CTRL_Z
492+
) # Send end char --> CTRL+Z
480493
return result
481494
return {"response": "Missing parameter", "status": Status.ERROR}
482495

@@ -495,7 +508,7 @@ def read_messages(self, cid=0):
495508
Result that includes "status" and "response" keys
496509
"""
497510
messages = []
498-
result = self.atcom.send_at_comm("AT+QMTRECV?","+QMTRECV:")
511+
result = self.atcom.send_at_comm("AT+QMTRECV?", "+QMTRECV:")
499512

500513
if result["status"] == Status.SUCCESS:
501514
prefix = f"+QMTRECV: {cid},"
@@ -533,7 +546,7 @@ def extract_messages(whole_message, prefix):
533546
if "0,0,0,0,0,0" in message[start_pos:]:
534547
pass
535548
else:
536-
messages.append(message[start_pos+len(prefix):])
549+
messages.append(message[start_pos + len(prefix) :])
537550

538551
# Clean and construct a dictionary.
539552
for message in messages:
@@ -546,10 +559,12 @@ def extract_messages(whole_message, prefix):
546559
message_inside = splitted_message[2][1:-1]
547560

548561
# Append the details of the message as a dict item.
549-
messages_dict.append({
550-
"message_id": int(splitted_message[0]),
551-
"topic": topic_inside,
552-
"message": message_inside
553-
})
562+
messages_dict.append(
563+
{
564+
"message_id": int(splitted_message[0]),
565+
"topic": topic_inside,
566+
"message": message_inside,
567+
}
568+
)
554569

555570
return messages_dict

0 commit comments

Comments
 (0)