Skip to content

Conversation

@LePailleurThibault
Copy link
Contributor

No description provided.

#
# See file LICENSE for full license details.
#
from .keep_alive_service import *
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'.keep_alive_service.*' imported but unused

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LePailleurThibault Is it a valid comment from hound?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, i don't know how this comment is generated and imports in a init file are never really "used".
However, for example, in the transport service there is this line
from wirepas_gateway.keep_alive_service import KeepAliveServiceThread.
The init.py is required but maybe not this line


Args:
sink_manager: The sink manager to send sinks the keep alive messages.
mqtt_wrapper: The mqtt wrapper to get access to queue level of the mqtt broker.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (91 > 79 characters)

)
if self.keep_alive_interval_s is not None:
buffer += KeepAliveMessage.encode_tlv_item(
KeepAliveType.KEEP_ALIVE_INTERVAL_TYPE, 2, self.keep_alive_interval_s, "H",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (91 > 79 characters)

Bits 1-7: Reserved for future use or other status indicators
rtc_timestamp_ms: Unix epoch timestamp in ms (milliseconds since January 1, 1970).
timezone_offset_mn: Time zone offset from UTC in minutes (-840 to +720).
keep_alive_interval_s: Interval in seconds until the next keepalive message is expected.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (96 > 79 characters)

Attributes:
version: The version number for the keep-alive message.
gateway_status: The running status of the gateway.
Bit 0: Backhaul (MQTT broker) Connectivity (0 = Disconnected, 1 = Connected)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (88 > 79 characters)

@sgunes-wirepas
Copy link
Contributor

Maybe a more general question, but why is the timezone offset needed in the keepalive messages? In locations with daylight saving time, the offset from UTC would change throughout the year.

@LePailleurThibault
Copy link
Contributor Author

LePailleurThibault commented Jan 9, 2025

These messages will be used for example in the dlms app where the application might update the connected meter local time with the UTC time + timezone offset.
But, that's a good point, I will change the timezone offset input to a timezone name, so that the module will handle daylight saving time changes.

KEEP_ALIVE_DST_EP = 67

# Timeouts and periods used for the keep alive service.
WM_MSG_RETRY_PERIOD_S = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 1s enough ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well if a message can't be sent to a sink 3 times in a row in ~2s, there are definitely issue with this sink. So not sending a keep alive message for that sink wouldn't be that bad.

src_ep=KEEP_ALIVE_SRC_EP,
dst_ep=KEEP_ALIVE_DST_EP,
qos=0,
initial_time=0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I recall right, it is already the default value, so could be skipped

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This argument is required by the method. I don't think there is any default value for the initial_time.

""" Main loop that send periodically keep alive message to the network. """
while True:
# Put a timer so that the message are periodic with a good precision
start_timer = time()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't time.monotonic() be used instead, to avoid issue if clock is changed during the process?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, monotonic would be much safer. How about the nodes, can it be a problem there if the keepalive message has an older timestamp than the previous one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will use a monotonic timer here. The gateway is supposed to be connected to a NTP server when running the keep alive service and the keep alive messages contain the UTC timestamp. This issue will never happen with this timestamp. Now if the application wants to use local time (based on UTC + timezone offset), the application itself should handle the consequence of DST.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more about the following (very unlikely) scenario:

  • NTP server(s) becomes unreachable during runtime, but not MQTT
  • Host clock drifts forward
  • NTP is reconnected and clock is updated
  • New keepalive message has an older timestamp than the previous one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time should not be included in the keep alive packet if the NTP server time is unavailable. However, at this moment, we do not have any NTP service-like API for this kind of issue. So there is no way of knowing if this time is valid without implementing a NTP server nearby.
But at the moment, when using the time of this service, the gateway is supposed to be synchronized with a NTP server.
And if it the issue to happen, the time will not be updated in the current applications if the clock difference is too big between 2 packets.

for sink_id in current_sinks:
self.send_keep_alive_msg_to_sink(self.sink_manager.get_sink(sink_id))

self.wait_for_next_keep_alive_message_iteration(self.keep_alive_interval_s, start_timer)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (100 > 79 characters)


if not current_sinks:
logging.error("No sinks are detected!")
self.wait_for_next_keep_alive_message_iteration(self.keep_alive_interval_s)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (91 > 79 characters)


return True

def wait_for_next_keep_alive_message_iteration(self, time_to_wait, start_timer=None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (89 > 79 characters)

)
if res != wmm.GatewayResultCode.GW_RES_OK and retries_left > 0:
sleep(WM_MSG_RETRY_PERIOD_S)
logging.debug("Retry sending the keep alive message that couldn't be sent to %s sink: %s. ",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (108 > 79 characters)

The interval in seconds between keep-alive messages.
keep_alive_timezone_name (str): Default to "Etc/UTC".
Time zone name used to set the timezone offset in the keep alive message.
Check https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (87 > 79 characters)

keep_alive_interval_s (int): Default to 300 seconds.
The interval in seconds between keep-alive messages.
keep_alive_timezone_name (str): Default to "Etc/UTC".
Time zone name used to set the timezone offset in the keep alive message.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (89 > 79 characters)


class KeepAliveType(IntEnum):
"""Keep alive fields TLV type enumerate."""
VERSION_TYPE = 0x01
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants