Skip to content

Commit

Permalink
RTC service ntp server to query can now be taken from an environment …
Browse files Browse the repository at this point in the history
…variable. (#265)
  • Loading branch information
LePailleurThibault authored Apr 12, 2024
1 parent 1df3804 commit 35af74e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions rtc_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The RTC service needs four parameters to start.
| WM_RTC_TIMEZONE_FROM_GATEWAY_CLOCK | A boolean to assert whether the timezone offset should be taken directly from the gateway clock or from the the parameter WM_RTC_TIMEZONE_OFFSET_S | The default value is False |
| WM_RTC_TIMEZONE_OFFSET_S | Timezone offset in seconds of the local time. | It is taken in account only if WM_RTC_TIMEZONE_FROM_GATEWAY_CLOCK is False. |
| WM_RTC_GET_TIME_FROM_LOCAL | Asserts if the rtc time is sent from local time or from a ntp server time | If set to True, it is assumed that gateways are synchronize. The default value is False |
| WM_RTC_NTP_SERVER_ADDRESS | Address of the ntp server to query the time if it is taken from an ntp server. | WM_RTC_GET_TIME_FROM_LOCAL must be set to False for that option to be taken into account |

RTC service is available as a docker image to ease the integration.

Expand Down
27 changes: 25 additions & 2 deletions rtc_service/rtc_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,24 @@ def __init__(
timezone_offset_s,
timezone_from_gateway_clock,
get_time_from_local,
ntp_server_address,
sink_manager
):
"""
Thread sending periodically time to synchronize the network.
Args:
period: The period to send gateway time to the network
timezone_offset_s: Offset of the local time in seconds
if timezone_from_gateway_clock is False.
timezone_from_gateway_clock: True if timezone offset must be taken from gateway clock.
False (default) means that the timezone offset is given by timezone_offset_s argument.
get_time_from_local: False(default) will force the gateway to ask time
from a ntp server before sending it to the network
True means that the time is taken directly from gateway.
Note: You must assure that gateway are synchronized if set to True
ntp_server_address: Address of the ntp server to query the time
if it is taken from an ntp server.
sink_manager: The sink manager to send sink the rtc informations
"""
Thread.__init__(self)
Expand All @@ -97,6 +108,7 @@ def __init__(
logging.info(f"Timezone offset is set to {self.timezone_offset_s}s")

self.get_time_from_local = get_time_from_local
self.ntp_server_address = ntp_server_address
if not self.get_time_from_local:
logging.info("RTC time is taken from a ntp server")
self.ntp_client = ntplib.NTPClient()
Expand All @@ -111,7 +123,7 @@ def publish_time(self):
"""
if not self.get_time_from_local:
try:
req = self.ntp_client.request('pool.ntp.org', version=3)
req = self.ntp_client.request(self.ntp_server_address, version=3)
timestamp = req.dest_time + req.offset
except ntplib.NTPException as err:
logging.warning("Couldn't get time from NTP server. (%s)", err)
Expand Down Expand Up @@ -158,6 +170,7 @@ def __init__(self, settings, **kwargs):
settings.timezone_offset_s,
settings.timezone_from_gateway_clock,
settings.get_time_from_local,
settings.ntp_server_address,
self.sink_manager,
)
self.synchronization_thread.start()
Expand Down Expand Up @@ -208,7 +221,7 @@ def main():
type=str2bool,
help=("True if timezone offset must be taken from gateway clock."
"False (default) means that the timezone offset is"
"given by utc_to_local_offset_s argument"),
"given by timezone_offset_s argument"),
)

parser.add_argument(
Expand All @@ -230,6 +243,16 @@ def main():
"Note: You must assure that gateway are synchronized if set to True"),
)

parser.add_argument(
"--ntp_server_address",
default=os.environ.get("WM_RTC_NTP_SERVER_ADDRESS", "pool.ntp.org"),
action="store",
type=str,
help=("Address of the ntp server to query the time if it is taken from an ntp server. "
"(WM_RTC_GET_TIME_FROM_LOCAL must be set to False "
"for that option to be taken into account)")
)

settings = parser.parse_args()

# Set default debug level
Expand Down

0 comments on commit 35af74e

Please sign in to comment.