96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
import os
|
|
import os.path
|
|
import sqlite3
|
|
import threading
|
|
|
|
|
|
def traversal_files(_path, _w2t):
|
|
# 功能:以列表的形式分别返回指定路径下的文件和文件夹,不包含子目录
|
|
# 参数:路径
|
|
# 返回值:路径下的文件夹列表 路径下的文件列表
|
|
if not os.path.exists(_path):
|
|
_w2t(f"数据文件夹{_path}不存在,请确认后重试......\n", "red", "PathNotExistError")
|
|
else:
|
|
dirs, files = [], []
|
|
for item in os.scandir(_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(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,
|
|
time DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime')),
|
|
level text,
|
|
module text,
|
|
content text
|
|
)
|
|
"""
|
|
)
|
|
return conn, cursor
|
|
|
|
|
|
def db_lock(func):
|
|
def wrapper(*args, **kwargs):
|
|
try:
|
|
lock.acquire(True)
|
|
ret = func(*args, **kwargs)
|
|
finally:
|
|
lock.release()
|
|
return ret
|
|
|
|
return wrapper
|
|
|
|
|
|
@db_lock
|
|
def insert_logdb(_level, _module, _content):
|
|
if db_state == "readwrite":
|
|
global conn, cursor, lock
|
|
if "move.monitor" in _content:
|
|
return
|
|
data = [_level, _module, _content]
|
|
cursor.execute("insert into logs (level, module, content) values (?, ?, ?)", data)
|
|
|
|
|
|
PREFIX = "assets" # for pyinstaller packaging
|
|
# PREFIX = "../assets" # for source code testing and debug
|
|
log_path = f"{PREFIX}/logs"
|
|
levels = ["DEBUG", "INFO", "WARNING", "ERROR"]
|
|
db_state = "readwrite"
|
|
data_dp, data_at, data_dd = {}, {}, {}
|
|
conn, cursor, w2t, tl_prg, f_records, stop, running = None, None, None, None, None, True, 0
|
|
|
|
ip_addr = "192.168.0.160"
|
|
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, count = 0.5, 57.3, 1024, 0
|
|
c_md, c_hr, c_ec, c_pd = None, None, None, None
|
|
lock = threading.Lock()
|
|
|
|
conn, cursor = init_logdb(conn, cursor)
|
|
|
|
# ============== ↓↓↓DEBUG CODE↓↓↓ ==============
|
|
# for i in range(100):
|
|
# insert_logdb("DEBUG", "clibs", 'running')
|
|
# insert_logdb("INFO", "clibs", 'running')
|
|
# insert_logdb("WARNING", "clibs", 'running')
|
|
# insert_logdb("ERROR", "clibs", 'running')
|
|
# with open(f"{log_path}/response.txt", mode="w", encoding="utf-8") as f_res:
|
|
# f_res.write("")
|
|
# with open(f"{log_path}/logs.txt", mode="w", encoding="utf-8") as f_res:
|
|
# f_res.write("")
|