-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcamera_setup.py
214 lines (186 loc) · 7.5 KB
/
camera_setup.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
"""
# ----------------------------------------------------------------
# [^∆^] --> Created on 8:10 PM Nov 12 2019
# [•∆•] --> Author : Kumm
# [^∆^] --> Done :-
# [•∆•] ------> httpserver
# [^∆^] ------> config.ini file initialiser
# [•∆•] ------> Google Service Account Json fetcher
# [•∆•] ------> wifi hostspot creator - pyaccesspoint lib
# [^∆^] ------> Connect to wifi
# [•∆•] ------> Add CSS to registration html page
# [•∆•] ------> default ssid , password = Smart Camera, 1234567890
# [^∆^] --> Todo :-
# [•∆•] ------> Add this file to crontab show that it executes @
# reboot
# [^∆^] --> Solved issues :-
# [^∆^] ------> Auto reloading configuration
# [^∆^] --> Known issues :-
# [^∆^] ------> Hardware support is not readily available for
# edge device
# ----------------------------------------------------------------
"""
import re
import os
import cv2
import cgi
import time
import json
import socket
import datetime
import threading
import configparser
from wireless import Wireless
from datetime import datetime
from google.api_core import datetime_helpers
from http.server import BaseHTTPRequestHandler, HTTPServer
from PyAccessPoint import pyaccesspoint
from google.cloud import firestore, storage
configPath = "config.ini"
ip = None
def end_server():
global server # , access_point
server.socket.close()
# access_point.stop()
server.shutdown()
def write_ini(data_dict, filename="config.ini"):
try:
global parser, current_ip
# config file should have sender_mail[SMID], sender_pass[SMPK],
# projectname[PJN], service_account_file_path[SFP],
# Hotspot_ssid[H_SSID], Hotspot_pass[H_PSK]
file = open(filename, "w+")
parser.add_section("CLOUD_CONFIG")
parser.set(parser.sections()[-1], "UID", data_dict["uid"])
parser.set(parser.sections()[-1], "CAM", data_dict["uid"] + "_deviceList")
parser.set(parser.sections()[-1], "LOG", data_dict["uid"] + "_intruderLog")
parser.set(parser.sections()[-1], "FAD", data_dict["uid"] + "_faceData")
parser.set(parser.sections()[-1], "TFD", data_dict["uid"] + "_trainFace")
parser.add_section("APP_CONFIG")
parser.set(parser.sections()[-1], "CameraInit", "False")
parser.set(parser.sections()[-1], "SSID", data_dict["wifiName"])
parser.set(parser.sections()[-1], "PSK", data_dict["wifiPass"])
parser.set(parser.sections()[-1], "H_SSID", data_dict["devWifiName"])
parser.set(parser.sections()[-1], "H_PSK", data_dict["devWifiPass"])
parser.set(parser.sections()[-1], "CameraName", data_dict["cameraName"])
parser.set(parser.sections()[-1], "CameraID", "")
parser.set(
parser.sections()[-1], "CameraURL", "rtsp://" + current_ip + ":8554/video"
)
parser.set(parser.sections()[-1], "mode", data_dict["mode"])
parser.set(parser.sections()[-1], "AppConnected", "true")
parser.set(parser.sections()[-1], "RMID", data_dict["rev_mailid"])
parser.set(parser.sections()[-1], "FUP", data_dict["fps"])
parser.write(file)
file.close()
return True
except:
return False
class Handler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
def do_HEAD(self):
self._set_headers()
# GET sends back a Hello world message
def do_GET(self):
self._set_headers()
self.wfile.write(json.dumps({"hello": "world", "received": "ok"}).encode())
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers.get("content-type"))
# refuse to receive non-json content
if ctype != "application/json":
self.send_response(400)
self.end_headers()
return
# read the message and convert it into a python dictionary
length = int(self.headers.get("content-length"))
message = json.loads(self.rfile.read(length))
if write_ini(message):
self._set_headers()
messsage = {"received": "ok"}
self.wfile.write(json.dumps(messsage).encode())
threading.Thread(target=end_server, daemon=True).start()
return
self.send_response(400)
self.end_headers()
return
parser = configparser.ConfigParser()
parser.optionxform = str
portal = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port_number = 80
result = parser.read(configPath)
if len(result) == 1 and parser["APP_CONFIG"].get("AppConnected") == "True":
filePath = parser["CLOUD_CONFIG"].get("SFP")
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.abspath(filePath)
wifi_ssid = parser["APP_CONFIG"].get("SSID")
logged_url = parser["APP_CONFIG"].get("CameraURL")
logged_name = parser["APP_CONFIG"].get("CameraName")
print("Configuration Loaded ...\nChecking Internet connection")
wireless = Wireless()
if wireless.current() != wifi_ssid:
wireless.connect(ssid=wifi_ssid, password=parser["APP_CONFIG"].get("PSK"))
print("Connecting to {} ".format(wifi_ssid), end=".")
wifi_connection_init = time.time()
while wireless.current() != wifi_ssid:
if time.time() - wifi_connection_init:
print(
"Cannot connect to {} \nSetup failed...Program Exitted".format(
wifi_ssid
),
end=".",
)
exit()
print(end=".")
continue
# delay added due to dns resolution issues
print("Waiting for DNS resolution")
time.sleep(10)
# check if connected to wifi
print("Checking cloud setup..")
portal.connect(("8.8.8.8", 80))
current_ip = portal.getsockname()[0]
db = firestore.Client()
if len(parser["APP_CONFIG"].get("CameraID").strip()) != 20:
col = db.collection(parser["CLOUD_CONFIG"].get("CAM"))
doc_ref = col.document()
parser.set("APP_CONFIG", "CameraID", doc_ref.id)
cam_document_template = {
"url": "rtsp://" + current_ip + ":8554/video",
"label": parser["APP_CONFIG"].get("CameraName"),
"addedOn": datetime.now(),
"editedOn": datetime.now(),
"lastSeen": None,
"statusOn": True,
"uid": doc_ref.id,
"mode": parser["APP_CONFIG"].get("mode"),
}
expected_result = doc_ref.set(cam_document_template)
while not expected_result.IsInitialized():
expected_result = doc_ref.set(cam_document_template)
parser.set("APP_CONFIG", "CameraURL", "rtsp://" + current_ip + ":8554/video")
with open("config.ini", "w") as file:
parser.write(file)
print("Starting smart camera")
os.system("python3 smart_camera.py")
elif len(result) == 1 and parser["APP_CONFIG"].get("AppConnected") == "False":
# Start wifi hotspot
access_point = pyaccesspoint.AccessPoint(
ssid=parser["APP_CONFIG"].get("H_SSID"),
password=parser["APP_CONFIG"].get("H_PSK"),
)
access_point.start()
print(
"Please connect to the hotspot : \nSSID : Smart Camera\nPassword : 1234567890"
)
# Starting http server
server_address = ("", port_number)
server = HTTPServer(server_address, Handler)
server.serve_forever()
# Get the form data
os.system("python3 camera_setup.py")
else:
print("Device Corrupted")
exit()
# execute smart_camera.py