devices.py 10 KB

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