basically done again

This commit is contained in:
2025-03-28 13:43:25 +08:00
parent 957071075d
commit b07f221bb6
19 changed files with 74 additions and 1560 deletions

51
aio.py
View File

@ -14,7 +14,7 @@ from matplotlib.widgets import Slider
matplotlib.use('QtAgg')
from PySide6.QtCore import Qt, QThread, Signal, QObject, QTimer
from PySide6.QtGui import QTextCursor, QFont, QPixmap, QColor, QBrush
from PySide6.QtGui import QTextCursor, QFont, QPixmap, QColor, QBrush, QIcon
from PySide6.QtWidgets import QMessageBox, QCheckBox, QSplashScreen, QApplication, QFrame, QLabel, QTreeWidgetItem, QFileDialog, QHeaderView, QDialog, QVBoxLayout, QPlainTextEdit
import codes.common.clibs as clibs
@ -25,12 +25,6 @@ from codes.autotest import do_current, do_brake
from codes.durable import factory_test
class MultiWindows:
login_window = None
reset_window = None
main_window = None
class ContentDialog(QDialog):
def __init__(self, content, parent=None):
super().__init__(parent)
@ -111,6 +105,7 @@ class MainWindow(main_window.Ui_MainWindow):
header.setSectionResizeMode(i, QHeaderView.ResizeMode.ResizeToContents)
# ========================= clibs =========================
self.setup_statusbar()
self.setWindowIcon(QIcon(f"{clibs.PREFIX}/media/icon.ico"))
# ========================= styleSheet =========================
tws = [self.tw_funcs, self.tw_docs]
for tw in tws:
@ -199,6 +194,7 @@ class MainWindow(main_window.Ui_MainWindow):
QMessageBox.warning(self, "停止运行", "运行过程中不建议停止运行,可能会损坏文件,如果确实需要停止运行,可以直接关闭窗口!")
def prog_reset(self):
self.tw_docs.setCurrentIndex(0)
self.pte_output.clear()
def file_browser(self):
@ -296,6 +292,7 @@ class MainWindow(main_window.Ui_MainWindow):
item = QTreeWidgetItem(self.treew_log)
for i in range(self.treew_log.columnCount()):
item.setText(i, str(record[i]))
item.setTextAlignment(i, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
self.treew_log.addTopLevelItem(item)
color_map = {"DEBUG": QColor(220, 220, 220), "INFO": QColor(144, 238, 144), "WARNING": QColor(255, 240, 210), "ERROR": QColor(255, 220, 220)}
brush = QBrush(color_map[record[2].upper()])
@ -319,11 +316,11 @@ class MainWindow(main_window.Ui_MainWindow):
@clibs.db_lock
def do_pre():
if self.is_searching is False:
first_id = self.treew_log.topLevelItem(0).text(0)
end = int(first_id)-1 if int(first_id)-1 > 0 else None
start = int(first_id)-100 if int(first_id)-100 > 0 else 0
first_id = int(self.treew_log.topLevelItem(0).text(0))
end = first_id-1 if first_id-1 > 0 else None
if end is None:
return
start = end-100 if end-100 > 0 else 1
clibs.cursor.execute(f"SELECT * FROM logs WHERE id BETWEEN {start} AND {end}")
records = clibs.cursor.fetchall()
@ -400,7 +397,7 @@ class MainWindow(main_window.Ui_MainWindow):
line_number = self.treew_log.topLevelItemCount()
last_id = int(self.treew_log.topLevelItem(line_number-1).text(0))
start = last_id + 1 if last_id % 100 == 0 else last_id - last_id % 100 + 1
end = int(last_id) + 100
end = int(start) + 100
if int(start) <= len_records:
clibs.cursor.execute(f"select * from logs where id between {start} and {end}")
records = clibs.cursor.fetchall()
@ -677,7 +674,7 @@ class MainWindow(main_window.Ui_MainWindow):
clibs.search_records = clibs.cursor.fetchall()
len_records = len(clibs.search_records)
pages_all = len_records // 100 if len_records % 100 == 0 else len_records // 100 + 1
remainder = len_records % 100
remainder = len_records % 100 if len_records % 100 != 0 else 100
records = clibs.search_records[-1 * remainder:]
return pages_all, records
@ -739,7 +736,7 @@ class MainWindow(main_window.Ui_MainWindow):
def md_conn(self):
if clibs.status["hmi"] == 0:
QMessageBox.warning(self, "告警", "操作Modbus连接之前需要先打开HMI连接")
QMessageBox.warning(self, "告警", "操作 Modbus 连接之前,需要先打开 HMI 连接!")
return
self.btn_md_conn.setDisabled(True)
@ -751,7 +748,7 @@ class MainWindow(main_window.Ui_MainWindow):
def ec_conn(self):
if clibs.status["hmi"] == 0:
QMessageBox.warning(self, "告警", "操作外部通信连接之前需要先打开HMI连接")
QMessageBox.warning(self, "告警", "操作 EC 连接之前,需要先打开 HMI 连接!")
return
self.btn_ec_conn.setDisabled(True)
@ -792,7 +789,7 @@ class MainWindow(main_window.Ui_MainWindow):
clibs.c_hr.logger("DEBUG", "aio", f"hmi: xService请求发送成功 {cmd_json}")
if clibs.status["hmi"] == 0:
QMessageBox.critical(self, "错误", "使用该功能之前需要先打开HMI连接")
QMessageBox.critical(self, "错误", "使用该功能之前,需要先打开 HMI 连接!")
return
if self.pte_hmi_send.toPlainText() == "":
return
@ -847,7 +844,7 @@ class MainWindow(main_window.Ui_MainWindow):
def ec_send(self):
if clibs.status["ec"] == 0:
QMessageBox.critical(self, "错误", "使用该功能之前,需要先打开MD连接!")
QMessageBox.critical(self, "错误", "使用该功能之前,需要先打开 EC 连接!")
return
if self.pte_ec_send.toPlainText() == "":
return
@ -866,7 +863,7 @@ class MainWindow(main_window.Ui_MainWindow):
cmd = self.cb_hmi_cmd.currentText()
self.pte_hmi_send.clear()
self.pte_him_recv.clear()
with open(f"assets/files/protocols/hmi/{cmd}.json", mode="r", encoding="utf-8") as f_hmi:
with open(f"{clibs.PREFIX}/files/protocols/hmi/{cmd}.json", mode="r", encoding="utf-8") as f_hmi:
hmi_dict = json.load(f_hmi)
t = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
hmi_dict["id"] = "@".join([cmd, t])
@ -877,7 +874,7 @@ class MainWindow(main_window.Ui_MainWindow):
self.pte_md_send.clear()
self.pte_md_recv.clear()
self.pte_md_send.appendPlainText(cmd)
with open(f"assets/files/protocols/md/{cmd}.txt", mode="r", encoding="utf-8") as f_md:
with open(f"{clibs.PREFIX}/files/protocols/md/{cmd}.txt", mode="r", encoding="utf-8") as f_md:
c_send = f_md.read()
self.pte_md_send.appendPlainText(c_send)
@ -885,16 +882,16 @@ class MainWindow(main_window.Ui_MainWindow):
cmd = self.cb_ec_cmd.currentText()
self.pte_ec_send.clear()
self.pte_ec_recv.clear()
with open(f"assets/files/protocols/ec/{cmd}.txt", mode="r", encoding="utf-8") as f_md:
with open(f"{clibs.PREFIX}/files/protocols/ec/{cmd}.txt", mode="r", encoding="utf-8") as f_md:
c_send = f_md.read()
self.pte_ec_send.appendPlainText(c_send)
def check_interval(self):
try:
interval = float(self.le_durable_interval.text())
interval = 300 if interval < 300 else int(interval)
interval = clibs.CYCLE if interval < clibs.CYCLE else int(interval)
except Exception:
interval = 300
interval = clibs.CYCLE
self.le_durable_interval.setText(str(interval))
def state_detection(self):
@ -934,11 +931,11 @@ class MainWindow(main_window.Ui_MainWindow):
event.ignore()
def setup_statusbar(self):
with open(f"assets/files/version/local_vers", mode="r", encoding="utf-8") as f_local:
with open(f"{clibs.PREFIX}/files/version/local_vers", mode="r", encoding="utf-8") as f_local:
local_vers = f_local.read().strip()
l_version, update = local_vers.split("@")
vers_info = f" v{l_version} Update@{update}"
with open(f"assets/files/version/server_vers", mode="r", encoding="utf-8") as f_server:
with open(f"{clibs.PREFIX}/files/version/server_vers", mode="r", encoding="utf-8") as f_server:
server_vers = f_server.read().strip()
update_label = QLabel()
@ -946,11 +943,11 @@ class MainWindow(main_window.Ui_MainWindow):
self.statusbar.addWidget(version_label, 0)
self.statusbar.addPermanentWidget(update_label, 0) # 添加到右侧
if local_vers == server_vers:
update_label.setText('<img src="assets/media/updated.png" width="12" height="12" /><font color="#0D8A3D" face="consolas" size="4"><b>&nbsp;当前是最新版本,继续保持!&nbsp;</b></font>')
update_label.setText(f'<img src="{clibs.PREFIX}/media/updated.png" width="12" height="12" /><font color="#0D8A3D" face="consolas" size="4"><b>&nbsp;当前是最新版本,继续保持!&nbsp;</b></font>')
elif local_vers > server_vers:
pass
elif local_vers < server_vers:
update_label.setText(f'''<a href="https://www.rustle.cc/aio.zip" style="text-decoration: none;"><img src="assets/media/upgrade.png" width="12" height="12" /><font color="#D81E06" face="consolas" size="4"><b>&nbsp;v{server_vers.split('@')[0]}已经发布,尽快更新至最新版本!&nbsp;</b></font></a>''')
update_label.setText(f'''<a href="https://www.rustle.cc/aio.zip" style="text-decoration: none;"><img src="{clibs.PREFIX}/media/upgrade.png" width="12" height="12" /><font color="#D81E06" face="consolas" size="4"><b>&nbsp;v{server_vers.split('@')[0]}已经发布,尽快更新至最新版本!&nbsp;</b></font></a>''')
version_label.setText(f'<font color="black" face="consolas" size="4"><b>&nbsp;{vers_info}</b></font>')
update_label.setOpenExternalLinks(True) # 允许超链接在浏览器中打开
@ -971,7 +968,7 @@ class InitWork(QThread):
req = request.Request(url_vers, headers=headers)
response = request.urlopen(req, timeout=clibs.INTERVAL * 10)
server_vers = response.read().decode("utf-8").strip()
with open("assets/files/version/server_vers", mode="w", encoding="utf-8") as f_server:
with open(f"{clibs.PREFIX}/files/version/server_vers", mode="w", encoding="utf-8") as f_server:
f_server.write(server_vers)
self.completed.emit("true")
except Exception as err:
@ -988,7 +985,7 @@ class SplashScreen(QApplication):
super().__init__(argv)
self.window = None
pixmap = QPixmap("./assets/media/splash.png")
pixmap = QPixmap(f"{clibs.PREFIX}/media/splash.png")
self.splash = QSplashScreen(pixmap, Qt.WindowType.WindowStaysOnTopHint)
scaled_pixmap = pixmap.scaled(800, 400, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
self.splash.setPixmap(scaled_pixmap)