qlsdk2 0.5.1__py3-none-any.whl → 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,7 @@
1
-
2
- from multiprocessing import Queue
3
- from threading import Thread
4
- from time import sleep, time_ns
5
- from typing import Any, Dict, Literal
6
-
7
1
  from loguru import logger
8
2
  from qlsdk.persist import RscEDFHandler
9
- from qlsdk.rsc.interface import IDevice, IParser
3
+ from qlsdk.rsc.interface import IDevice
10
4
  from qlsdk.rsc.command import *
11
- from qlsdk.rsc.parser.base import TcpMessageParser
12
5
  from qlsdk.rsc.device.base import QLBaseDevice
13
6
 
14
7
  class C64RS(QLBaseDevice):
@@ -17,123 +10,6 @@ class C64RS(QLBaseDevice):
17
10
 
18
11
  def __init__(self, socket):
19
12
  super().__init__(socket)
20
- self.socket = socket
21
-
22
- self._id = None
23
-
24
- # 设备信息
25
- self.device_id = None
26
- self.device_name = None
27
- self._device_no = None
28
- self.software_version = None
29
- self.hardware_version = None
30
- self.connect_time = None
31
- self.current_time = None
32
- # mV
33
- self.voltage = None
34
- # %
35
- self.battery_remain = None
36
- # %
37
- self.battery_total = None
38
- # persist
39
- self._recording = False
40
- self._storage_path = None
41
- self._file_prefix = None
42
-
43
- # 可设置参数
44
- # 采集:采样量程、采样率、采样通道
45
- # 刺激:刺激电流、刺激频率、刺激时间、刺激通道
46
- # 采样量程(mV):188、375、563、750、1125、2250、4500
47
- self._sample_range:Literal[188, 375, 563, 750, 1125, 2250, 4500] = 188
48
- # 采样率(Hz):250、500、1000、2000、4000、8000、16000、32000
49
- self._sample_rate:Literal[250, 500, 1000, 2000, 4000, 8000, 16000, 32000] = 500
50
- self._acq_channels = None
51
- self._acq_param = {
52
- "sample_range": 188,
53
- "sample_rate": 500,
54
- "channels": [],
55
- }
56
-
57
- self._stim_param = {
58
- "stim_type": 0, # 刺激类型:0-所有通道参数相同, 1: 通道参数不同
59
- "channels": [],
60
- "param": [{
61
- "channel_id": 0, #通道号 从0开始 -- 必填
62
- "waveform": 3, #波形类型:0-直流,1-交流 2-方波 3-脉冲 -- 必填
63
- "current": 1, #电流强度(mA) -- 必填
64
- "duration": 30, #平稳阶段持续时间(s) -- 必填
65
- "ramp_up": 5, #上升时间(s) 默认0
66
- "ramp_down": 5, #下降时间(s) 默认0
67
- "frequency": 500, #频率(Hz) -- 非直流必填
68
- "phase_position": 0, #相位 -- 默认0
69
- "duration_delay": "0", #延迟启动时间(s) -- 默认0
70
- "pulse_width": 0, #脉冲宽度(us) -- 仅脉冲类型电流有效, 默认100us
71
- },
72
- {
73
- "channel_id": 1, #通道号 从0开始 -- 必填
74
- "waveform": 3, #波形类型:0-直流,1-交流 2-方波 3-脉冲 -- 必填
75
- "current": 1, #电流强度(mA) -- 必填
76
- "duration": 30, #平稳阶段持续时间(s) -- 必填
77
- "ramp_up": 5, #上升时间(s) 默认0
78
- "ramp_down": 5, #下降时间(s) 默认0
79
- "frequency": 500, #频率(Hz) -- 非直流必填
80
- "phase_position": 0, #相位 -- 默认0
81
- "duration_delay": "0", #延迟启动时间(s) -- 默认0
82
- "pulse_width": 0, #脉冲宽度(us) -- 仅脉冲类型电流有效, 默认100us
83
- }
84
- ]
85
- }
86
-
87
- self.stim_paradigm = None
88
-
89
- signal_info = {
90
- "param" : None,
91
- "start_time" : None,
92
- "finished_time" : None,
93
- "packet_total" : None,
94
- "last_packet_time" : None,
95
- "state" : 0
96
- }
97
- stim_info = {
98
-
99
- }
100
- Impedance_info = {
101
-
102
- }
103
- # 信号采集状态
104
- # 信号数据包总数(一个信号采集周期内)
105
- # 信号采集参数
106
- # 电刺激状态
107
- # 电刺激开始时间(最近一次)
108
- # 电刺激结束时间(最近一次)
109
- # 电刺激参数
110
- # 启动数据解析线程
111
- # 数据存储状态
112
- # 存储目录
113
-
114
- #
115
- self.__signal_consumer: Dict[str, Queue[Any]]={}
116
- self.__impedance_consumer: Dict[str, Queue[Any]]={}
117
-
118
- # EDF文件处理器
119
- self._edf_handler = None
120
- self.storage_enable = True
121
-
122
- # self._parser: IParser = TcpMessageParser(self)
123
- # self._parser.start()
124
-
125
- # # 启动数据接收线程
126
- # self._accept = Thread(target=self.accept)
127
- # self._accept.daemon = True
128
- # self._accept.start()
129
-
130
- @property
131
- def device_no(self):
132
- return self._device_no
133
-
134
- @device_no.setter
135
- def device_no(self, value: str):
136
- self._device_no = value
137
13
 
138
14
  @classmethod
139
15
  def from_parent(cls, parent:IDevice) -> IDevice:
@@ -149,211 +25,5 @@ class C64RS(QLBaseDevice):
149
25
  self._edf_handler.set_device_no(self.device_no)
150
26
  self._edf_handler.set_storage_path(self._storage_path)
151
27
  self._edf_handler.set_file_prefix(self._file_prefix if self._file_prefix else 'C64RS')
152
-
153
- @property
154
- def edf_handler(self):
155
- if not self.storage_enable:
156
- return None
157
-
158
- if self._edf_handler is None:
159
- self.init_edf_handler()
160
-
161
- return self._edf_handler
162
-
163
- @property
164
- def acq_channels(self):
165
- if self._acq_channels is None:
166
- self._acq_channels = [i for i in range(1, 63)]
167
- return self._acq_channels
168
- @property
169
- def sample_range(self):
170
- return self._sample_range if self._sample_range else 188
171
- @property
172
- def sample_rate(self):
173
- return self._sample_rate if self._sample_rate else 500
174
- @property
175
- def resolution(self):
176
- return 24
177
-
178
- @property
179
- def signal_consumers(self):
180
- return self.__signal_consumer
181
-
182
- @property
183
- def impedance_consumers(self):
184
- return self.__impedance_consumer
185
-
186
- # 设置记录文件路径
187
- def set_storage_path(self, path):
188
- self._storage_path = path
189
-
190
- # 设置记录文件名称前缀
191
- def set_file_prefix(self, prefix):
192
- self._file_prefix = prefix
193
-
194
- # 接收socket消息
195
- def accept(self):
196
- while True:
197
- # 缓冲去4M
198
- data = self.socket.recv(4096*1024)
199
- if not data:
200
- logger.warning(f"设备{self.device_name}连接结束")
201
- break
202
-
203
- self._parser.append(data)
204
-
205
-
206
- # socket发送数据
207
- def send(self, data):
208
- self.socket.sendall(data)
209
-
210
- # 设置刺激参数
211
- def set_stim_param(self, param):
212
- self.stim_paradigm = param
213
-
214
- # 设置采集参数
215
- def set_acq_param(self, channels, sample_rate = 500, sample_range = 188):
216
- self._acq_param["channels"] = channels
217
- self._acq_param["sample_rate"] = sample_rate
218
- self._acq_param["sample_range"] = sample_range
219
- self._acq_channels = channels
220
- self._sample_rate = sample_rate
221
- self._sample_range = sample_range
222
-
223
- # 通用配置-TODO
224
- def set_config(self, key:str, val: str):
225
- pass
226
-
227
- def start_impedance(self):
228
- logger.info("启动阻抗测量")
229
- msg = StartImpedanceCommand.build(self).pack()
230
- logger.debug(f"start_impedance message is {msg.hex()}")
231
- self.socket.sendall(msg)
232
-
233
- def stop_impedance(self):
234
- logger.info("停止阻抗测量")
235
- msg = StopImpedanceCommand.build(self).pack()
236
- logger.debug(f"stop_impedance message is {msg.hex()}")
237
- self.socket.sendall(msg)
238
-
239
- def start_stimulation(self):
240
- if self.stim_paradigm is None:
241
- logger.warning("刺激参数未设置,请先设置刺激参数")
242
- return
243
- logger.info("启动电刺激")
244
- msg = StartStimulationCommand.build(self).pack()
245
- logger.debug(f"start_stimulation message is {msg.hex()}")
246
- self.socket.sendall(msg)
247
- t = Thread(target=self._stop_stimulation_trigger, args=(self.stim_paradigm.duration,))
248
- t.start()
249
-
250
- def _stop_stimulation_trigger(self, duration):
251
- delay = duration
252
- while delay > 0:
253
- sleep(1)
254
- delay -= 1
255
- logger.info(f"_stop_stimulation_trigger duration: {duration}")
256
- if self._edf_handler:
257
- self._edf_handler.trigger("stimulation should be stopped")
258
- else:
259
- logger.warning("stop stim trigger fail. no edf writer alive")
260
-
261
- def stop_stimulation(self):
262
- logger.info("停止电刺激")
263
- msg = StopStimulationCommand.pack()
264
- logger.debug(f"stop_stimulation message is {msg.hex()}")
265
- self.socket.sendall(msg)
266
-
267
- # 启动采集
268
- def start_acquisition(self, recording = True):
269
- logger.info("启动信号采集")
270
- self._recording = recording
271
- # 设置数据采集参数
272
- param_bytes = SetAcquisitionParamCommand.build(self).pack()
273
- # 启动数据采集
274
- start_bytes = StartAcquisitionCommand.build(self).pack()
275
- msg = param_bytes + start_bytes
276
- logger.debug(f"start_acquisition message is {msg.hex()}")
277
- self.socket.sendall(msg)
278
-
279
- # 停止采集
280
- def stop_acquisition(self):
281
- logger.info("停止信号采集")
282
- msg = StopAcquisitionCommand.build(self).pack()
283
- logger.debug(f"stop_acquisition message is {msg.hex()}")
284
- self.socket.sendall(msg)
285
- if self._edf_handler:
286
- # 发送结束标识
287
- self.edf_handler.write(None)
288
-
289
- # 订阅实时数据
290
- def subscribe(self, topic:str=None, q : Queue=None, type : Literal["signal","impedance"]="signal"):
291
-
292
- # 数据队列
293
- if q is None:
294
- q = Queue(maxsize=1000)
295
-
296
- # 队列名称
297
- if topic is None:
298
- topic = f"{type}_{time_ns()}"
299
-
300
- # 订阅生理电信号数据
301
- if type == "signal":
302
- # topic唯一,用来区分不同的订阅队列(下同)
303
- if topic in list(self.__signal_consumer.keys()):
304
- logger.warning(f"exists {type} subscribe of {topic}")
305
- else:
306
- self.__signal_consumer[topic] = q
307
-
308
- # 订阅阻抗数据
309
- if type == "impedance":
310
- if topic in list(self.__signal_consumer.keys()):
311
- logger.warning(f"exists {type} subscribe of {topic}")
312
- else:
313
- self.__impedance_consumer[topic] = q
314
-
315
- return topic, q
316
-
317
- def trigger(self, desc):
318
- if self._edf_handler:
319
- self.edf_handler.trigger(desc)
320
- else:
321
- logger.warning("no edf handler, no place to recording trigger")
322
-
323
- def __str__(self):
324
- return f'''
325
- Device:
326
- Name: {self.device_name},
327
- Type: {hex(self.device_type) if self.device_type else None},
328
- ID: {self.device_id if self.device_id else None},
329
- Software: {self.software_version},
330
- Hardware: {self.hardware_version},
331
- Connect Time: {self.connect_time},
332
- Current Time: {self.current_time},
333
- Voltage: {str(self.voltage) + "mV" if self.voltage else None},
334
- Battery Remain: {str(self.battery_remain)+ "%" if self.battery_remain else None},
335
- Battery Total: {str(self.battery_total) + "%" if self.battery_total else None}
336
- '''
337
-
338
- def __repr__(self):
339
- return f'''
340
- Device:
341
- Name: {self.device_name},
342
- Type: {hex(self.device_type)},
343
- ID: {self.device_id},
344
- Software: {self.software_version},
345
- Hardware: {self.hardware_version},
346
- Connect Time: {self.connect_time},
347
- Current Time: {self.current_time},
348
- Voltage: {self.voltage}mV,
349
- Battery Remain: {self.battery_remain}%,
350
- Battery Total: {self.battery_total}%
351
- '''
352
-
353
- def __eq__(self, other):
354
- return self.device_name == other.device_name and self.device_type == other.device_type and self.device_id == other.device_id
355
-
356
- def __hash__(self):
357
- return hash((self.device_name, self.device_type, self.device_id))
358
-
28
+ logger.debug(f"EDF Handler initialized")
359
29