fix(dashboard): add more websocket related logs
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 1m8s

This commit is contained in:
2025-10-29 23:10:25 +00:00
parent 866fd0eacc
commit 4478cdbcb3
5 changed files with 56 additions and 8 deletions

View File

@@ -28,12 +28,28 @@ const intermediateBrightnessAtoms = atom({
}) })
function App() { function App() {
const websocket = useRef(new WebSocket(`ws://${import.meta.env.VITE_API_HOST}/api/zigbee`)) const wsProtocol = window.location.protocol === "https:" ? "wss:" : "ws:"
const wsHost = import.meta.env.VITE_API_HOST || window.location.host
const websocket = useRef(new WebSocket(`${wsProtocol}//${wsHost}/api/zigbee`))
const store = useStore() const store = useStore()
useEffect(() => { useEffect(() => {
websocket.current.onmessage = (event) => { const ws = websocket.current
ws.onopen = () => {
console.log("WebSocket connected")
}
ws.onerror = (error) => {
console.error("WebSocket error:", error)
}
ws.onclose = () => {
console.log("WebSocket disconnected")
}
ws.onmessage = (event) => {
const data = JSON.parse(event.data) as JrpcRequest | JrpcResponse const data = JSON.parse(event.data) as JrpcRequest | JrpcResponse
if ("method" in data) { if ("method" in data) {
switch (data.method) { switch (data.method) {
@@ -45,14 +61,22 @@ function App() {
} }
} }
} }
return () => { return () => {
if (websocket.current.readyState === WebSocket.OPEN) { if (ws.readyState === WebSocket.OPEN) {
websocket.current.close() ws.close()
} }
} }
}, [store]) }, [store])
function setBrightness(deviceName: ZigbeeDeviceName, brightness: number) { function setBrightness(deviceName: ZigbeeDeviceName, brightness: number) {
const ws = websocket.current
if (ws.readyState !== WebSocket.OPEN) {
console.warn("WebSocket is not open. Current state:", ws.readyState)
return
}
const request: JrpcRequest<"setDeviceState"> = { const request: JrpcRequest<"setDeviceState"> = {
id: crypto.randomUUID(), id: crypto.randomUUID(),
jsonrpc: "2.0", jsonrpc: "2.0",
@@ -65,7 +89,9 @@ function App() {
: { state: "ON", brightness: Math.round((brightness / 100) * 254) }, : { state: "ON", brightness: Math.round((brightness / 100) * 254) },
}, },
} }
websocket.current.send(JSON.stringify(request))
ws.send(JSON.stringify(request))
console.log("Sent brightness change:", { deviceName, brightness })
} }
return ( return (

View File

@@ -5,7 +5,13 @@
import { queryOptions } from "@tanstack/react-query" import { queryOptions } from "@tanstack/react-query"
const API_BASE_URL = `http://${import.meta.env.VITE_API_HOST || "localhost:8000"}` const getApiBaseUrl = () => {
const protocol = window.location.protocol
const host = import.meta.env.VITE_API_HOST || window.location.host
return `${protocol}//${host}`
}
const API_BASE_URL = getApiBaseUrl()
// System Info // System Info
export interface BeszelSystemInfo { export interface BeszelSystemInfo {

View File

@@ -10,3 +10,7 @@ body {
-moz-osx-font-smoothing: grayscale; -moz-osx-font-smoothing: grayscale;
overscroll-behavior: none; overscroll-behavior: none;
} }
input[type="range"].brightness-slider {
@apply appearance-none w-full bg-transparent;
}

View File

@@ -5,7 +5,13 @@
import { queryOptions } from "@tanstack/react-query" import { queryOptions } from "@tanstack/react-query"
const API_BASE_URL = `http://${import.meta.env.VITE_API_HOST || "localhost:8000"}` const getApiBaseUrl = () => {
const protocol = window.location.protocol
const host = import.meta.env.VITE_API_HOST || window.location.host
return `${protocol}//${host}`
}
const API_BASE_URL = getApiBaseUrl()
// Disruption Summary // Disruption Summary
export interface DisruptionSummary { export interface DisruptionSummary {

View File

@@ -26,7 +26,13 @@ import {
Wind, Wind,
} from "lucide-react" } from "lucide-react"
const API_BASE_URL = `http://${import.meta.env.VITE_API_HOST || "localhost:3000"}` const getApiBaseUrl = () => {
const protocol = window.location.protocol
const host = import.meta.env.VITE_API_HOST || window.location.host
return `${protocol}//${host}`
}
const API_BASE_URL = getApiBaseUrl()
export const DEFAULT_LATITUDE = Number(import.meta.env.VITE_DEFAULT_LATITUDE) || 37.7749 export const DEFAULT_LATITUDE = Number(import.meta.env.VITE_DEFAULT_LATITUDE) || 37.7749
export const DEFAULT_LONGITUDE = Number(import.meta.env.VITE_DEFAULT_LONGITUDE) || -122.4194 export const DEFAULT_LONGITUDE = Number(import.meta.env.VITE_DEFAULT_LONGITUDE) || -122.4194