updated check public ip

setup try/catch for checking public IP address
This commit is contained in:
2020-01-28 14:57:17 -06:00
parent 85a45e832d
commit 625081b45e
6 changed files with 37 additions and 18 deletions

View File

@@ -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:

View File

@@ -3,7 +3,7 @@
"driverFileName":"PiFlow.py",
"deviceName":"piflow",
"driverId":"0280",
"releaseVersion":"17",
"releaseVersion":"18",
"files": {
"file1":"PiFlow.py",
"file2":"Channel.py",

View File

@@ -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]

View File

@@ -8,7 +8,7 @@
"file4": "Tags.py"
},
"deviceName": "plcfreshwater",
"releaseVersion": "5",
"releaseVersion": "7",
"driverFileName": "plcfreshwater.py",
"driverId": "0100"
}

View File

@@ -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']

View File

@@ -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]