summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scripts/sub_api.py23
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'