-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaskApp.py
executable file
·582 lines (458 loc) · 28.3 KB
/
flaskApp.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env python3
"""
Copyright (c) 2019 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
# web application GUI
from flask import Flask, render_template, request, jsonify, url_for, json, redirect
import csv
import shutil
from datetime import datetime
from flask_googlecharts import GoogleCharts
from flask_googlecharts import BarChart, MaterialLineChart, ColumnChart
from flask_googlecharts.utils import prep_data
from config import COLLECT_CAMERAS_MVSENSE_CAPABLE, NETWORK_ID
from compute import *
import time
import pytz # $ pip install pytz
import tzlocal # $ pip install tzlocal
#from flask_wtf import Form
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired, Email, NumberRange
import os
from subprocess import Popen
import time
import paho.mqtt.client as mqtt
from config import MQTT_SERVER,MQTT_PORT,MOTION_ALERT_ITERATE_COUNT,MOTION_ALERT_TRIGGER_PEOPLE_COUNT,MOTION_ALERT_PAUSE_TIME,TIMEOUT
from config import BOT_ACCESS_TOKEN
from webexteamssdk import WebexTeamsAPI
MOTION_ALERT_PEOPLE_COUNT_THRESHOLD = 3
MOTION_ALERT_DWELL_TIME = 60000
CROWD_EVENTS_MESSAGE_RECIPIENT= ''
app = Flask(__name__)
SECRET_KEY = os.urandom(32)
app.config['SECRET_KEY'] = SECRET_KEY
charts = GoogleCharts(app)
webexApi = WebexTeamsAPI(access_token=BOT_ACCESS_TOKEN)
ALL_CAMERAS_AND_ZONES={}
MQTT_TOPICS = []
client = mqtt.Client()
def load_all_cameras_details():
global ALL_CAMERAS_AND_ZONES
global MQTT_TOPICS
devices_data = getDevices()
if devices_data != 'link error':
AllDevices = json.loads(devices_data)
for theDevice in AllDevices:
theModel = theDevice["model"]
if theModel[:4] not in COLLECT_CAMERAS_MVSENSE_CAPABLE:
continue
#create dict entry in ALL_CAMERAS_AND_ZONES for this camera, just the top level values for now
if "name" in theDevice.keys():
theDeviceName = theDevice["name"]
else:
theDeviceName = "Camera "+theDevice["serial"]
ALL_CAMERAS_AND_ZONES[theDevice["serial"]]={'name': theDeviceName,
'zones': {}}
zonesdetaildata = getMVZones(theDevice["serial"])
if zonesdetaildata == 'link error':
continue
#print("getMVZones returned:", zonesdetaildata)
MVZonesDetails = json.loads(zonesdetaildata)
#now fill out the zones and corresponding details
theZoneDetailsDict={}
for zoneDetails in MVZonesDetails:
#we are not interested in cameras that have no zones defined, so we do not pull the details into the dict and
#we do not add it to the MQTT_TOPICS to monitor
if zoneDetails["zoneId"]!='0':
theZoneDetailsDict[zoneDetails["zoneId"]]={'label':zoneDetails["label"],
'_MONITORING_TRIGGERED': False,
'_MONITORING_MESSAGE_COUNT':0,
'_MONITORING_PEOPLE_TOTAL_COUNT':0,
'_TIMESTAMP':0,
'_TIMEOUT_COUNT':0,
'_TEST_TRIG_START':0}
MQTT_TOPICS.append("/merakimv/" + theDevice["serial"] + "/" + zoneDetails["zoneId"])
ALL_CAMERAS_AND_ZONES[theDevice["serial"]]['zones'] = theZoneDetailsDict
print("All cameras and zones info: ", ALL_CAMERAS_AND_ZONES)
print("MQTT Topics: ",MQTT_TOPICS)
def collect_zone_information(topic, payload):
## /merakimv/Q2GV-S7PZ-FGBK/123
parameters = topic.split("/")
serial_number = parameters[2]
zone_id = parameters[3]
# detect motion
global ALL_CAMERAS_AND_ZONES
# if motion monitoring triggered
if ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_TRIGGERED']:
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_MESSAGE_COUNT'] = ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_MESSAGE_COUNT'] + 1
if ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'] < payload['counts']['person']:
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'] = payload['counts']['person']
if payload['counts']['person'] > 0:
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMEOUT_COUNT'] = 0
elif payload['counts']['person'] == 0:
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMEOUT_COUNT'] = ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMEOUT_COUNT'] + 1
# Enough time has elapsed without action and the minimum number of Motion messages have been received to qualify for successful action
if ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMEOUT_COUNT'] >= TIMEOUT and ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_MESSAGE_COUNT'] >= MOTION_ALERT_ITERATE_COUNT:
# Minimum people count reached
if ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'] >= MOTION_ALERT_TRIGGER_PEOPLE_COUNT:
# notification
#print('---MESSAGE ALERT---' + serial_number, _MONITORING_PEOPLE_TOTAL_COUNT,_TIMESTAMP,payload['ts'])
#notify(serial_number, _MONITORING_PEOPLE_TOTAL_COUNT,_TIMESTAMP, payload['ts'])
#print('---ALERTED---')
# pause
time.sleep(MOTION_ALERT_PAUSE_TIME)
# reset
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_MESSAGE_COUNT'] = 0
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'] = 0
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_TRIGGERED'] = False
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP'] = 0
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMEOUT_COUNT'] = 0
# not a registered action
elif ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMEOUT_COUNT'] >= TIMEOUT and ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_MESSAGE_COUNT'] < MOTION_ALERT_ITERATE_COUNT:
# reset
print('---ALERT DISMISSED---')
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_MESSAGE_COUNT'] = 0
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'] = 0
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_TRIGGERED'] = False
_TIMESTAMP = 0
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMEOUT_COUNT'] = 0
# print(payload['counts']['person'])
if payload['counts']['person'] >= MOTION_ALERT_PEOPLE_COUNT_THRESHOLD:
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_TRIGGERED'] = True
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP'] = payload['ts']
#print("payload "+serial_number+": " + str(payload) +
# ", _MONITORING_TRIGGERED : " + str(_MONITORING_TRIGGERED) +
# ", _MONITORING_MESSAGE_COUNT : " + str(_MONITORING_MESSAGE_COUNT) +
# ", _MONITORING_PEOPLE_TOTAL_COUNT : " + str(_MONITORING_PEOPLE_TOTAL_COUNT)+
# ", timeout: "+str(_TIMEOUT_COUNT))
print(ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'],MOTION_ALERT_PEOPLE_COUNT_THRESHOLD)
if ( ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'] >= MOTION_ALERT_PEOPLE_COUNT_THRESHOLD ):
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP'] = payload['ts']
print("Timestamp: " + str(ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP']) + " TrigTimestamp: " + str(ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TEST_TRIG_START'])+" diff:"+str(ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP'] - ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TEST_TRIG_START']))
if (ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TEST_TRIG_START'] == 0):
# start mesuring a dwelling period
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TEST_TRIG_START'] = payload['ts']
if ((ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP'] - ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TEST_TRIG_START']) >= MOTION_ALERT_DWELL_TIME):
try:
theDTts = datetime.utcfromtimestamp(payload['ts'] / 1e3)
print("DateTime: ", theDTts)
theISOts = theDTts.isoformat() + "Z"
print("ISO timestamp: ", theISOts)
except Exception as ex:
print("Timestamp calc failed due to: \n {0}".format(ex))
theText=u"At least " + str(MOTION_ALERT_PEOPLE_COUNT_THRESHOLD) + " person(s) detected for more than " + str(int(MOTION_ALERT_DWELL_TIME/1000)) + " seconds on camera "+ALL_CAMERAS_AND_ZONES[serial_number]['name']+" for zone "+ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['label']
print("Text from Flask app: ",theText)
# spin off separate process to retrieve screenshot and send to Teams recipient. This can take a few
# seconds, especiall when waiting for the snapshot URL to return a valid image
# so we do not want it to block the main code doing all the detecting
Popen(f'python3 send.py "{theText}" {CROWD_EVENTS_MESSAGE_RECIPIENT} {serial_number} {theISOts}', shell=True)
print('---MESSAGE ALERT---' + serial_number, ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'], ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP'], payload['ts'])
#notify(serial_number, zone_id, ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'], ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP'], payload['ts'])
notify(ALL_CAMERAS_AND_ZONES[serial_number]['name'], serial_number, ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['label'], ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_MONITORING_PEOPLE_TOTAL_COUNT'], ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TIMESTAMP'], payload['ts'])
print('---ALERTED---')
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TEST_TRIG_START'] = 0
else:
ALL_CAMERAS_AND_ZONES[serial_number]['zones'][zone_id]['_TEST_TRIG_START'] = 0
def notify(camera_name,camera_serial, zone_label, count,timestampIN, timestampOUT):
with open('mvData.csv','a') as csvfile:
fieldnames = ['Camera','Serial', 'Zone', 'Time In','Time Out','Count']
writer=csv.DictWriter(csvfile,fieldnames=fieldnames)
writer.writerow({'Camera':camera_name,'Serial':camera_serial,'Zone':zone_label, 'Time In':timestampIN,'Time Out':timestampOUT, 'Count':count})
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
for topic in MQTT_TOPICS:
client.subscribe(topic)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
payload = json.loads(msg.payload.decode("utf-8"))
parameters = msg.topic.split("/")
#print("received a message: ", payload, " with parameters: ",parameters)
serial_number = parameters[2]
message_type = parameters[3]
#print("received a message type: ",message_type," serial: ",serial_number," index: ", index)
if message_type != 'raw_detections' and message_type != 'light':
print(message_type)
collect_zone_information(msg.topic, payload)
#if message_type == '0':
# collect_zone_information(msg.topic,payload)
# print("processing msg with topic: ", msg.topic)
def mvSenseThreadStart():
# mqtt
global client
try:
client.on_connect = on_connect
client.on_message = on_message
# client.username_pw_set("SPMlWZKRd0k9hc1D33Zvi11ncQWUBPJiujrg60X9Q77V7WoZQciW3793NVNdAkjS","")
client.connect(MQTT_SERVER, MQTT_PORT, 60)
#client.loop_forever()
client.loop_start()
except Exception as ex:
print("[MQTT]failed to connect or receive msg from mqtt, due to: \n {0}".format(ex))
def mvSenseThreadStop():
# mqtt
global client
try:
client.loop_stop()
except Exception as ex:
print("[MQTT]failed to stop, due to: \n {0}".format(ex))
class ConfigurationForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
peopleCount = StringField('PeopleCount', validators=[DataRequired()])
dwellTime = StringField('DwellTime', validators=[DataRequired()] )
@app.route('/start_mvsense')
def startMVSense():
#global theMVSenseThread
#theMVSenseThread.start()
mvSenseThreadStart()
return 'ok'
@app.route('/stop_mvsense')
def stopMVSense():
#global theMVSenseThread
#theMVSenseThread.stop()
mvSenseThreadStop()
return 'ok'
@app.route('/mvSense',methods=['GET','POST'])
def mvSense():
# open mv sense data
data = []
with open('mvData.csv') as csvfile:
reader = csv.DictReader(csvfile)
count = 0
arrayCount=0
flag=0
for row in reader:
print(row['Time In'])
#link = getMVLink(row['Serial'],row['Time In'])
#link = link.replace('{"url":"',"")
#link = link.replace('"}',"")
link = "/getsnapshot?serial="+row['Serial']+"×tamp="+row['Time In']
data.append({'Camera':row['Camera'],'Zone':row['Zone'],'timeIn':datetime.fromtimestamp(float(row['Time In'])/1000).strftime('%m-%d,%H:%M'),'timeOut':datetime.fromtimestamp(float(row['Time Out'])/1000).strftime('%m-%d,%H:%M'),'count':row['Count'],'link':link})
# print(len(data[0]['timestamps']))
return render_template("mvSense.html",data=data, numPersons=MOTION_ALERT_PEOPLE_COUNT_THRESHOLD, numSeconds=int(MOTION_ALERT_DWELL_TIME/1000))
@app.route('/pleasewait',methods=['GET'])
def pleasewait():
# this is for the GET to show the overview
return render_template("pleasewait.html", theReason='Getting crowd events for cameras on network: ' + NETWORK_ID)
@app.route('/getsnapshot',methods=['GET'])
def getsnapshot():
theSerial = request.args['serial']
theTimestamp = request.args['timestamp']
theISOts=""
try:
theDTts = datetime.utcfromtimestamp(int(theTimestamp) / 1e3)
print("DateTime: ", theDTts)
theISOts = theDTts.isoformat() + "Z"
print("ISO timestamp: ", theISOts)
except Exception as ex:
print("Timestamp calc failed due to: \n {0}".format(ex))
# retrieve the snapshot for that time
theScreenShotURL = ""
screenShotURLdata = getCameraScreenshot(theSerial, theISOts)
print("getCameraSCreenshot returned: ", screenShotURLdata)
if screenShotURLdata != 'link error':
screenShotURL = json.loads(screenShotURLdata)
theScreenShotURL = screenShotURL["url"]
wait_time = 7000
seconds = wait_time / 1000
#return redirect(theScreenShotURL, code=302)
return f"<html><body><p>You will be redirected in {seconds} seconds</p><script>var timer = setTimeout(function() {{window.location='{theScreenShotURL}'}}, {wait_time});</script></body></html>"
return redirect("/nosnapshot", code=302)
@app.route("/nosnapshot")
def nosnapshot():
return "There is no snapshot!"
@app.route('/',methods=['GET','POST'])
@app.route('/index',methods=['GET','POST'])
def index():
global MOTION_ALERT_PEOPLE_COUNT_THRESHOLD, CROWD_EVENTS_MESSAGE_RECIPIENT, MOTION_ALERT_DWELL_TIME
form = ConfigurationForm()
if form.validate_on_submit():
#copy the values from the form into variables
CROWD_EVENTS_MESSAGE_RECIPIENT=form.email.data
MOTION_ALERT_PEOPLE_COUNT_THRESHOLD=int(form.peopleCount.data)
MOTION_ALERT_DWELL_TIME=int(form.dwellTime.data)*1000
print("set email:", CROWD_EVENTS_MESSAGE_RECIPIENT, " PeopleCount: ",MOTION_ALERT_PEOPLE_COUNT_THRESHOLD," and dwellTime: ",MOTION_ALERT_DWELL_TIME)
return render_template("index.html", form=form)
return render_template("index.html", form=form)
@app.route('/mvOverview',methods=['GET','POST'])
def mvOverview():
# extract MVSense over view data for a camera from the analytics API
MVZones = []
animation_option = {"startup": True, "duration": 1000, "easing": 'out'}
#First we have the logic for creating the page with all the historical details of zone below,
#further down is the logic to show the Overview of all cameras and their respective zones
if request.method == 'POST':
# This is for the historical detail
zoneDetails = request.form['zone_details']
print("zoneDetails=",zoneDetails)
#zoneDetailsTuple contains: [camera serial, camera name, zone id, zone name]
zoneDetailsTuple=zoneDetails.split(',')
theSERIAL=zoneDetailsTuple[0]
theCameraNAME=zoneDetailsTuple[1]
theZoneID=zoneDetailsTuple[2]
theZoneNAME=zoneDetailsTuple[3]
data = getMVHistory(theSERIAL,theZoneID)
if data != 'link error':
print("getMVHistory returned:", data)
MVHistory = json.loads(data)
# add a chart
# now create the chart object using the serial as the name and the name of the device as the title
mv_history_chart = ColumnChart("mvhistorychart", options={"title": "Camera: "+theCameraNAME+" Zone: "+theZoneNAME,
"width": 1000,
"height": 500,
"hAxis.title": "Hour",
"animation": animation_option})
mv_history_chart.add_column("string", "Zone")
mv_history_chart.add_column("number", "Visitors")
print(data)
the_rows = []
theHoursDict = dict()
theHoursMaxEntrancesDict = dict()
theHoursMaxEntrancesTimestampDict = dict()
theLocalHoursMaxEntrancesTimestampDict = dict()
for j in range(len(MVHistory)):
# grab all events in MVHistory, then
# tabulate and summarize in hour blocks
# example startTS: "2019-08-05T17:06:46.312Z" example endTs: "2019-08-05T17:07:46.312Z"
# also, for each hour that has entrances, select the timeframe where there are
# the most and extract a snapshot 30 seconds after that timestamp to show below in the page
thisStartTs = MVHistory[j]["startTs"]
thisEndTs = MVHistory[j]["endTs"]
thisHour = thisEndTs.partition('T')[2][:2]
theEndTsTimeStamp=datetime.strptime(thisEndTs, "%Y-%m-%dT%H:%M:%S.%fZ")
thisMinuteMedTimestamp= time.mktime(theEndTsTimeStamp.timetuple())-30
thisMinuteMedISOts=datetime.fromtimestamp(thisMinuteMedTimestamp).isoformat()+"Z"
#convert to localtimezone
local_timezone = tzlocal.get_localzone() # get pytz tzinfo
local_timezone_str = str(local_timezone)
theLocalEndTsTimeStamp = theEndTsTimeStamp.replace(tzinfo=pytz.utc).astimezone(local_timezone)
thislocalMinuteMedTimestamp= time.mktime(theLocalEndTsTimeStamp.timetuple())-30
thislocalMinuteMedISOts = datetime.fromtimestamp(thislocalMinuteMedTimestamp).isoformat() + "Z"
localHour = thislocalMinuteMedISOts.partition('T')[2][:2]
#print("Timestamp string:",thisEndTs )
#print("Numerical equivalent: ", thisMinuteMedTimestamp)
#print("Local Numerical equivalent: ", thislocalMinuteMedTimestamp)
#print("ISO equivalent: ", thisMinuteMedISOts)
#print("local ISO equivalent: ", thislocalMinuteMedISOts)
thisEntrances = MVHistory[j]["entrances"]
# Now we will use localHour instead of thisHour as the Dict key to hold the accounting for body
# detection per hour since that is what is shown on the graph, it should behave the same otherwise
# as when we used thisHour originally, but show a local hour instead of UTC which was confusing.
if localHour in theHoursDict.keys():
#increase the number of entrances of this hour slot
theHoursDict[localHour]=theHoursDict[localHour]+thisEntrances
#check to see if the entrances for this minute are the most for this hour
if thisEntrances>theHoursMaxEntrancesDict[localHour]:
#if so, make these entrances the most for the timeframe and save the timestamp for the
#middle of the minute with the most entrances
theHoursMaxEntrancesDict[localHour]=thisEntrances
theHoursMaxEntrancesTimestampDict[localHour]=thisMinuteMedISOts
#keep track of local version as well
theLocalHoursMaxEntrancesTimestampDict[localHour]=thislocalMinuteMedISOts
else:
#if this is the first time we see this timeslot, make the current entrances
#the starting balance for the dict entry
theHoursDict[localHour] = thisEntrances
theHoursMaxEntrancesDict[localHour] = thisEntrances
#only keep timestamp if there is at least one entry detected
if thisEntrances>0:
theHoursMaxEntrancesTimestampDict[localHour] = thisMinuteMedISOts
theLocalHoursMaxEntrancesTimestampDict[localHour] = thislocalMinuteMedISOts
else:
theHoursMaxEntrancesTimestampDict[localHour]=''
theLocalHoursMaxEntrancesTimestampDict[localHour]=''
for dEntryKey in theHoursDict.keys():
the_rows.append([dEntryKey, theHoursDict[dEntryKey]])
mv_history_chart.add_rows(the_rows)
charts.register(mv_history_chart)
print("Max Entrances Timestamps: ", theHoursMaxEntrancesTimestampDict)
print("Max Local Entrances Timestamps: ", theLocalHoursMaxEntrancesTimestampDict)
#theScreenshots is an array of arays in the format [ timestamp string, snapshot URL ]
#this is to be passed to the form that will render them
theScreenshots=[]
for dTimeStampKey in theHoursMaxEntrancesTimestampDict.keys():
if theHoursMaxEntrancesTimestampDict[dTimeStampKey]!='':
screenShotURLdata=getCameraScreenshot(theSERIAL,theHoursMaxEntrancesTimestampDict[dTimeStampKey])
print("getCameraSCreenshot returned: ",screenShotURLdata)
if screenShotURLdata != 'link error':
screenShotURL = json.loads(screenShotURLdata)
#Passing theLocalHoursMaxEntrancesTimestampDict[dTimeStampKey] instead of theHoursMaxEntrancesTimestampDict[dTimeStampKey] below
#to show a local timestamp we calculated in a previous loop
theScreenshots.append([ theLocalHoursMaxEntrancesTimestampDict[dTimeStampKey], screenShotURL["url"]])
# wait for the URLs to be valid
print("Waiting 10 seconds...")
time.sleep(10)
return render_template("mvHistory.html", historyChart=mv_history_chart, snapshotsArray=theScreenshots, localTimezone=local_timezone_str)
else:
devices_data=getDevices()
if devices_data != 'link error':
AllDevices=json.loads(devices_data)
#theDeviceCharts is just a list (array) of the names of the charts constructed with the
#google charts flask library. They are to be iterated through to place on the History/details page
theDeviceCharts=[]
#theDeviceDetails is a list of the details of each camera device. Each entry has a serial number, label and
#a list of zones for which there is zoneID and label
theDeviceDetails=[]
theChartNum=0
for theDevice in AllDevices:
theModel=theDevice["model"]
if theModel[:4] not in COLLECT_CAMERAS_MVSENSE_CAPABLE:
continue
data=getMVOverview(theDevice["serial"])
if data == 'link error':
continue
print("getMVOverview returned:" , data)
MVZones=json.loads(data)
zonesdetaildata=getMVZones(theDevice["serial"])
if zonesdetaildata == 'link error':
continue
print("getMVZones returned:" , zonesdetaildata)
MVZonesDetails=json.loads(zonesdetaildata)
# add a chart
#first add the name of the chart to the list of charts to be displayed in the page
theDeviceCharts.append("chart"+str(theChartNum))
#now append the top level details of the camera for this chart to theDeviceDetails
theDeviceDetails.append([theDevice["serial"],theDevice["name"],[]])
#now create the chart object using the serial as the name and the name of the device as the title
mv_overview_chart = ColumnChart("chart"+str(theChartNum), options={"title": theDevice["name"],
"width": 800,
"height": 400,
"hAxis.title": "Hour",
"animation": animation_option})
mv_overview_chart.add_column("string", "Zone")
mv_overview_chart.add_column("number", "Visitors")
print(data)
the_rows = []
for j in range(len(MVZones)):
thisZone=MVZones[j]
#assuming same number of zone entries overviews than number of zones here
thisZoneDetails=MVZonesDetails[j]
the_rows.append([ str(thisZoneDetails["label"]), thisZone["entrances"] ])
# store away the zoneID and serial of the camera to pass to the form so when someone clicks
# on a bar or button to expand detail, it comes back to this function in the POST section
# to know which zone from which camera to use
#we are assuming below that we have a chart per each MV capable camera, if that changes
# we need to figure out aonther way to index theDeviceDetails or use some other method
# to store/retrieve the data besides a list of lists
theDeviceDetails[theChartNum][2].append([thisZoneDetails["zoneId"], thisZoneDetails["label"]])
mv_overview_chart.add_rows(the_rows)
charts.register(mv_overview_chart)
theChartNum+=1
print("Rendering overview form with:")
print("allTheDetails=",theDeviceDetails)
return render_template("mvOverview.html",allTheCharts=theDeviceCharts,allTheDetails=theDeviceDetails)
else:
return render_template('error.html'), 404
if __name__ == "__main__":
load_all_cameras_details()
app.run(host='0.0.0.0', port=5001, debug=True)