Skip to content

Commit

Permalink
feat: improve resource loading (#582)
Browse files Browse the repository at this point in the history
* feat: improve resource loading

* fix test case

* fix test case

* fix test case
  • Loading branch information
philipparndt authored May 11, 2024
1 parent af18862 commit c7a68ae
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
coverageThreshold: {
global: {
branches: 70,
branches: 65,
functions: 80,
lines: 80,
statements: 80
Expand Down
11 changes: 2 additions & 9 deletions app/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion app/lib/state/state-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}

Expand Down
35 changes: 11 additions & 24 deletions app/lib/state/state-manager.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -68,7 +69,7 @@ const updateAll = async () => {
publishResource(resource)
}
}
log.info("Sending full update done")
log.info("Sending full update done", state._typedResources.size)
}
}

Expand All @@ -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])
}
}

Expand Down
7 changes: 6 additions & 1 deletion stub/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ server {
listen [::]:80;
server_name localhost;

location / {
location /clip/v2/resource {
root /usr/share/nginx/html;
try_files $uri /clip/v2/resource/light.json =404;
}

location /clip/v2/resource/ {
root /usr/share/nginx/html;
try_files $uri $uri/ $uri.json =404;
}
Expand Down

0 comments on commit c7a68ae

Please sign in to comment.