✨ Adds promise based request reponse system
This commit is contained in:
@@ -6,13 +6,12 @@
|
||||
|
||||
/* eslint-disable */
|
||||
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
|
||||
import { messageTypeRegistry } from "./typeRegistry";
|
||||
import { KnownNetworkItem } from "./websocket_message";
|
||||
import type { FileDescriptorProto } from "ts-proto-descriptors";
|
||||
import { KnownNetworkItem, protoMetadata as protoMetadata1 } from "./websocket_message";
|
||||
|
||||
export const protobufPackage = "rest_message";
|
||||
|
||||
export interface WifiStatus {
|
||||
$type: "rest_message.WifiStatus";
|
||||
status: number;
|
||||
localIp: string;
|
||||
macAddress: string;
|
||||
@@ -27,7 +26,6 @@ export interface WifiStatus {
|
||||
}
|
||||
|
||||
export interface WifiSettings {
|
||||
$type: "rest_message.WifiSettings";
|
||||
hostname: string;
|
||||
priorityRssi: boolean;
|
||||
wifiNetworks: KnownNetworkItem[];
|
||||
@@ -35,7 +33,6 @@ export interface WifiSettings {
|
||||
|
||||
function createBaseWifiStatus(): WifiStatus {
|
||||
return {
|
||||
$type: "rest_message.WifiStatus",
|
||||
status: 0,
|
||||
localIp: "",
|
||||
macAddress: "",
|
||||
@@ -50,9 +47,7 @@ function createBaseWifiStatus(): WifiStatus {
|
||||
};
|
||||
}
|
||||
|
||||
export const WifiStatus: MessageFns<WifiStatus, "rest_message.WifiStatus"> = {
|
||||
$type: "rest_message.WifiStatus" as const,
|
||||
|
||||
export const WifiStatus: MessageFns<WifiStatus> = {
|
||||
encode(message: WifiStatus, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
|
||||
if (message.status !== 0) {
|
||||
writer.uint32(8).int32(message.status);
|
||||
@@ -196,7 +191,6 @@ export const WifiStatus: MessageFns<WifiStatus, "rest_message.WifiStatus"> = {
|
||||
|
||||
fromJSON(object: any): WifiStatus {
|
||||
return {
|
||||
$type: WifiStatus.$type,
|
||||
status: isSet(object.status) ? globalThis.Number(object.status) : 0,
|
||||
localIp: isSet(object.localIp) ? globalThis.String(object.localIp) : "",
|
||||
macAddress: isSet(object.macAddress) ? globalThis.String(object.macAddress) : "",
|
||||
@@ -249,10 +243,10 @@ export const WifiStatus: MessageFns<WifiStatus, "rest_message.WifiStatus"> = {
|
||||
return obj;
|
||||
},
|
||||
|
||||
create(base?: DeepPartial<WifiStatus>): WifiStatus {
|
||||
return WifiStatus.fromPartial(base ?? {});
|
||||
create<I extends Exact<DeepPartial<WifiStatus>, I>>(base?: I): WifiStatus {
|
||||
return WifiStatus.fromPartial(base ?? ({} as any));
|
||||
},
|
||||
fromPartial(object: DeepPartial<WifiStatus>): WifiStatus {
|
||||
fromPartial<I extends Exact<DeepPartial<WifiStatus>, I>>(object: I): WifiStatus {
|
||||
const message = createBaseWifiStatus();
|
||||
message.status = object.status ?? 0;
|
||||
message.localIp = object.localIp ?? "";
|
||||
@@ -269,15 +263,11 @@ export const WifiStatus: MessageFns<WifiStatus, "rest_message.WifiStatus"> = {
|
||||
},
|
||||
};
|
||||
|
||||
messageTypeRegistry.set(WifiStatus.$type, WifiStatus);
|
||||
|
||||
function createBaseWifiSettings(): WifiSettings {
|
||||
return { $type: "rest_message.WifiSettings", hostname: "", priorityRssi: false, wifiNetworks: [] };
|
||||
return { hostname: "", priorityRssi: false, wifiNetworks: [] };
|
||||
}
|
||||
|
||||
export const WifiSettings: MessageFns<WifiSettings, "rest_message.WifiSettings"> = {
|
||||
$type: "rest_message.WifiSettings" as const,
|
||||
|
||||
export const WifiSettings: MessageFns<WifiSettings> = {
|
||||
encode(message: WifiSettings, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
|
||||
if (message.hostname !== "") {
|
||||
writer.uint32(10).string(message.hostname);
|
||||
@@ -333,7 +323,6 @@ export const WifiSettings: MessageFns<WifiSettings, "rest_message.WifiSettings">
|
||||
|
||||
fromJSON(object: any): WifiSettings {
|
||||
return {
|
||||
$type: WifiSettings.$type,
|
||||
hostname: isSet(object.hostname) ? globalThis.String(object.hostname) : "",
|
||||
priorityRssi: isSet(object.priorityRssi) ? globalThis.Boolean(object.priorityRssi) : false,
|
||||
wifiNetworks: globalThis.Array.isArray(object?.wifiNetworks)
|
||||
@@ -356,10 +345,10 @@ export const WifiSettings: MessageFns<WifiSettings, "rest_message.WifiSettings">
|
||||
return obj;
|
||||
},
|
||||
|
||||
create(base?: DeepPartial<WifiSettings>): WifiSettings {
|
||||
return WifiSettings.fromPartial(base ?? {});
|
||||
create<I extends Exact<DeepPartial<WifiSettings>, I>>(base?: I): WifiSettings {
|
||||
return WifiSettings.fromPartial(base ?? ({} as any));
|
||||
},
|
||||
fromPartial(object: DeepPartial<WifiSettings>): WifiSettings {
|
||||
fromPartial<I extends Exact<DeepPartial<WifiSettings>, I>>(object: I): WifiSettings {
|
||||
const message = createBaseWifiSettings();
|
||||
message.hostname = object.hostname ?? "";
|
||||
message.priorityRssi = object.priorityRssi ?? false;
|
||||
@@ -368,26 +357,270 @@ export const WifiSettings: MessageFns<WifiSettings, "rest_message.WifiSettings">
|
||||
},
|
||||
};
|
||||
|
||||
messageTypeRegistry.set(WifiSettings.$type, WifiSettings);
|
||||
type ProtoMetaMessageOptions = {
|
||||
options?: { [key: string]: any };
|
||||
fields?: { [key: string]: { [key: string]: any } };
|
||||
oneof?: { [key: string]: { [key: string]: any } };
|
||||
nested?: { [key: string]: ProtoMetaMessageOptions };
|
||||
};
|
||||
|
||||
export interface ProtoMetadata {
|
||||
fileDescriptor: FileDescriptorProto;
|
||||
references: { [key: string]: any };
|
||||
dependencies?: ProtoMetadata[];
|
||||
options?: {
|
||||
options?: { [key: string]: any };
|
||||
services?: {
|
||||
[key: string]: { options?: { [key: string]: any }; methods?: { [key: string]: { [key: string]: any } } };
|
||||
};
|
||||
messages?: { [key: string]: ProtoMetaMessageOptions };
|
||||
enums?: { [key: string]: { options?: { [key: string]: any }; values?: { [key: string]: { [key: string]: any } } } };
|
||||
};
|
||||
}
|
||||
|
||||
export const protoMetadata: ProtoMetadata = {
|
||||
fileDescriptor: {
|
||||
"name": "rest_message.proto",
|
||||
"package": "rest_message",
|
||||
"dependency": ["websocket_message.proto"],
|
||||
"publicDependency": [],
|
||||
"weakDependency": [],
|
||||
"optionDependency": [],
|
||||
"messageType": [{
|
||||
"name": "WifiStatus",
|
||||
"field": [{
|
||||
"name": "status",
|
||||
"number": 1,
|
||||
"label": 1,
|
||||
"type": 5,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "status",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "local_ip",
|
||||
"number": 2,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "localIp",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "mac_address",
|
||||
"number": 3,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "macAddress",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "rssi",
|
||||
"number": 4,
|
||||
"label": 1,
|
||||
"type": 2,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "rssi",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "ssid",
|
||||
"number": 5,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "ssid",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "bssid",
|
||||
"number": 6,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "bssid",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "channel",
|
||||
"number": 7,
|
||||
"label": 1,
|
||||
"type": 13,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "channel",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "subnet_mask",
|
||||
"number": 8,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "subnetMask",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "gateway_ip",
|
||||
"number": 9,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "gatewayIp",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "dns_ip_1",
|
||||
"number": 10,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "dnsIp1",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "dns_ip_2",
|
||||
"number": 11,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "dnsIp2",
|
||||
"options": undefined,
|
||||
"proto3Optional": true,
|
||||
}],
|
||||
"extension": [],
|
||||
"nestedType": [],
|
||||
"enumType": [],
|
||||
"extensionRange": [],
|
||||
"oneofDecl": [{ "name": "_dns_ip_2", "options": undefined }],
|
||||
"options": undefined,
|
||||
"reservedRange": [],
|
||||
"reservedName": [],
|
||||
"visibility": 0,
|
||||
}, {
|
||||
"name": "WifiSettings",
|
||||
"field": [{
|
||||
"name": "hostname",
|
||||
"number": 1,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "hostname",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "priority_rssi",
|
||||
"number": 2,
|
||||
"label": 1,
|
||||
"type": 8,
|
||||
"typeName": "",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "priorityRssi",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}, {
|
||||
"name": "wifi_networks",
|
||||
"number": 3,
|
||||
"label": 3,
|
||||
"type": 11,
|
||||
"typeName": ".socket_message.KnownNetworkItem",
|
||||
"extendee": "",
|
||||
"defaultValue": "",
|
||||
"oneofIndex": 0,
|
||||
"jsonName": "wifiNetworks",
|
||||
"options": undefined,
|
||||
"proto3Optional": false,
|
||||
}],
|
||||
"extension": [],
|
||||
"nestedType": [],
|
||||
"enumType": [],
|
||||
"extensionRange": [],
|
||||
"oneofDecl": [],
|
||||
"options": undefined,
|
||||
"reservedRange": [],
|
||||
"reservedName": [],
|
||||
"visibility": 0,
|
||||
}],
|
||||
"enumType": [],
|
||||
"service": [],
|
||||
"extension": [],
|
||||
"options": undefined,
|
||||
"sourceCodeInfo": {
|
||||
"location": [{
|
||||
"path": [2],
|
||||
"span": [5, 0, 21],
|
||||
"leadingComments":
|
||||
' Note: This is most likely a "temporary" proto that will be redone, as these endpoints are static for the esp32, which means we are forced to use WiFi for communication\n',
|
||||
"trailingComments": "",
|
||||
"leadingDetachedComments": [],
|
||||
}],
|
||||
},
|
||||
"syntax": "proto3",
|
||||
"edition": 0,
|
||||
},
|
||||
references: { ".rest_message.WifiStatus": WifiStatus, ".rest_message.WifiSettings": WifiSettings },
|
||||
dependencies: [protoMetadata1],
|
||||
};
|
||||
|
||||
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
|
||||
|
||||
export type DeepPartial<T> = T extends Builtin ? T
|
||||
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
|
||||
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
|
||||
: T extends {} ? { [K in Exclude<keyof T, "$type">]?: DeepPartial<T[K]> }
|
||||
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
|
||||
: Partial<T>;
|
||||
|
||||
type KeysOfUnion<T> = T extends T ? keyof T : never;
|
||||
export type Exact<P, I extends P> = P extends Builtin ? P
|
||||
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
|
||||
|
||||
function isSet(value: any): boolean {
|
||||
return value !== null && value !== undefined;
|
||||
}
|
||||
|
||||
export interface MessageFns<T, V extends string> {
|
||||
readonly $type: V;
|
||||
export interface MessageFns<T> {
|
||||
encode(message: T, writer?: BinaryWriter): BinaryWriter;
|
||||
decode(input: BinaryReader | Uint8Array, length?: number): T;
|
||||
fromJSON(object: any): T;
|
||||
toJSON(message: T): unknown;
|
||||
create(base?: DeepPartial<T>): T;
|
||||
fromPartial(object: DeepPartial<T>): T;
|
||||
create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
|
||||
fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,10 @@
|
||||
import { writable } from 'svelte/store'
|
||||
import {
|
||||
WebsocketMessage,
|
||||
type MessageFns,
|
||||
protoMetadata as websocket_md
|
||||
CorrelationRequest,
|
||||
CorrelationResponse,
|
||||
protoMetadata as websocket_md,
|
||||
type MessageFns
|
||||
} from '$lib/platform_shared/websocket_message'
|
||||
import * as WebsocketMessages from '$lib/platform_shared/websocket_message'
|
||||
|
||||
@@ -11,6 +13,13 @@ export const MESSAGE_TYPE_TO_TAG = new Map<MessageFns<unknown>, number>()
|
||||
export const MESSAGE_KEY_TO_TAG = new Map<string, number>()
|
||||
export const MESSAGE_TAG_TO_KEY = new Map<number, string>()
|
||||
|
||||
type CorrelationRequestData = Omit<CorrelationRequest, 'correlationId'>
|
||||
type PendingRequest = {
|
||||
resolve: (response: CorrelationResponse) => void
|
||||
reject: (error: Error) => void
|
||||
timeoutId: ReturnType<typeof setTimeout>
|
||||
}
|
||||
|
||||
const websocketMessageType = websocket_md.fileDescriptor.messageType?.find(
|
||||
(msg: { name: string }) => msg.name === 'WebsocketMessage'
|
||||
)
|
||||
@@ -75,8 +84,11 @@ export const encodeMessage = (data: WebsocketMessage): Uint8Array<ArrayBuffer> =
|
||||
function createWebSocket() {
|
||||
const message_listeners = new Map<number, Set<(data?: unknown) => void>>()
|
||||
const event_listeners = new Map<string, Set<(data?: unknown) => void>>()
|
||||
const pending_requests = new Map<number, PendingRequest>()
|
||||
const { subscribe, set } = writable(false)
|
||||
const reconnectTimeoutTime = 500000
|
||||
const requestTimeoutTime = 10000
|
||||
let correlationIdCounter = 0
|
||||
let unresponsiveTimeoutId: ReturnType<typeof setTimeout>
|
||||
let reconnectTimeoutId: ReturnType<typeof setTimeout>
|
||||
let ws: WebSocket
|
||||
@@ -109,9 +121,20 @@ function createWebSocket() {
|
||||
ws.onmessage = frame => {
|
||||
resetUnresponsiveCheck()
|
||||
const { tag, msg } = decodeMessage(frame.data)
|
||||
if (msg.correlationResponse) {
|
||||
const pending = pending_requests.get(msg.correlationResponse.correlationId)
|
||||
if (pending) {
|
||||
clearTimeout(pending.timeoutId)
|
||||
pending_requests.delete(msg.correlationResponse.correlationId)
|
||||
pending.resolve(msg.correlationResponse)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (tag) {
|
||||
const key = MESSAGE_TAG_TO_KEY.get(tag)!
|
||||
message_listeners.get(tag)?.forEach(listener => listener(msg[key as keyof typeof msg]))
|
||||
message_listeners
|
||||
.get(tag)
|
||||
?.forEach(listener => listener(msg[key as keyof typeof msg]))
|
||||
}
|
||||
}
|
||||
ws.onerror = ev => disconnect('error', ev)
|
||||
@@ -211,6 +234,25 @@ function createWebSocket() {
|
||||
return () => {
|
||||
unsubscribe_event(event_type, listener)
|
||||
}
|
||||
},
|
||||
request: (data: CorrelationRequestData): Promise<CorrelationResponse> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||||
reject(new Error('WebSocket not connected'))
|
||||
return
|
||||
}
|
||||
|
||||
const correlationId = ++correlationIdCounter
|
||||
const timeoutId = setTimeout(() => {
|
||||
pending_requests.delete(correlationId)
|
||||
reject(new Error(`Request timeout (id: ${correlationId})`))
|
||||
}, requestTimeoutTime)
|
||||
|
||||
pending_requests.set(correlationId, { resolve, reject, timeoutId })
|
||||
|
||||
const request = CorrelationRequest.create({ correlationId, ...data })
|
||||
send(WebsocketMessage.create({ correlationRequest: request }))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,45 +4,23 @@
|
||||
import { socket } from '$lib/stores'
|
||||
import { Connection } from '$lib/components/icons'
|
||||
import I2CSetting from './i2cSetting.svelte'
|
||||
import {
|
||||
I2CDevice,
|
||||
I2CScanData,
|
||||
I2CScanDataRequest
|
||||
} from '$lib/platform_shared/websocket_message'
|
||||
|
||||
// TODO: Delete this completely, this should be done on esp side, as it decides what addresses are actually valid, as for example ICM20948 and MPU6050 can have same address
|
||||
// const i2cDevices = [
|
||||
// { address: 30, part_number: 'HMC5883', name: '3-Axis Digital Compass/Magnetometer IC' },
|
||||
// { address: 41, part_number: 'BNO055', name: '9-Axis Absolute Orientation Sensor' },
|
||||
// { address: 64, part_number: 'PCA9685', name: '16-channel PWM driver default address' },
|
||||
// { address: 72, part_number: 'ADS1115', name: '4-channel 16-bit ADC' },
|
||||
// {
|
||||
// address: 104,
|
||||
// part_number: 'MPU6050',
|
||||
// name: 'Six-Axis (Gyro + Accelerometer) MEMS MotionTracking™ Devices'
|
||||
// },
|
||||
// { address: 115, part_number: 'PAJ7620U2', name: 'Gesture sensor' },
|
||||
// { address: 119, part_number: 'BMP085', name: 'Temp/Barometric' }
|
||||
// ]
|
||||
import type { I2CDevice } from '$lib/platform_shared/websocket_message'
|
||||
|
||||
let active_devices: I2CDevice[] = $state([])
|
||||
|
||||
let isLoading = $state(false)
|
||||
|
||||
onMount(() => {
|
||||
const unsub = socket.on(I2CScanData, handleScan)
|
||||
triggerScan()
|
||||
return () => unsub
|
||||
})
|
||||
|
||||
const handleScan = (data: I2CScanData) => {
|
||||
active_devices = data.devices
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
const triggerScan = () => {
|
||||
const triggerScan = async () => {
|
||||
isLoading = true
|
||||
socket.sendEvent(I2CScanDataRequest, {})
|
||||
try {
|
||||
const response = await socket.request({ i2cScanDataRequest: {} })
|
||||
active_devices = response.i2cScanData?.devices ?? []
|
||||
} finally {
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user