Skip to content

Commit

Permalink
Bump python-velbustcp 1.2.6
Browse files Browse the repository at this point in the history
Add NTP synctime configuration
Migrate velbus.ntp config to ntp.
Clean up configuration hook
  • Loading branch information
pva committed Oct 17, 2019
1 parent 76c4b2a commit 9e4f346
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 23 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
59 changes: 44 additions & 15 deletions snap/hooks/configure
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion snap/hooks/install
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion snap/hooks/post-refresh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
set -e

# Check if certificate needs to be generated
sh $SNAP/00-gen-cert
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
6 changes: 3 additions & 3 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -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: |
Expand All @@ -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:
Expand All @@ -28,4 +28,4 @@ parts:
python:
plugin: python
python-version: python3
python-packages: [pyserial]
python-packages: [pyserial, pytz, tzlocal]

0 comments on commit 9e4f346

Please sign in to comment.