From 0981a6bd6f084a9cd4ae2bf2924ba62269848e2b Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Fri, 8 Mar 2013 02:45:24 +0100 Subject: sub_api: handle failure of sensor/server more gracefully --- scripts/sub_api.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'scripts') diff --git a/scripts/sub_api.py b/scripts/sub_api.py index 307ff7f..8d1eb32 100644 --- a/scripts/sub_api.py +++ b/scripts/sub_api.py @@ -2,26 +2,29 @@ import json import urllib +import time +import sys import os.path base_path = os.path.realpath(os.path.dirname(__file__)) base_path = os.path.join(base_path, '..') - -#declarations - -input_url = "http://taifun.local.sublab.org/subcan.json" json_template = os.path.join(base_path, "template", "template.spaceapi.json") +input_url = "http://taifun.local.sublab.org/subcan.json" output_file = os.path.join(base_path, "public", "status.json") status_dict = { "open" : True , "closed" : False } -#read door status -status_json = json.load(urllib.urlopen(input_url)) -door_status = status_json["door.lock"]["text"] - -#fill status in template sublab_status = json.load(open(json_template)) -sublab_status["open"] = status_dict[door_status] +# What to set on unknown status +sublab_status["open"] = False + +try: + status_json = json.load(urllib.urlopen(input_url)) + if time.time() <= status_json["door.lock"]["ts"] + 120: + door_status = status_json["door.lock"]["text"] + sublab_status["open"] = status_dict[door_status] +except Exception: + sys.excepthook(*sys.exc_info()) #atomically place new status new_output_file = output_file + '.new' -- cgit v1.2.1