diff --git a/README.md b/README.md index 08438b3..82de605 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,21 @@ snap set velbus-tcp serial.port=/dev/ttyAMA0 ## NTP -The application can broadcast the system time on the bus, it will do so at startup and after every hour transition. +The application can broadcast the system time on the bus, this is option disabled by default. -This is option disabled by default. You can enable/disable NTP by using +You can enable/disable NTP by using -`snap set velbus-tcp velbus.ntp=true|false` +`snap set velbus-tcp ntp.enabled=true|false` + +When enabled, it will broadcast the time at startup, after that; + +If ntp.synctime is not set or is set to empty, it will do so at every hour transition and at DST transitions, whichever is closer. + +If ntp.synctime is set, it will broadcast at the specified time and at DST transitions, whichever is closer. + +To change the the sync time use + +`snap set velbus-tcp ntp.synctime='03:00'|''` ## TCP binding diff --git a/snap/hooks/configure b/snap/hooks/configure index 19d61ff..5e88c58 100755 --- a/snap/hooks/configure +++ b/snap/hooks/configure @@ -10,8 +10,9 @@ import ipaddress def parse_config(setting): return setting.split(",") -# Velbus -ntp = subprocess.check_output(["snapctl", "get", "velbus.ntp"]).decode("utf-8").strip() +# NTP +ntp_enabled = subprocess.check_output(["snapctl", "get", "ntp.enabled"]).decode("utf-8").strip() +ntp_synctime = subprocess.check_output(["snapctl", "get", "ntp.synctime"]).decode("utf-8").strip() # TCP tcp_hosts = parse_config(subprocess.check_output(["snapctl", "get", "tcp.host"]).decode("utf-8").strip()) tcp_ports = parse_config(subprocess.check_output(["snapctl", "get", "tcp.port"]).decode("utf-8").strip()) @@ -36,18 +37,46 @@ logging_output = subprocess.check_output(["snapctl", "get", "logging.output"]).d config = dict() -# Velbus +# NTP -config["velbus"] = {} +config["ntp"] = {} -if ntp == "": - ntp = "false" - -if ntp != "true" and ntp != "false": - print("Given value for velbus.ntp '{0}' is not valid, should be bool".format(ntp)) +if not ntp_enabled in ["true", "false"]: + print("Given value for ntp.enabled '{0}' is not valid, should be bool".format(ntp_enabled)) exit(1) -config["velbus"]["ntp"] = True if ntp == "true" else False +config["ntp"]["enabled"] = True if ntp_enabled == "true" else False + + # Validate sync time +if ntp_synctime != "": + + splitted = ntp_synctime.split(":") + + if len(splitted) != 2: + print("The provided sync time has an invalid format '{0}', should be 'hh:mm' or empty".format(ntp_synctime)) + exit(1) + + try: + hh = int(splitted[0]) + except: + print("The provided sync time hour is invalid '{0}'".format(splitted[0])) + exit(1) + + if not 0 <= hh <= 23: + print("The provided sync time hour is invalid '{0}'".format(hh)) + exit(1) + + try: + mm = int(splitted[1]) + except: + print("The provided sync time minute is invalid '{0}'".format(splitted[1])) + exit(1) + + if not 0 <= mm <= 59: + print("The provided sync time minute is invalid '{0}'".format(mm)) + exit(1) + +config["ntp"]["synctime"] = ntp_synctime # TCP @@ -89,15 +118,15 @@ for idx, host in enumerate(tcp_hosts): print("Given TCP port '{0}' is not an integer".format([port])) exit(1) - if temp_tcp_port < 1 or temp_tcp_port > 65565: + if not 1 <= temp_tcp_port <= 65565: print("Given TCP port '{0}' should be between 1 and 65565".format([port])) exit(1) - if relay != "true" and relay != "false": + if not relay in ["true", "false"]: print("Given value for tcp.relay '{0}' is not valid, should be bool".format(relay)) exit(1) - if ssl != "true" and ssl != "false": + if not ssl in ["true", "false"]: print("Given value for tcp.ssl '{0}' is not valid, should be bool".format(ssl)) exit(1) @@ -127,7 +156,7 @@ for idx, host in enumerate(tcp_hosts): print("Given tcp.cert does not exist at '{0}'".format(cert)) exit(1) - if auth != "true" and auth != "false": + if not auth in ["true", "false"]: print("Given value for tcp.auth '{0}' is not valid, should be bool".format(auth)) exit(1) @@ -147,7 +176,7 @@ for idx, host in enumerate(tcp_hosts): config["connections"].append(connection) # Serial -if serial_autodiscover != "true" and serial_autodiscover != "false": +if not serial_autodiscover in ["true", "false"]: print("Given value for serial.autodiscover '{0}' is not valid, should be bool".format(serial_autodiscover)) exit(1) diff --git a/snap/hooks/install b/snap/hooks/install index 41bce9e..6a186a8 100755 --- a/snap/hooks/install +++ b/snap/hooks/install @@ -1,7 +1,8 @@ #!/bin/sh set -e -snapctl set velbus.ntp=false +snapctl set ntp.enabled=false +snapctl set ntp.synctime= snapctl set tcp.host=0.0.0.0 snapctl set tcp.port=27015 snapctl set tcp.relay=true diff --git a/snap/hooks/post-refresh b/snap/hooks/post-refresh index 15967a1..8450f1f 100755 --- a/snap/hooks/post-refresh +++ b/snap/hooks/post-refresh @@ -2,4 +2,14 @@ set -e # Check if certificate needs to be generated -sh $SNAP/00-gen-cert \ No newline at end of file +sh $SNAP/00-gen-cert + +# Change velbus.ntp config to ntp.enabled +ntp=$(snapctl get velbus.ntp) +if [ ! -z $ntp ]; then + snapctl set ntp.enabled=$ntp + snapctl set ntp.synctime= + + # Reset to empty as there is no way to unset it yet + snapctl set velbus.ntp= +fi \ No newline at end of file diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 20df6d2..c0e3359 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: velbus-tcp -version: 1.2.5 +version: 1.2.6 base: core18 summary: Python application that bridges a Velbus installation with TCP description: | @@ -18,7 +18,7 @@ parts: src: plugin: dump source: https://github.com/velbus/python-velbustcp.git - source-tag: 1.2.5 + source-tag: 1.2.6 stage-packages: [openssl] extra: @@ -28,4 +28,4 @@ parts: python: plugin: python python-version: python3 - python-packages: [pyserial] + python-packages: [pyserial, pytz, tzlocal]