sys_comm.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. from datetime import datetime, timezone, timedelta
  2. import os
  3. from enum import Enum
  4. import numpy
  5. import threading
  6. import re
  7. alarm_conf = {
  8. "retention_time": 60,
  9. "retention_keep_time": 30,
  10. "retention_alarm_time": 180,
  11. "toilet": {
  12. "retention_time": 60,
  13. "retention_keep_time": 30,
  14. "retention_alarm_time": 900
  15. }
  16. }
  17. # 系统配置
  18. g_sys_conf_mtx = threading.Lock()
  19. g_sys_conf = {
  20. "module_name": "LAS",
  21. "host_ip": "10.206.0.8",
  22. "platform": 0, # 平台,0:windows本地,1:云服务器
  23. "sp_id": "", # 服务程序id
  24. # 数据库相关参数
  25. "db_host": "localhost",
  26. "db_username": "root",
  27. "db_password": "Hfln@1024",
  28. # ssh
  29. "ssh_host": "119.45.12.173",
  30. "ssh_port": 22,
  31. "ssh_username": "root",
  32. "ssh_password": "Hfln@667788",
  33. }
  34. # 日志配置
  35. g_log_conf_mtx = threading.Lock()
  36. g_log_conf = {
  37. "module_name": "LAS",
  38. "log_path": "./log/",
  39. "log_lvl": 0,
  40. "max_log_size": 50 * 1024 * 1024,
  41. "max_log_files": 10,
  42. }
  43. # 日志文件锁
  44. g_log_file_mtx = threading.Lock()
  45. # 获取当前utc时间(毫秒)
  46. def get_utc_time_ms():
  47. now = datetime.now()
  48. utc_time = now.astimezone(timezone.utc)
  49. utc_timestamp_ms = int(utc_time.timestamp() * 1000)
  50. return utc_timestamp_ms
  51. # 获取当前utc时间(秒)
  52. def get_utc_time_s():
  53. now = datetime.now()
  54. utc_time = now.astimezone(timezone.utc)
  55. utc_timestamp_s = int(utc_time.timestamp())
  56. return utc_timestamp_s
  57. # 获取当前北京时间(毫秒)
  58. def get_bj_time_ms():
  59. now = datetime.now()
  60. return now.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
  61. # 获取当前北京时间(秒)
  62. def get_bj_time_s():
  63. now = datetime.now()
  64. return now.strftime("%Y-%m-%d %H:%M:%S")
  65. # utc时间转北京时间(秒)
  66. def utc_to_bj_s(utc_ts:int) -> str:
  67. utc_seconds = utc_ts
  68. utc_time = datetime.fromtimestamp(utc_seconds, tz=timezone.utc)
  69. bj_time = utc_time.astimezone(timezone(timedelta(hours=8)))
  70. return bj_time.strftime("%Y-%m-%d %H:%M:%S")
  71. # 时区
  72. UTC_TZ = timezone.utc
  73. BJ_TZ = timezone(timedelta(hours=8))
  74. # 毫秒 UTC → 北京时间
  75. def utc_to_bj_ms(utc_ms: int) -> str:
  76. dt_utc = datetime.fromtimestamp(utc_ms / 1000, tz=UTC_TZ)
  77. dt_bj = dt_utc.astimezone(BJ_TZ)
  78. return dt_bj.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] # 保留毫秒
  79. # 毫秒 北京时间 → UTC
  80. def bj_to_utc_ms(bj_str: str) -> int:
  81. """
  82. bj_str 格式: "YYYY-MM-DD HH:MM:SS.sss"
  83. """
  84. dt_bj = datetime.strptime(bj_str, "%Y-%m-%d %H:%M:%S.%f")
  85. dt_bj = dt_bj.replace(tzinfo=BJ_TZ)
  86. dt_utc = dt_bj.astimezone(UTC_TZ)
  87. return int(dt_utc.timestamp() * 1000)
  88. # 秒 UTC → 北京时间
  89. def utc_to_bj_s(utc_s: int) -> str:
  90. dt_utc = datetime.fromtimestamp(utc_s, tz=UTC_TZ)
  91. dt_bj = dt_utc.astimezone(BJ_TZ)
  92. return dt_bj.strftime("%Y-%m-%d %H:%M:%S")
  93. # 秒 北京时间 → UTC
  94. def bj_to_utc_s(bj_str: str) -> int:
  95. """
  96. bj_str 格式: "YYYY-MM-DD HH:MM:SS"
  97. """
  98. dt_bj = datetime.strptime(bj_str, "%Y-%m-%d %H:%M:%S")
  99. dt_bj = dt_bj.replace(tzinfo=BJ_TZ)
  100. dt_utc = dt_bj.astimezone(UTC_TZ)
  101. return int(dt_utc.timestamp())
  102. # 控制日志文件数量
  103. def manage_log_files(max_files, log_path:str):
  104. log_files = sorted(
  105. (f for f in os.listdir(log_path) if f.endswith(".log")),
  106. key=lambda x: os.path.getctime(os.path.join(log_path, x))
  107. )
  108. while len(log_files) > max_files:
  109. oldest_file = log_files.pop(0)
  110. os.remove(os.path.join(log_path, oldest_file))
  111. # 打印日志
  112. def LOG(text='', title=''):
  113. with g_log_conf_mtx:
  114. module_name = g_log_conf["module_name"]
  115. log_path = g_log_conf["log_path"]
  116. max_log_size = g_log_conf["max_log_size"]
  117. max_files = g_log_conf["max_log_files"]
  118. formatted_now = get_bj_time_ms()
  119. file = f"{module_name}.log"
  120. with g_log_file_mtx:
  121. log_file = os.path.join(log_path, file)
  122. # 检查日志文件大小并处理文件重命名
  123. if os.path.exists(log_file) and os.path.getsize(log_file) >= max_log_size:
  124. timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
  125. new_log_file = f"{log_path}{module_name}_{timestamp}.log"
  126. os.rename(log_file, new_log_file)
  127. # 控制日志文件数量
  128. manage_log_files(max_files, log_path)
  129. # 写入日志内容
  130. with open(log_file, 'a') as f:
  131. f.write('[%s][%s]:%s\n' % (formatted_now, title, text))
  132. # DEBUG日志, 等级0
  133. def LOGDBG(text=''):
  134. if g_log_conf["log_lvl"] <= 0:
  135. LOG(text=text, title="DEBUG")
  136. # INFO日志,等级1
  137. def LOGINFO(text=''):
  138. if g_log_conf["log_lvl"] <= 1:
  139. LOG(text=text, title="INFO")
  140. # WARN日志,等级2
  141. def LOGWARN(text=''):
  142. if g_log_conf["log_lvl"] <= 2:
  143. LOG(text=text, title="WARN")
  144. # ERROR日志,等级3
  145. def LOGERR(text=''):
  146. if g_log_conf["log_lvl"] <= 3:
  147. LOG(text=text, title="ERROR")
  148. # 模型
  149. class MODEL_E(Enum):
  150. MODEL_LIBO = 0 # 李博
  151. MODEL_ANDA = 1 # 安大
  152. e_model = MODEL_E.MODEL_LIBO
  153. # 姿态分类
  154. class POSE_CLASS_E(Enum):
  155. POSE_CLASS_3 = 3 # 3类
  156. POSE_CLASS_4 = 4 # 4类
  157. POSE_CLASS_5 = 5 # 5类
  158. e_pose_class = POSE_CLASS_E.POSE_CLASS_4
  159. # 姿态
  160. class POSE_E(Enum):
  161. POSE_INVALID = -1 # 无效值
  162. POSE_0 = 0 # 躺(跌倒)
  163. POSE_1 = 1 # 坐在椅子上
  164. POSE_2 = 2 # 坐在地上
  165. POSE_3 = 3 # 蹲
  166. POSE_4 = 4 # 站
  167. POSE_5 = 5 # 坐
  168. POSE_6 = 6 # 躺在沙发上
  169. POSE_7 = 7 # 躺在其他
  170. # 实时姿态
  171. realtime_pose:int = POSE_E.POSE_0.value
  172. pose_mutex = threading.Lock()
  173. def get_tracker_targets(point_cloud:list):
  174. target_point = numpy.mean(point_cloud, axis=0).tolist()
  175. tracker_targets = []
  176. tracker_targets.append(target_point)
  177. return tracker_targets
  178. # 获取目标target(多个点)
  179. def get_tracker_targets_mult(point_cloud:list):
  180. target_point = numpy.mean(point_cloud, axis=0).tolist()
  181. tracker_targets = []
  182. tracker_targets.append(target_point)
  183. return tracker_targets
  184. # 设备接入响应错误码
  185. class DEV_EC(int):
  186. succeed = 0 # 成功
  187. unauthorized = 401 # 未授权
  188. forbidden = 403 # 禁止访问,会话已过期
  189. conflict = 409 # 冲突,重复提交
  190. # 检查topic
  191. def check_topic(pattern:str, topic:str) -> bool:
  192. return bool(re.match(pattern, topic))