AIO/codes/common/clibs.py

101 lines
3.4 KiB
Python

import os
import os.path
import threading
import sqlite3
from PySide6.QtCore import Signal, QThread
def traversal_files(dir_path):
if not os.path.exists(dir_path):
logger("ERROR", "clibs", f"数据文件夹{dir_path}不存在,请确认后重试......", color="red")
else:
dirs, files = [], []
for item in os.scandir(dir_path):
if item.is_dir():
dirs.append(item.path.replace("\\", "/"))
elif item.is_file():
files.append(item.path.replace("\\", "/"))
return dirs, files
def init_logdb():
global conn, cursor
conn = sqlite3.connect(":memory:", isolation_level=None, check_same_thread=False, cached_statements=2048)
cursor = conn.cursor()
cursor.execute("PRAGMA journal_mode=wal")
cursor.execute("PRAGMA wal_checkpoint=TRUNCATE")
cursor.execute("PRAGMA synchronous=normal")
cursor.execute("PRAGMA temp_store=memory")
cursor.execute("PRAGMA mmap_size=30000000000")
cursor.execute("PRAGMA cache_size=200000")
cursor.execute(
"""
create table if not exists logs(
id integer primary key autoincrement,
timestamp DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime')),
level text,
module text,
content text
)
"""
)
def db_lock(func):
def wrapper(*args, **kwargs):
try:
lock.acquire(True)
ret = func(*args, **kwargs)
finally:
lock.release()
return ret
return wrapper
class LoggerHandler(QThread):
signal = Signal(str, str)
def __init__(self, /):
super().__init__()
@db_lock
def logger(self, level, module, content, color="black", flag="both"):
global cursor, stop_flag
if "move.monitor" in content:
return
if level.upper() == "DEBUG":
flag = "cursor"
if flag == "signal":
self.signal.emit(content, color)
elif flag == "cursor":
cursor.execute(f"INSERT INTO logs (level, module, content) VALUES (?, ?, ?)", (level, module, content))
elif flag == "both":
self.signal.emit(content, color)
cursor.execute(f"INSERT INTO logs (level, module, content) VALUES (?, ?, ?)", (level, module, content))
if level.upper() == "ERROR":
stop_flag = False
raise Exception()
PREFIX = "resources/assets" # for pyinstaller
# PREFIX = "assets" # for local testing
log_path = f"{PREFIX}/logs"
lock = threading.Lock()
running = [0, 0, 0, 0, 0, 0, 0] # 制动数据/转矩数据/激光数据/精度数据/制动自动化/转矩自动化/耐久数据采集
stop_flag = False
functions = ["制动数据处理", "转矩数据处理", "激光数据处理", "精度数据处理", "制动自动化测试", "转矩自动化测试", "耐久数据采集"]
levels = ["DEBUG", "INFO", "WARNING", "ERROR"]
ip_addr, ssh_port, socket_port, xService_port, external_port, modbus_port, upgrade_port = "", 22, 5050, 6666, 8080, 502, 4567
username, password = "luoshi", "luoshi2019"
INTERVAL, RADIAN, MAX_FRAME_SIZE, MAX_LOG_NUMBER, CYCLE = 1, 57.3, 1024, 10, 300
c_md, c_hr, c_ec, c_pd, conn, cursor, search_records, logger, count = None, None, None, None, None, None, None, None, 0
status = {"mysql": 0, "hmi": 0, "md": 0, "ec": 0}
c_joint_vel, c_servo_trq, c_sensor_trq, c_estimate_trans_trq, c_safety_estop = 1, 2, 3, 4, 3 # 各个指标所在列
init_logdb()