添加U盘升级功能

This commit is contained in:
ranchuan
2023-09-22 18:26:14 +08:00
parent 4292478f67
commit d8e41f133b
9 changed files with 304 additions and 14 deletions

103
daemon/daemon.py Normal file
View File

@@ -0,0 +1,103 @@
#!/usr/bin/python3
import os
import sys
import shutil
import threading
import re
import time
# 定义守护进程
# 日志文件路径
log_filepath = '/home/root/log/daemon_log.txt'
def write_info(text:str):
fm = '%Y-%m-%d %X'
nowtime = time.strftime(fm, time.localtime())
with open(log_filepath, 'a') as fp:
fp.write(nowtime+ " | "+text+"\n")
print(text)
def _do_cmd(cmd:str):
write_info(cmd)
ret = os.popen(cmd).readlines()
for i in range(len(ret)):
ret[i]=ret[i].strip()
write_info(ret[i])
return ret
class auto_updata(object):
def __init__(self):
self.sd_path="/run/media/sda"
self.file_path=self.sd_path+'/updata'
self.sd_inserd_state = False
self.file_list=[]
def sd_check(self):
ack=os.path.exists(self.sd_path)
if(ack!=self.sd_inserd_state):
if(ack==True):
write_info("sd inserd.")
self.copy_file()
else:
write_info("sd extracted.")
self.sd_inserd_state=ack
def copy_file(self):
self.file_list.clear()
try:
self.file_list=os.listdir(self.file_path)
except Exception as e:
write_info(str(e))
return False
for i in self.file_list:
write_info("|---| "+i)
_do_cmd("systemctl stop atk-qtapp-start.service")
_do_cmd("mkdir /home/root/config")
_do_cmd("cp "+self.find_file_by_type(".elf")+" /usr/local/QDesktop-fb")
_do_cmd("chmod 777 /usr/local/QDesktop-fb")
_do_cmd("cp "+self.find_file_by_type(".bin")+" /home/root/config/checker_slave.bin")
_do_cmd("cp "+self.find_file_by_type("scheme.json")+" /home/root/config/checker_ye_cfg.json")
_do_cmd("cp "+self.file_path+"/cfg.json /home/root/config/cfg.json")
_do_cmd("cp "+self.find_file_by_type(".axf")+" /lib/firmware/checker_m4.axf")
# _do_cmd("cp "+self.find_file_by_type(".dtb")+" /boot/stm32mp157d-atk.dtb")
_do_cmd("sync")
_do_cmd("systemctl restart atk-qtapp-start.service")
write_info("autoupdata end.")
return True
def find_file_by_type(self,type:str):
for i in self.file_list:
if(i[-len(type):]==type):
return self.file_path+'/'+i
return "unknown"
if __name__ == '__main__':
dir_name=os.path.dirname(log_filepath)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
updata=auto_updata()
while True:
updata.sd_check()
time.sleep(5)
# if __name__ == "__main__":
# scan_files("/run/media")

14
daemon/pydeamon.service Normal file
View File

@@ -0,0 +1,14 @@
[Unit]
Description=pydaemon daemon
After=rc-local.service
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/home/root
ExecStart=/usr/bin/python3 daemon.py
Restart=always
[Install]
WantedBy=multi-user.target