temperature.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import glob
  2. import os
  3. from pathlib import Path
  4. import pandas as pd
  5. from fastapi import APIRouter, HTTPException
  6. from fastapi.responses import JSONResponse
  7. from app.logger import logger
  8. from app.models.AutoDiagModel import AutoDiagInput
  9. from app.models.TemperatureInput import TemperatureInput
  10. from app.models.TemperatureThresholdInput import TemperatureThresholdInput
  11. from app.services.Auto_diag import Auto_diag
  12. from app.services.MSET_Temp import MSET_Temp
  13. router = APIRouter()
  14. # 全局:{ windCode: { turbine: { channel: model, … }, … }, … }
  15. MODEL_STORE: dict[str, dict[str, dict[str, MSET_Temp]]] = {}
  16. # 英文→中文映射
  17. cn_map = {
  18. 'main_bearing_temperature': '主轴承温度',
  19. 'gearbox_oil_temperature': '齿轮箱油温',
  20. 'generatordrive_end_bearing_temperature': '发电机驱动端轴承温度',
  21. 'generatornon_drive_end_bearing_temperature': '发电机非驱动端轴承温度'
  22. }
  23. @router.on_event("startup")
  24. def load_all_models():
  25. for f in glob.glob("./app/temperature-models/*/*/*.pkl"):
  26. path = Path(f)
  27. ch = os.path.splitext(path.name)[0]
  28. MODEL_STORE.setdefault(path.parent.parent.name, {}).setdefault(path.parent.name, {})[ch] = MSET_Temp.load_model(f)
  29. logger.info(f"模型加载完成: { {k: list(v.keys()) for k, v in MODEL_STORE.items()} }")
  30. @router.post("/temperature/threshold")
  31. async def route_threshold(inp: TemperatureThresholdInput):
  32. """
  33. 输入:
  34. {
  35. "windCode": "WOF091200030",
  36. "windTurbineNumberList": ["WOG01355"],
  37. "startTime": "2024-06-01 00:00",
  38. "endTime": "2024-06-05 01:00",
  39. "pageNo": 1,
  40. "pageSize": 10
  41. }
  42. 输出:
  43. {
  44. "data": {
  45. "type": "temperature_threshold",
  46. "records": [
  47. {
  48. "wind_turbine_number": "WOG01355",
  49. "time_stamp": "2025-06-01 00:05:00",
  50. "temp_channel": "主轴承温度",
  51. "SPRT_score": 0.12,
  52. "status": "正常"
  53. },
  54. ...
  55. ],
  56. "totalSize": 42
  57. },
  58. "code": 200,
  59. "message": "success"
  60. }
  61. """
  62. # 1) 校验模型是否存在
  63. if inp.windCode not in MODEL_STORE:
  64. raise HTTPException(404, f"无模型:{inp.windCode}")
  65. # 2) 为每台待分析风机,拉数据并推理
  66. records = []
  67. for turbine in inp.windTurbineNumberList:
  68. if turbine not in MODEL_STORE[inp.windCode]:
  69. continue
  70. analyzer = MSET_Temp(inp.windCode, [turbine], inp.startTime, inp.endTime)
  71. df = analyzer._get_data_by_filter()
  72. if df.empty:
  73. continue
  74. df['time_stamp'] = pd.to_datetime(df['time_stamp'])
  75. for eng, cn in cn_map.items():
  76. if eng not in df.columns:
  77. continue
  78. sub = df[['time_stamp', eng]].dropna()
  79. arr = sub[eng].values.reshape(-1, 1)
  80. ts = sub['time_stamp'].dt.strftime("%Y-%m-%d %H:%M:%S").tolist()
  81. model = MODEL_STORE[inp.windCode][turbine].get(eng)
  82. if not model:
  83. continue
  84. flags = model.predict_SPRT(arr, decisionGroup=1)
  85. for i, sc in enumerate(flags):
  86. records.append({
  87. "wind_turbine_number": turbine,
  88. "time_stamp": ts[i],
  89. "temp_channel": cn,
  90. "SPRT_score": sc,
  91. "status": "危险" if sc > 0.99 else "正常"
  92. })
  93. # 分页返回
  94. total = len(records)
  95. start = (inp.pageNo - 1) * inp.pageSize
  96. end = start + inp.pageSize
  97. return {
  98. "data": {
  99. "type": "temperature_threshold",
  100. "records": records[start:end],
  101. "totalSize": total
  102. },
  103. "code": 200,
  104. "message": "success"
  105. }
  106. @router.post("/SPRT/trend")
  107. async def route_trend(inp: TemperatureInput):
  108. """
  109. 输入:
  110. {
  111. "windCode": "WOF091200030",
  112. "windTurbineNumberList": ["WOG01355"],
  113. "startTime": "2024-06-01 00:00",
  114. "endTime": "2024-06-05 01:00"
  115. }
  116. 输出:
  117. {
  118. "data": {
  119. "type": "SPRT_trend",
  120. "main_bearing": {"timestamps": [...], "values": [...]},
  121. "gearbox_oil": {"timestamps": [...], "values": [...]},
  122. "generator_drive_end": {"timestamps": [...], "values": [...]},
  123. "generator_nondrive_end": {"timestamps": [...], "values": [...]}
  124. },
  125. "code": 200,
  126. "message": "success"
  127. }
  128. """
  129. if inp.windCode not in MODEL_STORE:
  130. raise HTTPException(404, f"无模型:{inp.windCode}")
  131. turbines_out = []
  132. for turbine in inp.windTurbineNumberList:
  133. if turbine not in MODEL_STORE[inp.windCode]:
  134. continue
  135. analyzer = MSET_Temp(inp.windCode, [turbine], inp.startTime, inp.endTime)
  136. df = analyzer._get_data_by_filter()
  137. if df.empty:
  138. continue
  139. df['time_stamp'] = pd.to_datetime(df['time_stamp'])
  140. ch_data = {}
  141. for eng, key in {
  142. 'main_bearing_temperature': 'main_bearing',
  143. 'gearbox_oil_temperature': 'gearbox_oil',
  144. 'generatordrive_end_bearing_temperature': 'generator_drive_end',
  145. 'generatornon_drive_end_bearing_temperature': 'generator_nondrive_end'
  146. }.items():
  147. if eng not in df.columns:
  148. ch_data[key] = {"timestamps": [], "values": []}
  149. continue
  150. sub = df[['time_stamp', eng]].dropna()
  151. ts = sub['time_stamp'].dt.strftime("%Y-%m-%d %H:%M:%S").tolist()
  152. arr = sub[eng].values.reshape(-1, 1)
  153. model = MODEL_STORE[inp.windCode][turbine].get(eng)
  154. vals = model.predict_SPRT(arr, decisionGroup=1) if model else []
  155. ch_data[key] = {"timestamps": ts, "values": vals}
  156. # turbines_out.append({
  157. # "wind_turbine_number": turbine,
  158. # **ch_data
  159. # })
  160. return {
  161. "data": {
  162. "type": "SPRT_trend",
  163. **ch_data
  164. },
  165. "code": 200,
  166. "message": "success"
  167. }
  168. if __name__ == "__main__":
  169. import uvicorn
  170. uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
  171. @router.post("/autodiag/{autodiagType}")
  172. async def perform_diagnosis(autodiagType: str, input_data: AutoDiagInput):
  173. """
  174. 执行自动诊断分析
  175. 参数:
  176. autodiagType: 诊断类型
  177. input_data: 包含ids, windCode, engine_code的输入数据
  178. 返回:
  179. 诊断结果,包含状态码列表和统计信息
  180. """
  181. autodiag_map = {
  182. "Unbalance": "Unbalance_diag", # 不平衡诊断
  183. "Misalignment": "Misalignment_diag", # 不对中诊断
  184. "Looseness": "Looseness_diag", # 松动诊断
  185. "Bearing": "Bearing_diag", # 轴承诊断
  186. "Gear": "Gear_diag" # 齿轮诊断
  187. }
  188. if autodiagType not in autodiag_map:
  189. raise HTTPException(status_code=400, detail="非可用的诊断类型")
  190. try:
  191. # 初始化诊断类
  192. autodiag = Auto_diag(input_data.ids, input_data.windCode, input_data.engine_code)
  193. # 获取诊断方法
  194. func = getattr(autodiag, autodiag_map[autodiagType])
  195. # 执行诊断
  196. if callable(func):
  197. result = func() # 直接返回格式化后的结果
  198. return JSONResponse(content=result)
  199. except ValueError as e:
  200. # 专门捕获齿轮诊断的错误
  201. if "Can not perform gearbox diagnosis" in str(e):
  202. return JSONResponse(
  203. status_code=200,
  204. content={
  205. "code": 400,
  206. "message": str(e)
  207. }
  208. )
  209. elif "当前采集频率不适合进行诊断分析" in str(e):
  210. return JSONResponse(
  211. status_code=200,
  212. content={
  213. "code": 405,
  214. "message": str(e)
  215. }
  216. )
  217. # 其他ValueError
  218. raise HTTPException(status_code=400, detail=str(e))
  219. except Exception as e:
  220. raise HTTPException(status_code=500, detail=str(e))