sys_comm.py 6.3 KB

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