forked from rnbguy/pysphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misphere.py
338 lines (284 loc) · 10.1 KB
/
misphere.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import json
import socket
import os
import threading
import time
import io
import select
import netifaces as ni
from msgids import *
class Session():
pass
class MiSphereConn:
ms_ip = '192.168.42.1'
ms_tcp_port = 7878
ms_uk_port = 8787
ms_fs_port = 50422
def init(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.socket.connect((self.ms_ip, self.ms_tcp_port))
self.socket_1 = socket.socket()
self.recv_handle_live = True
self.recv_thread = threading.Thread(target=self.recv_handler)
self.recv_thread.daemon = True
self.session = Session()
self.session.conf = {}
self.session.locks = {}
self.last_send = 0
def handle_data(self, func):
self.handle_data = func
def recv_handler(self):
while self.recv_handle_live:
r, _, _ = select.select([self.socket], [], [], 0.5)
if r:
resps = self.socket.recv(512).decode()
while True:
resp_dict, idx = json.JSONDecoder().raw_decode(resps)
self.resp_handler(resp_dict)
if idx >= len(resps):
break
else:
resps = resps[idx:]
def resp_handler(self, data):
if 'rval' in data:
print(data)
# assert(data['rval'] >= 0)
self.handle_data(data)
if data['msg_id'] == CONNECT:
self.session.token = data['param']
elif data['msg_id'] == DISCONNECT:
self.recv_handle_live = False
elif data['msg_id'] in [GET_CAMERA, GET_STORAGE, GET_DEVICE]:
cdata = {}
for e in data:
if e not in ['rval', 'msg_id']:
cdata[e] = data[e]
self.session.conf[data['msg_id']] = cdata
else:
print("UNKNOWN", data)
if data['msg_id'] in self.session.locks:
self.session.locks[data['msg_id']].release()
print('[UNLOCK]', data['msg_id'])
def connect_8787(self):
self.socket_1.connect((self.ms_ip, self.ms_uk_port))
def poweroff(self):
self.send(TURN_OFF)
def create_payload(self, msg_id, token, params=None):
payload = dict()
payload['msg_id'] = msg_id
if params is not None:
for k, v in params.items():
payload[k] = str(v) if k == 'param' else v
payload['token'] = token
return payload
def send(self, msg_id, params=None, token=None):
if msg_id not in self.session.locks:
self.session.locks[msg_id] = threading.Lock()
print("[LOCK] ", msg_id)
if self.session.locks[msg_id].locked():
return 0
else:
self.session.locks[msg_id].acquire()
if token is None:
token = self.session.token
payload = self.create_payload(msg_id, token, params)
data = json.dumps(payload).encode()
print(data)
timediff = time.time() - self.last_send
if timediff < 0.5:
time.sleep(0.5 - timediff)
self.last_send = time.time()
return self.socket.send(data)
def recv(self):
response = self.socket.recv(1024)
response = json.loads(response.decode())
return response
def send_recv(self, msg_id, params=None, token=None):
self.send(msg_id, params, token)
return self.recv()
def get_mode(self):
self.send(GET_CAMERA)
def connect(self):
self.recv_handle_live = True
self.recv_thread.start()
self.send(CONNECT, token=0)
while self.session.locks[CONNECT].locked():
time.sleep(.2)
print('token is {}'.format(self.session.token))
self.get_details()
def set_sub_camera(self, sub_cam, value=0):
if sub_cam == 'Timer':
self.send(SET_PHOTO_TIMER, {'param': value})
elif sub_cam == 'Intervalometer':
self.send(SET_PHOTO_INTERVALOMETER, {'param': value})
elif sub_cam == 'Bracketing':
self.send(SET_PHOTO_BRACKETING, {'param': int(2 * value)})
elif sub_cam == 'None':
self.send(SET_PHOTO_TIMER, {'param': 0})
self.send(SET_PHOTO_INTERVALOMETER, {'param': 0})
self.send(SET_PHOTO_BRACKETING, {'param': 0})
def set_sub_video(self, sub_vid, value=0):
if sub_vid == 'Short video':
self.send(SET_VIDEO_SHORT, {'param': value})
elif sub_vid == 'Timelapse':
self.send(SET_VIDEO_TIMELAPSE, {'param': value})
elif sub_vid == 'Slow motion':
self.send(SET_VIDEO_SLOWMO, {'param': value})
elif sub_vid == 'None':
self.send(SET_VIDEO_SHORT, {'param': 0})
self.send(SET_VIDEO_TIMELAPSE, {'param': 0})
self.send(SET_VIDEO_SLOWMO, {'param': 0})
def get_details(self):
self.get_camera_details()
self.get_storage_details()
self.get_device_details()
def is_set_to_photo(self):
return self.send(GET_CAMERA)['mode'] == 1
def is_set_to_video(self):
return self.send(GET_CAMERA)['mode'] == 0
def switch_to_video(self):
return self.send(SWITCH_MODE, {'param': 0})
def switch_to_photo(self):
return self.send(SWITCH_MODE, {'param': 1})
def switch_mode(self):
if self.is_set_to_photo():
return self.switch_to_video()
elif self.is_set_to_video():
return self.switch_to_photo()
def get_camera_details(self):
return self.send(GET_CAMERA)
def get_storage_details(self):
return self.send(GET_STORAGE)
def get_device_details(self):
return self.send(GET_DEVICE)
def set_photo_EV(self, value):
return self.send(PHOTO_EV, {'param': int(2 * value)})
def set_photo_ISO(self, value):
return self.send(PHOTO_ISO, {'param': value})
def set_photo_WB(self, value):
return self.send(PHOTO_WB, {'param': value})
def get_info(self, filepath=''):
self.send(FILE_INFO,
{
"param": "/tmp/SD0/DCIM/20171210/f0297728.jpg",
}
)
return self.recv()
def get_thumb(self, filepath=''):
self.send(FILE_PREVIEW,
{
"param": "/tmp/SD0/DCIM/20171210/f0297728.jpg",
"type": "thumb"
}
)
return self.recv()
def register_tcp(self, params={}):
for e in ni.interfaces():
if e.lower().startswith('w'):
ip = ni.ifaddresses(e)[ni.AF_INET][0]['addr']
if ip.startswith("192.168.42."):
break
params = {"param": ip, "type": "TCP"}
self.send(REGISTER_TCP, params)
return self.recv()
def click_picture(self, long_press=False):
self.send(PHOTO_LONGPRESS if long_press else PHOTO_PRESS)
def record_video(self, long_press=False):
self.send(VIDEO_LONGPRESS if long_press else VIDEO_REC_START)
def record_stop(self):
self.send(VIDEO_REC_STOP)
def change_camera_res(self, value):
l = {
'6912x3456': 0,
'3456x1728 (stitched)': 1,
'6912x3456 (stitched)': 2,
'6912x3456 with RAW': 3
}
self.send(PHOTO_RES, {'param': l[value]})
def change_camera_wb(self, value):
l = {
'Auto': 0,
'Outdoors': 1,
'Overcast': 2,
'Incandescent': 3,
'Fluorescent': 4
}
self.send(PHOTO_WB, {'param': l[value]})
def change_camera_iso(self, value):
l = {
'Auto': 0,
'50': 50,
'100': 100,
'200': 200,
'400': 400,
'800': 800,
'1600': 1600
}
self.send(PHOTO_ISO, {'param': l[value]})
def change_camera_shutter(self, value):
print("yo")
def l(val):
if val == 'Auto':
return 0
else:
if val.startswith('1/'):
val = val[2:]
val = int(val)
val += 1 << 15
else:
val = int(val)
return val
self.send(PHOTO_SHUTTERTIME, {'param': l(value)})
def change_camera_ev(self, value):
self.send(PHOTO_EV, {'param': int(2 * value)})
def change_video_res(self, value):
l = {
'3456x1728 | 30fps': 0,
'2304x1152 | 30fps': 1,
'2304x1152 | 60fps': 2,
'2048x512 | 120fps': 3,
'2048x512 | 120fps bottom (Bullet time)': 4,
'3456x1728 | 30fps HighBitrate': 5
}
self.send(VIDEO_RES, {'param': l[value]})
def change_video_wb(self, value):
l = {
'Auto': 0,
'Outdoors': 1,
'Overcast': 2,
'Incandescent': 3,
'Fluorescent': 4
}
self.send(VIDEO_WB, {'param': l[value]})
def change_video_ev(self, value):
self.send(VIDEO_EV, {'param': int(2 * value)})
def set_auto_off(self, value):
self.send(AUTO_TURN_OFF, {'param': value})
def set_buzzer_volume(self, value):
self.send(SET_BUZZER_VOLUME, {'param': value})
def set_led(self, value):
self.send(SET_LED, {'param': int(value)})
def disconnect(self):
self.send(DISCONNECT)
while self.session.locks[DISCONNECT].locked():
time.sleep(0.5)
self.recv_handle_live = False
self.recv_thread.join()
def test(self, l, h):
import msgids
known = set()
for e in vars(msgids):
if not e.startswith('__'):
known.add(msgids.__dict__[e])
for i in range(l, h + 1):
self.send(i)
def __del__(self):
self.socket.close()
self.socket_1.close()
if __name__ == '__main__':
cam = MiSphereConn()
cam.connect()
# cam.click_picture()
# cam.disconnect()