smartpi 0.1.11__py3-none-any.whl → 0.1.13__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.
smartpi/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  __all__ = ["base_driver"]
2
2
 
3
- __version__ = "0.1.11"
3
+ __version__ = "0.1.13"
4
4
 
smartpi/base_driver.py CHANGED
@@ -331,7 +331,7 @@ def update_request() -> Optional[bytes]:
331
331
  if time.time() - start_time > 3:
332
332
  print("读取超时")
333
333
  buffer.clear()
334
- return -1
334
+ return None
335
335
 
336
336
  """查询最大通讯长度"""
337
337
  def read_max_com_len() -> Optional[bytes]:
@@ -347,7 +347,7 @@ def read_max_com_len() -> Optional[bytes]:
347
347
  if time.time() - start_time > 3:
348
348
  print("读取超时")
349
349
  buffer.clear()
350
- return -1
350
+ return None
351
351
 
352
352
  """下载文件的信息"""
353
353
  #def download_massage() -> Optional[bytes]:
@@ -363,7 +363,7 @@ def read_max_com_len() -> Optional[bytes]:
363
363
  # if time.time() - start_time > 3:
364
364
  # print("读取超时")
365
365
  # buffer.clear()
366
- # return -1
366
+ # return None
367
367
  #
368
368
  #"""查询设备状态"""
369
369
  #def read_device_status() -> Optional[bytes]:
@@ -379,7 +379,7 @@ def read_max_com_len() -> Optional[bytes]:
379
379
  # if time.time() - start_time > 3:
380
380
  # print("读取超时")
381
381
  # buffer.clear()
382
- # return -1
382
+ # return None
383
383
  #
384
384
  #"""发送页校验码"""
385
385
  #def write_page_check() -> Optional[bytes]:
@@ -395,7 +395,7 @@ def read_max_com_len() -> Optional[bytes]:
395
395
  # if time.time() - start_time > 3:
396
396
  # print("读取超时")
397
397
  # buffer.clear()
398
- # return -1
398
+ # return None
399
399
  #
400
400
  #"""发送页数据"""
401
401
  #def write_page_check() -> Optional[bytes]:
@@ -411,7 +411,7 @@ def read_max_com_len() -> Optional[bytes]:
411
411
  # if time.time() - start_time > 3:
412
412
  # print("读取超时")
413
413
  # buffer.clear()
414
- # return -1
414
+ # return None
415
415
 
416
416
  ###############################################################################读取传感器信息
417
417
 
smartpi/gui.py CHANGED
@@ -1,28 +1,97 @@
1
1
  import socket
2
2
  import json
3
3
  import sys
4
+ import threading
5
+ import queue
6
+ import atexit
4
7
 
5
8
  # 连接对象(模块级单例)
6
9
  _connection = None
10
+ # 命令队列
11
+ _command_queue = queue.Queue(maxsize=1000)
12
+ # 后台发送线程
13
+ _sender_thread = None
14
+ # 线程停止标志
15
+ _stop_event = threading.Event()
7
16
 
8
17
  def init(host="127.0.0.1", port=65167):
9
18
  """初始化GUI连接"""
10
- global _connection
19
+ global _connection, _sender_thread, _stop_event
11
20
  if _connection is None:
12
- _connection = socket.create_connection((host, port))
21
+ try:
22
+ _connection = socket.create_connection((host, port))
23
+ _connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
24
+ except ConnectionRefusedError:
25
+ print(f"无法连接到GUI服务器 {host}:{port}", file=sys.stderr)
26
+ return
27
+
28
+ # 启动后台发送线程
29
+ _stop_event.clear()
30
+ _sender_thread = threading.Thread(target=_send_worker, daemon=True)
31
+ _sender_thread.start()
13
32
  _send({"type": "clear"})
14
33
 
15
- def _send(cmd):
16
- """发送命令到服务器"""
17
- if _connection is None and "pytest" not in sys.modules: # 允许测试环境不初始化
18
- raise ConnectionError("GUI not initialized. Call gui.init() first.")
34
+ def _send_worker():
35
+ """后台发送线程的工作函数"""
36
+ while not _stop_event.is_set():
37
+ try:
38
+ # 从队列中获取命令,最多等待0.01秒
39
+ cmd = _command_queue.get(timeout=0.01)
40
+
41
+ if _connection:
42
+ data = json.dumps(cmd) + "\n"
43
+ _connection.sendall(data.encode())
44
+
45
+ # 标记任务完成
46
+ _command_queue.task_done()
47
+ except queue.Empty:
48
+ continue
49
+ except (BrokenPipeError, ConnectionResetError):
50
+ # 连接断开,尝试重新连接
51
+ _reconnect()
52
+ except Exception as e:
53
+ print(f"发送错误: {e}", file=sys.stderr)
54
+ time.sleep(0.1)
55
+
56
+ def _reconnect():
57
+ """尝试重新连接服务器"""
58
+ global _connection
19
59
  if _connection:
20
- _connection.sendall((json.dumps(cmd) + "\n").encode())
60
+ try:
61
+ _connection.close()
62
+ except:
63
+ pass
64
+ _connection = None
65
+
66
+ # 尝试重新连接
67
+ try:
68
+ _connection = socket.create_connection(("127.0.0.1", 65167))
69
+ _connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
70
+ _send({"type": "clear"})
71
+ except Exception as e:
72
+ print(f"重新连接失败: {e}", file=sys.stderr)
73
+
74
+ def _send(cmd):
75
+ """发送命令到服务器(线程安全)"""
76
+ # 如果没有连接,直接返回
77
+ if _connection is None and "pytest" not in sys.modules:
78
+ return
79
+
80
+ # 将命令放入队列由后台线程发送
81
+ try:
82
+ _command_queue.put(cmd, block=False)
83
+ except queue.Full:
84
+ # 队列满时丢弃最旧的一条命令
85
+ try:
86
+ _command_queue.get_nowait()
87
+ except queue.Empty:
88
+ pass
89
+ _command_queue.put(cmd, block=False)
21
90
 
22
91
  def show_text(x, y, text, color="black", size=16):
23
92
  _send({"type": "text", "x": x, "y": y, "text": text, "color": color, "size": size})
24
93
 
25
- def print(text): # 使用print作为函数名,因为调用时使用gui.print()
94
+ def print(text):
26
95
  _send({"type": "print", "text": text})
27
96
 
28
97
  def println(text):
@@ -51,11 +120,18 @@ def clear():
51
120
 
52
121
  def close():
53
122
  """关闭GUI连接"""
54
- global _connection
123
+ global _connection, _sender_thread, _stop_event
124
+ # 设置停止事件
125
+ _stop_event.set()
126
+ if _sender_thread:
127
+ # 等待发送线程退出
128
+ _sender_thread.join(timeout=0.5)
55
129
  if _connection:
56
- _connection.close()
130
+ try:
131
+ _connection.close()
132
+ except:
133
+ pass
57
134
  _connection = None
58
135
 
59
136
  # 注册程序退出时自动关闭连接
60
- import atexit
61
137
  atexit.register(close)
smartpi/servo.py CHANGED
@@ -95,7 +95,7 @@ def set_speed(port:bytes,speed:int) -> Optional[bytes]:
95
95
  if response == None:
96
96
  return None
97
97
  else:
98
- return 0
98
+ return 0
99
99
 
100
100
  #���ֶ������ת�� port:����P�˿ڣ�code:����(0~65535)��speed:�ٶ�(-100~100)��
101
101
  def set_code_speed(port:bytes,code:int,speed:int) -> Optional[bytes]:
@@ -119,7 +119,7 @@ def set_code_speed(port:bytes,code:int,speed:int) -> Optional[bytes]:
119
119
  if response == None:
120
120
  return None
121
121
  else:
122
- return 0
122
+ return 0
123
123
 
124
124
  #���ֶ������ֵ���� port:����P�˿ڣ�
125
125
  def reset_encode(port:bytes) -> Optional[bytes]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: smartpi
3
- Version: 0.1.11
3
+ Version: 0.1.13
4
4
  Summary: A library use for H2-RCU
5
5
  Author: ZMROBO
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,18 +1,18 @@
1
- smartpi/__init__.py,sha256=CD_hKLtgmxyccH-ALP0bESCFPd6DvzMJQDqN9sKrtwg,55
2
- smartpi/base_driver.py,sha256=RVZQX1wbSVToJ_fsvxeVcc-v31LnND3YUPCybyUZa3s,18021
1
+ smartpi/__init__.py,sha256=hSlULYmtHrtLpplBFTQagFL4pXXCSoMgkNVFfklgtzc,55
2
+ smartpi/base_driver.py,sha256=FmnwZFqKE2HVUsDoqxXigdX1YlElcY07YkLnlvKY_x8,18033
3
3
  smartpi/color_sensor.py,sha256=sTqD3jApjmc6qHMrDyEy2UjaRt8vhJZNR88vzgUiLKs,496
4
4
  smartpi/cw2015.py,sha256=1lAF-pi_ye_ya1AZQS1sjbgsDf7MThO5IAskKsNGBzA,5695
5
- smartpi/gui.py,sha256=E98_soyWbEf_dwYhXZgMSXrgY5QuYoDTuFCPK63flIQ,2102
5
+ smartpi/gui.py,sha256=M0VXAxk9VmW8VXSbbcRfOtZUVigWIDn0pEEYZHMTeI0,4416
6
6
  smartpi/humidity.py,sha256=xtALQ_IlcwR2RCYvopCSmeNajB45kQU_ckk6FZ0rqko,497
7
7
  smartpi/led.py,sha256=n3_k1jGcQptfGXhezDLaYzH6UptgluP4Ze6qP_Y4WmU,536
8
8
  smartpi/light_sensor.py,sha256=kWmoXklYS1QG0eDz4Qn9FF4WueR3GARb3OwQSkXqUNA,569
9
9
  smartpi/motor.py,sha256=uvuAwt2j5LjdLaMfNisXqaGh1ro3fZDvHU8IXd2fn9Q,4527
10
10
  smartpi/move.py,sha256=3qzrJCGA-qbsLXBpklY2DErtw0jlzMELzozjhEvRzKs,6028
11
- smartpi/servo.py,sha256=0KfWM1PEBL2u15-lZnzav_qTWErf0ldeuX265c6oUPM,4623
11
+ smartpi/servo.py,sha256=B6X3yCoEz82qqpUIE5MSO0Eg9YZJ5zDzJEcRpioZpUo,4625
12
12
  smartpi/temperature.py,sha256=px2YeqgG63nPkyhJA1wDg3dwYx_oOCYuhMjtsVm_YO0,460
13
13
  smartpi/touch_sensor.py,sha256=F6IIQGewNRhC9U1RbHpVzuGYqb8H41lpeQ1Ejwsc_T8,438
14
14
  smartpi/ultrasonic.py,sha256=0meczFKXFLUt92kLxipeEc37vb5duvJjPs4kgtlpO8M,622
15
- smartpi-0.1.11.dist-info/METADATA,sha256=Vthva8Lex9bj8dr-QULiRbD0GuitBBR_0hRnCLlFRZQ,311
16
- smartpi-0.1.11.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
17
- smartpi-0.1.11.dist-info/top_level.txt,sha256=PoLhUCmWAiQUg5UeN2fS-Y1iQyBbF2rdUlizXtpHGRQ,8
18
- smartpi-0.1.11.dist-info/RECORD,,
15
+ smartpi-0.1.13.dist-info/METADATA,sha256=fyS1LxSRoLa2FGN9JpZ7QYayqJ6zx57Nyt_mTBP6JI0,311
16
+ smartpi-0.1.13.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
17
+ smartpi-0.1.13.dist-info/top_level.txt,sha256=PoLhUCmWAiQUg5UeN2fS-Y1iQyBbF2rdUlizXtpHGRQ,8
18
+ smartpi-0.1.13.dist-info/RECORD,,