-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompute.py
executable file
·291 lines (250 loc) · 12.3 KB
/
compute.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
"""
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.
"""
import csv
import shutil
import json, requests
import time
from datetime import datetime
from config import _FILTER_TIME, _SESSION_TIME, timeWindow, rssiThreshold
#import values from DB query
#MERAKI_API_KEY = test.MERAKI_API_KEY
# opens cmxdata and formats to show only relevant information
# if time difference is within session time, the times are considered within the same session, if they are out, they are considered 2 different sessions
def getCMXHours(data):
arrayCount = 0
newData = []
for x in data:
timeEntries = len(x['timestamps'])
if timeEntries > 1:
firstSeen =0
time2=timeEntries-1
time1=timeEntries-2
startFlag=0
timeCount=0
while time1 >= 0:
if (int(x['timestamps'][time1]) - int(x['timestamps'][time2])) < _SESSION_TIME and startFlag == 0 and firstSeen == 0:
# print(str(x['timestamps'][time1]) + ' - ' + str(x['timestamps'][time2]) + ' = ' + str(int(x['timestamps'][time1]) - int(x['timestamps'][time2])) + ' - less than 300, firstseen = 0')
newData.append({'MAC':x['MAC'],'timeData':[{'firstSeen':x['timestamps'][time2]}],'visits':1, 'totalTime':(int(x['timestamps'][time1]) - int(x['timestamps'][time2]))})
if time1 == 0:
newData[arrayCount]['timeData'][timeCount]['lastSeen']=x['timestamps'][time1]
timeCount=timeCount+1
startFlag=1
# startTimeTemp=time2
firstSeen=1
time2 = time1
time1 = time1-1
elif int(x['timestamps'][time1]) - int(x['timestamps'][time2]) < _SESSION_TIME and startFlag == 0 and firstSeen == 1:
# print(str(x['timestamps'][time1]) + ' - ' + str(x['timestamps'][time2]) + ' = ' + str(int(x['timestamps'][time1]) - int(x['timestamps'][time2])) + ' - less than 300, startflag=0')
if time1 != 0:
newData[arrayCount]['timeData'].append({'firstSeen':x['timestamps'][time2]})
newData[arrayCount]['visits']=int(newData[arrayCount]['visits'])+1
newData[arrayCount]['totalTime']=int(newData[arrayCount]['totalTime']) + (int(x['timestamps'][time1]) - int(x['timestamps'][time2]))
startFlag=1
time2 = time1
time1 = time1-1
elif int(x['timestamps'][time1]) - int(x['timestamps'][time2]) < _SESSION_TIME and startFlag == 1 :
# print(newData[arrayCount]['totalTime'])
# print(str(x['timestamps'][time1]) + ' - ' + str(x['timestamps'][time2]) + ' = ' + str(int(x['timestamps'][time1]) - int(x['timestamps'][time2])) + ' less than 300, startflag=1')
newData[arrayCount]['totalTime']=int(newData[arrayCount]['totalTime']) + (int(x['timestamps'][time1]) - int(x['timestamps'][time2]))
if time1 == 0:
newData[arrayCount]['timeData'][timeCount]['lastSeen']=x['timestamps'][time1]
timeCount=timeCount+1
time2 = time1
time1 = time1-1
elif int(x['timestamps'][time1]) - int(x['timestamps'][time2]) >= _SESSION_TIME and startFlag == 1:
# print(str(x['timestamps'][time1]) + ' - ' + str(x['timestamps'][time2]) + ' = ' + str(int(x['timestamps'][time1]) - int(x['timestamps'][time2])) +' greater than 300, startflag reset')
newData[arrayCount]['timeData'][timeCount]['lastSeen']=x['timestamps'][time2]
startFlag=0
timeCount=timeCount+1
time2 = time1
time1 = time1-1
elif int(x['timestamps'][time1]) - int(x['timestamps'][time2]) >= _SESSION_TIME and startFlag == 0:
# print(str(x['timestamps'][time1]) + ' - ' + str(x['timestamps'][time2]) + ' = ' + str(int(x['timestamps'][time1]) - int(x['timestamps'][time2])) + ' greater than 300, startflag=0')
time2 = time1
time1 = time1-1
if timeCount > 0:
arrayCount=arrayCount+1
return(newData)
# filters out cmx data seen over a certain period of time
def cmxFilterHours(data):
newData=getCMXHours(data)
returnData=[]
for x in newData:
if x['totalTime'] < _FILTER_TIME:
returnData.append(x)
return(returnData)
# gets meraki MV video link
def getMVLink(serial_number,timestamp):
from flaskApp import Setup
setupEntry = Setup.query.order_by(Setup.id.desc()).first().__dict__
MERAKI_API_KEY = setupEntry.get('meraki_api_key')
NETWORK_ID = setupEntry.get('network_id')
# Get video link
url = "https://api.meraki.com/api/v0/networks/"+NETWORK_ID+"/cameras/"+str(serial_number)+"/videoLink?timestamp="+str(timestamp)
headers = {
'X-Cisco-Meraki-API-Key': MERAKI_API_KEY,
"Content-Type": "application/json"
}
resp = requests.request("GET", url, headers=headers)
# print(resp)
if int(resp.status_code / 100) == 2:
return(resp.text)
return('link error')
# gets meraki MV activity summary overview for a camera
def getMVOverview(serial_number):
from flaskApp import Setup
setupEntry = Setup.query.order_by(Setup.id.desc()).first().__dict__
MERAKI_API_KEY = setupEntry.get('meraki_api_key')
_APMACADDR = setupEntry.get('ap_mac_address')
# Get video link
url = "https://api.meraki.com/api/v0/devices/"+serial_number+"/camera/analytics/overview?timespan=604800"
headers = {
'X-Cisco-Meraki-API-Key': MERAKI_API_KEY,
"Content-Type": "application/json"
}
resp = requests.request("GET", url, headers=headers)
print("URL: ", url)
print("Call to MV overview response: ", resp)
if int(resp.status_code / 100) == 2:
return(resp.text)
return('link error')
# gets meraki MV zones for a camera
def getMVZones(serial_number):
from flaskApp import Setup
setupEntry = Setup.query.order_by(Setup.id.desc()).first().__dict__
MERAKI_API_KEY = setupEntry.get('meraki_api_key')
# Get video link
url = "https://api.meraki.com/api/v0/devices/"+serial_number+"/camera/analytics/zones"
headers = {
'X-Cisco-Meraki-API-Key': MERAKI_API_KEY,
"Content-Type": "application/json"
}
resp = requests.request("GET", url, headers=headers)
print("URL: ", url)
print("Call to MV zones response: ", resp)
if int(resp.status_code / 100) == 2:
return(resp.text)
return('link error')
# gets meraki MV activity summary overview for a camera
def getCameraScreenshot(serial_number,timestamp):
from flaskApp import Setup
setupEntry = Setup.query.order_by(Setup.id.desc()).first().__dict__
MERAKI_API_KEY = setupEntry.get('meraki_api_key')
NETWORK_ID = setupEntry.get('network_id')
# Get video link
url = "https://api.meraki.com/api/v0/networks/"+NETWORK_ID+"/cameras/"+serial_number+"/snapshot"
# headers = {
# 'X-Cisco-Meraki-API-Key': api_key,
# "Content-Type": "application/json"
# }
headers = {
'X-Cisco-Meraki-API-Key': MERAKI_API_KEY,
'cache-control': "no-cache",
}
querystring = {"timestamp": timestamp}
payload = ""
resp = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print("URL: ", url)
print("Timestamp: ",timestamp)
print("Call to camera snapshot response: ", resp)
if int(resp.status_code / 100) == 2:
return(resp.text)
return('link error')
# gets meraki MV history summary overview for a camera
def getMVHistory(serial_number, zone):
from flaskApp import Setup
setupEntry = Setup.query.order_by(Setup.id.desc()).first().__dict__
print(setupEntry)
MERAKI_API_KEY = setupEntry.get('meraki_api_key')
# Get video link
url = "https://api.meraki.com/api/v0/devices/"+serial_number+"/camera/analytics/zones/"+zone+"/history?timespan=50400"
headers = {
'X-Cisco-Meraki-API-Key': MERAKI_API_KEY,
"Content-Type": "application/json"""
}
resp = requests.request("GET", url, headers=headers)
# print(resp)
if int(resp.status_code / 100) == 2:
return(resp.text)
return('link error')
# gets meraki devices
def getDevices():
from flaskApp import Setup
setupEntry = Setup.query.order_by(Setup.id.desc()).first().__dict__
MERAKI_API_KEY = setupEntry.get('meraki_api_key')
NETWORK_ID = setupEntry.get('network_id')
# Get video link
url = "https://api.meraki.com/api/v0/networks/"+NETWORK_ID+"/devices/"
headers = {
'X-Cisco-Meraki-API-Key': MERAKI_API_KEY,
"Content-Type": "application/json"""
}
resp = requests.request("GET", url, headers=headers)
# print(resp)
if int(resp.status_code / 100) == 2:
return(resp.text)
return('link error')
# Aggregates amount of activity per hour throughout the day
def computeCMXActivity(data):
newData = getCMXHours(data)
timesArray=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for x in newData:
for y in x['timeData']:
inTime = int(datetime.fromtimestamp(float(y['firstSeen'])).strftime('%H'))
outTime = int(datetime.fromtimestamp(float(y['lastSeen'])).strftime('%H'))
# print('---'+inTime+'---'+outTime+'---'+count)
# count = count + 1
while inTime <= outTime:
timesArray[inTime] = timesArray[inTime] + 1
inTime = inTime+1
return(timesArray)
# returns array of activity throughout the day of Mv activity
def computeMVActivity(data):
timesArray=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for x in data:
inTime = int(datetime.fromtimestamp(float(x['timeIn'])/1000).strftime('%H'))
outTime = int(datetime.fromtimestamp(float(x['timeOut'])/1000).strftime('%H'))
#
while inTime <= outTime:
timesArray[inTime] = timesArray[inTime] + int(x['count'])
inTime = inTime+1
return(timesArray)
# correlate MV sense data with cmx data
# when MV sense is triggered, check what MAC addresses have been found within certain time window and RSSI threshold
# Time window for devices to be considered is slightly larger than the inTime and outTime of mv data
def getCorrelation(cmxData,mvData):
data=[]
count=0
for x in mvData:
inTime = int(x['timeIn'])/1000
outTime = int(x['timeOut'])/1000
people = x['count']
flag=0
# restrict to those timestamp differences of more than one second
if outTime>inTime+1:
for y in cmxData:
for z in y['timestamps']:
if int(z['ts']) >= (inTime-timeWindow) and int(z['ts']) <= (outTime+timeWindow) and int(z['rssi']) > rssiThreshold and flag == 0:
data.append({'inTime':datetime.fromtimestamp(float(inTime)).strftime('%H:%M:%S'),'outTime':datetime.fromtimestamp(float(outTime)).strftime('%H:%M:%S'), 'count':people, 'devices':[{'MAC':y['MAC'],'time':datetime.fromtimestamp(float(z['ts'])).strftime('%H:%M:%S'),'rssi':z['rssi']}]})
flag=1
if int(z['ts']) >= (inTime-timeWindow) and int(z['ts']) <= (outTime+timeWindow) and int(z['rssi']) > rssiThreshold and flag == 1:
update=1
for d in data[count]['devices']:
if y['MAC'] in d.values():
update=0
if update==1:
data[count]['devices'].append({'MAC':y['MAC'],'time':datetime.fromtimestamp(float(outTime)).strftime('%H:%M:%S'),'rssi':z['rssi']})
if flag==1:
count+=1
return(data)