From d6f94609dfb6c0aa9a1499504e23e4324d67cbcd Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Mon, 16 Oct 2017 11:36:45 -0500 Subject: [PATCH] Adds utilities, persistance, and period sends of Watchdog --- .../python-driver/Maps.py | 7 ++++ .../python-driver/persistence.py | 21 ++++++++++ .../python-driver/utilities.py | 11 +++++ .../{{cookiecutter.driver_name}}.py | 41 ++++++------------- 4 files changed, 52 insertions(+), 28 deletions(-) create mode 100644 {{cookiecutter.driver_name}}/python-driver/persistence.py create mode 100644 {{cookiecutter.driver_name}}/python-driver/utilities.py diff --git a/{{cookiecutter.driver_name}}/python-driver/Maps.py b/{{cookiecutter.driver_name}}/python-driver/Maps.py index 96328eb..a57347a 100644 --- a/{{cookiecutter.driver_name}}/python-driver/Maps.py +++ b/{{cookiecutter.driver_name}}/python-driver/Maps.py @@ -1,3 +1,10 @@ """Holds map values for {{cookiecutter.driver_name}}.""" {{cookiecutter.driver_name}}_map = {} + +def reverse_map(value, map_): + """Perform the opposite of mapping to an object.""" + for x in map_: + if map_[x] == value: + return x + return None diff --git a/{{cookiecutter.driver_name}}/python-driver/persistence.py b/{{cookiecutter.driver_name}}/python-driver/persistence.py new file mode 100644 index 0000000..ed65271 --- /dev/null +++ b/{{cookiecutter.driver_name}}/python-driver/persistence.py @@ -0,0 +1,21 @@ +"""Data persistance functions.""" +# if more advanced persistence is needed, use a sqlite database +import json + + +def load(filename="persist.json"): + """Load persisted settings from the specified file.""" + try: + with open(filename, 'r') as persist_file: + return json.load(persist_file) + except Exception: + return False + + +def store(persist_obj, filename="persist.json"): + """Store the persisting settings into the specified file.""" + try: + with open(filename, 'w') as persist_file: + return json.dump(persist_obj, persist_file) + except Exception: + return False diff --git a/{{cookiecutter.driver_name}}/python-driver/utilities.py b/{{cookiecutter.driver_name}}/python-driver/utilities.py new file mode 100644 index 0000000..4e2ec20 --- /dev/null +++ b/{{cookiecutter.driver_name}}/python-driver/utilities.py @@ -0,0 +1,11 @@ +"""Utility functions for the driver.""" +import socket + + +def get_public_ip_address(): + """Find the public IP Address of the host device.""" + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(("8.8.8.8", 80)) + ip = s.getsockname()[0] + s.close() + return ip diff --git a/{{cookiecutter.driver_name}}/python-driver/{{cookiecutter.driver_name}}.py b/{{cookiecutter.driver_name}}/python-driver/{{cookiecutter.driver_name}}.py index 0ac9573..d266aa5 100644 --- a/{{cookiecutter.driver_name}}/python-driver/{{cookiecutter.driver_name}}.py +++ b/{{cookiecutter.driver_name}}/python-driver/{{cookiecutter.driver_name}}.py @@ -4,37 +4,21 @@ import threading from device_base import deviceBase from Channel import Channel, write_tag, BoolArrayChannels from Maps import {{cookiecutter.driver_name}}_map as maps +from Maps import reverse_map +import persistence +from utilities import get_public_ip_address import json import time -import socket _ = None -try: - with open("persist.json", 'r') as persist_file: - persist = json.load(persist_file) -except Exception: - persist = {} +# GLOBAL VARIABLES +WATCHDOG_SEND_PERIOD = 3600 # Seconds, the longest amount of time before sending the watchdog status +PLC_IP_ADDRESS = "192.168.1.10" +CHANNELS = [] -plc_ip_address = "192.168.1.10" - - -def reverse_map(value, map_): - """Perform the opposite of mapping to an object.""" - for x in map_: - if map_[x] == value: - return x - return None - -def get_public_ip_address(): - """Find the public IP Address of the host device.""" - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(("8.8.8.8", 80)) - ip = s.getsockname()[0] - s.close() - return ip - -channels = [] +# PERSISTENCE FILE +persist = persistence.load() class start(threading.Thread, deviceBase): @@ -71,6 +55,7 @@ class start(threading.Thread, deviceBase): self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, '{{cookiecutter.driver_name}}') watchdog = self.rigpump_watchdog() self.sendtodbDev(1, 'watchdog', watchdog, 0, '{{cookiecutter.driver_name}}') + watchdog_send_timestamp = time.time() send_loops = 0 watchdog_loops = 0 @@ -79,7 +64,7 @@ class start(threading.Thread, deviceBase): if self.forceSend: print "FORCE SEND: TRUE" - for c in channels: + for c in CHANNELS: if c.read(self.forceSend): self.sendtodbDev(1, c.mesh_name, c.value, 0, '{{cookiecutter.driver_name}}') @@ -96,7 +81,7 @@ class start(threading.Thread, deviceBase): watchdog_loops += 1 if (watchdog_loops >= watchdog_check_after): test_watchdog = self.rigpump_watchdog() - if not test_watchdog == watchdog: + if not test_watchdog == watchdog or (time.time() - watchdog_send_timestamp) > WATCHDOG_SEND_PERIOD: self.sendtodbDev(1, 'watchdog', test_watchdog, 0, '{{cookiecutter.driver_name}}') watchdog = test_watchdog @@ -128,7 +113,7 @@ class start(threading.Thread, deviceBase): new_val = json.loads(str(value).replace("'", '"')) tag_n = str(new_val['tag']) # "cmd_Start" val_n = new_val['val'] - w = write_tag(str(plc_ip_address), tag_n, val_n) + w = write_tag(str(PLC_IP_ADDRESS), tag_n, val_n) print("Result of {{cookiecutter.driver_name}}_writeplctag(self, {}, {}) = {}".format(name, value, w)) if w is None: w = "Error writing to PLC..."