only change lastchange timestamp if the state has changed

This commit is contained in:
Micha Stöcker 2016-08-05 12:59:13 +02:00
parent 080817d1af
commit af85c6144b
2 changed files with 31 additions and 8 deletions

1
lastState.txt Normal file
View file

@ -0,0 +1 @@
0 1470352249

View file

@ -7,6 +7,7 @@ import logging
fileName = "json" fileName = "json"
url = "http://status.krautspace.de/status/button/" url = "http://status.krautspace.de/status/button/"
laststateFile = 'lastState.txt'
data = None data = None
# Raum Status abrufen # Raum Status abrufen
@ -15,7 +16,7 @@ try:
except: except:
logging.exception("urllib2") logging.exception("urllib2")
else: else:
# Status lessen # Status lesen
data = response.read(1) data = response.read(1)
# Data validieren # Data validieren
if len(data) != 1 or data not in ("0", "1"): if len(data) != 1 or data not in ("0", "1"):
@ -33,19 +34,40 @@ except:
# Bereich in dem der Status steht # Bereich in dem der Status steht
state = raw["state"] state = raw["state"]
# letzte Verarbeitungszeit setzen
state["lastchange"] = int(time.time()) try:
fp = file(laststateFile)
lastState, oldTimestamp = fp.readline().split()
fp.close()
except:
logging.exception("file")
exit(1)
# Status setzen # Status setzen
if data: if data:
if data == "0": if lastState != data:
state["open"] = False currentTimestamp = int(time.time())
state["message"] = "no human being on location" if data == "0":
state["lastchange"] = currentTimestamp
state["open"] = False
state["message"] = "no human being on location"
else:
state["lastchange"] = currentTimestamp
state["open"] = True
state["message"] = "open for public"
try:
fp = open( laststateFile, 'w' )
fp.write(data + ' ' + str(currentTimestamp))
fp.close()
except:
logging.exception("file")
exit(1)
else: else:
state["open"] = True state["lastchange"] = oldTimestamp
state["message"] = "open for public"
else: else:
state["open"] = None state["open"] = None
state["message"] = "open for public" state["message"] = "open for public"
state["lastchange"] = oldTimestamp
# JSON Decodieren und ausgeben # JSON Decodieren und ausgeben
print json.dumps(raw) print json.dumps(raw)