Fix: Handle OSError: [Errno 113] Host is unreachable

Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
Thomas Klaehn 2022-05-31 09:55:37 +02:00
parent bc04332c92
commit 64157b00e0
2 changed files with 30 additions and 18 deletions

View File

@ -69,13 +69,19 @@ class Control(threading.Thread):
url = "http://" + config["host"] + ":" + str(config["port"])
if now > on_time and now <= off_time and not self.water_state[water_index]:
client = xmlrpc.client.ServerProxy(url)
client.switch_relay(water["relay"], True)
self.water_state[water_index] = client.get_relay_state(water["relay"])
try:
client = xmlrpc.client.ServerProxy(url)
client.switch_relay(water["relay"], True)
self.water_state[water_index] = client.get_relay_state(water["relay"])
except OSError as error:
print(error)
elif now > off_time and self.water_state[water_index]:
client = xmlrpc.client.ServerProxy(url)
client.switch_relay(water["relay"], False)
self.water_state[water_index] = client.get_relay_state(water["relay"])
try:
client = xmlrpc.client.ServerProxy(url)
client.switch_relay(water["relay"], False)
self.water_state[water_index] = client.get_relay_state(water["relay"])
except OSError as error:
print(error)
water_index += 1
time.sleep(1)
@ -114,13 +120,16 @@ def get_sample(idx='0'):
relay = int(water["relay"])
res = {}
url = "http://" + cfg["host"] + ":" + str(cfg["port"])
client = xmlrpc.client.ServerProxy(url)
water = {}
water['id'] = str(idx)
water['state'] = client.get_relay_state(relay)
res['water'] = water
response = make_response(jsonify(res), 200)
break
try:
client = xmlrpc.client.ServerProxy(url)
water = {}
water['id'] = str(idx)
water['state'] = client.get_relay_state(relay)
res['water'] = water
response = make_response(jsonify(res), 200)
break
except OSError as error:
print(error)
return response
@ -138,10 +147,13 @@ def patch_sample(idx='0'):
water = cfg["water"]
relay = int(water["relay"])
url = "http://" + cfg["host"] + ":" + str(cfg["port"])
client = xmlrpc.client.ServerProxy(url)
client.switch_relay(relay, record["waterstate"])
response = make_response("", 204)
break
try:
client = xmlrpc.client.ServerProxy(url)
client.switch_relay(relay, record["waterstate"])
response = make_response("", 204)
break
except OSError as error:
print(error)
return response

View File

@ -6,7 +6,7 @@ from setuptools import setup
from setuptools.command.install import install
NAME = 'home'
VERSION = '1'
VERSION = '2'
AUTHOR = 'Thomas Klaehn'
EMAIL = 'tkl@blackfinn.de'
PACKAGES = ['home']