feat: Refactor BedHardware to support both Serial and MQTT implementations
- Added MQTT support to BedHardware, allowing for connection to MQTT brokers. - Created BedHardwareMQTT and BedHardwareSerial classes to handle respective connections. - Introduced a unified BedHardwareConfig interface for configuration management. - Implemented event forwarding from the underlying implementations to the BedHardware class. - Added MQTT adapter for handling MQTT connections and message subscriptions. - Updated package.json to include the mqtt library as a dependency. - Created a singleton MQTTService for managing MQTT client instances. - Enhanced error handling and logging throughout the BedHardware and MQTT classes.
This commit is contained in:
parent
0c5c7bcb5f
commit
fb87e74ec9
8 changed files with 753 additions and 158 deletions
53
types/bedhardware.ts
Normal file
53
types/bedhardware.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
export interface PinState {
|
||||
pin: number;
|
||||
state: number;
|
||||
name: string;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export interface PinChange {
|
||||
pin: number;
|
||||
previousState: number;
|
||||
currentState: number;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export interface BedHardwareConfig {
|
||||
type: 'serial' | 'mqtt';
|
||||
serial?: {
|
||||
portPath: string;
|
||||
baudRate?: number;
|
||||
};
|
||||
mqtt?: {
|
||||
topics?: {
|
||||
pinState: string;
|
||||
pinChange: string;
|
||||
initialization: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export interface IBedHardware {
|
||||
connect(): Promise<void>;
|
||||
disconnect(): Promise<void>;
|
||||
getPinState(pin: number): PinState | undefined;
|
||||
getAllPinStates(): PinState[];
|
||||
isConnected(): boolean;
|
||||
|
||||
// Event emitter methods on(event: 'connected', listener: () => void): this;
|
||||
on(event: 'disconnected', listener: () => void): this;
|
||||
on(event: 'error', listener: (error: Error) => void): this;
|
||||
on(event: 'initialized', listener: () => void): this;
|
||||
on(event: 'pinInitialized', listener: (pinState: PinState) => void): this;
|
||||
on(event: 'pinChanged', listener: (pinChange: PinChange) => void): this;
|
||||
on(event: string, listener: (...args: unknown[]) => void): this;
|
||||
emit(event: 'connected'): boolean;
|
||||
emit(event: 'disconnected'): boolean;
|
||||
emit(event: 'error', error: Error): boolean;
|
||||
emit(event: 'initialized'): boolean;
|
||||
emit(event: 'pinInitialized', pinState: PinState): boolean;
|
||||
emit(event: 'pinChanged', pinChange: PinChange): boolean;
|
||||
emit(event: string, ...args: unknown[]): boolean;
|
||||
}
|
||||
|
||||
export type BedHardwareType = 'serial' | 'mqtt';
|
Loading…
Add table
Add a link
Reference in a new issue