-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
147 lines (112 loc) · 4.09 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
import requests
import json
from requests.auth import HTTPDigestAuth
from flask_cors import CORS
from flask import jsonify, Flask, request, render_template, send_from_directory
import os
import sys
import threading
import webview as gui
# from gevent.pywsgi import WSGIServer
url = 'http://192.168.1.108/cgi-bin/videoStatServer.cgi?action=getSummary&channel=1'
exampleData = """summary.Channel=0
summary.EnteredSubtotal.Hour=22
summary.EnteredSubtotal.Today=12
summary.EnteredSubtotal.Total=1
summary.EnteredSubtotal.TotalInTimeSection=33
summary.ExitedSubtotal.Hour=11
summary.ExitedSubtotal.Today=12
summary.ExitedSubtotal.Total=10
summary.ExitedSubtotal.TotalInTimeSection=22
summary.InsideSubtotal.Total=12
summary.RuleName=NumberStat
summary.UTC=1615580510"""
# First Api call (get people count)
url = 'http://192.168.1.108/cgi-bin/videoStatServer.cgi?action=getSummary&channel=1'
# Second Api call (reset camera people count to 0)
clearUrl = 'http://192.168.1.108/cgi-bin/videoStatServer.cgi?action=clearSectionStat&'
# Variable to fix the camera bug
# shambles = 0
# Define function to import external files when using PyInstaller.
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
SettingsFile = resource_path('settings.json')
def write_json(data, filename=SettingsFile):
with open(filename, 'w') as f:
json.dump(data, f)
app = Flask(__name__, template_folder=resource_path('out/'), static_folder=resource_path('out/'), static_url_path='')
CORS(app)
# Web View Routes
@app.route('/')
def index():
return render_template('index.html')
@app.route('/settings')
def settings():
return render_template('settings.html')
@app.route('/api')
def getDahua():
def getData():
MaxPeople = 1
# Dahua = requests.get(url, auth=HTTPDigestAuth('admin', 'Lupata1488*'))
with open(SettingsFile, 'r') as openfile:
Item = json.load(openfile)
MaxPeople = Item['MaxPeople']
# Turns all values to a list of lines
# DahuaValues = Dahua.text.splitlines()
DahuaValues = exampleData.splitlines()
# DahuaValues = exampleData.splitlines()
# Total of people entered today:
PeopleInString = DahuaValues[4]
PeopleIn = int(PeopleInString.split("=", 1)[1])
# Total number of people exited today:
PeopleOutString = DahuaValues[8]
PeopleOut = int(PeopleOutString.split("=", 1)[1])
# Total number of people inside right now + bug fix
PeopleCount = PeopleIn - PeopleOut
# Number of people still allowed to enter
AllowedToEnter = MaxPeople - PeopleCount
# Return peopleCount, people in, people out
res = {"PeopleIn": PeopleIn, "PeopleOut": PeopleOut,
"PeopleCount": PeopleCount, "MaxPeople": MaxPeople}
return res
data = getData()
if data["PeopleCount"] < 0:
requests.get(clearUrl, auth=HTTPDigestAuth('admin', 'Lupata1488*'))
data = jsonify(getData())
data.status_code = 200
return data
data = jsonify(getData())
data.status_code = 200
return data
@app.route('/firstboot')
def fixDahua():
requests.get(clearUrl, auth=HTTPDigestAuth('admin', 'Lupata1488*'))
res = jsonify('success')
res.status_code = 200
return res
@app.route('/changeVals', methods=['POST'])
def changeVals():
num = request.json['maxPeople']
write_json({"MaxPeople": num})
return jsonify('success'), 200
def runApp():
app.run(host="0.0.0.0", port=8000)
def runEEl():
gui.create_window('testapp','out/index.html')
gui.start(gui='gtk')
if __name__ == '__main__':
# init_gui(app, 5000)
# gui.create_window('testapp',app)
# gui.start(gui='gtk')
t = threading.Thread(target=runApp)
t.daemon = True
t.start()
gui.create_window("Re:count","http://localhost:8000/")
gui.start()
sys.exit()