From 64157b00e08a3331f05744347301ee095ac96fa3 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Tue, 31 May 2022 09:55:37 +0200 Subject: [PATCH] Fix: Handle OSError: [Errno 113] Host is unreachable Signed-off-by: Thomas Klaehn --- home/app.py | 46 +++++++++++++++++++++++++++++----------------- setup.py | 2 +- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/home/app.py b/home/app.py index e326537..2c01dea 100644 --- a/home/app.py +++ b/home/app.py @@ -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 diff --git a/setup.py b/setup.py index 1bbde7b..ed7b62e 100644 --- a/setup.py +++ b/setup.py @@ -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']