feat: improve campus bike discovery and add independent live preview
This commit is contained in:
+20
-4
@@ -12,7 +12,7 @@
|
||||
- 判定用最近 20 分钟内的采集点(每车取最新一条),坐标系与车辆一致(GCJ02)。
|
||||
"""
|
||||
import datetime as dt
|
||||
import math
|
||||
import math
|
||||
import sqlite3
|
||||
import random
|
||||
import threading
|
||||
@@ -414,15 +414,25 @@ def _user_email(user_id):
|
||||
return get_user_alert_email(user_id)
|
||||
|
||||
|
||||
def record_rule_result(rule_id, status, message):
|
||||
conn = get_db()
|
||||
conn.execute("UPDATE subscribe_rules SET last_run_at=datetime('now','+8 hours'), last_run_status=?, last_run_message=? WHERE id=?",
|
||||
(status, str(message)[:500], rule_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def fire_rule(rule: dict):
|
||||
"""执行一条规则:按筛选条件统计候选车辆;需要时自动开锁并邮件汇报。"""
|
||||
from db import get_user_alert_email
|
||||
locs = load_locations()
|
||||
loc = next((l for l in locs if l["id"] == rule["location_id"]), None)
|
||||
to_email = get_user_alert_email(rule["user_id"])
|
||||
if not to_email:
|
||||
if rule.get("notify_enabled") and not to_email:
|
||||
record_rule_result(rule["id"], "failed", "未配置提醒邮箱")
|
||||
return False
|
||||
if loc is None:
|
||||
record_rule_result(rule["id"], "failed", "订阅停车点已删除")
|
||||
return False
|
||||
|
||||
flt = rule_filters(rule)
|
||||
@@ -498,7 +508,11 @@ def fire_rule(rule: dict):
|
||||
|
||||
plain = f"{title}\n\n{lead}\n\n" + "\n".join(f"{k}:{v}" for k, v in details)
|
||||
html_body = _render_html(title, lead, details)
|
||||
return send_email(to_email, f"【小白超人】{title}", plain, html_body)
|
||||
delivered = send_email(to_email, f"【小白超人】{title}", plain, html_body) if rule.get("notify_enabled") else True
|
||||
status = "failed" if (unlock_result and not unlock_result.get("success")) or not delivered else "success" if candidates else "no_match"
|
||||
message = title + (";邮件发送失败,请检查提醒邮箱和服务配置" if not delivered else "")
|
||||
record_rule_result(rule["id"], status, message)
|
||||
return status != "failed"
|
||||
|
||||
# ========== 调度 ==========
|
||||
|
||||
@@ -538,10 +552,12 @@ def _tick():
|
||||
if not rule.get("notify_enabled") and not rule.get("auto_unlock"):
|
||||
continue
|
||||
try:
|
||||
record_rule_result(rule["id"], "running", "正在检查符合条件的车辆")
|
||||
ok = fire_rule(rule)
|
||||
except Exception as e:
|
||||
ok = False
|
||||
print(f"[订阅提醒] 规则 {rule['id']} 发送失败: {e}")
|
||||
record_rule_result(rule["id"], "failed", "执行失败,请检查凭证有效期和网络后重试")
|
||||
print(f"[订阅提醒] 规则 {rule['id']} 执行失败: {type(e).__name__}")
|
||||
print(f"[订阅提醒] 规则 {rule['id']} 已触发 (user={rule['user_id']}, 发送={ok})")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user