From e4bafbe2a341ce33fbfb485c1878226653375983 Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Fri, 18 Jul 2025 11:10:54 +0800 Subject: refactor command IDs --- packages/airmx/src/airmx.ts | 5 ++--- packages/airmx/src/eagle.ts | 8 ++++++-- packages/airmx/src/snow.ts | 9 ++++++--- packages/airmx/src/types.ts | 5 ----- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/airmx/src/airmx.ts b/packages/airmx/src/airmx.ts index 5b397db..8c30049 100644 --- a/packages/airmx/src/airmx.ts +++ b/packages/airmx/src/airmx.ts @@ -9,7 +9,6 @@ import type { EagleListener, EagleControlData, } from './types.js' -import { Command } from './types.js' import { Signer } from './util.js' import type { CommandMessage } from './messages.js' @@ -111,10 +110,10 @@ export class Airmx { this.#validateMessage(t.deviceId, str, data.sig) switch (data.cmdId) { - case Command.SnowStatus: + case SnowStatus.commandId(): this.#notifySnow(SnowStatus.from(t.deviceId, data)) break - case Command.EagleStatus: + case EagleStatus.commandId(): this.#notifyEagle(EagleStatus.from(t.deviceId, data)) break } diff --git a/packages/airmx/src/eagle.ts b/packages/airmx/src/eagle.ts index b3f28e3..124222b 100644 --- a/packages/airmx/src/eagle.ts +++ b/packages/airmx/src/eagle.ts @@ -7,9 +7,13 @@ export class EagleStatus { public readonly message: Message, ) {} + static commandId() { + return 210 + } + static from(deviceId: number, message: Message) { - if (message.cmdId !== 210) { - throw new Error('Eagle status expects a message with command ID 210.') + if (message.cmdId !== this.commandId()) { + throw new Error(`Eagle status expects a message with command ID ${this.commandId()}.`) } return new this(deviceId, message) diff --git a/packages/airmx/src/snow.ts b/packages/airmx/src/snow.ts index 38242fa..3517dcd 100644 --- a/packages/airmx/src/snow.ts +++ b/packages/airmx/src/snow.ts @@ -1,5 +1,4 @@ import { - Command, type Message, type SnowStatusData, BatteryState, @@ -14,10 +13,14 @@ export class SnowStatus { // } + static commandId() { + return 200 + } + static from(deviceId: number, message: Message) { - if (message.cmdId !== Command.SnowStatus) { + if (message.cmdId !== this.commandId()) { throw new Error( - `Snow status expects a message with command ID "${Command.SnowStatus}".`, + `Snow status expects a message with command ID "${this.commandId()}".`, ) } diff --git a/packages/airmx/src/types.ts b/packages/airmx/src/types.ts index 6d6bbc7..ef7ab89 100644 --- a/packages/airmx/src/types.ts +++ b/packages/airmx/src/types.ts @@ -15,11 +15,6 @@ export interface Device { export type SnowListener = (status: SnowStatus) => void export type EagleListener = (status: EagleStatus) => void -export enum Command { - SnowStatus = 200, - EagleStatus = 210, -} - export enum MessageSource { Snow = 1, Eagle = 2, -- cgit v1.2.3