xCore通信暂时无问题了
This commit is contained in:
@ -2,7 +2,7 @@ import os
|
||||
import os.path
|
||||
import sqlite3
|
||||
import threading
|
||||
import json
|
||||
|
||||
|
||||
def traversal_files(_path, _w2t):
|
||||
# 功能:以列表的形式分别返回指定路径下的文件和文件夹,不包含子目录
|
||||
@ -21,17 +21,15 @@ def traversal_files(_path, _w2t):
|
||||
return dirs, files
|
||||
|
||||
|
||||
def init_logdb(conn, cursor):
|
||||
conn = sqlite3.connect(":memory:", isolation_level=None, check_same_thread=False, cached_statements=4096)
|
||||
# connect = sqlite3.connect("log.db", isolation_level=None, check_same_thread=False, cached_statements=256)
|
||||
# time text default (datetime('now', 'localtime')),
|
||||
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(
|
||||
def init_logdb(_conn, _cursor):
|
||||
_conn = sqlite3.connect(":memory:", isolation_level=None, check_same_thread=False, cached_statements=4096)
|
||||
_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(
|
||||
"""
|
||||
create table if not exists logs(
|
||||
id integer primary key autoincrement,
|
||||
@ -42,7 +40,7 @@ def init_logdb(conn, cursor):
|
||||
)
|
||||
"""
|
||||
)
|
||||
return conn, cursor
|
||||
return _conn, _cursor
|
||||
|
||||
|
||||
def db_lock(func):
|
||||
@ -53,6 +51,7 @@ def db_lock(func):
|
||||
finally:
|
||||
lock.release()
|
||||
return ret
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
@ -60,11 +59,12 @@ def db_lock(func):
|
||||
def insert_logdb(_level, _module, _content):
|
||||
if db_state == "readwrite":
|
||||
global conn, cursor, lock
|
||||
if "move.monitor" in _content:
|
||||
return
|
||||
data = [_level, _module, repr(_content)]
|
||||
cursor.execute("insert into logs (level, module, content) values (?, ?, ?)", data)
|
||||
|
||||
|
||||
|
||||
def insert_logdb_multi(data):
|
||||
if db_state == "readwrite":
|
||||
global conn, cursor, lock
|
||||
@ -89,9 +89,10 @@ class GetThreadResult(threading.Thread):
|
||||
|
||||
def get_result(self):
|
||||
threading.Thread.join(self) # 等待线程执行完毕
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
return self.result
|
||||
except Exception as Err:
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
@ -131,17 +132,13 @@ lock = threading.Lock()
|
||||
|
||||
conn, cursor = init_logdb(conn, cursor)
|
||||
|
||||
# ============== ↓↓↓DEBUG CODE↓↓↓ ==============
|
||||
# for i in range(100):
|
||||
# insert_logdb("DEBUG", "clibs", 'this is a DEBUG log -0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\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.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n\t\t\t\t-0.0,\n')
|
||||
#
|
||||
# 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("")
|
||||
# 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("")
|
||||
|
@ -343,9 +343,10 @@ class HmiRequest(object):
|
||||
try:
|
||||
self.__close_hmi = True
|
||||
self.__is_connected = False
|
||||
time.sleep(clibs.interval)
|
||||
time.sleep(clibs.interval*2)
|
||||
self.__c.close()
|
||||
self.__c_xs.close()
|
||||
time.sleep(clibs.interval*2)
|
||||
except:
|
||||
...
|
||||
|
||||
@ -388,8 +389,8 @@ class HmiRequest(object):
|
||||
data = conn.recv(clibs.MAX_FRAME_SIZE)
|
||||
if data:
|
||||
# print(f"data = {data}")
|
||||
with open(f"{clibs.log_path}/logs.txt", mode="a", encoding="utf-8") as f_logs:
|
||||
f_logs.write(str(data) + "\n")
|
||||
# with open(f"{clibs.log_path}/logs.txt", mode="a", encoding="utf-8") as f_logs:
|
||||
# f_logs.write(str(data) + "\n")
|
||||
self.__get_response(data)
|
||||
else:
|
||||
sel.unregister(conn)
|
||||
@ -407,16 +408,11 @@ class HmiRequest(object):
|
||||
clibs.insert_logdb("ERROR", "openapi", f"hr-unpackage: 报错信息 {Err}")
|
||||
|
||||
def __get_headers(self, index, data):
|
||||
print(f"index = {index}")
|
||||
if index + 8 < len(data):
|
||||
frm_value = int.from_bytes(data[index:index + 2], byteorder="big")
|
||||
pkg_value = int.from_bytes(data[index + 2:index + 6], byteorder="big")
|
||||
protocol = int.from_bytes(data[index + 6:index + 7], byteorder="big")
|
||||
reserved = int.from_bytes(data[index + 7:index + 8], byteorder="big")
|
||||
print(f"reserved = {reserved}")
|
||||
print(f"protocol = {protocol}")
|
||||
print(f"frm_value = {frm_value}")
|
||||
print(f"pkg_value = {pkg_value}")
|
||||
if reserved == 0 and protocol == 2:
|
||||
return index + 8, frm_value, pkg_value
|
||||
else:
|
||||
@ -426,37 +422,32 @@ class HmiRequest(object):
|
||||
else:
|
||||
self.__half_pkg = data[index:]
|
||||
self.__half_pkg_flag = True
|
||||
index += clibs.MAX_FRAME_SIZE
|
||||
return index, 0, 0
|
||||
return -1, 0, 0
|
||||
|
||||
def __get_response(self, data):
|
||||
if self.__half_pkg_flag:
|
||||
print(f"self.__half_pkg = {self.__half_pkg}")
|
||||
len_one = len(self.__half_pkg)
|
||||
len_another = 8 - len_one
|
||||
headers = self.__half_pkg + data[:len_another]
|
||||
self.__index = len_another
|
||||
frm_value = int.from_bytes(headers[0:2], byteorder="big")
|
||||
pkg_value = int.from_bytes(headers[2:6], byteorder="big")
|
||||
print(f"headers = {headers}")
|
||||
frm_value, pkg_value, self.__index = 0, 0, 0
|
||||
|
||||
while self.__index < len(data):
|
||||
print(f"top valid data = {self.__valid_data_length}")
|
||||
if self.__valid_data_length < 0:
|
||||
print(f"invalid data: {data}")
|
||||
raise Exception()
|
||||
if self.__is_first_frame:
|
||||
if self.__half_pkg_flag:
|
||||
len_one = len(self.__half_pkg)
|
||||
len_another = 8 - len_one
|
||||
headers = self.__half_pkg + data[:len_another]
|
||||
self.__index = len_another
|
||||
frm_value = int.from_bytes(headers[0:2], byteorder="big")
|
||||
pkg_value = int.from_bytes(headers[2:6], byteorder="big")
|
||||
self.__half_pkg_flag = False
|
||||
else:
|
||||
self.__index, frm_value, pkg_value = self.__get_headers(self.__index, data)
|
||||
if self.__half_pkg_flag:
|
||||
break
|
||||
|
||||
if frm_value - pkg_value == 6:
|
||||
if len(data[self.__index:]) >= pkg_value:
|
||||
self.__response = data[self.__index:self.__index + pkg_value]
|
||||
self.__index += pkg_value
|
||||
with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
# with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
# f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
clibs.insert_logdb("DEBUG", "openapi", self.__response.decode())
|
||||
self.__response = b""
|
||||
self.__leftovers = 0
|
||||
@ -501,11 +492,13 @@ class HmiRequest(object):
|
||||
self.__valid_data_length -= self.__leftovers
|
||||
self.__leftovers = 0
|
||||
if self.__valid_data_length == 0:
|
||||
with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
# with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
# f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
clibs.insert_logdb("DEBUG", "openapi", self.__response.decode())
|
||||
self.__response = b""
|
||||
self.__is_first_frame = True
|
||||
continue # 此时应该重新 get_headers
|
||||
|
||||
if len(data[self.__index:]) == 2:
|
||||
self.__half_frm = data[self.__index:]
|
||||
self.__index += clibs.MAX_FRAME_SIZE
|
||||
@ -524,8 +517,8 @@ class HmiRequest(object):
|
||||
self.__response += data[:self.__leftovers]
|
||||
self.__index = self.__leftovers
|
||||
self.__leftovers = 0
|
||||
with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
# with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
# f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
clibs.insert_logdb("DEBUG", "openapi", self.__response.decode())
|
||||
self.__response = b""
|
||||
self.__is_first_frame = True
|
||||
@ -537,9 +530,17 @@ class HmiRequest(object):
|
||||
# 4. len(data[self.__index:]) = 0
|
||||
if self.__half_frm_flag != -1:
|
||||
if self.__half_frm_flag == 2:
|
||||
self.__index = int.from_bytes(self.__half_frm)
|
||||
self.__response += data[:self.__index]
|
||||
self.__valid_data_length -= len(data[:self.__index])
|
||||
frm_value = int.from_bytes(self.__half_frm)
|
||||
if len(data) >= frm_value:
|
||||
self.__response += data[:frm_value]
|
||||
self.__leftovers = 0
|
||||
self.__valid_data_length -= len(data[:frm_value])
|
||||
self.__index = frm_value
|
||||
elif len(data) < frm_value:
|
||||
self.__response += data
|
||||
self.__leftovers = frm_value - len(data)
|
||||
self.__valid_data_length -= len(data)
|
||||
self.__index += clibs.MAX_FRAME_SIZE
|
||||
self.__half_frm_flag = -1
|
||||
elif self.__half_frm_flag == 1:
|
||||
frm_value = int.from_bytes(self.__half_frm + data[0:1])
|
||||
@ -569,11 +570,13 @@ class HmiRequest(object):
|
||||
self.__half_frm_flag = -1
|
||||
|
||||
if self.__valid_data_length == 0:
|
||||
with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
# with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
# f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
clibs.insert_logdb("DEBUG", "openapi", self.__response.decode())
|
||||
self.__response = b""
|
||||
self.__is_first_frame = True
|
||||
continue
|
||||
|
||||
if len(data[self.__index:]) == 2:
|
||||
self.__half_frm = data[self.__index:]
|
||||
self.__index += clibs.MAX_FRAME_SIZE
|
||||
@ -597,11 +600,13 @@ class HmiRequest(object):
|
||||
self.__index += frm_value
|
||||
self.__valid_data_length -= frm_value
|
||||
if self.__valid_data_length == 0:
|
||||
with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
# with open(f"{clibs.log_path}/response.txt", mode="a", encoding="utf-8") as f_res:
|
||||
# f_res.write(f"{json.loads(self.__response.decode())}" + "\n")
|
||||
clibs.insert_logdb("DEBUG", "openapi", self.__response.decode())
|
||||
self.__response = b""
|
||||
self.__is_first_frame = True
|
||||
continue
|
||||
|
||||
if len(data[self.__index:]) == 2:
|
||||
self.__index += clibs.MAX_FRAME_SIZE
|
||||
self.__half_frm = data[self.__index:]
|
||||
@ -621,9 +626,8 @@ class HmiRequest(object):
|
||||
self.__valid_data_length -= len(data[self.__index:])
|
||||
self.__index += clibs.MAX_FRAME_SIZE
|
||||
else:
|
||||
self.__index += 1
|
||||
else:
|
||||
self.__index = 0
|
||||
clibs.insert_logdb("ERROR", "openapi", "hr-get_response: Will never be here")
|
||||
raise Exception("WillNeverBeHere")
|
||||
|
||||
@staticmethod
|
||||
def __package_xs(cmd):
|
||||
@ -667,18 +671,16 @@ class HmiRequest(object):
|
||||
if state is None:
|
||||
self.close()
|
||||
clibs.w2t(f"请求 {msg_id} 发送失败......", "red", "ReqSendFailed")
|
||||
for _ in range(60):
|
||||
for _ in range(3):
|
||||
time.sleep(clibs.interval * 2)
|
||||
try:
|
||||
clibs.lock.acquire(True)
|
||||
clibs.cursor.execute(f"select * from logs where content like '{f_text}'")
|
||||
clibs.cursor.execute(f"select content from logs where content like '{f_text}'")
|
||||
res = len(clibs.cursor.fetchall())
|
||||
finally:
|
||||
clibs.lock.release()
|
||||
if res >= 2:
|
||||
break
|
||||
else:
|
||||
time.sleep(clibs.interval*2)
|
||||
continue
|
||||
else:
|
||||
clibs.insert_logdb("ERROR", "openapi", f"hr: 无法找到请求 {msg_id} 的响应")
|
||||
self.close()
|
||||
|
Reference in New Issue
Block a user