diff options
author | Christian Franke <nobody@nowhere.ws> | 2013-03-08 02:45:24 +0100 |
---|---|---|
committer | Christian Franke <nobody@nowhere.ws> | 2013-03-08 02:45:24 +0100 |
commit | 0981a6bd6f084a9cd4ae2bf2924ba62269848e2b (patch) | |
tree | 547a802aaaeef180a337aa8069a047a7dcc59c32 /scripts | |
parent | a2b4038bac754df712b1cd25a7d474d2a4d7d015 (diff) |
sub_api: handle failure of sensor/server more gracefully
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/sub_api.py | 23 |
1 files changed, 13 insertions, 10 deletions
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' |