-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathController_backend.py
269 lines (240 loc) · 6.5 KB
/
Controller_backend.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
from flask import Flask, request, send_file
from onvif import ONVIFCamera
from time import sleep
from wsdiscovery import WSDiscovery, QName, Scope
import urllib2
import time
import requests
import json
import threading
app = Flask(__name__)
def SetIPL(IP, Port, Login = 'admin', Password = 'Supervisor'):
class IPL(object):
def __init__(self, IP, Port, Login = 'admin', Password = 'Supervisor'):
self.IP = IP
self.Port = Port
self.Login = Login
self.Password = Password
self.Mycam = ONVIFCamera(self.IP, self.Port, self.Login, self.Password, '/usr/local/wsdl')
try:
self.Media = self.Mycam.create_media_service()
self.Token = self.Media.GetProfiles()[0]._token
except:
print ("Device doesn't support MEDIA profile")
self.Media = 'error'
self.Token = 'error'
try:
self.Ptz = self.Mycam.create_ptz_service()
except:
self.Ptz = 'error'
print ("Device doesn't support PTZ profile")
def test(self):
print (self.IP + ' ' + self.Port + ' ' + self.Token + ' ')
SMT = IPL (IP, Port, Login, Password)
return(SMT)
@app.route('/Discovery_original', methods=['POST'])
def Discovery_original():
global IPList
global IPL
del IPList[:]
wsd = WSDiscovery()
wsd.start()
ret = wsd.searchServices()
stroka = ''
for service in ret:
addrs = service.getXAddrs()[0]
x = addrs.find('/')
ip = addrs[x+1:]
x = addrs.find('/')
y = addrs.rfind(':')
if y <= x :
y = addrs[x+2:].find('/')
ip = addrs[x+2:x+2+y]
port = '80'
else:
ip = addrs[x+2:y]
x = addrs[y:].find('/')
port = addrs[y+1:y+x]
if ip[9] == '1':
print (ip + ' ' + port)
# try:
SMT = SetIPL(ip, port)
if SMT.Ptz == 'error':
print ('error')
else:
IPList.append(SMT)
print('ip=' + ip + ' port=' + port)
#except:
# print ('something happend')
IPList.sort(key=lambda x: x.IP)
for i, IPL in enumerate(IPList):
stroka = stroka + IPL.IP + ',' + IPL.Port + ',' + str(i) + ','
wsd.stop()
f = open ('discovery.txt', 'w')
f.write(stroka)
f.close()
return('ODiscovery_ready')
ManList = []
IPList = []
f = open('Man_add.txt', 'w')
f.close()
@app.route('/Discovery1', methods=['POST'])
def Discovery1():
f = open ('discovery.txt', 'r')
stroka = f.read()
f.close()
f = open ('Man_add.txt', 'r')
stroka1 = f.read()
f.close()
i = stroka + stroka1
print(i)
return (stroka + stroka1)
@app.route('/Discovery', methods=['POST'])
def Discovery():
global IPList
global ManList
stroka = ''
for i, IPL in enumerate(IPList):
stroka = stroka + IPL.IP + ',' + IPL.Port + ',' + str(i) + ','
for i, IPL in enumerate(ManList):
stroka = stroka + IPL.IP + ',' + IPL.Port + ',' + str(i + len(IPList)) + ','
return(stroka)
def Camera_select(number):
global IPList
global ManList
if number+1 > len(IPList):
i = number - len(IPList)-1
print(i)
SMT = ManList[i]
else:
i = number
print (i)
SMT = IPList[i]
return(SMT)
@app.route('/GetSnapshot', methods=['POST'])
def GetSnapshot():
i = int(request.args.get("number"))
SMT = Camera_select(i)
media = SMT.Media
token = SMT.Token
GetUri = media.GetSnapshotUri({'ProfileToken':token}).Uri
filename = "img.jpg"
resource = urllib2.urlopen(GetUri)
out = open("./img.jpg", 'wb')
out.write(resource.read())
out.close()
return send_file(filename, mimetype='image/jpg')
@app.route('/AddCamera', methods=['POST'])
def AddCamera():
global IPList
global ManList
global IPL
ip = request.args.get("ip")
port = request.args.get("port")
login = request.args.get("login")
Password = request.args.get("password")
SMT = SetIPL(ip, port, login, Password)
ManList.append(SMT)
f = open('Man_add.txt', 'a')
stroka = str(ip) + ',' + str(port) + ','
f.write(stroka)
f.close()
return("added")
@app.route('/TestDiscovery', methods=['POST'])
def TestDiscovery():
global IPList
for IPL in IPList:
IPL.test()
return('Everything fine')
@app.route('/GotoPreset', methods=['POST'])
def GotoPreset():
i = int(request.args.get("number"))
SMT = Camera_select(i)
Preset = SMT.Ptz.create_type('GotoPreset')
Preset.ProfileToken = SMT.Token
SMT.Ptz.GotoPreset({'ProfileToken':Preset.ProfileToken,'PresetToken':request.args.get("PresetNumber"),'Speed':1})
print(request.args.get("PresetNumber"))
return("GotoPreset" + request.args.get("PresetNumber"))
@app.route('/SetPreset', methods=['POST'])
def SetPreset():
i = int(request.args.get("number"))
SMT = Camera_select(i)
ptz = SMT.Ptz
token = SMT.Token
Preset = ptz.create_type('SetPreset')
Preset.ProfileToken = token
Preset.PresetName = "name"
Preset.PresetToken = request.args.get("PresetNumber")
ptz.SetPreset(Preset)
return("SetPreset " + Preset.PresetToken)
@app.route('/ZoomIn', methods=['POST'])
def ZoomIn():
i = int(request.args.get("number"))
SMT = Camera_select(i)
ptz = SMT.Ptz
token = SMT.Token
Cmove = ptz.create_type('ContinuousMove')
Cmove.ProfileToken = token
Cmove.Velocity.Zoom._x = 0.2
ptz.ContinuousMove(Cmove)
return("ZoomIn")
@app.route('/ZoomOut', methods=['POST'])
def ZoomOut():
global IPList
i = int(request.args.get("number"))
SMT = Camera_select(i)
ptz = SMT.Ptz
token = SMT.Token
Cmove = ptz.create_type('ContinuousMove')
Cmove.ProfileToken = token
Cmove.Velocity.Zoom._x = -0.2
ptz.ContinuousMove(Cmove)
return("ZoomOut")
@app.route('/ContinuousMove', methods=['POST'])
def CMove():
global IPList
i1 = request.args.get("number")
i = int(i1)
SMT = Camera_select(i)
ptz = SMT.Ptz
token = SMT.Token
Cmove = ptz.create_type('ContinuousMove')
#Cmove = IPList[i].Ptz.create_type('ContinuousMove')
Cmove.ProfileToken = token
Cmove.Velocity.PanTilt._x = request.args.get("x")
Cmove.Velocity.PanTilt._y = request.args.get("y")
ptz.ContinuousMove(Cmove)
print (SMT.IP + "moving")
return("moving")
@app.route('/MoveStop', methods=['POST'])
def MoveStop():
i = int(request.args.get("number"))
SMT = Camera_select(i)
ptz = SMT.Ptz
token = SMT.Token
ptz.Stop({'ProfileToken': token})
return("stopped")
@app.route('/GotoHome', methods=['POST'])
def GotoHome():
i = int(request.args.get("number"))
SMT = Camera_select(i)
ptz = SMT.Ptz
token = SMT.Token
Preset = ptz.create_type('GotoHomePosition')
Preset.ProfileToken = token
ptz.GotoHomePosition({'ProfileToken':Preset.ProfileToken})
return ('done')
def OD():
threading.Timer(120.0, OD).start()
Discovery_original()
time.sleep(0.01)
def StartServer():
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081)
thread = threading.Thread(target=StartServer, args=())
thread2 = threading.Thread(target=Discovery_original, args=())
thread.start()
thread2.start()
thread2.join()
OD()
thread.join()