forked from hickerspace/REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.py
67 lines (52 loc) · 1.4 KB
/
room.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from datetime import *
import time
import json
from flask import jsonify
from .logger import log
from .conf import *
"""
JSON output for /room.
"""
def getStatus():
try:
with open(ROOM_STATUS_FILE, 'r') as f:
return json.loads(f.read())
except IOError:
log.exception("Could not read %s." % ROOM_STATUS_FILE)
message = { 'success': False, 'status': 'Room status record unreadable.' }
resp = jsonify(message)
resp.status_code = 500
return resp
else:
f.close()
"""
Boolean
"""
def isRoomOpen():
return getStatus()["open"]
"""
This method gets called when a new status gets submitted
"""
def submitStatus(open_):
try:
with open(ROOM_STATUS_FILE, 'r') as f:
room = json.loads(f.read())
if room['open'] != open_:
with open(ROOM_STATUS_FILE, 'w') as f:
newStatus = { 'since': int(time.time()),
'open': open_ }
addToArchive({'open': open_, 'lastchange': int(time.time())})
f.write(json.dumps(newStatus))
return jsonify({ 'success': True })
except IOError:
log.exception("Could not read/write %s." % ROOM_STATUS_FILE)
message = { 'success': False, 'status': 'Room status record unwriteable/unreadable.' }
resp = jsonify(message)
resp.status_code = 500
return resp
def addToArchive(newStatus):
with open(ROOM_ARCHIVE_FILE, 'r') as f:
archive = json.loads(f.read())
archive.append(newStatus)
with open(ROOM_ARCHIVE_FILE, 'w') as f:
json.dump(archive, f)