From d779d7975570b7e915d06f3273466cd2e4900b40 Mon Sep 17 00:00:00 2001 From: Philipp Arndt <2f.mail@gmx.de> Date: Sat, 11 May 2024 08:53:14 +0200 Subject: [PATCH] feat: improve resource loading --- app/lib/app.ts | 11 ++------- app/lib/state/state-event-handler.ts | 2 +- app/lib/state/state-manager.ts | 35 +++++++++------------------- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/app/lib/app.ts b/app/lib/app.ts index 09944a79..d892da1e 100644 --- a/app/lib/app.ts +++ b/app/lib/app.ts @@ -2,25 +2,18 @@ import { connectMqtt } from "./mqtt/mqtt-client" import { destroySSE, initSSE } from "./SSEClient" import { log } from "./logger" import cron from "node-cron" -import { initStateManagerFromHue } from "./state/state-manager" -import { loadAllResources } from "./api/v2/hue-api-v2" +import { initStateManagerFromHue, state } from "./state/state-manager" export const triggerFullUpdate = async () => { log.info("Updating devices") await initStateManagerFromHue() - log.info("Updating devices done") + log.info("Updating devices done", `${state._typedResources.size} resources`) } export const startApp = async () => { const mqttCleanUp = await connectMqtt() await triggerFullUpdate() - const level = log.level() - if (level === "DEBUG") { - const resources = await loadAllResources() - log.debug("All resources", JSON.stringify(resources)) - } - initSSE() log.info("Scheduling hourly-full-update.") diff --git a/app/lib/state/state-event-handler.ts b/app/lib/state/state-event-handler.ts index 6852116f..9b7cd9c3 100644 --- a/app/lib/state/state-event-handler.ts +++ b/app/lib/state/state-event-handler.ts @@ -32,7 +32,7 @@ const handleResource = (data: HueEventData) => { publishResource(newResource) } else { - log.debug(`No resource found with id ${data.id}`) + log.warn(`No resource found with id ${data.id}`) } } diff --git a/app/lib/state/state-manager.ts b/app/lib/state/state-manager.ts index ccd5032a..315118bb 100644 --- a/app/lib/state/state-manager.ts +++ b/app/lib/state/state-manager.ts @@ -1,6 +1,7 @@ import { isLight } from "../api/v2/types/light" import { isRoom, Room } from "../api/v2/types/room" import { + loadAllResources, loadDevices, loadTyped } from "../api/v2/hue-api-v2" @@ -68,7 +69,7 @@ const updateAll = async () => { publishResource(resource) } } - log.info("Sending full update done") + log.info("Sending full update done", state._typedResources.size) } } @@ -84,29 +85,15 @@ export const initStateManagerFromHue = async () => { state.addTypedResources(rooms.data) } - for (const typeName of [ - "bridge", - "bridge_home", - "button", - "device_power", - "device_power", - "device_software_update", - "entertainment", - "grouped_light", - "light", - "light_level", - "motion", - "relative_rotary", - "temperature", - "zgp_connectivity", - "zigbee_connectivity", - "grouped_motion", - "grouped_light_level", - "scene" - ]) { - const resources = await loadTyped(typeName) - if (resources) { - state.addTypedResources(resources.data) + const resources = await loadAllResources() + if (!resources) { + log.error("Failed to load resources") + process.exit(1) + } + + for (const resource of resources.data) { + if (!isRoom(resource)) { + state.addTypedResources([resource]) } }