结构优化

This commit is contained in:
冯佳
2025-07-31 17:02:08 +08:00
parent 627aa51235
commit 54bf9f6d94
5 changed files with 176 additions and 266 deletions

View File

@ -1,3 +1,3 @@
User,User_pass,Wifi_SSID,Modbus_IP,Modbus_Port,NFC_ID,Reserve User,User_pass,Wifi_SSID,Modbus_IP,Modbus_Port,NFC_ID,Reserve
测试用户,0000,zhizhan-2,10.10.100.254,8899,0000,0000 正式测试,0000,zhizhan-2,10.10.100.254,8899,0000,1111
正式用户,0000,USR-DR404_3EB0,10.10.100.254,8899,0000,0000 正式用户,0000,USR-DR404_3EB0,10.10.100.254,8899,0000,0000

1 User User_pass Wifi_SSID Modbus_IP Modbus_Port NFC_ID Reserve
2 测试用户 正式测试 0000 zhizhan-2 10.10.100.254 8899 0000 0000 1111
3 正式用户 0000 USR-DR404_3EB0 10.10.100.254 8899 0000 0000

246
kv/app.kv
View File

@ -1,43 +1,36 @@
ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其切换 ScreenManager:
MainScreen: # 主屏幕 MainScreen:
name: 'main' # 屏幕名称,用于导航切换 name: 'main'
HomeScreen: # 首页屏幕 HomeScreen:
name: 'home' # 首页,显示应用主要内容和功能入口 name: 'home'
LoginScreen: # 登录屏幕 LoginScreen:
name: 'login' # 用户认证界面 name: 'login'
ProfileScreen: # 个人资料屏幕 ProfileScreen:
name: "profile" # 显示用户个人信息的界面 name: "profile"
HistoryScreen: # 历史记录屏幕 HistoryScreen:
name: 'history' # 显示用户操作历史 name: 'history'
ModifyCurrentParamScreen:
name: 'modify_current_param'
ModifyCurrentParamScreen: # 修改参数屏幕 ModifyVoltageParamScreen:
name: 'modify_current_param' # 用于修改系统参数的界面 name: 'modify_voltage_param'
ModifyVoltageParamScreen: # 修改电压参数屏幕 ModifyLeakageParamScreen:
name: 'modify_voltage_param' # 用于修改电压参数的界面 name: 'modify_leakage_param'
ModifyLeakageParamScreen: # 修改漏电参数屏幕 ModifySystemParamScreen:
name: 'modify_leakage_param' # 用于修改漏电参数的 name: 'modify_system_param'
ModifySystemParamScreen: # 修改系统参数屏幕 ModifyProtectionParamScreen:
name: 'modify_system_param' # 用于修改系统设置的界面 name: 'modify_protection_param'
ModifyProtectionParamScreen: # 修改保护参数屏幕 RealTimeCurveScreen:
name: 'modify_protection_param' # 用于修改保护参数的界面 name: 'real_time_curve'
RealTimeCurveScreen: # 实时曲线屏幕 ControlCommandScreen:
name: 'real_time_curve' # 显示实时数据曲线的界面 name: 'control_command'
ControlCommandScreen: # 控制命令屏幕 AboutScreen:
name: 'control_command' # 用于发送控制命令的界面 name: 'about'
AboutScreen: # 关于屏幕
name: 'about' # 显示应用信息和开发者信息的界
<MainScreen>: <MainScreen>:
MDFloatLayout: MDFloatLayout:
# Image:
# source: 'assets/logo.png'
# size_hint: None, 0.9
# width: dp(150)
# pos_hint: {'center_x': 0.1, 'center_y': 0.95}
Image: Image:
source: 'assets/img.png' source: 'assets/img.png'
size_hint: 1, 1 size_hint: 1, 1
@ -83,27 +76,6 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
size: self.size size: self.size
pos: self.pos pos: self.pos
radius: [5] radius: [5]
# Button:
# text: "注 册"
# size_hint: None, None
# font_name: "BPoppins"
# width: dp(350)
# height: dp(59)
# pos_hint: {'center_x': 0.5, 'center_y': 0.19}
# background_color: 0,0,0,0
# color: rgba(52, 0, 231, 255)
# on_touch_down: app.change_cursor(True)
# on_touch_up: app.change_cursor(False)
# on_release:
# root.manager.transition.direction = "left"
# root.manager.current = "signup"
# canvas.before:
# Color:
# rgb: rgba(52, 0, 231, 255)
# Line:
# width: 1.2
# rounded_rectangle: self.x, self.y, self.width, self.height, 5,5,5,5,100
<DrawerClickableItem@MDNavigationDrawerItem> <DrawerClickableItem@MDNavigationDrawerItem>
<HomeScreen>: <HomeScreen>:
@ -242,8 +214,8 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
id : user id : user
title: "欢迎使用" title: "欢迎使用"
text: "功能总览" text: "功能总览"
title_font_style: "H5" # 使用自定义标题字体 title_font_style: "H5"
text_font_style: "Caption" # 使用自定义正文字体 text_font_style: "Caption"
text_color: "#6c7272" text_color: "#6c7272"
text_font_size: '13sp' text_font_size: '13sp'
title_color: "#00f0ff" title_color: "#00f0ff"
@ -560,7 +532,7 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
size_hint_y: None size_hint_y: None
height: '30dp' height: '30dp'
MDLabel: MDLabel:
text: "Your Details" text: "信息"
font_name: "BPoppins" font_name: "BPoppins"
font_size: "14sp" font_size: "14sp"
color: [0,0,0,1] color: [0,0,0,1]
@ -581,55 +553,41 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
MDList: MDList:
spacing: "0dp" spacing: "0dp"
OneLineIconListItem: OneLineIconListItem:
text: profile_user_name.text id: profile_user_name
text: ""
IconLeftWidget: IconLeftWidget:
icon: "account-outline" icon: ""
OneLineIconListItem: OneLineIconListItem:
id : profile_user_pass id : profile_user_pass
text: "" text: ""
IconLeftWidget: IconLeftWidget:
icon: "email-outline" icon: ""
OneLineIconListItem: OneLineIconListItem:
id : profile_modbus_ip id : profile_modbus_ip
text: "" text: ""
IconLeftWidget: IconLeftWidget:
icon: "dialpad" icon: ""
OneLineIconListItem:
id : profile_modbus_port
text: ""
IconLeftWidget:
icon: ""
OneLineIconListItem: OneLineIconListItem:
id : profile_wifi_ssid id : profile_wifi_ssid
text: "" text: ""
IconLeftWidget: IconLeftWidget:
icon: "phone-outline" icon: "wifi"
MDLabel: OneLineIconListItem:
text: "Other Details" id : profile_nfc_id
font_name: "BPoppins"
font_size: "14sp"
size_hint_y: None
height: self.texture_size[1]
padding: "12dp"
pos_hint: {'center_y': 0.9}
ScrollView:
MDList:
spacing: "0dp"
OneLineAvatarListItem:
id : profile_branch
text: "" text: ""
ImageLeftWidget: IconLeftWidget:
source:'assets/branch.png' icon: "nfc"
size_hint: 2, .68 OneLineIconListItem:
pos: self.pos id : profile_reserve
OneLineAvatarListItem:
id : profile_semester
text: "" text: ""
ImageLeftWidget: IconLeftWidget:
source:'assets/semister.png' icon: ""
size_hint: 2, .68
pos: self.pos
<HistoryScreen>: <HistoryScreen>:
MDFloatLayout: MDFloatLayout:
@ -720,31 +678,6 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
spacing: "2dp" spacing: "2dp"
Widget: Widget:
MDBottomAppBar:
height: "62dp"
elevation: 10
MDActionBottomAppBarButton:
icon: "home"
pos_hint:{'center_x':0.15, 'center_y': 0.5}
on_press: root.manager.current = 'home'
MDActionBottomAppBarButton:
icon: "tools"
pos_hint:{'center_x':0.38, 'center_y': 0.5}
on_press: root.manager.current = 'borrow_book'
MDActionBottomAppBarButton:
icon: "thumb-up-outline"
pos_hint:{'center_x':0.61, 'center_y': 0.5}
on_press: root.manager.current = 'recommend'
MDActionBottomAppBarButton:
icon: "history"
pos_hint:{'center_x':0.85, 'center_y': 0.5}
on_press:
root.manager.current = 'history'
# app.book_history()
MDIconButton: MDIconButton:
icon: "arrow-left" icon: "arrow-left"
pos_hint: {'center_y': 0.95} pos_hint: {'center_y': 0.95}
@ -810,7 +743,7 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
MDTextField: MDTextField:
id: edit_name id: edit_user_name
text: "" text: ""
hint_text: "Enter Name*" hint_text: "Enter Name*"
font_name: "MPoppins" font_name: "MPoppins"
@ -833,12 +766,12 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
MDTextField: MDTextField:
id: edit_prn id: edit_user_pass
text: "" text: ""
hint_text: "Provide your PRN*" hint_text: "User_pass*"
font_name: "MPoppins" font_name: "MPoppins"
font_size: "15sp" font_size: "15sp"
helper_text: "unable to change prn directly" helper_text: "Provide your password"
helper_text_mode: "on_focus" helper_text_mode: "on_focus"
readonly: True readonly: True
icon_right: "dialpad" icon_right: "dialpad"
@ -857,15 +790,15 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
width: dp(0.5) width: dp(0.5)
rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100 rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100
MDTextField: MDTextField:
id: edit_email id: edit_modbus_ip
text: "" text: ""
hint_text: "Provide Email-id*" hint_text: "Modbus_IP*"
font_name: "MPoppins" font_name: "MPoppins"
font_size: "15sp" font_size: "15sp"
readonly: True readonly: True
helper_text: "unable to change email directly" helper_text: "Provide your Modbus_IP"
helper_text_mode: "on_focus" helper_text_mode: "on_focus"
icon_right: "email-arrow-left" icon_right: "network"
min_text_length: 11 min_text_length: 11
icon_color: app.theme_cls.primary_color icon_color: app.theme_cls.primary_color
pos_hint: {"center_x": 0.5, "center_y": 0.6025} pos_hint: {"center_x": 0.5, "center_y": 0.6025}
@ -881,15 +814,15 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100 rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100
MDTextField: MDTextField:
id:edit_no id:edit_modbus_port
text: "" text: ""
hint_text: "Enter WhatsApp Number*" hint_text: "Modbus_Port*"
font_name: "MPoppins" font_name: "MPoppins"
font_size: "15sp" font_size: "15sp"
readonly: True readonly: True
helper_text: "unable to change number directly" helper_text: "unable to change number directly"
helper_text_mode: "on_focus" helper_text_mode: "on_focus"
icon_right: "whatsapp" icon_right: "dialpad"
icon_color: app.theme_cls.primary_color icon_color: app.theme_cls.primary_color
input_filter: 'int' input_filter: 'int'
max_text_length: 10 max_text_length: 10
@ -906,14 +839,14 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100 rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100
MDTextField: MDTextField:
id: edit_branch id: edit_nfc_id
text: "" text: ""
hint_text: "Branch*" hint_text: "NFC_ID*"
font_name: "MPoppins" font_name: "MPoppins"
font_size: "15sp" font_size: "15sp"
helper_text: "Provide your Branch" helper_text: "Provide your NFC_ID"
helper_text_mode: "on_focus" helper_text_mode: "on_focus"
icon_right: "source-branch" icon_right: "nfc"
min_text_length: 11 min_text_length: 11
icon_color: app.theme_cls.primary_color icon_color: app.theme_cls.primary_color
pos_hint: {"center_x": 0.5, "center_y": 0.3625} pos_hint: {"center_x": 0.5, "center_y": 0.3625}
@ -929,12 +862,12 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100 rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100
MDTextField: MDTextField:
id: edit_sem id: edit_wifi_ssid
text: "" text: ""
hint_text: "Semester*" hint_text: "wifi ssid*"
font_name: "MPoppins" font_name: "MPoppins"
font_size: "15sp" font_size: "15sp"
helper_text: "Provide your semester like I,II,III only*" helper_text: "Provide your wifi ssid"
helper_text_mode: "on_focus" helper_text_mode: "on_focus"
icon_right: "roman-numeral-2" icon_right: "roman-numeral-2"
min_text_length: 11 min_text_length: 11
@ -951,8 +884,34 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
width: dp(0.5) width: dp(0.5)
rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100 rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100
MDTextField:
id: edit_reserve
text: ""
hint_text: "Reserve*"
font_name: "MPoppins"
font_size: "15sp"
helper_text: "Provide your Reserve"
helper_text_mode: "on_focus"
icon_right: "nfc"
min_text_length: 11
icon_color: app.theme_cls.primary_color
pos_hint: {"center_x": 0.5, "center_y": 0.1225}
size_hint_x: None
width: dp(335)
on_touch_down: app.change_cursor(True)
on_touch_up: app.change_cursor(False)
canvas.before:
Color:
rgb: rgba(127, 127, 127, 255)
Line:
width: dp(0.5)
rounded_rectangle: self.x-5, self.y+20, self.width+10, self.height-25, 10,10,10,10,100
Button: Button:
text: "Save Profile & Continue" text: "保存配置"
font_name: "BPoppins" font_name: "BPoppins"
size_hint: 0.66, 0.065 size_hint: 0.66, 0.065
pos_hint: {'center_x': 0.5, 'center_y': 0.17} pos_hint: {'center_x': 0.5, 'center_y': 0.17}
@ -972,21 +931,6 @@ ScreenManager: # 屏幕管理器,用于管理应用中所有的屏幕及其
pos: self.pos pos: self.pos
radius: [25] radius: [25]
MDTextButton:
id : edit_private_credential_butn
text: "Do you want edit prn/email/No."
pos_hint: {'center_x': 0.5, 'center_y': 0.12}
color: rgba(68,78,132,255)
font_name: "BPoppins"
font_size: '13sp'
on_touch_down: app.change_cursor(True)
on_touch_up: app.change_cursor(False)
on_release:
root.manager.transition.direction= "down"
app.send_otp(edit_no.text)
app.otp_button_fun("1")
root.manager.current="otp"
<RealTimeCurveScreen>: <RealTimeCurveScreen>:
MDFloatLayout: # 使用浮动布局,可以自由控制子组件的位置 MDFloatLayout: # 使用浮动布局,可以自由控制子组件的位置

168
main.py
View File

@ -87,51 +87,31 @@ class app(MDApp):
wifi_status_text = StringProperty("") wifi_status_text = StringProperty("")
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.signup_branch = None
self.user_name = None self.user_name = None
self.rec_grid = None
self.recommend_screen = None
self.book_input = None
self.data_tables = None
self.search_grid = None
self.search_field = None
self.renew_grid = None
self.renewable_books = []
self.renew = self.renewable_books
self.history_grid = None
self.books_history = []
self.history = self.books_history
self.image_label_grid = None
self.books = []
self.items = self.books
self.book_name = None
self.isbn = None
self.a = None
self.back_button = None
self.user = None
self.dialog = None self.dialog = None
self.dialog2 = None self.dialog2 = None
self.profile_edit_screen = None self.profile_edit_screen = None
self.edit_email = None
self.edit_prn = None self.edit_user_pass = None
self.edit_name = None self.edit_user_name = None
self.edit_wifi_ssid = None self.edit_wifi_ssid = None
self.edit_branch = None self.edit_modbus_ip = None
self.profile_semester = None self.edit_modbus_port = None
self.profile_branch = None self.edit_nfc_id = None
self.profile_wifi_ssid = None self.edit_reserve = None
self.profile_prn = None
self.profile_user_pass = None self.profile_user_pass = None
self.profile_user_name = None self.profile_user_name = None
self.user_no = None self.profile_wifi_ssid = None
self.user_email = None self.profile_modbus_ip = None
self.user_prn = None self.profile_modbus_port = None
self.d2 = None self.profile_nfc_id = None
self.d3 = None self.profile_reserve = None
self.d4 = None
self.user_pass = None self.user_pass = None
self.d1 = None self.d1 = None
self.edit_semester = None self.edit_semester = None
@ -218,43 +198,29 @@ class app(MDApp):
#############################################ALL INPUT TEXT############################################################ #############################################ALL INPUT TEXT############################################################
def on_start(self): def on_start(self):
# 从根窗口中获取名为"login"的屏幕(登录界面)
login_screen = self.root.get_screen("login") login_screen = self.root.get_screen("login")
# 从根窗口中获取名为"home"的屏幕(主界面)
home_screen = self.root.get_screen("home") home_screen = self.root.get_screen("home")
# 通过界面ID获取主界面中的用户信息组件
self.user = home_screen.ids.user self.user = home_screen.ids.user
# 从根窗口中获取名为"profile"的屏幕(个人资料界面)
profile_screen = self.root.get_screen("profile") profile_screen = self.root.get_screen("profile")
# 通过界面ID获取个人资料界面中的姓名展示组件
self.profile_user_name = profile_screen.ids.profile_user_name self.profile_user_name = profile_screen.ids.profile_user_name
# 通过界面ID获取个人资料界面中的邮箱展示组件
self.profile_user_pass = profile_screen.ids.profile_user_pass self.profile_user_pass = profile_screen.ids.profile_user_pass
# 通过界面ID获取个人资料界面中的PRN身份标识展示组件
self.profile_prn = profile_screen.ids.profile_prn
# 通过界面ID获取个人资料界面中的手机号展示组件
self.profile_wifi_ssid = profile_screen.ids.profile_wifi_ssid self.profile_wifi_ssid = profile_screen.ids.profile_wifi_ssid
# 通过界面ID获取个人资料界面中的所属部门/专业展示组件 self.profile_modbus_ip = profile_screen.ids.profile_modbus_ip
self.profile_branch = profile_screen.ids.profile_branch self.profile_modbus_port = profile_screen.ids.profile_modbus_port
# 通过界面ID获取个人资料界面中的年级/学期展示组件 self.profile_nfc_id = profile_screen.ids.profile_nfc_id
self.profile_semester = profile_screen.ids.profile_semester self.profile_reserve = profile_screen.ids.profile_reserve
# 从根窗口中获取名为"profile_edit"的屏幕(个人资料编辑界面)
self.profile_edit_screen = self.root.get_screen("profile_edit") self.profile_edit_screen = self.root.get_screen("profile_edit")
# 通过界面ID获取资料编辑界面中的姓名编辑输入框组件
self.edit_name = self.profile_edit_screen.ids.edit_name self.edit_user_name = self.profile_edit_screen.ids.edit_user_name
# 通过界面ID获取资料编辑界面中的邮箱编辑输入框组件 self.edit_user_pass = self.profile_edit_screen.ids.edit_user_pass
self.edit_email = self.profile_edit_screen.ids.edit_email self.edit_wifi_ssid = self.profile_edit_screen.ids.edit_wifi_ssid
# 通过界面ID获取资料编辑界面中的PRN身份标识编辑输入框组件 self.edit_modbus_ip = self.profile_edit_screen.ids.edit_modbus_ip
self.edit_prn = self.profile_edit_screen.ids.edit_prn self.edit_modbus_port = self.profile_edit_screen.ids.edit_modbus_port
# 通过界面ID获取资料编辑界面中的手机号编辑输入框组件 self.edit_nfc_id = self.profile_edit_screen.ids.edit_nfc_id
self.edit_wifi_ssid = self.profile_edit_screen.ids.edit_no self.edit_reserve = self.profile_edit_screen.ids.edit_reserve
# 通过界面ID获取资料编辑界面中的所属部门/专业编辑选择组件
self.edit_branch = self.profile_edit_screen.ids.edit_branch
# 通过界面ID获取资料编辑界面中的年级/学期编辑选择组件
self.edit_semester = self.profile_edit_screen.ids.edit_sem
self.root.bind(current=self.on_screen_changed) self.root.bind(current=self.on_screen_changed)
@ -450,7 +416,7 @@ class app(MDApp):
def read_modbus_registers(self, slave_id=1, address=0, count=1): def read_modbus_registers(self, slave_id=1, address=0, count=1):
""" """
通用的Modbus保持寄存器读取函数(优化版) 通用的Modbus保持寄存器读取函数
:param slave_id: 从机ID :param slave_id: 从机ID
:param address: 寄存器地址 :param address: 寄存器地址
:param count: 读取数量 :param count: 读取数量
@ -482,10 +448,10 @@ class app(MDApp):
def write_modbus_register(self, slave_id=1, address=0, value=None): def write_modbus_register(self, slave_id=1, address=0, value=None):
""" """
通用写入Modbus保持寄存器单个寄存器),低耦合版 通用写入Modbus保持寄存器(单个寄存器)
:param slave_id: 从机ID :param slave_id: 从机ID
:param address: 寄存器地址 :param address: 寄存器地址
:param value: 要写入的值整数0-65535 :param value: 要写入的值(整数,0-65535)
:return: {'success': bool, 'msg': str} :return: {'success': bool, 'msg': str}
""" """
return self.modbus_master.write_single_register(slave_id, address, value) return self.modbus_master.write_single_register(slave_id, address, value)
@ -640,22 +606,25 @@ class app(MDApp):
if wifi_id == csv_ssid: if wifi_id == csv_ssid:
# 通过列名获取用户信息 # 通过列名获取用户信息
name = row['User'] name = row['User']
prn = row['Reserve'] # 根据实际列名调整
email = row['Reserve'] # 根据实际列名调整
number = row['Reserve'] # 根据实际列名调整
password = row['User_pass'] password = row['User_pass']
branch = row['Reserve'] # 根据实际列名调整 wifi_ssid = row['Wifi_SSID']
semester = row['Reserve'] # 根据实际列名调整 modbus_ip = row['Modbus_IP']
modbus_port = row['Modbus_Port']
nfc_id = row['NFC_ID']
reserve = row['Reserve']
# 更新界面显示的用户信息 # 更新界面显示的用户信息
self.root.current = "home" self.root.current = "home"
self.user.text = f"[b]Hey! {name}[/b]" self.user.text = f"[b]Hey! {name}[/b]"
self.profile_user_name.text = self.edit_name.text = name
self.profile_user_pass.text = self.edit_email.text = email self.profile_user_name.text = self.edit_user_name.text = name
self.profile_prn.text = self.edit_prn.text = prn self.profile_user_pass.text = self.edit_user_pass.text = password
self.profile_wifi_ssid.text = row['Wifi_SSID'] self.profile_wifi_ssid.text = self.edit_wifi_ssid.text = wifi_ssid
self.profile_semester.text = self.edit_semester.text = semester self.profile_modbus_ip.text = self.edit_modbus_ip.text = modbus_ip
self.profile_branch.text = self.edit_branch.text = branch self.profile_modbus_port.text = self.edit_modbus_port.text = modbus_port
self.profile_nfc_id.text = self.edit_nfc_id.text = nfc_id
self.profile_reserve.text = self.edit_reserve.text = reserve
# 连接Modbus设备 # 连接Modbus设备
self.connect_modbus() self.connect_modbus()
@ -675,7 +644,7 @@ class app(MDApp):
master = None master = None
try: try:
# 获取当前WiFi SSID(保持不变) # 获取当前WiFi SSID
if platform == "android": if platform == "android":
from jnius import autoclass from jnius import autoclass
PythonActivity = autoclass('org.kivy.android.PythonActivity') PythonActivity = autoclass('org.kivy.android.PythonActivity')
@ -687,7 +656,7 @@ class app(MDApp):
else: else:
current_wifi_id = "zhizhan-2" current_wifi_id = "zhizhan-2"
# 读取CSV配置(保持不变) # 读取CSV配置
with open("data/Users.csv", "r", encoding="utf-8") as file: with open("data/Users.csv", "r", encoding="utf-8") as file:
csv_reader = csv.DictReader(file) csv_reader = csv.DictReader(file)
for row in csv_reader: for row in csv_reader:
@ -701,16 +670,16 @@ class app(MDApp):
Clock.schedule_once(lambda dt: self.dialog1("未找到匹配的Modbus配置")) Clock.schedule_once(lambda dt: self.dialog1("未找到匹配的Modbus配置"))
return return
# 断开现有连接(保持不变) # 断开现有连接
self.modbus_master.disconnect() self.modbus_master.disconnect()
# 创建新连接并测试(保持不变) # 创建新连接并测试
connect_result = self.modbus_master.connect(modbus_ip, modbus_port) connect_result = self.modbus_master.connect(modbus_ip, modbus_port)
if not connect_result['success']: if not connect_result['success']:
Clock.schedule_once(lambda dt: self.dialog1(connect_result['msg'])) Clock.schedule_once(lambda dt: self.dialog1(connect_result['msg']))
return return
print(f"Modbus连接成功 (modbus-tk)\nIP: {modbus_ip}\n端口: {modbus_port}") print(f"Modbus连接成功\nIP: {modbus_ip}\n端口: {modbus_port}")
Clock.schedule_once(lambda dt: self.dialog1(f"Modbus连接成功\nIP: {modbus_ip}\n端口: {modbus_port}")) # Clock.schedule_once(lambda dt: self.dialog1(f"Modbus连接成功\nIP: {modbus_ip}\n端口: {modbus_port}"))
except FileNotFoundError as e: except FileNotFoundError as e:
# 修复:将异常信息转为字符串或通过默认参数传递 # 修复:将异常信息转为字符串或通过默认参数传递
@ -722,7 +691,6 @@ class app(MDApp):
Clock.schedule_once(lambda dt, msg=err_msg: self.dialog1(msg)) Clock.schedule_once(lambda dt, msg=err_msg: self.dialog1(msg))
self.modbus_master = None self.modbus_master = None
except socket.error as e: except socket.error as e:
# 修复:提前格式化错误信息
err_msg = f"网络连接失败: {e}" err_msg = f"网络连接失败: {e}"
Clock.schedule_once(lambda dt, msg=err_msg: self.dialog1(msg)) Clock.schedule_once(lambda dt, msg=err_msg: self.dialog1(msg))
self.modbus_master = None self.modbus_master = None
@ -735,28 +703,32 @@ class app(MDApp):
threading.Thread(target=_connect_in_background, daemon=True).start() threading.Thread(target=_connect_in_background, daemon=True).start()
def edit_profile(self): def edit_profile(self):
if self.edit_name.text == "" or self.edit_prn.text == "" or self.edit_email.text == "" or self.edit_number.text == "": if self.edit_user_name.text == "" or self.edit_user_pass.text == "" or self.edit_wifi_ssid.text == "" or self.edit_modbus_ip.text == "" or self.edit_modbus_port.text == "" or self.edit_nfc_id.text == "" or self.edit_reserve.text == "":
check = "Enter all required fields" check = "Enter all required fields"
return self.dialog1(check) return self.dialog1(check)
else: else:
user_data = { user_data = {
'User': self.edit_name.text, 'User': self.edit_user_name.text,
'User_pass': self.edit_prn.text, 'User_pass': self.edit_user_pass.text,
'Wifi_SSID': self.edit_email.text, 'Wifi_SSID': self.edit_wifi_ssid.text,
'Modbus_IP': self.edit_number.text, 'Modbus_IP': self.edit_modbus_ip.text,
'Modbus_Port': self.password.text, 'Modbus_Port': self.edit_modbus_port.text,
'Reserve_branch': self.edit_branch.text, 'NFC_ID': self.edit_nfc_id.text,
'Reserve_semester': self.edit_semester.text 'Reserve': self.edit_reserve.text
} }
update_user_profile(user_data, self.profile_wifi_ssid.text) update_user_profile(user_data, self.profile_wifi_ssid.text)
self.verify(True) self.verify(True)
self.root.current = "home" self.root.current = "home"
def secure_profile(self): def secure_profile(self):
self.profile_edit_screen.ids.edit_email.readonly = True self.profile_edit_screen.ids.edit_user_pass.readonly = True
self.profile_edit_screen.ids.edit_prn.readonly = True self.profile_edit_screen.ids.edit_wifi_ssid.readonly = True
self.profile_edit_screen.ids.edit_no.readonly = True self.profile_edit_screen.ids.edit_modbus_ip.readonly = True
self.profile_edit_screen.ids.edit_private_credential_butn.disabled = False self.profile_edit_screen.ids.edit_modbus_port.readonly = True
self.profile_edit_screen.ids.edit_nfc_id.readonly = True
self.profile_edit_screen.ids.edit_reserve.readonly = False
from user_data_manager import update_user_profile from user_data_manager import update_user_profile

View File

@ -35,7 +35,7 @@ class ModbusClient:
def read_holding_registers(self, slave_id, address, count): def read_holding_registers(self, slave_id, address, count):
if not self.master: if not self.master:
return {'success': False, 'data': None, 'msg': "Modbus未初始化请先连接"} return {'success': False, 'data': None, 'msg': "Modbus未初始化,请先连接"}
try: try:
result = self.master.execute( result = self.master.execute(
slave=slave_id, slave=slave_id,
@ -52,7 +52,7 @@ class ModbusClient:
def write_single_register(self, slave_id, address, value): def write_single_register(self, slave_id, address, value):
if not self.master: if not self.master:
return {'success': False, 'msg': "Modbus未连接请先初始化连接"} return {'success': False, 'msg': "Modbus未连接,请先初始化连接"}
if value is None: if value is None:
return {'success': False, 'msg': "未指定要写入的值"} return {'success': False, 'msg': "未指定要写入的值"}
try: try:

View File

@ -2,10 +2,10 @@ import csv
def update_user_profile(user_data, profile_wifi_ssid): def update_user_profile(user_data, profile_wifi_ssid):
""" """
Updates user profile information in the Users.csv file. 更新Users.csv文件中的用户配置文件信息。
Args: 参数:
user_data (dict): A dictionary containing user profile data (e.g., 'User', 'User_pass', etc.). user_data:一个包含用户配置文件数据的字典
profile_wifi_ssid (str): The Wifi_SSID of the user to update. profile_wifi_ssid:要更新的用户的Wifi_SSID
""" """
with open('data/Users.csv', "r", encoding="utf-8") as file: with open('data/Users.csv', "r", encoding="utf-8") as file:
csv_reader = csv.DictReader(file, delimiter=",") csv_reader = csv.DictReader(file, delimiter=",")
@ -18,18 +18,12 @@ def update_user_profile(user_data, profile_wifi_ssid):
row['Wifi_SSID'] = user_data['Wifi_SSID'] row['Wifi_SSID'] = user_data['Wifi_SSID']
row['Modbus_IP'] = user_data['Modbus_IP'] row['Modbus_IP'] = user_data['Modbus_IP']
row['Modbus_Port'] = user_data['Modbus_Port'] row['Modbus_Port'] = user_data['Modbus_Port']
# The original code had 'Reserve' overwritten twice. Assuming it's a single 'Reserve' column. row['NFC_ID'] = user_data['NFC_ID']
# If there are two distinct 'Reserve' fields (e.g., branch and semester), the CSV header and logic need clarification. row['Reserve'] = user_data['Reserve']
# For now, I'll use the last assigned value for 'Reserve' as per original logic.
row['Reserve'] = user_data['Reserve_branch'] # Assuming this is for branch
row['Reserve'] = user_data['Reserve_semester'] # This will overwrite the previous 'Reserve' value
break break
with open('data/Users.csv', newline="", mode="w", encoding="utf-8") as file: with open('data/Users.csv', newline="", mode="w", encoding="utf-8") as file:
# The original header had two 'Reserve' columns. This is unusual for CSV and might be a typo. header = ["User", "User_pass", "Wifi_SSID", "Modbus_IP", "Modbus_Port", "NFC_ID", "Reserve"]
# I will use a single 'Reserve' column as it's more common, or clarify if two are truly needed.
# Based on the original code, it seems like the second 'Reserve' assignment overwrites the first.
header = ["User", "User_pass", "Wifi_SSID", "Modbus_IP", "Modbus_Port", "Reserve"]
csv_writer = csv.DictWriter(file, fieldnames=header) csv_writer = csv.DictWriter(file, fieldnames=header)
csv_writer.writeheader() csv_writer.writeheader()
csv_writer.writerows(rows) csv_writer.writerows(rows)