devices.py 12 KB

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