devices.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. from fastapi import APIRouter, 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.basic import build_acatah_freq_set
  7. from app.controllers.equipment.ahu.supply_air_temperature_set import get_next_supply_air_temperature_set
  8. from app.controllers.equipment.ahu.supply_air_temperature_set import build_acatah_supply_air_temperature_set
  9. from app.controllers.equipment.ahu.switch import build_acatah_switch_set
  10. from app.controllers.equipment.ahu.thermal_mode import get_thermal_mode, build_acatah_thermal_mode_set
  11. from app.controllers.equipment.fcu.basic import build_acatfc_instructions
  12. from app.controllers.equipment.fcu.early_start import build_acatfc_early_start_prediction
  13. from app.controllers.equipment.fcu.early_start import get_recommended_early_start_time
  14. from app.controllers.equipment.pau.freq_set import get_next_acatfu_freq_set, build_acatfu_freq_set
  15. from app.controllers.equipment.pau.supply_air_temperature_set import get_next_acatfu_supply_air_temperature_set
  16. from app.controllers.equipment.pau.supply_air_temperature_set import build_acatfu_supply_air_temperature
  17. from app.controllers.equipment.pau.switch import get_switch_action, build_acatfu_switch_set
  18. from app.controllers.equipment.vav import build_acatva_instructions
  19. from app.controllers.equipment.ventilation_fan.switch import build_acvtsf_switch_set
  20. from app.controllers.equipment.vrf.basic import build_acatvi_instructions
  21. from app.models.domain.devices import DevicesInstructionsBaseResponse, DevicesEarlyStartTime
  22. router = APIRouter()
  23. @router.get('/instructions/acatah/thermal-mode-set', response_model=DevicesInstructionsBaseResponse)
  24. async def get_acatah_thermal_mode_set(
  25. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  26. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  27. ):
  28. thermal_mode = await get_thermal_mode(project_id, device_id)
  29. return {
  30. 'projectId': project_id,
  31. 'equipId': device_id,
  32. 'output': {
  33. 'thermalModeSet': thermal_mode
  34. }
  35. }
  36. @router.post('/instructions/acatah/thermal-mode-set', response_model=domain_devices.ACATAHThermalModeSetResponse)
  37. async def get_acatah_thermal_mode_set_v2(params: domain_devices.ACATAHThermalModeSetRequest):
  38. thermal_mode = build_acatah_thermal_mode_set(params)
  39. logger.info(f'{params.device_id}: thermal mode set - {thermal_mode}')
  40. resp = {'thermal_mode_set': thermal_mode}
  41. return resp
  42. @router.get('/instructions/acatah/supply-air-temperature-set', response_model=DevicesInstructionsBaseResponse)
  43. async def get_acatah_supply_air_temperature_set(
  44. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  45. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  46. ):
  47. next_supply_air_temperature_set = await get_next_supply_air_temperature_set(project_id, device_id)
  48. return {
  49. 'projectId': project_id,
  50. 'equipId': device_id,
  51. 'output': {
  52. 'supplyAirTemperatureSet': next_supply_air_temperature_set
  53. }
  54. }
  55. @router.post(
  56. '/instructions/acatah/supply-air-temperature-set',
  57. response_model=domain_devices.ACATAHSupplyAirTempSetResponse
  58. )
  59. async def get_acatah_supply_air_temperature_set_v2(params: domain_devices.ACATAHSupplyAirTempSetRequest):
  60. supply_air_temperature_set = build_acatah_supply_air_temperature_set(params)
  61. logger.info(supply_air_temperature_set)
  62. resp = {'supply_air_temperature_set': supply_air_temperature_set}
  63. return resp
  64. @router.post('/instructions/acatah/freq-set', response_model=domain_devices.ACATAHFreqSetResponse)
  65. async def get_acatah_freq_set(params: domain_devices.ACATAHFreqSetRequest):
  66. freq_set = await build_acatah_freq_set(params)
  67. logger.info(params)
  68. logger.info(f'{params.device_id}: freq set - {freq_set}')
  69. resp = {'freq_set': freq_set}
  70. return resp
  71. @router.post('/instructions/acatah/switch-set', response_model=domain_devices.ACATAHSwitchSetResponse)
  72. async def get_acatah_switch_set(params: domain_devices.ACATAHSwitchSetRequest):
  73. switch_set = await build_acatah_switch_set(params)
  74. logger.info(params)
  75. logger.info(f'{params.device_id}: switch set - {switch_set}')
  76. resp = {'switch_set': switch_set}
  77. return resp
  78. @router.post('/instructions/acvtsf/switch-set', response_model=domain_devices.ACVTSFSwitchSetResponse)
  79. async def get_acvtsf_switch_set(params: domain_devices.ACVTSFSwitchSetRequest):
  80. switch_set = await build_acvtsf_switch_set(params)
  81. logger.info(params)
  82. logger.info(f'{params.device_id}: switch set - {switch_set}')
  83. resp = {'switch_set': switch_set}
  84. return resp
  85. @router.get('/instructions/acatfu/equip-switch-set', response_model=DevicesInstructionsBaseResponse)
  86. async def get_acatfu_equip_switch_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. action = await get_switch_action(project_id, device_id)
  91. return {
  92. 'projectId': project_id,
  93. 'equipId': device_id,
  94. 'output': {
  95. 'equipSwitchSet': action
  96. }
  97. }
  98. @router.post('/instructions/acatfu/switch-set', response_model=domain_devices.ACATFUSwitchSetResponse)
  99. async def get_acatfu_switch_set(params: domain_devices.ACATFUSwitchSetRequest):
  100. switch_set = await build_acatfu_switch_set(params)
  101. logger.info(params)
  102. logger.info(f'{params.device_id}: switch set - {switch_set}')
  103. resp = {'switch_set': switch_set}
  104. return resp
  105. @router.get('/instructions/acatfu/supply-air-temperature-set', response_model=DevicesInstructionsBaseResponse)
  106. async def get_acatfu_supply_air_temperature_set(
  107. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  108. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  109. ):
  110. temperature_set = await get_next_acatfu_supply_air_temperature_set(project_id, device_id)
  111. return {
  112. 'projectId': project_id,
  113. 'equipId': device_id,
  114. 'output': {
  115. 'supplyAirTemperatureSet': temperature_set
  116. }
  117. }
  118. @router.post(
  119. '/instructions/acatfu/supply-air-temperature-set',
  120. response_model=domain_devices.ACATFUSupplyAirTempSetResponse
  121. )
  122. async def get_acatfu_supply_air_temperature_set_v2(params: domain_devices.ACATFUSupplyAirTempSetRequest):
  123. supply_air_temperature_set = build_acatfu_supply_air_temperature(params)
  124. logger.info(supply_air_temperature_set)
  125. resp = {'supply_air_temperature_set': supply_air_temperature_set}
  126. return resp
  127. @router.get('/instructions/acatfu/freq-set', response_model=DevicesInstructionsBaseResponse)
  128. async def get_acatfu_freq_set(
  129. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  130. device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
  131. ):
  132. freq_set = await get_next_acatfu_freq_set(project_id, device_id)
  133. return {
  134. 'projectId': project_id,
  135. 'equipId': device_id,
  136. 'output': {
  137. 'fanFreqSet': freq_set
  138. }
  139. }
  140. @router.post('/instruction/acatfu/freq-set', response_model=domain_devices.ACATFUFreqSetResponse)
  141. async def get_acatfu_freq_set(params: domain_devices.ACATFUFreqSetRequest):
  142. freq_set = build_acatfu_freq_set(params)
  143. logger.info(f'{params.device_id} - {freq_set}')
  144. resp = {'freq_set': freq_set}
  145. return resp
  146. @router.get('/early-start/prediction/acatfc', response_model=DevicesEarlyStartTime)
  147. async def get_acatfc_early_start_time(
  148. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  149. space_id: str = Query(..., max_length=50, regex='^Sp', alias='spaceId'),
  150. db: Session = Depends(get_db)
  151. ):
  152. minutes = await get_recommended_early_start_time(db, project_id, space_id)
  153. return {
  154. 'projectId': project_id,
  155. 'spaceId': space_id,
  156. 'minutes': minutes
  157. }
  158. @router.post('/prediction/acatfc/early-start', response_model=domain_devices.ACATFCEarlyStartPredictionResponse)
  159. async def get_acatfc_early_start_prediction(
  160. params: domain_devices.ACATFCEarlyStartPredictionRequest,
  161. db: Session = Depends(get_db)
  162. ):
  163. minutes = await build_acatfc_early_start_prediction(params, db)
  164. logger.info(params)
  165. logger.info(f'{params.space_id} - {minutes}')
  166. resp = {'minutes': minutes}
  167. return resp
  168. @router.post('/instructions/acatvi', response_model=domain_devices.ACATVIInstructionsResponse)
  169. async def get_acatvi_instructions(params: domain_devices.ACATVIInstructionsRequest):
  170. instructions = await build_acatvi_instructions(params)
  171. logger.info(params)
  172. logger.info(f'{params.device_id} - {instructions}')
  173. return instructions
  174. @router.post('/instructions/acatfc', response_model=domain_devices.ACATFCInstructionsResponse)
  175. async def get_acatfc_instructions(params: domain_devices.ACATFCInstructionsRequest):
  176. instructions = await build_acatfc_instructions(params)
  177. logger.info(params)
  178. logger.info(f'{params.device_id} - {instructions}')
  179. response = domain_devices.ACATFCInstructionsResponse(
  180. onOff=instructions.switch_set,
  181. speed=instructions.speed_set,
  182. temperature=instructions.temperature_set,
  183. mode=instructions.switch_set,
  184. water=instructions.water_valve_switch_set
  185. )
  186. return response
  187. @router.post('/instructions/acatva', response_model=domain_devices.ACATVAInstructionsResponse)
  188. async def get_acatva_instructions(params: domain_devices.ACATVAInstructionsRequest):
  189. instructions = await build_acatva_instructions(params)
  190. logger.info(params)
  191. logger.info(f'{params.device_id} - {instructions}')
  192. response = domain_devices.ACATVAInstructionsResponse(
  193. SupplyAirFlowSet=instructions.supply_air_flow_set,
  194. VirtualRealtimeTemperature=instructions.virtual_realtime_temperature,
  195. TargetTemperatureSet=instructions.virtual_temperature_target_set
  196. )
  197. return response