diff --git a/piflow/PiFlow.py b/piflow/PiFlow.py index 7e3c0d1..7d84d89 100644 --- a/piflow/PiFlow.py +++ b/piflow/PiFlow.py @@ -45,7 +45,7 @@ class start(threading.Thread, deviceBase): mqtt=mqtt, Nodes=Nodes) self.daemon = True - self.version = "17" + self.version = "18" self.finished = threading.Event() self.force_send = False self.public_ip_address = "" @@ -124,7 +124,7 @@ class start(threading.Thread, deviceBase): test_public_ip = get_public_ip_address() test_public_ip = test_public_ip[:-1] test_private_ip = get_private_ip_address() - if not test_public_ip == self.public_ip_address: + if not test_public_ip == self.public_ip_address and not test_public_ip == "0.0.0.0": self.sendtodbDev(1, 'public_ip_address', test_public_ip, 0, 'PiFlow') self.public_ip_address = test_public_ip if not test_private_ip == self.private_ip_address: diff --git a/piflow/config.txt b/piflow/config.txt index 90e42f0..b4f288f 100644 --- a/piflow/config.txt +++ b/piflow/config.txt @@ -3,7 +3,7 @@ "driverFileName":"PiFlow.py", "deviceName":"piflow", "driverId":"0280", -"releaseVersion":"17", +"releaseVersion":"18", "files": { "file1":"PiFlow.py", "file2":"Channel.py", diff --git a/piflow/utilities.py b/piflow/utilities.py index 3fed960..4cca34a 100644 --- a/piflow/utilities.py +++ b/piflow/utilities.py @@ -16,8 +16,12 @@ def get_private_ip_address(): def get_public_ip_address(): ip_address = "0.0.0.0" - with contextlib.closing(urllib.urlopen("httpd://checkip.amazonaws.com")) as url: - ip_address = url.read() + try: + with contextlib.closing(urllib.urlopen("httpd://checkip.amazonaws.com")) as url: + ip_address = url.read() + except Exception as e: + print("Could not resolve address: {}".format(e)) + return ip_address return ip_address[:-1] diff --git a/plcfreshwater/config.txt b/plcfreshwater/config.txt index 8839d0d..4df87d6 100644 --- a/plcfreshwater/config.txt +++ b/plcfreshwater/config.txt @@ -8,7 +8,7 @@ "file4": "Tags.py" }, "deviceName": "plcfreshwater", - "releaseVersion": "5", + "releaseVersion": "7", "driverFileName": "plcfreshwater.py", "driverId": "0100" } \ No newline at end of file diff --git a/plcfreshwater/plcfreshwater.py b/plcfreshwater/plcfreshwater.py index faf58c3..1b9d21d 100644 --- a/plcfreshwater/plcfreshwater.py +++ b/plcfreshwater/plcfreshwater.py @@ -52,7 +52,7 @@ class start(threading.Thread, deviceBase): mqtt=mqtt, Nodes=Nodes) self.daemon = True - self.version = "5" + self.version = "7" self.finished = threading.Event() self.force_send = False self.public_ip_address = "" @@ -104,13 +104,19 @@ class start(threading.Thread, deviceBase): log.warning("FORCE SEND: TRUE") for chan in CHANNELS: - val = chan.read() - if chan.check(val, self.force_send): - self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'plcfreshwater') - #time.sleep(TAG_DATAERROR_SLEEPTIME) # sleep to allow Micro800 to handle ENET requests + try: + val = chan.read() + if chan.check(val, self.force_send): + self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'plcfreshwater') + #time.sleep(TAG_DATAERROR_SLEEPTIME) # sleep to allow Micro800 to handle ENET requests + except Exception as e: + log.error("Something went wrong in read: {}".format(e)) # print("plcfreshwater driver still alive...") - plc_ping = os.system("ping -c 1 " + IP_TABLE[self.mac]) + try: + plc_ping = os.system("ping -c 1 " + IP_TABLE[self.mac] + " > /dev/null 2>&1") + except Exception as e: + log.error("something went wrong in ping: {}".format(e)) if plc_ping == 0: if not self.plc_ping_status == "OK": self.sendtodbDev(1, "plc_ping","OK", 0, 'plcfreshwater') @@ -139,14 +145,18 @@ class start(threading.Thread, deviceBase): test_public_ip = get_public_ip_address() test_public_ip = test_public_ip test_private_ip = get_private_ip_address() - if not test_public_ip == self.public_ip_address: + if not test_public_ip == self.public_ip_address and not test_public_ip == "0.0.0.0": self.sendtodbDev(1, 'public_ip_address', test_public_ip, 0, 'plcfreshwater') self.public_ip_address = test_public_ip if not test_private_ip == self.private_ip_address: self.sendtodbDev(1, 'private_ip_address', test_private_ip, 0, 'plcfreshwater') self.private_ip_address = test_private_ip - hostname = "google.com" - response = os.system("ping -c 1 " + hostname) + hostname = "8.8.8.8" + response = 1 + try: + response = os.system("ping -c 1 " + hostname + " > /dev/null 2>&1") + except Exception as e: + print("Something went wrong in ping: {}".format(e)) #and then check the response... if response == 0: @@ -157,7 +167,7 @@ class start(threading.Thread, deviceBase): self.ping_counter += 1 if self.ping_counter >= 3: - log.info("Rebooting because no internet detected") + print("Rebooting because no internet detected") os.system('reboot') @@ -169,6 +179,7 @@ class start(threading.Thread, deviceBase): def plcfreshwater_writeplctag(self, name, value): """Write a value to the PLC.""" + from Channel import write_tag new_val = json.loads(str(value).replace("'", '"')) tag_n = str(new_val['tag']) # "cmd_Start" val_n = new_val['val'] diff --git a/plcfreshwater/utilities.py b/plcfreshwater/utilities.py index 00f596d..bd90c33 100644 --- a/plcfreshwater/utilities.py +++ b/plcfreshwater/utilities.py @@ -17,8 +17,12 @@ def get_private_ip_address(): def get_public_ip_address(): ip_address = "0.0.0.0" - with contextlib.closing(urllib.urlopen("http://checkip.amazonaws.com")) as url: - ip_address = url.read() + try: + with contextlib.closing(urllib.urlopen("http://checkip.amazonaws.com")) as url: + ip_address = url.read() + except Exception as e: + print("could not resolve check IP: {}".format(e)) + return ip_address return ip_address[:-1]