from fastapi import APIRouter, Request, Depends, Query, HTTPException, status from common.security import valid_access_token from sqlalchemy.orm import Session from sqlalchemy.sql import func from common.auth_user import * from sqlalchemy import text from pydantic import BaseModel from common.BigDataCenterAPI import * from database import get_db from typing import List from utils import * from utils.risk import * import json import traceback from datetime import datetime, timedelta router = APIRouter() @router.post('/temperature') async def mine(request: Request,db: Session = Depends(get_db)): try: body = await request.json() args = body.get("args") print(args) result = [] if args == "6h_precipitation": result.append( {"create_time":"", "pic_url":"https://soc.gd121.cn/sk6hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071400_00600-00000.PNG" } ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk6hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071500_00600-00000.PNG" } ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk6hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071600_00600-00000.PNG" } ) elif args == "24h_precipitation": result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071400_02400-00000.PNG"} ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071500_02400-00000.PNG"} ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071600_02400-00000.PNG"} ) elif args == "hourly_temperature": result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/skt/2024/10/MSP1_AGD_MANOBS_T_L88_AGD_202410071300_00000-00000.PNG"} ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/skt/2024/10/MSP1_AGD_MANOBS_T_L88_AGD_202410071400_00000-00000.PNG"} ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/skt/2024/10/MSP1_AGD_MANOBS_T_L88_AGD_202410071500_00000-00000.PNG"} ) elif args == "24h_max_temperature": result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24htmax/2024/10/MSP1_AGD_MANOBS_TMA_L88_AGD_202410071300_02400-00000.PNG" } ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24htmax/2024/10/MSP1_AGD_MANOBS_TMA_L88_AGD_202410071400_02400-00000.PNG" } ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24htmax/2024/10/MSP1_AGD_MANOBS_TMA_L88_AGD_202410071500_02400-00000.PNG" } ) elif args == "24h_min_temperature": result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24htmin/2024/10/MSP1_AGD_MANOBS_TMI_L88_AGD_202410071300_02400-00000.PNG"} ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24htmin/2024/10/MSP1_AGD_MANOBS_TMI_L88_AGD_202410071400_02400-00000.PNG"} ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/sk24htmin/2024/10/MSP1_AGD_MANOBS_TMI_L88_AGD_202410071500_02400-00000.PNG"} ) elif args == "24h_variable_temperature": result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/skt24hdt/2024/10/MSP1_AGD_MANOBS_TD_L88_AGD_202410071300_02400-00000.PNG"} ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/skt24hdt/2024/10/MSP1_AGD_MANOBS_TD_L88_AGD_202410071400_02400-00000.PNG"} ) result.append( {"create_time": "", "pic_url": "https://soc.gd121.cn/skt24hdt/2024/10/MSP1_AGD_MANOBS_TD_L88_AGD_202410071500_02400-00000.PNG"} ) print(result) return { "code": 200, "msg": "成功", "data": {"max_level":result} } except Exception as e: db.rollback() traceback.print_exc() raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}") def make_url(dt): base_url = "https://soc.gd121.cn/cappi/{年}/{月}/CAPPI_440000_{年月日时}0000.png!wbdstyle" # 格式化日期和时间 year = dt.strftime("%Y") month = dt.strftime("%m") day = dt.strftime("%d") hour = dt.strftime("%H") # 构造完整的URL url = base_url.format(年=year, 月=month, 年月日时=f"{year}{month}{day}{hour}") return url @router.get('/radar_chart') @router.post('/radar_chart') async def mine(request: Request,db: Session = Depends(get_db)): try: now = datetime.now()-timedelta(hours=1) urls = [] for i in range(12): dt = now - timedelta(hours=i) if dt.hour == 0 and i > 0: dt -= timedelta(days=1) print(dt) urls.append( { "time":dt.strftime("%Y-%m-%d %H:00:00"), "url":make_url(dt) } ) urls = urls[::-1] return { "code": 200, "msg": "成功", "data": urls } except Exception as e: db.rollback() traceback.print_exc() raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")