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

View File

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