v1.2.8
This commit is contained in:
@@ -115,10 +115,7 @@ class OTAManager:
|
||||
"""设置OTA模式(内部方法)"""
|
||||
with self._lock:
|
||||
self._ota_mode = mode
|
||||
|
||||
# ==================== 业务方法 ====================
|
||||
# 注意:这些方法会调用 ota.py 中的实际实现函数
|
||||
# 为了保持向后兼容,实际的实现仍然在 ota.py 中
|
||||
|
||||
|
||||
def is_archive_file(self, filename):
|
||||
"""
|
||||
@@ -628,7 +625,7 @@ class OTAManager:
|
||||
download_dir = self.get_download_timestamp_dir()
|
||||
return f"{download_dir}/{default_name}"
|
||||
|
||||
def download_file(self, url, filename):
|
||||
def download_file_via_wifi(self, url, filename):
|
||||
"""从指定 URL 下载文件,根据文件类型自动选择文本或二进制模式,并支持MD5校验"""
|
||||
try:
|
||||
self.logger.info(f"正在从 {url} 下载文件...")
|
||||
@@ -694,47 +691,47 @@ class OTAManager:
|
||||
except Exception as e:
|
||||
return f"下载失败!发生未知错误: {e}"
|
||||
|
||||
def direct_ota_download(self, ota_url):
|
||||
"""直接执行 OTA 下载(假设已有网络)"""
|
||||
# def direct_ota_download(self, ota_url):
|
||||
# """直接执行 OTA 下载(假设已有网络)"""
|
||||
|
||||
self._set_ota_url(ota_url)
|
||||
self._start_update_thread()
|
||||
# self._set_ota_url(ota_url)
|
||||
# self._start_update_thread()
|
||||
|
||||
try:
|
||||
if not ota_url:
|
||||
from network import safe_enqueue
|
||||
safe_enqueue({"result": "ota_failed", "reason": "missing_url"}, 2)
|
||||
return
|
||||
# try:
|
||||
# if not ota_url:
|
||||
# from network import safe_enqueue
|
||||
# safe_enqueue({"result": "ota_failed", "reason": "missing_url"}, 2)
|
||||
# return
|
||||
|
||||
parsed_url = urlparse(ota_url)
|
||||
host = parsed_url.hostname
|
||||
port = parsed_url.port or (443 if parsed_url.scheme == 'https' else 80)
|
||||
# parsed_url = urlparse(ota_url)
|
||||
# host = parsed_url.hostname
|
||||
# port = parsed_url.port or (443 if parsed_url.scheme == 'https' else 80)
|
||||
|
||||
if not network_manager.is_server_reachable(host, port, timeout=8):
|
||||
from network import safe_enqueue
|
||||
safe_enqueue({"result": "ota_failed", "reason": f"无法连接 {host}:{port}"}, 2)
|
||||
return
|
||||
# if not network_manager.is_server_reachable(host, port, timeout=8):
|
||||
# from network import safe_enqueue
|
||||
# safe_enqueue({"result": "ota_failed", "reason": f"无法连接 {host}:{port}"}, 2)
|
||||
# return
|
||||
|
||||
downloaded_filename = self.get_filename_from_url(ota_url, default_name="main_tmp")
|
||||
self.logger.info(f"[OTA] 下载文件将保存为: {downloaded_filename}")
|
||||
self.logger.info(f"[OTA] 开始下载: {ota_url}")
|
||||
result_msg = self.download_file(ota_url, downloaded_filename)
|
||||
self.logger.info(f"[OTA] {result_msg}")
|
||||
# downloaded_filename = self.get_filename_from_url(ota_url, default_name="main_tmp")
|
||||
# self.logger.info(f"[OTA] 下载文件将保存为: {downloaded_filename}")
|
||||
# self.logger.info(f"[OTA] 开始下载: {ota_url}")
|
||||
# result_msg = self.download_file(ota_url, downloaded_filename)
|
||||
# self.logger.info(f"[OTA] {result_msg}")
|
||||
|
||||
if "成功" in result_msg or "下载成功" in result_msg:
|
||||
if self.apply_ota_and_reboot(ota_url, downloaded_filename):
|
||||
return
|
||||
else:
|
||||
from network import safe_enqueue
|
||||
safe_enqueue({"result": result_msg}, 2)
|
||||
# if "成功" in result_msg or "下载成功" in result_msg:
|
||||
# if self.apply_ota_and_reboot(ota_url, downloaded_filename):
|
||||
# return
|
||||
# else:
|
||||
# from network import safe_enqueue
|
||||
# safe_enqueue({"result": result_msg}, 2)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"OTA 异常: {str(e)}"
|
||||
self.logger.error(error_msg)
|
||||
from network import safe_enqueue
|
||||
safe_enqueue({"result": "ota_failed", "reason": error_msg}, 2)
|
||||
finally:
|
||||
self._stop_update_thread()
|
||||
# except Exception as e:
|
||||
# error_msg = f"OTA 异常: {str(e)}"
|
||||
# self.logger.error(error_msg)
|
||||
# from network import safe_enqueue
|
||||
# safe_enqueue({"result": "ota_failed", "reason": error_msg}, 2)
|
||||
# finally:
|
||||
# self._stop_update_thread()
|
||||
|
||||
def download_file_via_4g(self, url, filename,
|
||||
total_timeout_ms=600000,
|
||||
@@ -766,7 +763,7 @@ class OTAManager:
|
||||
return False, "bad_url (no host)"
|
||||
|
||||
# 很多 ML307R 的 MHTTP 对 https 不稳定;对已知域名做降级
|
||||
|
||||
|
||||
if isinstance(url, str) and url.startswith("https://static.shelingxingqiu.com/"):
|
||||
base_url = "https://static.shelingxingqiu.com"
|
||||
# TODO:使用https,看看是否能成功
|
||||
@@ -862,7 +859,7 @@ class OTAManager:
|
||||
downgraded_base_url = base_url.replace("https://", "http://")
|
||||
resp = hardware_manager.at_client.send(f'AT+MHTTPCREATE="{downgraded_base_url}"', "OK", 8000)
|
||||
hid = _parse_httpid(resp)
|
||||
|
||||
|
||||
return hid, resp
|
||||
|
||||
def _fetch_range_into_buf(start, want_len, out_buf, full_reset=False):
|
||||
@@ -1221,7 +1218,7 @@ class OTAManager:
|
||||
self.logger.info(f"[OTA] 下载文件将保存为: {downloaded_filename}")
|
||||
|
||||
self.logger.info(f"[NET] 已确认可访问 {host}:{port},开始下载...")
|
||||
result = self.download_file(ota_url, downloaded_filename)
|
||||
result = self.download_file_via_wifi(ota_url, downloaded_filename)
|
||||
self.logger.info(result)
|
||||
|
||||
if "成功" in result or "下载成功" in result:
|
||||
|
||||
Reference in New Issue
Block a user