temperature.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from fastapi import APIRouter
  2. from starlette.responses import JSONResponse
  3. from app.models.TemperatureInput import TemperatureInput
  4. from app.models.TemperatureThresholdInput import TemperatureThresholdInput
  5. from app.services.MSET_Temp import MSET_Temp
  6. router = APIRouter()
  7. @router.post("/temperature/threshold")
  8. async def route_threshold(input_data: TemperatureThresholdInput):
  9. """
  10. 阈值分析接口(带分页)
  11. - 输入:
  12. {
  13. "windCode": "WOF046400029",
  14. "windTurbineNumberList": ["WOG01312"],
  15. "startTime": "2023-11-01 00:00",
  16. "endTime": "2023-11-05 12:00",
  17. "pageNo": 2,
  18. "pageSize": 20
  19. }
  20. - 返回:
  21. {
  22. "data": {
  23. "type": "temperature_threshold",
  24. "records": [
  25. {
  26. "time_stamp": "2024-06-08 00:05:00",
  27. "temp_channel": "主轴承温度",
  28. "SPRT_score": 0.123,
  29. "status": "正常"
  30. },
  31. ...
  32. ]
  33. "totalSize": 741
  34. },
  35. "code": 200,
  36. "message": "success"
  37. }
  38. """
  39. try:
  40. analyzer = MSET_Temp(
  41. windCode=input_data.windCode,
  42. windTurbineNumberList=input_data.windTurbineNumberList,
  43. startTime=input_data.startTime,
  44. endTime=input_data.endTime
  45. )
  46. records = analyzer.check_threshold().to_dict(orient="records")
  47. total = len(records)
  48. start = (input_data.pageNo - 1) * input_data.pageSize
  49. end = start + input_data.pageSize
  50. paginated = records[start:end]
  51. return {
  52. "data": {
  53. "type": "temperature_threshold",
  54. "records": paginated,
  55. "totalSize": total
  56. },
  57. "code": 200,
  58. "message": "success"
  59. }
  60. except Exception as e:
  61. return JSONResponse(
  62. status_code=500,
  63. content={
  64. "code": 500,
  65. "message": "analysis failed",
  66. "detail": str(e)
  67. }
  68. )
  69. # SPRT趋势分析
  70. @router.post("/SPRT/trend")
  71. async def route_trend(input_data: TemperatureInput):
  72. """
  73. 趋势分析接口:
  74. - 输入:
  75. {
  76. "windCode": "WOF091200030",
  77. "windTurbineNumberList": ["WOG01351"],
  78. "startTime": "2023-11-01 00:00",
  79. "endTime": "2023-11-05 12:00"
  80. }
  81. - 返回:
  82. {
  83. "data": {
  84. "type": "SPRT_trend",
  85. "main_bearing": {"timestamps": [...], "values": [...]}, # 主轴承温度
  86. "gearbox_oil": {"timestamps": [...], "values": [...]}, # 齿轮箱油温
  87. "generator_drive_end": {"timestamps": [...], "values": [...]}, # 发电机驱动端轴承温度
  88. "generator_nondrive_end": {"timestamps": [...], "values": [...]} # 发电机非驱动端轴承温度
  89. },
  90. "code": 200,
  91. "message": "success"
  92. }
  93. """
  94. try:
  95. analyzer = MSET_Temp(
  96. windCode=input_data.windCode,
  97. windTurbineNumberList=input_data.windTurbineNumberList,
  98. startTime=input_data.startTime,
  99. endTime=input_data.endTime
  100. )
  101. # get_trend() 已经返回形如 {"data": { … 四个 key … }} 的字典
  102. result = analyzer.get_trend()
  103. # 组装最终响应
  104. return {
  105. "data": {
  106. "type": "SPRT_trend",
  107. **result.get("data", {}) # 四个通道的数据或空对象
  108. },
  109. "code": 200,
  110. "message": "success"
  111. }
  112. except Exception as e:
  113. return JSONResponse(
  114. status_code=500,
  115. content={
  116. "code": 500,
  117. "message": "analysis failed",
  118. "detail": str(e)
  119. }
  120. )