96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
|
|
# coding=utf-8
|
|||
|
|
import hashlib
|
|||
|
|
import logging
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
import uuid
|
|||
|
|
from datetime import datetime
|
|||
|
|
|
|||
|
|
# Ensure sys path includes root for imports if not already
|
|||
|
|
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||
|
|
if project_root not in sys.path:
|
|||
|
|
sys.path.append(project_root)
|
|||
|
|
|
|||
|
|
from DbKit.Db import Db
|
|||
|
|
from Config.Config import DB_URL
|
|||
|
|
from Model.StationProfile import StationProfile
|
|||
|
|
from Model.StationStatus import StationStatus
|
|||
|
|
from Model.StationPriceSchedule import StationPriceSchedule
|
|||
|
|
|
|||
|
|
# 配置日志
|
|||
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|||
|
|
logger = logging.getLogger(__name__)
|
|||
|
|
|
|||
|
|
class TelaiDianService:
|
|||
|
|
def __init__(self):
|
|||
|
|
self.db = Db(db_url=DB_URL)
|
|||
|
|
self.station_profile_model = StationProfile()
|
|||
|
|
self.station_status_model = StationStatus()
|
|||
|
|
self.station_price_schedule_model = StationPriceSchedule()
|
|||
|
|
self.operator = "特来电"
|
|||
|
|
|
|||
|
|
def generate_id(self):
|
|||
|
|
return str(uuid.uuid4())
|
|||
|
|
|
|||
|
|
def get_hash(self, s: str) -> str:
|
|||
|
|
return hashlib.md5(s.encode('utf-8')).hexdigest()
|
|||
|
|
|
|||
|
|
async def init_db(self):
|
|||
|
|
await self.db.init_db()
|
|||
|
|
|
|||
|
|
async def close_db(self):
|
|||
|
|
await self.db.close()
|
|||
|
|
|
|||
|
|
async def save_station_data(self, station_name, address, prices):
|
|||
|
|
"""
|
|||
|
|
保存场站及其价格数据到数据库
|
|||
|
|
"""
|
|||
|
|
if not prices:
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
station_hash = self.get_hash(station_name)
|
|||
|
|
now = datetime.now()
|
|||
|
|
|
|||
|
|
# 将价格转换为 24 小时的 schedule 格式 (0-23)
|
|||
|
|
hourly_schedule = [0.0] * 24
|
|||
|
|
for p in prices:
|
|||
|
|
try:
|
|||
|
|
start_hour = int(p['start'].split(':')[0])
|
|||
|
|
end_hour = int(p['end'].split(':')[0])
|
|||
|
|
price = float(p['price'])
|
|||
|
|
|
|||
|
|
# 处理跨天的情况(如 23:00 - 01:00)
|
|||
|
|
curr = start_hour
|
|||
|
|
while curr != end_hour:
|
|||
|
|
hourly_schedule[curr] = price
|
|||
|
|
curr = (curr + 1) % 24
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.error(f"解析价格时段失败: {p}, error: {e}")
|
|||
|
|
|
|||
|
|
async with await self.db.get_session() as session:
|
|||
|
|
# 1. 保存 Profile
|
|||
|
|
profile_id = self.generate_id()
|
|||
|
|
await self.station_profile_model.save(
|
|||
|
|
session=session,
|
|||
|
|
id=profile_id,
|
|||
|
|
station_hash=station_hash,
|
|||
|
|
operator=self.operator,
|
|||
|
|
station_name=station_name,
|
|||
|
|
address=address,
|
|||
|
|
valid_start_time=now
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 2. 保存价格
|
|||
|
|
schedule_id = self.generate_id()
|
|||
|
|
await self.station_price_schedule_model.save(
|
|||
|
|
session=session,
|
|||
|
|
id=schedule_id,
|
|||
|
|
station_hash=station_hash,
|
|||
|
|
schedule_json=hourly_schedule,
|
|||
|
|
valid_start_time=now
|
|||
|
|
)
|
|||
|
|
await session.commit()
|
|||
|
|
|
|||
|
|
logger.info(f"成功保存场站数据: {station_name}")
|
|||
|
|
return True
|