-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmvSense.py
executable file
·175 lines (128 loc) · 6.26 KB
/
mvSense.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
"""
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.
"""
# monitors for motion trigger from Meraki Camera MV Sense and writes it to a .txt file
import json, requests
import time
import paho.mqtt.client as mqtt
import csv
#from config import MQTT_SERVER,MQTT_PORT,MQTT_TOPIC,MERAKI_API_KEY,NETWORK_ID,COLLECT_CAMERAS_SERIAL_NUMBERS,COLLECT_ZONE_IDS,MOTION_ALERT_PEOPLE_COUNT_THRESHOLD,MOTION_ALERT_ITERATE_COUNT,MOTION_ALERT_TRIGGER_PEOPLE_COUNT,MOTION_ALERT_PAUSE_TIME,TIMEOUT
from config import MQTT_SERVER,MQTT_PORT,MQTT_TOPIC, COLLECT_ZONE_IDS,MOTION_ALERT_PEOPLE_COUNT_THRESHOLD,MOTION_ALERT_ITERATE_COUNT,MOTION_ALERT_TRIGGER_PEOPLE_COUNT,MOTION_ALERT_PAUSE_TIME,TIMEOUT
from flaskApp import MERAKI_API_KEY, NETWORK_ID, camera_serial_number, db, mvDataTbl
COLLECT_CAMERAS_SERIAL_NUMBERS = []
COLLECT_CAMERAS_SERIAL_NUMBERS.append(camera_serial_number)
print(MERAKI_API_KEY)
print(NETWORK_ID)
print(COLLECT_CAMERAS_SERIAL_NUMBERS)
_MONITORING_TRIGGERED = False
_MONITORING_MESSAGE_COUNT = 0
_MONITORING_PEOPLE_TOTAL_COUNT = 0
_TIMESTAMP = 0
_TIMEOUT_COUNT = 0
def collect_zone_information(topic, payload):
## /merakimv/Q2GV-S7PZ-FGBK/123
parameters = topic.split("/")
serial_number = parameters[2]
zone_id = parameters[3]
index = len([i for i, x in enumerate(COLLECT_ZONE_IDS) if x == zone_id])
# if not wildcard or not in the zone_id list or equal to 0 (whole camera)
if COLLECT_ZONE_IDS[0] != "*":
if index == 0 or zone_id == "0":
return
# detect motion
global _MONITORING_TRIGGERED, _MONITORING_MESSAGE_COUNT, _MONITORING_PEOPLE_TOTAL_COUNT, _TIMESTAMP, TIMEOUT, _TIMEOUT_COUNT
# if motion monitoring triggered
if _MONITORING_TRIGGERED:
_MONITORING_MESSAGE_COUNT = _MONITORING_MESSAGE_COUNT + 1
if _MONITORING_PEOPLE_TOTAL_COUNT < payload['counts']['person']:
_MONITORING_PEOPLE_TOTAL_COUNT = payload['counts']['person']
if payload['counts']['person'] > 0:
_TIMEOUT_COUNT = 0
elif payload['counts']['person'] == 0:
_TIMEOUT_COUNT = _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 _TIMEOUT_COUNT >= TIMEOUT and _MONITORING_MESSAGE_COUNT >= MOTION_ALERT_ITERATE_COUNT:
# Minimum people count reached
if _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
_MONITORING_MESSAGE_COUNT = 0
_MONITORING_PEOPLE_TOTAL_COUNT = 0
_MONITORING_TRIGGERED = False
_TIMESTAMP = 0
_TIMEOUT_COUNT = 0
# not a registered action
elif _TIMEOUT_COUNT >= TIMEOUT and _MONITORING_MESSAGE_COUNT < MOTION_ALERT_ITERATE_COUNT:
# reset
print('---ALERT DISMISSED---')
_MONITORING_MESSAGE_COUNT = 0
_MONITORING_PEOPLE_TOTAL_COUNT = 0
_MONITORING_TRIGGERED = False
_TIMESTAMP = 0
_TIMEOUT_COUNT = 0
# print(payload['counts']['person'])
if payload['counts']['person'] >= MOTION_ALERT_PEOPLE_COUNT_THRESHOLD:
_MONITORING_TRIGGERED = True
_TIMESTAMP = payload['ts']
print('.')
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))
def notify(serial_number,count,timestampIN, timestampOUT):
with open('mvData.csv','a') as csvfile:
fieldnames = ['Time In','Time Out','Count']
writer=csv.DictWriter(csvfile,fieldnames=fieldnames)
writer.writerow({'Time In':timestampIN,'Time Out':timestampOUT, 'Count':count})
#write rows to DB
print('writing data to mv_data_tbl')
mvDataWrite = mvDataTbl(timeIn=timestampIN, timeOut=timestampOUT, count=count)
db.session.add(mvDataWrite)
db.session.commit()
print('data committed to mv_data_tbl')
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe(MQTT_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("/")
serial_number = parameters[2]
message_type = parameters[3]
index = len([i for i, x in enumerate(COLLECT_CAMERAS_SERIAL_NUMBERS) if x == serial_number])
# filter camera
if COLLECT_CAMERAS_SERIAL_NUMBERS[0] != "*":
if index == 0:
return
# 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)
if __name__ == "__main__":
# mqtt
try:
client = mqtt.Client()
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()
except Exception as ex:
print("[MQTT]failed to connect or receive msg from mqtt, due to: \n {0}".format(ex))