api_tempdiag.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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": "WOF046400029",
  32. "windTurbineNumberList": ["WOG01312"],
  33. "startTime": "2023-11-01 00:00",
  34. "endTime": "2023-11-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. "totalSize": 741
  52. },
  53. "code": 200,
  54. "message": "success"
  55. }
  56. """
  57. try:
  58. analyzer = MSET_Temp(
  59. windCode=input_data.windCode,
  60. windTurbineNumberList=input_data.windTurbineNumberList,
  61. startTime=input_data.startTime,
  62. endTime=input_data.endTime
  63. )
  64. records = analyzer.check_threshold().to_dict(orient="records")
  65. total = len(records)
  66. start = (input_data.pageNo - 1) * input_data.pageSize
  67. end = start + input_data.pageSize
  68. paginated = records[start:end]
  69. return {
  70. "data": {
  71. "type": "temperature_threshold",
  72. "records": paginated,
  73. "totalSize": total
  74. },
  75. "code": 200,
  76. "message": "success"
  77. }
  78. except Exception as e:
  79. return JSONResponse(
  80. status_code=500,
  81. content={
  82. "code": 500,
  83. "message": "analysis failed",
  84. "detail": str(e)
  85. }
  86. )
  87. # SPRT趋势分析
  88. @app.post("/SPRT/trend")
  89. async def route_trend(input_data: TemperatureInput):
  90. """
  91. 趋势分析接口:
  92. - 输入:
  93. {
  94. "windCode": "WOF091200030",
  95. "windTurbineNumberList": ["WOG01351"],
  96. "startTime": "2023-11-01 00:00",
  97. "endTime": "2023-11-05 12:00"
  98. }
  99. - 返回:
  100. {
  101. "data": {
  102. "type": "SPRT_trend",
  103. "main_bearing": {"timestamps": [...], "values": [...]}, # 主轴承温度
  104. "gearbox_oil": {"timestamps": [...], "values": [...]}, # 齿轮箱油温
  105. "generator_drive_end": {"timestamps": [...], "values": [...]}, # 发电机驱动端轴承温度
  106. "generator_nondrive_end": {"timestamps": [...], "values": [...]} # 发电机非驱动端轴承温度
  107. },
  108. "code": 200,
  109. "message": "success"
  110. }
  111. """
  112. try:
  113. analyzer = MSET_Temp(
  114. windCode=input_data.windCode,
  115. windTurbineNumberList=input_data.windTurbineNumberList,
  116. startTime=input_data.startTime,
  117. endTime=input_data.endTime
  118. )
  119. # get_trend() 已经返回形如 {"data": { … 四个 key … }} 的字典
  120. result = analyzer.get_trend()
  121. # 组装最终响应
  122. return {
  123. "data": {
  124. "type": "SPRT_trend",
  125. **result.get("data", {}) # 四个通道的数据或空对象
  126. },
  127. "code": 200,
  128. "message": "success"
  129. }
  130. except Exception as e:
  131. return JSONResponse(
  132. status_code=500,
  133. content={
  134. "code": 500,
  135. "message": "analysis failed",
  136. "detail": str(e)
  137. }
  138. )
  139. # # 温度趋势分析(暂未调用)
  140. # @app.post("/temperature/trend")
  141. # async def route_trend(input_data: TemperatureInput):
  142. # """
  143. # 趋势分析接口:
  144. # - 输入:
  145. # {
  146. # "windCode": "WOF01000010",
  147. # "windTurbineNumberList": ["WOG00542"],
  148. # "startTime": "2023-01-01 00:00",
  149. # "endTime": "2023-01-05 12:00"
  150. # }
  151. # - 返回:
  152. # {
  153. # "data": {
  154. # "type": "temperature_trend",
  155. # "timestamps": [ "2024-06-08 00:00:00", ... ],
  156. # "channels": [
  157. # { "temp_channel": "main_bearing_temperature", "values": [24.5, 24.7, ...] },
  158. # ...
  159. # ],
  160. # "unit": "°C"
  161. # },
  162. # "code": 200,
  163. # "message": "success"
  164. # }
  165. # """
  166. # try:
  167. # analyzer = MSET_Temp(
  168. # windCode=input_data.windCode,
  169. # windTurbineNumberList=input_data.windTurbineNumberList,
  170. # startTime=input_data.startTime,
  171. # endTime=input_data.endTime
  172. # )
  173. # result = analyzer.get_trend()
  174. # return {
  175. # "data": {
  176. # "type": "temperature_trend",
  177. # **result
  178. # },
  179. # "code": 200,
  180. # "message": "success"
  181. # }
  182. # except Exception as e:
  183. # return JSONResponse(
  184. # status_code=500,
  185. # content={
  186. # "code": 500,
  187. # "message": "analysis failed",
  188. # "detail": str(e)
  189. # }
  190. # )
  191. if __name__ == "__main__":
  192. uvicorn.run(
  193. "main:app",
  194. host="0.0.0.0",
  195. port=8000,
  196. reload=True
  197. )