devices.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import numpy as np
  2. from fastapi import APIRouter, Depends
  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.common import build_acatah_instructions
  9. from app.controllers.equipment.ahu.supply_air_temperature_set import (
  10. build_acatah_supply_air_temperature_set,
  11. )
  12. from app.controllers.equipment.ahu.switch import build_acatah_switch_set
  13. from app.controllers.equipment.ahu.thermal_mode import build_acatah_thermal_mode_set
  14. from app.controllers.equipment.fcu.basic import (
  15. build_acatfc2_instructions,
  16. build_acatfc4_instructions,
  17. build_acatfc2_instructions_v2,
  18. build_acatfc4_instructions_v2,
  19. )
  20. from app.controllers.equipment.fcu.early_start import (
  21. build_acatfc_early_start_prediction,
  22. )
  23. from app.controllers.equipment.pau.freq_set import build_acatfu_freq_set
  24. from app.controllers.equipment.pau.supply_air_temperature_set import (
  25. build_acatfu_supply_air_temperature,
  26. )
  27. from app.controllers.equipment.pau.switch import build_acatfu_switch_set, build_co2_acatfu_switch
  28. from app.controllers.equipment.vav import (
  29. build_acatva_instructions,
  30. build_acatva_instructions_for_JM,
  31. )
  32. from app.controllers.equipment.ventilation_fan.switch import build_acvtsf_switch_set
  33. from app.controllers.equipment.vrf.basic import build_acatvi_instructions
  34. from app.controllers.equipment.vrf.mode import build_acatvi_mode
  35. router = APIRouter()
  36. @router.post(
  37. "/instructions/acatah/thermal-mode-set",
  38. response_model=domain_devices.ACATAHThermalModeSetResponse,
  39. )
  40. async def get_acatah_thermal_mode_set_v2(
  41. params: domain_devices.ACATAHThermalModeSetRequest,
  42. ):
  43. thermal_mode = build_acatah_thermal_mode_set(params)
  44. logger.info(f"{params.device_id}: thermal mode set - {thermal_mode}")
  45. resp = {"thermal_mode_set": thermal_mode}
  46. return resp
  47. @router.post(
  48. "/instructions/acatah/supply-air-temperature-set",
  49. response_model=domain_devices.ACATAHSupplyAirTempSetResponse,
  50. )
  51. async def get_acatah_supply_air_temperature_set_v2(
  52. params: domain_devices.ACATAHSupplyAirTempSetRequest,
  53. ):
  54. supply_air_temperature_set = build_acatah_supply_air_temperature_set(params)
  55. logger.info(params)
  56. logger.info(
  57. f"{params.device_id}: supply_air_temperature_set - {supply_air_temperature_set}"
  58. )
  59. resp = {"supply_air_temperature_set": supply_air_temperature_set}
  60. return resp
  61. @router.post(
  62. "/instructions/acatah/freq-set", response_model=domain_devices.ACATAHFreqSetResponse
  63. )
  64. async def get_acatah_freq_set(params: domain_devices.ACATAHFreqSetRequest):
  65. freq_set = await build_acatah_freq_set(params)
  66. logger.info(params)
  67. logger.info(f"{params.device_id}: freq set - {freq_set}")
  68. resp = {"freq_set": freq_set}
  69. return resp
  70. @router.post(
  71. "/instructions/acatah/switch-set",
  72. response_model=domain_devices.ACATAHSwitchSetResponse,
  73. )
  74. async def get_acatah_switch_set(params: domain_devices.ACATAHSwitchSetRequest):
  75. switch_set = await build_acatah_switch_set(params)
  76. logger.info(params)
  77. logger.info(f"{params.device_id}: switch set - {switch_set}")
  78. resp = {"switch_set": switch_set}
  79. return resp
  80. @router.post(
  81. "/instructions/acatah", response_model=domain_devices.ACATAHInstructionsResponse
  82. )
  83. async def get_acatah_instructions(params: domain_devices.ACATAHInstructionsRequest):
  84. instructions = await build_acatah_instructions(params)
  85. logger.info(f"{params.device_id}: {instructions}")
  86. return instructions
  87. @router.post(
  88. "/instructions/acvtsf/switch-set",
  89. response_model=domain_devices.ACVTSFSwitchSetResponse,
  90. )
  91. async def get_acvtsf_switch_set(params: domain_devices.ACVTSFSwitchSetRequest):
  92. switch_set = await build_acvtsf_switch_set(params)
  93. logger.info(params)
  94. logger.info(f"{params.device_id}: switch set - {switch_set}")
  95. resp = {"switch_set": switch_set}
  96. return resp
  97. @router.post(
  98. "/instructions/acatfu/switch-set",
  99. response_model=domain_devices.ACATFUSwitchSetResponse,
  100. )
  101. async def get_acatfu_switch_set(params: domain_devices.ACATFUSwitchSetRequest):
  102. switch_set = await build_acatfu_switch_set(params)
  103. logger.info(params)
  104. logger.info(f"{params.device_id}: switch set - {switch_set}")
  105. resp = {"switch_set": switch_set}
  106. return resp
  107. @router.post("/instructions/acatfu/switch-set/co2", response_model=domain_devices.ACATFUSwitchSetResponse)
  108. async def get_acatfu_co2_switch(params: domain_devices.ACATFUCO2SwitchSetRequest):
  109. switch_set = await build_co2_acatfu_switch(params)
  110. logger.info(params)
  111. logger.info(f"{params.device_id}: switch set - {switch_set}")
  112. resp = {"switch_set": switch_set}
  113. return resp
  114. @router.post(
  115. "/instructions/acatfu/supply-air-temperature-set",
  116. response_model=domain_devices.ACATFUSupplyAirTempSetResponse,
  117. )
  118. async def get_acatfu_supply_air_temperature_set_v2(
  119. params: domain_devices.ACATFUSupplyAirTempSetRequest,
  120. ):
  121. supply_air_temperature_set = build_acatfu_supply_air_temperature(params)
  122. logger.info(supply_air_temperature_set)
  123. resp = {"supply_air_temperature_set": supply_air_temperature_set}
  124. return resp
  125. @router.post(
  126. "/instructions/acatfu/freq-set", response_model=domain_devices.ACATFUFreqSetResponse
  127. )
  128. async def get_acatfu_freq_set(params: domain_devices.ACATFUFreqSetRequest):
  129. freq_set = await build_acatfu_freq_set(params)
  130. logger.info(f"{params.device_id} - {freq_set}")
  131. resp = {"freq_set": freq_set}
  132. return resp
  133. @router.post(
  134. "/prediction/acatfc/early-start",
  135. response_model=domain_devices.ACATFCEarlyStartPredictionResponse,
  136. )
  137. async def get_acatfc_early_start_prediction(
  138. params: domain_devices.ACATFCEarlyStartPredictionRequest,
  139. db: Session = Depends(get_db),
  140. ):
  141. minutes = await build_acatfc_early_start_prediction(params, db)
  142. logger.info(params)
  143. logger.info(f"{params.space_id} - {minutes}")
  144. resp = {"minutes": minutes}
  145. return resp
  146. @router.post(
  147. "/instructions/acatvi", response_model=domain_devices.ACATVIInstructionsResponse
  148. )
  149. async def get_acatvi_instructions(
  150. params: domain_devices.ACATVIInstructionsRequest, db: Session = Depends(get_db)
  151. ):
  152. instructions = await build_acatvi_instructions(params)
  153. logger.info(params)
  154. logger.info(f"{params.device_id} - {instructions}")
  155. return instructions
  156. @router.post(
  157. "/instructions/acatvi/mode", response_model=domain_devices.ACATVIModeResponse
  158. )
  159. async def get_acatvi_mode(params: domain_devices.ACATVIModeRequest):
  160. new_mode = await build_acatvi_mode(params)
  161. logger.info(params)
  162. logger.info(
  163. f"floor space temperature list: {params.space_temperature_list}"
  164. f"- new mode: {new_mode}"
  165. )
  166. return {"mode": new_mode}
  167. @router.post(
  168. "/instructions/acatfc", response_model=domain_devices.ACATFCInstructionsResponse
  169. )
  170. async def get_acatfc_instructions(params: domain_devices.ACATFC2InstructionsRequest):
  171. instructions = await build_acatfc2_instructions(params)
  172. logger.info(params)
  173. logger.info(f"{params.device_id} - {instructions}")
  174. response = domain_devices.ACATFCInstructionsResponse(
  175. onOff=instructions.switch_set,
  176. speed=instructions.speed_set,
  177. temperature=instructions.temperature_set,
  178. mode=instructions.mode_set,
  179. water=instructions.water_valve_switch_set,
  180. )
  181. return response
  182. @router.post(
  183. "/instructions/acatfc4", response_model=domain_devices.ACATFCInstructionsResponse
  184. )
  185. async def get_acatfc4_instructions(params: domain_devices.ACATFC4InstructionsRequest):
  186. instructions = await build_acatfc4_instructions(params)
  187. logger.info(params)
  188. logger.info(f"{params.device_id} - {instructions}")
  189. response = domain_devices.ACATFCInstructionsResponse(
  190. onOff=instructions.switch_set,
  191. speed=instructions.speed_set,
  192. temperature=instructions.temperature_set,
  193. mode=instructions.mode_set,
  194. water=instructions.water_valve_switch_set,
  195. )
  196. return response
  197. @router.post(
  198. "/instructions/acatfc2/v2",
  199. response_model=domain_devices.ACATFC2InstructionsResponse,
  200. )
  201. async def get_acatfc2_instruction_v2(params: domain_devices.ACATFC2InstructionsRequest):
  202. instructions = await build_acatfc2_instructions_v2(params)
  203. logger.info(params)
  204. logger.info(f"{params.device_id} - {instructions}")
  205. return instructions
  206. @router.post(
  207. "/instructions/acatfc4/v2",
  208. response_model=domain_devices.ACATFC4InstructionsResponse,
  209. )
  210. async def get_acatfc4_instructions_v2(params: domain_devices.ACATFC4InstructionsRequest):
  211. instructions = await build_acatfc4_instructions_v2(params)
  212. logger.info(params)
  213. logger.info(f"{params.device_id} - {instructions}")
  214. return instructions
  215. @router.post(
  216. "/instructions/acatva", response_model=domain_devices.ACATVAInstructionsResponse
  217. )
  218. async def get_acatva_instructions(params: domain_devices.ACATVAInstructionsRequest):
  219. instructions = await build_acatva_instructions(params)
  220. logger.info(params)
  221. logger.info(f"{params.device_id} - {instructions}")
  222. resp = dict()
  223. if not np.isnan(instructions.supply_air_flow_set):
  224. resp.update({"SupplyAirFlowSet": instructions.supply_air_flow_set})
  225. if not np.isnan(instructions.virtual_realtime_temperature):
  226. resp.update(
  227. {"VirtualRealtimeTemperature": instructions.virtual_realtime_temperature}
  228. )
  229. if not np.isnan(instructions.virtual_temperature_target_set):
  230. resp.update(
  231. {"TargetTemperatureSet": instructions.virtual_temperature_target_set}
  232. )
  233. return resp
  234. @router.post(
  235. "/instructions/acatva/v2",
  236. response_model=domain_devices.ACATVAInstructionsResponseV2,
  237. )
  238. async def get_acatva_instructions_v2(
  239. params: domain_devices.ACATVAInstructionsRequestV2,
  240. ):
  241. instructions = await build_acatva_instructions_for_JM(params)
  242. logger.info(params)
  243. logger.info(f"{params.device_id} - {instructions}")
  244. return instructions