forked from irungentoo/Xiaomi_Yi_4k_Camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
105 lines (84 loc) · 2.69 KB
/
controller.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
import socket
import time
import json
class Camera:
CAMERA_PORT = 7878
def send_packet(self, packet):
#print(packet)
self.sock.send(str.encode(packet))
def send_connect(self):
packet = '{"msg_id":257,"param":0,"token":0,"heartbeat":1}'
self.send_packet(packet)
def send_get_info(self):
packet = '{"msg_id":3,"token":' + str(self.token) + '}'
self.send_packet(packet)
def send_start_recording(self):
packet = '{"msg_id":513,"token":' + str(self.token) + '}'
self.send_packet(packet)
def send_stop_recording(self):
packet = '{"msg_id":514,"token":' + str(self.token) + '}'
self.send_packet(packet)
def read_connect(self):
data = self.sock.recv(1024)
return json.loads(data)["param"]
def __init__(self, ip):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((ip, self.CAMERA_PORT))
self.send_connect()
token = self.read_connect()
self.token = token
self.sock.setblocking(0)
self.last_sent = time.time()
def do(self):
if (time.time() - self.last_sent > 15):
self.send_get_info()
self.last_sent = time.time()
try:
data = self.sock.recv(8192)
#print(data)
#json_data = json.loads(data)
#print(json_data)
except:
pass
class Cameras:
cameras = []
def append(self, camera):
self.cameras.append(camera)
def do(self):
for a in self.cameras:
a.do()
def start_recording(self):
for a in self.cameras:
a.send_start_recording()
def stop_recording(self):
for a in self.cameras:
a.send_stop_recording()
if __name__ == "__main__":
import kbhit
import sys
if (len(sys.argv) == 1):
print(sys.argv[0] + " <ip camera 1> <ip camera 2> ... <ip camera n>")
exit(1)
cameras = Cameras()
sys.argv.pop(0)
for a in sys.argv:
cameras.append(Camera(a))
kb = kbhit.KBHit()
print("The program initialized successfully")
print("Use space to send a start recording command and then space again to stop")
recording = False
while True:
cameras.do()
if kb.kbhit():
ch = kb.getch()
if ch == chr(0x20):
if (recording):
print("stop recording")
cameras.stop_recording()
recording = False
else:
print("start recording")
cameras.start_recording()
recording = True
else:
time.sleep(0.1)