This commit is contained in:
HuangHai
2026-01-12 21:33:21 +08:00
parent 2051cb61c2
commit 4612ee2d81
18 changed files with 457 additions and 191 deletions

View File

@@ -36,6 +36,25 @@ class AiTeJiYiChongService:
def get_hash(self, s: str) -> str:
return hashlib.md5(s.encode('utf-8')).hexdigest()
def _to_float(self, value) -> float:
"""
从字符串中提取浮点数,过滤掉非数字字符
"""
if value is None:
return 0.0
if isinstance(value, (int, float)):
return float(value)
import re
try:
# 提取第一个匹配的数字部分(包含小数点)
match = re.search(r"[-+]?\d*\.?\d+", str(value))
if match:
return float(match.group())
return 0.0
except:
return 0.0
async def init_db(self):
await self.db.init_db()
@@ -163,10 +182,23 @@ class AiTeJiYiChongService:
# 处理 piles 字段
piles_data = station.get("piles")
total, free = 0, 0
standardized_piles = []
if isinstance(piles_data, list):
for p in piles_data:
total += int(p.get("total", 0))
free += int(p.get("free", 0))
for idx, p in enumerate(piles_data):
try:
t = int(p.get("total", 0))
f = int(p.get("free", 0))
total += t
free += f
standardized_piles.append({
"pile_no": f"G{idx+1}",
"type": p.get("type", "未知"),
"power": "",
"status_text": f"空闲{f}/总{t}",
"remark": "列表页VL"
})
except:
pass
await self.station_status_model.save(
session=session,
@@ -174,8 +206,8 @@ class AiTeJiYiChongService:
station_hash=station_hash,
total_piles=total,
free_piles=free,
piles_detail_json=piles_data,
current_price=float(station.get("price", 0)) if station.get("price") else 0.0,
piles_detail_json=standardized_piles if standardized_piles else None,
current_price=self._to_float(station.get("price")),
parking_info=station.get("parking", ""),
distance=station.get("distance", ""),
valid_start_time=now
@@ -234,10 +266,23 @@ class AiTeJiYiChongService:
# 处理 piles 字段
piles_data = station.get("piles")
total, free = 0, 0
standardized_piles = []
if isinstance(piles_data, list):
for p in piles_data:
total += int(p.get("total", 0))
free += int(p.get("free", 0))
for idx, p in enumerate(piles_data):
try:
t = int(p.get("total", 0))
f = int(p.get("free", 0))
total += t
free += f
standardized_piles.append({
"pile_no": f"G{idx+1}",
"type": p.get("type", "未知"),
"power": "",
"status_text": f"空闲{f}/总{t}",
"remark": "列表页VL"
})
except:
pass
await self.station_status_model.save(
session=session,
@@ -245,8 +290,8 @@ class AiTeJiYiChongService:
station_hash=station_hash,
total_piles=total,
free_piles=free,
piles_detail_json=piles_data,
current_price=float(station.get("price", 0)) if station.get("price") else 0.0,
piles_detail_json=standardized_piles if standardized_piles else None,
current_price=self._to_float(station.get("price")),
parking_info=station.get("parking", ""),
distance=station.get("distance", ""),
valid_start_time=now