From cb567b2c7dfa3782fe1f465b5a236b392917754a Mon Sep 17 00:00:00 2001 From: YueL1331 <358700404@qq.com> Date: Fri, 16 Jan 2026 13:14:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E8=87=AA=E5=8A=A8=E7=88=AC=E5=8F=96?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=89=8D=E7=AB=AF=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E4=BC=A0=E8=BE=93=E8=AE=BE=E5=A4=87=E7=BB=91=E5=AE=9A=E5=92=8C?= =?UTF-8?q?iccid=E7=9A=84=E5=85=B3=E7=B3=BB=EF=BC=8C=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E6=B2=A1=E6=9C=89=E6=8E=A5=E6=94=B6=E5=88=B0?= =?UTF-8?q?=EF=BC=8C=E7=8E=B0=E5=9C=A8=E4=BF=AE=E6=94=B9=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2_1banben/app.py | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/2_1banben/app.py b/2_1banben/app.py index 8632f14..270028f 100644 --- a/2_1banben/app.py +++ b/2_1banben/app.py @@ -109,7 +109,29 @@ def auto_monitor_job(app): device.current_value = item.get('value') device.latest_time = item.get('target_time') device.check_time = current_time - device.json_data = json.dumps(item.get('raw_json', {}), ensure_ascii=False) + + # =========== ✅ 核心修复开始:防止丢失 bound_iccid =========== + + # 1. 准备容器:先读取数据库里现有的 JSON 数据 + old_json = {} + try: + if device.json_data: + old_json = json.loads(device.json_data) + except Exception: + old_json = {} + + # 2. 获取爬虫新数据 + new_json = item.get('raw_json', {}) + + # 3. 合并数据:只更新新爬取到的字段,保留 old_json 里的 bound_iccid + if isinstance(new_json, dict): + old_json.update(new_json) + + # 4. 存回数据库 + device.json_data = json.dumps(old_json, ensure_ascii=False) + + # =========== ✅ 核心修复结束 =========== + device.offset = calculate_offset(item.get('target_time')) db.session.add(DeviceHistory( @@ -125,28 +147,8 @@ def auto_monitor_job(app): print("⚠️ [定时任务-爬虫] 未获取到数据") except Exception as e: print(f"❌ [定时任务-爬虫] 异常: {e}") - - # --- 任务 B: IoT 同步 --- - if sync_iot_data_service: - try: - # 1. 获取数据 - iot_list = sync_iot_data_service() - # 2. 保存入库 (复用 api.py 中的逻辑) - count_iot, err = save_iot_cards_to_db(iot_list) - if err: - print(f"❌ [定时任务-IoT] 错误: {err}") - else: - print(f"✅ [定时任务-IoT] 更新 {count_iot} 张") - except Exception as e: - print(f"❌ [定时任务-IoT] 异常: {e}") - - # 统一提交事务 - try: - db.session.commit() - print("💾 [定时任务] 数据库事务提交完成") - except Exception as e: - db.session.rollback() - print(f"❌ [定时任务] 提交失败: {e}") + import traceback + traceback.print_exc() # ==============================================================================