api_diag.py 9.0 KB

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