-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
149 lines (112 loc) · 4.4 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from flask import Flask, render_template, request
import datetime
import json
import requests
# ***************************************************************************
# CONFIG
ON_PRODUCTION = True
app = Flask(__name__)
SCANNER_URL = "http://anusr-scanner-container://scan.json"
DAYS_NUM = 7
BUILDINGS = ["Chifley", "Hancock", "Law", "Menzies"]
# GET request to grab the scan.json file from the scanner container
def getScannerData():
s = requests.Session()
text = s.get(SCANNER_URL)
s.close()
return text.content
# Generate a list of dates in the format: YYYY-MM-DD
def generateDates(num_days):
date_list = []
start_date = datetime.datetime.today()
for n in range(num_days):
date = start_date + datetime.timedelta(days=n)
date = date.strftime("%Y-%m-%d")
date_list.append(date)
return date_list
# Convert times to minutes for easier comparison
def convertStringToMinutes(time):
# Just a single value, not a list
if type(time) != list:
split = time.split(':')
hours = int(split[0]) * 60
minutes = int(split[1])
new_time = hours + minutes
return new_time
# Convert a list of times
new_list = []
for t in time:
split = t.split(':')
hours = int(split[0]) * 60
minutes = int(split[1])
new_list.append(hours + minutes)
return new_list
# This function is passed through to the HTML form to help convert minutes to time strings
@app.context_processor
def utility_processor():
def convertMinutesToString(mins):
hours = int(mins / 60)
minutes = int(mins - hours * 60)
outStr = str(hours).zfill(2) + ':' + str(minutes).zfill(2)
return outStr
return dict(convertMinutesToString=convertMinutesToString)
@app.route('/')
def home():
return render_template('home.html', buildingList=BUILDINGS, dateList=generateDates(DAYS_NUM))
@app.route('/', methods=['POST'])
def filter():
selected_building = request.form['building']
selected_date = request.form['date']
# Where date comes from depends in we're on production or not
if ON_PRODUCTION:
my_content = getScannerData()
else:
text = open("static/data/scan.json", "r")
my_content = text.read()
text.close()
# Convert text to json and slice relevant info
parsed_json = (json.loads(my_content))
sliced_json = parsed_json[selected_building][selected_date]
for room in sliced_json:
sliced_json[room] = convertStringToMinutes(sliced_json[room])
# print(json.dumps(sliced_json, indent=4, sort_keys=True))
# Generate Boolean table
bool_array = []
# Go through each time slot (i.e. 00:00-00:15, 00:15-00:30, etc)
time_start = convertStringToMinutes(request.form['start_time'])
# time_start = 540
time_end = convertStringToMinutes(request.form['end_time'])
increment = 15
# for c in range(int(time_end / 15) + 1 - int(time_start / 15)):
time_range = int(time_end / 15) + 0 - int(time_start / 15)
if time_end > 1425:
time_range += 1
for c in range(time_range):
bool_row = []
# Go through each of the rooms
for room in sliced_json:
booked = False
# Grab pairs of times and see if we are within the range
for i in range(0, len(sliced_json[room]), 2):
start = sliced_json[room][i]
end = sliced_json[room][i + 1]
time_current = time_start + c * increment
if start <= time_current < end:
booked = True
break
bool_row.append(booked)
bool_array.append(bool_row)
# Format last scan time
scan_end = datetime.datetime.strptime(parsed_json["Scan_End"], "%Y-%m-%d %H:%M:%S.%f")
scan_end = scan_end.strftime("%Y-%m-%d %H:%M:%S")
return render_template('home.html', buildingList=BUILDINGS, dateList=generateDates(DAYS_NUM),
buildingSelected=selected_building, dateSelected=selected_date,
startTimeSelected=time_start, endTimeSelected=time_end,
startTime=time_start, titles=sliced_json.keys(), bool_booked=bool_array,
last_scan=scan_end)
if __name__ == '__main__':
app.debug = not ON_PRODUCTION
if ON_PRODUCTION:
app.run(host='0.0.0.0', port=80)
else:
app.run(host='127.0.0.1', port=5000)