-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf2_slaves.py
335 lines (281 loc) · 11.4 KB
/
f2_slaves.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
import win32gui
import win32api
import win32con
import obsws_python as obs
from abc import ABC, abstractmethod
import socket
import pythoncom
import win32com
import win32com.client
from f2_logging import logprint
import os
from datetime import datetime
import subprocess
import time
pythoncom.CoInitialize()
def log_slave(*args, **kargs):
logprint("[Slave] " + ' '.join(str(a) for a in args))
class Slave(ABC):
def __init__(self):
self.ready = False
@abstractmethod
def check_ready(self)->bool:
return True
@abstractmethod
def start(self)->None:
pass
@abstractmethod
def stop(self)->None:
pass
def switch(self, switch:bool):
mode = (self.check_ready(), switch)
if mode == (True, True):
log_slave(self.__class__, "found and start")
self.start()
self.release()
elif mode == (True, False):
log_slave(self.__class__, "found and stop")
self.stop()
self.release()
elif mode == (False, True):
log_slave(self.__class__, "not openned")
elif mode == (False, False):
self.release()
else:
print('mode', mode)
raise OSError("Unknown mode")
def release(self):
pass
class Slave_OBS_tagger(Slave):
def __init__(self, ip='localhost', port=4455):
super().__init__()
self.ip = ip
self.port = port
self.hwnd = None
def check_ready(self) -> bool:
self.tbg = time.time()
if self.hwnd is None or not self.hwnd.base_client.ws.connected:
try:
cl = obs.ReqClient(host=self.ip, port=self.port)
resp = cl.send("GetInputList", {"inputKind": "text_gdiplus_v2"})
inputName_l = [d['inputName'] for d in resp.inputs]
sceneName = cl.get_current_program_scene().current_program_scene_name
print('sceneName', sceneName)
if 'TextFrame' not in inputName_l:
cl.send("CreateInput", {"sceneName":sceneName,
"inputName": "TextFrame",
"inputKind": "text_gdiplus_v2"})
resp = cl.send("GetSceneItemId", {"sceneName":sceneName,
"sourceName": "TextFrame"})
self.scene_item_id = resp.scene_item_id
self.sceneName = sceneName
self.hwnd = cl
except Exception:
self.hwnd = None
self.ready = self.hwnd is not None
else:
self.sceneName = self.hwnd.get_current_program_scene().current_program_scene_name
resp = self.hwnd.send("GetSceneItemId", {"sceneName": self.sceneName,
"sourceName": "TextFrame"})
self.scene_item_id = resp.scene_item_id
return self.ready
def start(self):
if self.hwnd:
try:
self.hwnd.send("SetInputSettings", {"inputName": "TextFrame", "inputSettings": {
'align': 'right', 'bk_opacity': 100,'color': '#FF0000',
'text': '000', 'bk_color': 4278190335,
'font': {'face': 'Courier New', 'flags': 0, 'size': 60, 'style': 'Regular'}}})
self.hwnd.send("SetSceneItemEnabled", {"sceneName":self.sceneName,
"sceneItemId": self.scene_item_id,
"sceneItemEnabled": True})
while True:
tpass = int((time.time() - self.tbg) * 1000)
if tpass > 999: break
text = f'{tpass:03d}'
self.hwnd.send("SetInputSettings", {"inputName": "TextFrame", "inputSettings": {
'text': text}})
time.sleep(0.01)
# self.hwnd.send("SetInputSettings", {"inputName": "TextFrame", "inputSettings": {
# 'bk_color': 4278233685, 'text': ' '}})
self.hwnd.send("SetSceneItemEnabled", {"sceneName":self.sceneName,
"sceneItemId": self.scene_item_id,
"sceneItemEnabled": False})
except:
self.hwnd = None
def stop(self):
if self.hwnd:
try:
# self.hwnd.send("SetInputSettings", {"inputName": "TextFrame", "inputSettings": {
# 'bk_color': 4278190080, 'text': ' '}})
self.hwnd.send("SetSceneItemEnabled", {"sceneName":self.sceneName,
"sceneItemId": self.scene_item_id,
"sceneItemEnabled": False})
except:
self.hwnd = None
class Slave_OBS(Slave):
def __init__(self, ip='localhost', port=4455):
super().__init__()
self.ip = ip
self.port = port
self.hwnd = None
def check_ready(self) -> bool:
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.settimeout(0.1)
if tcp_socket.connect_ex((self.ip, self.port)) == 0: # connection successful
tcp_socket.close()
else:
self.hwnd, self.ready = None, False
return self.ready
try:
cl = obs.ReqClient(host=self.ip, port=self.port)
except Exception:
cl = None
self.hwnd = cl
self.ready = cl is not None
return self.ready
def start(self):
if self.hwnd:
try:
self.hwnd.start_record()
except:
pass
def stop(self):
if self.hwnd:
try:
self.hwnd.stop_record()
except:
pass
def release(self):
if self.hwnd:
self.hwnd.base_client.ws.close()
self.hwnd=None
class Slave_Miniscope(Slave):
def __init__(self, ip='localhost', port=20172, label='Miniscope'):
super().__init__()
self.hwnd = None
self.ip = ip
self.port = port
self.send_start_byte = "start_record".encode("utf-8")
self.send_stop_byte = "stop_record".encode("utf-8")
self.device_name = label
def check_ready(self) -> bool:
self.release()
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.settimeout(0.1)
if tcp_socket.connect_ex((self.ip, self.port)) == 0: # connection successful
log_slave(f'Found {self.device_name}.')
else:
tcp_socket = None
self.hwnd = tcp_socket
self.ready = tcp_socket is not None
return self.ready
def start(self):
send_data_byte = "start_record".encode("utf-8")
if self.hwnd:
self.hwnd.send(send_data_byte)
def stop(self):
send_data_byte = "stop_record".encode("utf-8")
if self.hwnd:
self.hwnd.send(send_data_byte)
def __read_response(self):
from_server_msg = self.hwnd.recv(1024)
print(from_server_msg.decode("utf-8"))
def release(self):
if self.hwnd:
self.hwnd.close()
del self.hwnd
self.hwnd = None
class Slave_EPStudio(Slave_Miniscope):
def __init__(self, ip='10.50.36.170', port=20172, label='EPStudio'):
super().__init__(ip=ip, port=port, label=label)
class Slave_USVOld(Slave):
def __init__(self):
super().__init__()
self.titlename = "Avisoft-RECORDER USGH (RECORDER.INI)"
self.hwnd = None
self.shell = win32com.client.Dispatch("WScript.Shell")
def check_ready(self) -> bool:
hwnd = win32gui.FindWindow(None, self.titlename)
if hwnd==0:
self.hwnd = None
else:
self.hwnd = hwnd
log_slave('Found USV.')
self.ready = self.hwnd is not None
return self.ready
def start(self):
self.shell.SendKeys('%')
log_slave('USV_Start', self.hwnd)
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(self.hwnd)
win32api.keybd_event(win32con.VK_CONTROL,0,0,0)
win32api.keybd_event(win32con.VK_F3,0,0,0) # F3
win32api.keybd_event(win32con.VK_F3,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
win32api.keybd_event(win32con.VK_CONTROL,0,win32con.KEYEVENTF_KEYUP,0)
self.shell.SendKeys('%')
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(self.hwnd)
win32api.keybd_event(win32con.VK_CONTROL,0,0,0)
win32api.keybd_event(win32con.VK_F3,0,0,0) # F3
win32api.keybd_event(win32con.VK_F3,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
win32api.keybd_event(win32con.VK_CONTROL,0,win32con.KEYEVENTF_KEYUP,0)
def stop(self):
self.shell.SendKeys('%')
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(self.hwnd)
win32api.keybd_event(win32con.VK_CONTROL,0,0,0)
win32api.keybd_event(win32con.VK_F5,0,0,0) # F5
win32api.keybd_event(win32con.VK_F5,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
win32api.keybd_event(win32con.VK_CONTROL,0,win32con.KEYEVENTF_KEYUP,0)
self.shell.SendKeys('%')
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(self.hwnd)
win32api.keybd_event(win32con.VK_CONTROL,0,0,0)
win32api.keybd_event(win32con.VK_F5,0,0,0) # F5
win32api.keybd_event(win32con.VK_F5,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
win32api.keybd_event(win32con.VK_CONTROL,0,win32con.KEYEVENTF_KEYUP,0)
log_slave('USV Stopped', self.hwnd)
class Slave_USV(Slave_USVOld):
def start(self):
cmdstart = 'CMCDDE.EXE RECORDER main "start"'
os.popen(cmdstart) #非阻塞式
self.shell.SendKeys('%')
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(self.hwnd)
log_slave('USV_Start')
def stop(self):
cmdstop = 'CMCDDE.EXE RECORDER main "stop"'
os.popen(cmdstop) #非阻塞式
self.shell.SendKeys('%')
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(self.hwnd)
log_slave('USV Stopped')
class Slave_RTSP_CAM(Slave):
def __init__(self, rtsp_list):
super().__init__()
self.rtsp_list = rtsp_list
self.outdir = 'E:/rtsp_cam'
if rtsp_list:
os.makedirs(self.outdir, exist_ok=True)
self.process_l = []
def check_ready(self) -> bool:
self.release()
return True
def start(self):
now = datetime.now()
formatted_time = now.strftime('%Y-%m-%d_%H:%M:%S')
cmd_l = []
for i, rtsp in enumerate(self.rtsp_list):
filename = os.path.join(self.outdir, f'{formatted_time}_cam{i+1}.mp4')
cmd_l.append(f'ffmpeg -i {rtsp} -c copy {filename}')
self.process_l = []
for cmd in cmd_l:
self.process_l.append(subprocess.Popen(cmd, shell=True))
def stop(self):
for p in self.process_l:
p.terminate()
self.process_l = []
def release(self):
if len(self.process_l):
self.stop()