devices.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from fastapi import APIRouter, BackgroundTasks, Depends, Query
  2. from loguru import logger
  3. from sqlalchemy.orm import Session
  4. import app.models.domain.devices as domain_devices
  5. from app.api.dependencies.db import get_db
  6. from app.controllers.equipment.ahu.supply_air_temperature_set import get_next_supply_air_temperature_set
  7. from app.controllers.equipment.ahu.thermal_mode import get_thermal_mode
  8. from app.controllers.equipment.ahu.basic import build_acatah_freq_set
  9. from app.controllers.equipment.ahu.switch import build_acatah_switch_set
  10. from app.controllers.equipment.fcu.basic import build_acatfc_instructions
  11. from app.controllers.equipment.fcu.early_start import get_recommended_early_start_time
  12. from app.controllers.equipment.pau.freq_set import get_next_acatfu_freq_set
  13. from app.controllers.equipment.pau.supply_air_temperature_set import get_next_acatfu_supply_air_temperature_set
  14. from app.controllers.equipment.pau.switch import get_switch_action
  15. from app.controllers.equipment.vav import build_acatva_instructions
  16. from app.controllers.equipment.vrf.basic import build_acatvi_instructions
  17. from app.controllers.equipment.ventilation_fan.switch import build_acvtsf_switch_set
  18. from app.models.domain.devices import DevicesInstructionsBaseResponse, DevicesEarlyStartTime
  19. router = APIRouter()
  20. @router.get('/instructions/acatah/thermal-mode-set', response_model=DevicesInstructionsBaseResponse)
  21. async def get_acatah_thermal_mode_set(
  22. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  23. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  24. ):
  25. thermal_mode = await get_thermal_mode(project_id, device_id)
  26. return {
  27. 'projectId': project_id,
  28. 'equipId': device_id,
  29. 'output': {
  30. 'thermalModeSet': thermal_mode
  31. }
  32. }
  33. @router.get('/instructions/acatah/supply-air-temperature-set', response_model=DevicesInstructionsBaseResponse)
  34. async def get_acatah_supply_air_temperature_set(
  35. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  36. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  37. ):
  38. next_supply_air_temperature_set = await get_next_supply_air_temperature_set(project_id, device_id)
  39. return {
  40. 'projectId': project_id,
  41. 'equipId': device_id,
  42. 'output': {
  43. 'supplyAirTemperatureSet': next_supply_air_temperature_set
  44. }
  45. }
  46. @router.post('/instructions/acatah/freq-set', response_model=domain_devices.ACATAHFreqSetResponse)
  47. async def get_acatah_freq_set(params: domain_devices.ACATAHFreqSetRequest):
  48. freq_set = await build_acatah_freq_set(params)
  49. logger.info(params)
  50. logger.info(f'{params.device_id}: freq set - {freq_set}')
  51. return freq_set
  52. @router.post('/instructions/acatah/switch-set', response_model=domain_devices.ACATAHSwitchSetResponse)
  53. async def get_acatah_switch_set(params: domain_devices.ACATAHSwitchSetRequest):
  54. switch_set = await build_acatah_switch_set(params)
  55. logger.info(params)
  56. logger.info(f'{params.device_id}: switch set - {switch_set}')
  57. return switch_set
  58. @router.post('/instructions/acvtsf/switch-set', response_model=domain_devices.ACVTSFSwitchSetResponse)
  59. async def get_acvtsf_switch_set(params: domain_devices.ACVTSFSwitchSetRequest):
  60. switch_set = await build_acvtsf_switch_set(params)
  61. logger.info(params)
  62. logger.info(f'{params.device_id}: switch set - {switch_set}')
  63. return switch_set
  64. # @router.post('/on-ratio-experiment/fcu/{fcu_id}/target/{target}/period/{period}')
  65. # async def run_on_ratio_experiment(fcu_id: str, target: float, period: int, background_tasks: BackgroundTasks):
  66. # background_tasks.add_task(start_on_ratio_mode, fcu_id, target, period)
  67. # return {
  68. # 'fcu': fcu_id,
  69. # 'target': target,
  70. # 'period': period
  71. # }
  72. @router.get('/instructions/acatfu/equip-switch-set', response_model=DevicesInstructionsBaseResponse)
  73. async def get_acatfu_equip_switch_set(
  74. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  75. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  76. ):
  77. action = await get_switch_action(project_id, device_id)
  78. return {
  79. 'projectId': project_id,
  80. 'equipId': device_id,
  81. 'output': {
  82. 'equipSwitchSet': action
  83. }
  84. }
  85. @router.get('/instructions/acatfu/supply-air-temperature-set', response_model=DevicesInstructionsBaseResponse)
  86. async def get_acatfu_supply_air_temperature_set(
  87. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  88. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  89. ):
  90. temperature_set = await get_next_acatfu_supply_air_temperature_set(project_id, device_id)
  91. return {
  92. 'projectId': project_id,
  93. 'equipId': device_id,
  94. 'output': {
  95. 'supplyAirTemperatureSet': temperature_set
  96. }
  97. }
  98. @router.get('/instructions/acatfu/freq-set', response_model=DevicesInstructionsBaseResponse)
  99. async def get_acatfu_freq_set(
  100. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  101. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  102. ):
  103. freq_set = await get_next_acatfu_freq_set(project_id, device_id)
  104. return {
  105. 'projectId': project_id,
  106. 'equipId': device_id,
  107. 'output': {
  108. 'fanFreqSet': freq_set
  109. }
  110. }
  111. @router.get('/early-start/prediction/acatfc', response_model=DevicesEarlyStartTime)
  112. async def get_acatfc_early_start_time(
  113. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  114. space_id: str = Query(..., max_length=50, regex='^Sp', alias='spaceId'),
  115. db: Session = Depends(get_db)
  116. ):
  117. minutes = await get_recommended_early_start_time(db, project_id, space_id)
  118. return {
  119. 'projectId': project_id,
  120. 'spaceId': space_id,
  121. 'minutes': minutes
  122. }
  123. @router.post('/instructions/acatvi', response_model=domain_devices.ACATVIInstructionsResponse)
  124. async def get_acatvi_instructions(params: domain_devices.ACATVIInstructionsRequest):
  125. instructions = await build_acatvi_instructions(params)
  126. logger.info(params)
  127. logger.info(f'{params.device_id} - {instructions}')
  128. return instructions
  129. @router.post('/instructions/acatfc', response_model=domain_devices.ACATFCInstructionsResponse)
  130. async def get_acatfc_instructions(params: domain_devices.ACATFCInstructionsRequest):
  131. instructions = await build_acatfc_instructions(params)
  132. logger.info(params)
  133. logger.info(f'{params.device_id} - {instructions}')
  134. response = domain_devices.ACATFCInstructionsResponse(
  135. onOff=instructions.switch_set,
  136. speed=instructions.speed_set,
  137. temperature=instructions.temperature_set,
  138. mode=instructions.switch_set,
  139. water=instructions.water_valve_switch_set
  140. )
  141. return response
  142. @router.post('/instructions/acatva', response_model=domain_devices.ACATVAInstructionsResponse)
  143. async def get_acatva_instructions(params: domain_devices.ACATVAInstructionsRequest):
  144. instructions = await build_acatva_instructions(params)
  145. logger.info(params)
  146. logger.info(f'{params.device_id} - {instructions}')
  147. response = domain_devices.ACATVAInstructionsResponse(
  148. SupplyAirFlowSet=instructions.supply_air_flow_set,
  149. VirtualRealtimeTemperature=instructions.virtual_realtime_temperature,
  150. TargetTemperatureSet=instructions.virtual_temperature_target_set
  151. )
  152. return response