Skip to content

Commit 639160a

Browse files
committed
added _ns suffix to vars and func
1 parent 89bf045 commit 639160a

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __init__(
181181
self._is_connected = False
182182
self._msg_size_lim = MQTT_MSG_SZ_LIM
183183
self._pid = 0
184-
self._last_msg_sent_timestamp: int = 0
184+
self._last_msg_sent_timestamp_ns: int = 0
185185
self.logger = NullLogger()
186186
"""An optional logging attribute that can be set with with a Logger
187187
to enable debug logging."""
@@ -220,7 +220,7 @@ def __init__(
220220
self.client_id = client_id
221221
else:
222222
# assign a unique client_id
223-
time_int = (self.get_monotonic_time() % 10000000000) // 10000000
223+
time_int = (self.get_monotonic_ns_time() % 10000000000) // 10000000
224224
self.client_id = f"cpy{randint(0, time_int)}{randint(0, 99)}"
225225
# generated client_id's enforce spec.'s length rules
226226
if len(self.client_id.encode("utf-8")) > 23 or not self.client_id:
@@ -244,7 +244,7 @@ def __init__(
244244
self.on_subscribe = None
245245
self.on_unsubscribe = None
246246

247-
def get_monotonic_time(self) -> float:
247+
def get_monotonic_ns_time(self) -> float:
248248
"""
249249
Provide monotonic time in nanoseconds. Based on underlying implementation
250250
this might result in imprecise time, that will result in the library
@@ -256,12 +256,12 @@ def get_monotonic_time(self) -> float:
256256

257257
return int(time.monotonic() * 1000000000)
258258

259-
def diff_ns(self, stamp):
259+
def diff_ns(self, stamp_ns):
260260
"""
261261
Taking timestamp differences using nanosecond ints before dividing
262262
should maintain precision.
263263
"""
264-
return (self.get_monotonic_time() - stamp) / 1000000000
264+
return (self.get_monotonic_ns_time() - stamp_ns) / 1000000000
265265

266266
def __enter__(self):
267267
return self
@@ -534,9 +534,9 @@ def _connect(
534534
if self._username is not None:
535535
self._send_str(self._username)
536536
self._send_str(self._password)
537-
self._last_msg_sent_timestamp = self.get_monotonic_time()
537+
self._last_msg_sent_timestamp_ns = self.get_monotonic_ns_time()
538538
self.logger.debug("Receiving CONNACK packet from broker")
539-
stamp = self.get_monotonic_time()
539+
stamp_ns = self.get_monotonic_ns_time()
540540
while True:
541541
op = self._wait_for_msg()
542542
if op == 32:
@@ -552,7 +552,7 @@ def _connect(
552552
return result
553553

554554
if op is None:
555-
if self.diff_ns(stamp) > self._recv_timeout:
555+
if self.diff_ns(stamp_ns) > self._recv_timeout:
556556
raise MMQTTException(
557557
f"No data received from broker for {self._recv_timeout} seconds."
558558
)
@@ -589,7 +589,7 @@ def disconnect(self) -> None:
589589
self._connection_manager.close_socket(self._sock)
590590
self._is_connected = False
591591
self._subscribed_topics = []
592-
self._last_msg_sent_timestamp = 0
592+
self._last_msg_sent_timestamp_ns = 0
593593
if self.on_disconnect is not None:
594594
self.on_disconnect(self, self.user_data, 0)
595595

@@ -602,14 +602,14 @@ def ping(self) -> list[int]:
602602
self.logger.debug("Sending PINGREQ")
603603
self._sock.send(MQTT_PINGREQ)
604604
ping_timeout = self.keep_alive
605-
stamp = self.get_monotonic_time()
606-
self._last_msg_sent_timestamp = stamp
605+
stamp_ns = self.get_monotonic_ns_time()
606+
self._last_msg_sent_timestamp_ns = stamp_ns
607607
rc, rcs = None, []
608608
while rc != MQTT_PINGRESP:
609609
rc = self._wait_for_msg()
610610
if rc:
611611
rcs.append(rc)
612-
if self.diff_ns(stamp) > ping_timeout:
612+
if self.diff_ns(stamp_ns) > ping_timeout:
613613
raise MMQTTException("PINGRESP not returned from broker.")
614614
return rcs
615615

@@ -678,11 +678,11 @@ def publish(
678678
self._sock.send(pub_hdr_fixed)
679679
self._sock.send(pub_hdr_var)
680680
self._sock.send(msg)
681-
self._last_msg_sent_timestamp = self.get_monotonic_time()
681+
self._last_msg_sent_timestamp_ns = self.get_monotonic_ns_time()
682682
if qos == 0 and self.on_publish is not None:
683683
self.on_publish(self, self.user_data, topic, self._pid)
684684
if qos == 1:
685-
stamp = self.get_monotonic_time()
685+
stamp_ns = self.get_monotonic_ns_time()
686686
while True:
687687
op = self._wait_for_msg()
688688
if op == 0x40:
@@ -696,7 +696,7 @@ def publish(
696696
return
697697

698698
if op is None:
699-
if self.diff_ns(stamp) > self._recv_timeout:
699+
if self.diff_ns(stamp_ns) > self._recv_timeout:
700700
raise MMQTTException(
701701
f"No data received from broker for {self._recv_timeout} seconds."
702702
)
@@ -755,12 +755,12 @@ def subscribe(self, topic: Optional[Union[tuple, str, list]], qos: int = 0) -> N
755755
self.logger.debug(f"SUBSCRIBING to topic {t} with QoS {q}")
756756
self.logger.debug(f"payload: {payload}")
757757
self._sock.send(payload)
758-
stamp = self.get_monotonic_time()
759-
self._last_msg_sent_timestamp = stamp
758+
stamp_ns = self.get_monotonic_ns_time()
759+
self._last_msg_sent_timestamp_ns = stamp_ns
760760
while True:
761761
op = self._wait_for_msg()
762762
if op is None:
763-
if self.diff_ns(stamp) > self._recv_timeout:
763+
if self.diff_ns(stamp_ns) > self._recv_timeout:
764764
raise MMQTTException(
765765
f"No data received from broker for {self._recv_timeout} seconds."
766766
)
@@ -832,13 +832,13 @@ def unsubscribe(self, topic: Optional[Union[str, list]]) -> None:
832832
for t in topics:
833833
self.logger.debug(f"UNSUBSCRIBING from topic {t}")
834834
self._sock.send(payload)
835-
self._last_msg_sent_timestamp = self.get_monotonic_time()
835+
self._last_msg_sent_timestamp_ns = self.get_monotonic_ns_time()
836836
self.logger.debug("Waiting for UNSUBACK...")
837837
while True:
838-
stamp = self.get_monotonic_time()
838+
stamp_ns = self.get_monotonic_ns_time()
839839
op = self._wait_for_msg()
840840
if op is None:
841-
if self.diff_ns(stamp) > self._recv_timeout:
841+
if self.diff_ns(stamp_ns) > self._recv_timeout:
842842
raise MMQTTException(
843843
f"No data received from broker for {self._recv_timeout} seconds."
844844
)
@@ -938,26 +938,26 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
938938
self._connected()
939939
self.logger.debug(f"waiting for messages for {timeout} seconds")
940940

941-
stamp = self.get_monotonic_time()
941+
stamp_ns = self.get_monotonic_ns_time()
942942
rcs = []
943943

944944
while True:
945-
if self.diff_ns(self._last_msg_sent_timestamp) >= self.keep_alive:
945+
if self.diff_ns(self._last_msg_sent_timestamp_ns) >= self.keep_alive:
946946
# Handle KeepAlive by expecting a PINGREQ/PINGRESP from the server
947947
self.logger.debug(
948948
"KeepAlive period elapsed - requesting a PINGRESP from the server..."
949949
)
950950
rcs.extend(self.ping())
951951
# ping() itself contains a _wait_for_msg() loop which might have taken a while,
952952
# so check here as well.
953-
if self.diff_ns(stamp) > timeout:
953+
if self.diff_ns(stamp_ns) > timeout:
954954
self.logger.debug(f"Loop timed out after {timeout} seconds")
955955
break
956956

957957
rc = self._wait_for_msg()
958958
if rc is not None:
959959
rcs.append(rc)
960-
if self.diff_ns(stamp) > timeout:
960+
if self.diff_ns(stamp_ns) > timeout:
961961
self.logger.debug(f"Loop timed out after {timeout} seconds")
962962
break
963963

@@ -1063,7 +1063,7 @@ def _sock_exact_recv(
10631063
:param float timeout: timeout, in seconds. Defaults to keep_alive
10641064
:return: byte array
10651065
"""
1066-
stamp = self.get_monotonic_time()
1066+
stamp_ns = self.get_monotonic_ns_time()
10671067
if not self._backwards_compatible_sock:
10681068
# CPython/Socketpool Impl.
10691069
rc = bytearray(bufsize)
@@ -1078,7 +1078,7 @@ def _sock_exact_recv(
10781078
recv_len = self._sock.recv_into(mv, to_read)
10791079
to_read -= recv_len
10801080
mv = mv[recv_len:]
1081-
if self.diff_ns(stamp) > read_timeout:
1081+
if self.diff_ns(stamp_ns) > read_timeout:
10821082
raise MMQTTException(
10831083
f"Unable to receive {to_read} bytes within {read_timeout} seconds."
10841084
)
@@ -1098,7 +1098,7 @@ def _sock_exact_recv(
10981098
recv = self._sock.recv(to_read)
10991099
to_read -= len(recv)
11001100
rc += recv
1101-
if self.diff_ns(stamp) > read_timeout:
1101+
if self.diff_ns(stamp_ns) > read_timeout:
11021102
raise MMQTTException(
11031103
f"Unable to receive {to_read} bytes within {read_timeout} seconds."
11041104
)

0 commit comments

Comments
 (0)