From eb77b689516bc11c62cc4089fc6cd497d3cab1f7 Mon Sep 17 00:00:00 2001 From: Steve Clement Date: Sat, 11 Jun 2016 21:38:30 +0200 Subject: [PATCH] - updated main --- jsonTest.py | 29 +++++++++++++++++++++-------- main.py | 18 +++++++++++++++++- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/jsonTest.py b/jsonTest.py index c7b1afa..f184a68 100755 --- a/jsonTest.py +++ b/jsonTest.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# import json import urllib.request as ur import urllib.parse as par -import datetime +from datetime import timedelta, datetime import time url = "https://level2.lu/spaceapi/" @@ -14,18 +16,29 @@ data = json.loads(xml.decode('utf-8')) stateOpen = data['state']['open'] stateLast = data['state']['lastchange'] -now = int(time.time()) +now = time.time() -print("Last triggered: " + str((now - stateLast))) +def getTime(seconds): + sec = timedelta(seconds=int(seconds)) + d = datetime(1,1,1) + sec -print(stateLast) + if d.day-1 > 0: + return("{} day(s), {} hour(s), {} minute(s) and {} seconds".format(d.day-1, d.hour, d.minute, d.second)) + elif d.hour > 0: + return("{} hour(s), {} minute(s) and {} seconds".format(d.hour, d.minute, d.second)) + elif d.minute > 0: + return("{} minute(s) and {} seconds".format(d.minute, d.second)) + else: + return("{} seconds".format(d.second)) + +st = getTime(now- stateLast) + +print("Last triggered: " + st) if stateOpen == True: print("Space is open!") + countPeople = data['sensors']['people_now_present'] + print("We are " + str(countPeople[0]['value'])) type(countPeople) else: print("We are closed :(") -countPeople = data['sensors']['people_now_present'] -type(countPeople) - -print("We are " + str(countPeople[0]['value'])) \ No newline at end of file diff --git a/main.py b/main.py index 788f5b2..8183312 100755 --- a/main.py +++ b/main.py @@ -1,4 +1,6 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# try: import Image @@ -22,7 +24,9 @@ import json # Rows and chain length are both required parameters: matrix = Adafruit_RGBmatrix(32, 3) -r = requests.get('https://spaceapi.syn2cat.lu/status/json') +spaceAPI = [ "https://level2.lu/spaceapi/", "https://spaceapi.syn2cat.lu/status/json" ] + +r = requests.get(spaceAPI[0]) obj = json.loads(r.text) @@ -58,5 +62,17 @@ def closedSpace(): matrix.SetImage(image.im.id, n, 1) time.sleep(0.025) +def getTime(seconds): + sec = timedelta(seconds=int(seconds)) + d = datetime(1,1,1) + sec + + if d.day-1 > 0: + return("{} day(s), {} hour(s), {} minute(s) and {} seconds".format(d.day-1, d.hour, d.minute, d.second)) + elif d.hour > 0: + return("{} hour(s), {} minute(s) and {} seconds".format(d.hour, d.minute, d.second)) + elif d.minute > 0: + return("{} minute(s) and {} seconds".format(d.minute, d.second)) + else: + return("{} seconds".format(d.second)) if __name__ == "__main__": main()