2025-10-28 01:11:57 +00:00
|
|
|
import type { ZigbeeDeviceName, ZigbeeDeviceStates } from "@eva/zigbee"
|
2025-10-30 00:13:08 +00:00
|
|
|
import { nanoid } from "nanoid"
|
|
|
|
|
|
|
|
|
|
export type JrpcRequestId = string & { __brand: "JrpcRequestId" }
|
2025-10-28 01:11:57 +00:00
|
|
|
|
|
|
|
|
export type JrpcSchema = {
|
2025-10-30 00:13:08 +00:00
|
|
|
subscribeToDevice(p: { deviceName: ZigbeeDeviceName }): true
|
|
|
|
|
unsubscribeFromDevice(p: { deviceName: ZigbeeDeviceName }): true
|
|
|
|
|
setDeviceState(p: { deviceName: ZigbeeDeviceName; state: unknown }): true
|
|
|
|
|
showDeviceState<DeviceName extends ZigbeeDeviceName>(
|
|
|
|
|
p: { [K in ZigbeeDeviceName]: { deviceName: K; state: ZigbeeDeviceStates[K] } }[DeviceName],
|
|
|
|
|
): ZigbeeDeviceStates[ZigbeeDeviceName]
|
2025-10-28 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type JrpcRequest<Method extends keyof JrpcSchema = keyof JrpcSchema> = {
|
|
|
|
|
[M in keyof JrpcSchema]: {
|
2025-10-30 00:13:08 +00:00
|
|
|
id: JrpcRequestId
|
2025-10-28 01:11:57 +00:00
|
|
|
jsonrpc: "2.0"
|
|
|
|
|
method: M
|
2025-10-30 00:13:08 +00:00
|
|
|
params: Parameters<JrpcSchema[M]>[0]
|
2025-10-28 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
}[Method]
|
|
|
|
|
|
|
|
|
|
export type JrpcResponse<Method extends keyof JrpcSchema = keyof JrpcSchema> = {
|
|
|
|
|
[M in keyof JrpcSchema]:
|
|
|
|
|
| {
|
2025-10-30 00:13:08 +00:00
|
|
|
id: JrpcRequestId
|
2025-10-28 01:11:57 +00:00
|
|
|
jsonrpc: "2.0"
|
2025-10-30 00:13:08 +00:00
|
|
|
result: ReturnType<JrpcSchema[M]>
|
2025-10-28 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
| {
|
2025-10-30 00:13:08 +00:00
|
|
|
id: JrpcRequestId
|
2025-10-28 01:11:57 +00:00
|
|
|
jsonrpc: "2.0"
|
|
|
|
|
error: string
|
|
|
|
|
}
|
|
|
|
|
}[Method]
|
2025-10-30 00:13:08 +00:00
|
|
|
|
|
|
|
|
export function newJrpcRequestId(): JrpcRequestId {
|
|
|
|
|
return nanoid()
|
|
|
|
|
}
|