__init__.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
  2. from common.security import valid_access_token
  3. from sqlalchemy.orm import Session
  4. from sqlalchemy.sql import func
  5. from common.auth_user import *
  6. from sqlalchemy import text
  7. from pydantic import BaseModel
  8. from common.BigDataCenterAPI import *
  9. from database import get_db
  10. from typing import List
  11. from utils import *
  12. from utils.risk import *
  13. import json
  14. import traceback
  15. from datetime import datetime, timedelta
  16. router = APIRouter()
  17. @router.post('/temperature')
  18. async def mine(request: Request,db: Session = Depends(get_db)):
  19. try:
  20. body = await request.json()
  21. args = body.get("args")
  22. print(args)
  23. result = []
  24. if args == "6h_precipitation":
  25. result.append(
  26. {"create_time":"",
  27. "pic_url":"https://soc.gd121.cn/sk6hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071400_00600-00000.PNG"
  28. }
  29. )
  30. result.append(
  31. {"create_time": "",
  32. "pic_url": "https://soc.gd121.cn/sk6hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071500_00600-00000.PNG"
  33. }
  34. )
  35. result.append(
  36. {"create_time": "",
  37. "pic_url": "https://soc.gd121.cn/sk6hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071600_00600-00000.PNG"
  38. }
  39. )
  40. elif args == "24h_precipitation":
  41. result.append(
  42. {"create_time": "",
  43. "pic_url": "https://soc.gd121.cn/sk24hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071400_02400-00000.PNG"}
  44. )
  45. result.append(
  46. {"create_time": "",
  47. "pic_url": "https://soc.gd121.cn/sk24hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071500_02400-00000.PNG"}
  48. )
  49. result.append(
  50. {"create_time": "",
  51. "pic_url": "https://soc.gd121.cn/sk24hrain/2024/10/MSP1_AGD_MANOBS_PRCPV_L88_AGD_202410071600_02400-00000.PNG"}
  52. )
  53. elif args == "hourly_temperature":
  54. result.append(
  55. {"create_time": "",
  56. "pic_url": "https://soc.gd121.cn/skt/2024/10/MSP1_AGD_MANOBS_T_L88_AGD_202410071300_00000-00000.PNG"}
  57. )
  58. result.append(
  59. {"create_time": "",
  60. "pic_url": "https://soc.gd121.cn/skt/2024/10/MSP1_AGD_MANOBS_T_L88_AGD_202410071400_00000-00000.PNG"}
  61. )
  62. result.append(
  63. {"create_time": "",
  64. "pic_url": "https://soc.gd121.cn/skt/2024/10/MSP1_AGD_MANOBS_T_L88_AGD_202410071500_00000-00000.PNG"}
  65. )
  66. elif args == "24h_max_temperature":
  67. result.append(
  68. {"create_time": "",
  69. "pic_url": "https://soc.gd121.cn/sk24htmax/2024/10/MSP1_AGD_MANOBS_TMA_L88_AGD_202410071300_02400-00000.PNG"
  70. }
  71. )
  72. result.append(
  73. {"create_time": "",
  74. "pic_url": "https://soc.gd121.cn/sk24htmax/2024/10/MSP1_AGD_MANOBS_TMA_L88_AGD_202410071400_02400-00000.PNG"
  75. }
  76. )
  77. result.append(
  78. {"create_time": "",
  79. "pic_url": "https://soc.gd121.cn/sk24htmax/2024/10/MSP1_AGD_MANOBS_TMA_L88_AGD_202410071500_02400-00000.PNG"
  80. }
  81. )
  82. elif args == "24h_min_temperature":
  83. result.append(
  84. {"create_time": "",
  85. "pic_url": "https://soc.gd121.cn/sk24htmin/2024/10/MSP1_AGD_MANOBS_TMI_L88_AGD_202410071300_02400-00000.PNG"}
  86. )
  87. result.append(
  88. {"create_time": "",
  89. "pic_url": "https://soc.gd121.cn/sk24htmin/2024/10/MSP1_AGD_MANOBS_TMI_L88_AGD_202410071400_02400-00000.PNG"}
  90. )
  91. result.append(
  92. {"create_time": "",
  93. "pic_url": "https://soc.gd121.cn/sk24htmin/2024/10/MSP1_AGD_MANOBS_TMI_L88_AGD_202410071500_02400-00000.PNG"}
  94. )
  95. elif args == "24h_variable_temperature":
  96. result.append(
  97. {"create_time": "",
  98. "pic_url": "https://soc.gd121.cn/skimg/dt/BA3F7CC721A162A2149136AFE95CD8FE/A754169EFBBB6FA567B77735CED069F4.png"}
  99. )
  100. result.append(
  101. {"create_time": "",
  102. "pic_url": "https://soc.gd121.cn/skimg/dt/BA3F7CC721A162A2149136AFE95CD8FE/8A4271D80721F5497CEA242D0BD775BB.png"}
  103. )
  104. result.append(
  105. {"create_time": "",
  106. "pic_url": "https://soc.gd121.cn/skimg/dt/BA3F7CC721A162A2149136AFE95CD8FE/368D21EBBEA399BC2A119D0E54316C48.png"}
  107. )
  108. print(result)
  109. return {
  110. "code": 200,
  111. "msg": "成功",
  112. "data":
  113. {"max_level":result}
  114. }
  115. except Exception as e:
  116. db.rollback()
  117. traceback.print_exc()
  118. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  119. def make_url(dt):
  120. base_url = "https://soc.gd121.cn/cappi/{年}/{月}/CAPPI_440000_{年月日时}0000.png!wbdstyle"
  121. # 格式化日期和时间
  122. year = dt.strftime("%Y")
  123. month = dt.strftime("%m")
  124. day = dt.strftime("%d")
  125. hour = dt.strftime("%H")
  126. # 构造完整的URL
  127. url = base_url.format(年=year, 月=month, 年月日时=f"{year}{month}{day}{hour}")
  128. return url
  129. @router.get('/radar_chart')
  130. @router.post('/radar_chart')
  131. async def mine(request: Request,db: Session = Depends(get_db)):
  132. try:
  133. now = datetime.now()-timedelta(hours=1)
  134. urls = []
  135. for i in range(12):
  136. dt = now - timedelta(hours=i)
  137. if dt.hour == 0 and i > 0:
  138. dt -= timedelta(days=1)
  139. print(dt)
  140. urls.append(
  141. {
  142. "time":dt.strftime("%Y-%m-%d %H:00:00"),
  143. "url":make_url(dt)
  144. }
  145. )
  146. urls = urls[::-1]
  147. return {
  148. "code": 200,
  149. "msg": "成功",
  150. "data":
  151. urls
  152. }
  153. except Exception as e:
  154. db.rollback()
  155. traceback.print_exc()
  156. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")