api_tempdiag.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # main.py
  2. from fastapi import FastAPI, HTTPException
  3. from fastapi.responses import JSONResponse
  4. from typing import List
  5. from pydantic import BaseModel, model_validator
  6. import uvicorn
  7. from Temp_Diag import MSET_Temp
  8. app = FastAPI(title="Temperature Diagnosis API")
  9. class TemperatureInput(BaseModel):
  10. windCode: str
  11. windTurbineNumberList: List[str]
  12. startTime: str # e.g. "2024-06-08 00:00"
  13. endTime: str # e.g. "2024-06-08 01:00"
  14. @model_validator(mode='before')
  15. def normalize_fields(cls, values):
  16. # 确保 windTurbineNumberList 是列表
  17. raw = values.get('windTurbineNumberList')
  18. if isinstance(raw, str):
  19. values['windTurbineNumberList'] = [raw]
  20. return values
  21. class TemperatureThresholdInput(TemperatureInput):
  22. pageNo: int # 必填
  23. pageSize: int # 必填
  24. # 阈值分析
  25. @app.post("/temperature/threshold")
  26. async def route_threshold(input_data: TemperatureThresholdInput):
  27. """
  28. 阈值分析接口(带分页)
  29. - 输入:
  30. {
  31. "windCode": "WOF01000010",
  32. "windTurbineNumberList": ["WOG00542"],
  33. "startTime": "2023-01-01 00:00",
  34. "endTime": "2023-01-05 12:00",
  35. "pageNo": 2,
  36. "pageSize": 20
  37. }
  38. - 返回:
  39. {
  40. "data": {
  41. "type": "temperature_threshold",
  42. "records": [
  43. {
  44. "time_stamp": "2024-06-08 00:05:00",
  45. "temp_channel": "主轴承温度",
  46. "SPRT_score": 0.123,
  47. "status": "正常"
  48. },
  49. ...
  50. ]
  51. },
  52. "code": 200,
  53. "message": "success"
  54. }
  55. """
  56. try:
  57. analyzer = MSET_Temp(
  58. windCode=input_data.windCode,
  59. windTurbineNumberList=input_data.windTurbineNumberList,
  60. startTime=input_data.startTime,
  61. endTime=input_data.endTime
  62. )
  63. records = analyzer.check_threshold().to_dict(orient="records")
  64. start = (input_data.pageNo - 1) * input_data.pageSize
  65. end = start + input_data.pageSize
  66. paginated = records[start:end]
  67. return {
  68. "data": {
  69. "type": "temperature_threshold",
  70. "records": paginated
  71. },
  72. "code": 200,
  73. "message": "success"
  74. }
  75. except Exception as e:
  76. return JSONResponse(
  77. status_code=500,
  78. content={
  79. "code": 500,
  80. "message": "analysis failed",
  81. "detail": str(e)
  82. }
  83. )
  84. # SPRT趋势分析
  85. @app.post("/SPRT/trend")
  86. async def route_trend(input_data: TemperatureInput):
  87. """
  88. 趋势分析接口:
  89. - 输入:
  90. {
  91. "windCode": "WOF01000010",
  92. "windTurbineNumberList": ["WOG00542"],
  93. "startTime": "2023-01-01 00:00",
  94. "endTime": "2023-01-05 12:00"
  95. }
  96. - 返回:
  97. {
  98. "data": {
  99. "type": "SPRT_trend",
  100. "main_bearing": {"timestamps": [...], "values": [...]}, # 主轴承温度
  101. "gearbox_oil": {"timestamps": [...], "values": [...]}, # 齿轮箱油温
  102. "generator_drive_end": {"timestamps": [...], "values": [...]}, # 发电机驱动端轴承温度
  103. "generator_nondrive_end": {"timestamps": [...], "values": [...]} # 发电机非驱动端轴承温度
  104. },
  105. "code": 200,
  106. "message": "success"
  107. }
  108. """
  109. try:
  110. analyzer = MSET_Temp(
  111. windCode=input_data.windCode,
  112. windTurbineNumberList=input_data.windTurbineNumberList,
  113. startTime=input_data.startTime,
  114. endTime=input_data.endTime
  115. )
  116. # get_trend() 已经返回形如 {"data": { … 四个 key … }} 的字典
  117. result = analyzer.get_trend()
  118. # 组装最终响应
  119. return {
  120. "data": {
  121. "type": "SPRT_trend",
  122. **result.get("data", {}) # 四个通道的数据或空对象
  123. },
  124. "code": 200,
  125. "message": "success"
  126. }
  127. except Exception as e:
  128. return JSONResponse(
  129. status_code=500,
  130. content={
  131. "code": 500,
  132. "message": "analysis failed",
  133. "detail": str(e)
  134. }
  135. )
  136. # # 温度趋势分析(暂未调用)
  137. # @app.post("/temperature/trend")
  138. # async def route_trend(input_data: TemperatureInput):
  139. # """
  140. # 趋势分析接口:
  141. # - 输入:
  142. # {
  143. # "windCode": "WOF01000010",
  144. # "windTurbineNumberList": ["WOG00542"],
  145. # "startTime": "2023-01-01 00:00",
  146. # "endTime": "2023-01-05 12:00"
  147. # }
  148. # - 返回:
  149. # {
  150. # "data": {
  151. # "type": "temperature_trend",
  152. # "timestamps": [ "2024-06-08 00:00:00", ... ],
  153. # "channels": [
  154. # { "temp_channel": "main_bearing_temperature", "values": [24.5, 24.7, ...] },
  155. # ...
  156. # ],
  157. # "unit": "°C"
  158. # },
  159. # "code": 200,
  160. # "message": "success"
  161. # }
  162. # """
  163. # try:
  164. # analyzer = MSET_Temp(
  165. # windCode=input_data.windCode,
  166. # windTurbineNumberList=input_data.windTurbineNumberList,
  167. # startTime=input_data.startTime,
  168. # endTime=input_data.endTime
  169. # )
  170. # result = analyzer.get_trend()
  171. # return {
  172. # "data": {
  173. # "type": "temperature_trend",
  174. # **result
  175. # },
  176. # "code": 200,
  177. # "message": "success"
  178. # }
  179. # except Exception as e:
  180. # return JSONResponse(
  181. # status_code=500,
  182. # content={
  183. # "code": 500,
  184. # "message": "analysis failed",
  185. # "detail": str(e)
  186. # }
  187. # )
  188. if __name__ == "__main__":
  189. uvicorn.run(
  190. "main:app",
  191. host="0.0.0.0",
  192. port=8000,
  193. reload=True
  194. )