feat: Refactor BedHardware to use a singleton instance and remove serial implementation
This commit is contained in:
parent
fb87e74ec9
commit
fd8cacd62b
4 changed files with 25 additions and 54 deletions
|
@ -1,19 +1,15 @@
|
|||
import { EventEmitter } from 'events';
|
||||
import { IBedHardware, PinState, PinChange, BedHardwareConfig } from '../types/bedhardware';
|
||||
import { BedHardwareSerial } from './BedHardwareSerial';
|
||||
import { BedHardwareMQTT } from './BedHardwareMQTT';
|
||||
|
||||
/**
|
||||
* BedHardware - Factory class for creating bed hardware implementations
|
||||
* BedHardware - MQTT-based bed hardware implementation
|
||||
*
|
||||
* Usage:
|
||||
* // MQTT (connects to broker.hivemq.com with base topic /Jtkcp2N/pressurebed/)
|
||||
* MQTT (connects to broker.hivemq.com with base topic /Jtkcp2N/pressurebed/)
|
||||
* const hardware = BedHardware.createSimpleMQTT();
|
||||
*
|
||||
* // Serial
|
||||
* const hardware = BedHardware.createSerial('COM3', 9600);
|
||||
*
|
||||
* // With custom topics
|
||||
* With custom topics
|
||||
* const hardware = BedHardware.createMQTT({
|
||||
* topics: {
|
||||
* pinState: '/custom/pin/state',
|
||||
|
@ -24,18 +20,10 @@ import { BedHardwareMQTT } from './BedHardwareMQTT';
|
|||
*/
|
||||
export class BedHardware extends EventEmitter implements IBedHardware {
|
||||
private implementation: IBedHardware;
|
||||
|
||||
constructor(config: BedHardwareConfig) {
|
||||
super();
|
||||
|
||||
if (config.type === 'serial') {
|
||||
if (!config.serial?.portPath) {
|
||||
throw new Error('Serial port path is required for serial connection');
|
||||
}
|
||||
this.implementation = new BedHardwareSerial(
|
||||
config.serial.portPath,
|
||||
config.serial.baudRate || 9600 );
|
||||
} else if (config.type === 'mqtt') {
|
||||
if (config.type === 'mqtt') {
|
||||
this.implementation = new BedHardwareMQTT({
|
||||
topics: config.mqtt?.topics
|
||||
});
|
||||
|
@ -88,14 +76,8 @@ export class BedHardware extends EventEmitter implements IBedHardware {
|
|||
isConnected(): boolean {
|
||||
return this.implementation.isConnected();
|
||||
}
|
||||
|
||||
// Static factory methods for convenience
|
||||
static createSerial(portPath: string, baudRate: number = 9600): BedHardware {
|
||||
return new BedHardware({
|
||||
type: 'serial',
|
||||
serial: { portPath, baudRate }
|
||||
});
|
||||
} static createMQTT(config?: {
|
||||
static createMQTT(config?: {
|
||||
topics?: {
|
||||
pinState: string;
|
||||
pinChange: string;
|
||||
|
@ -114,13 +96,12 @@ export class BedHardware extends EventEmitter implements IBedHardware {
|
|||
mqtt: {}
|
||||
});
|
||||
}
|
||||
|
||||
// Static method to list available serial ports (for serial implementation)
|
||||
static async listSerialPorts(): Promise<string[]> {
|
||||
return BedHardwareSerial.listPorts();
|
||||
}
|
||||
}
|
||||
|
||||
// Export all classes for direct access if needed
|
||||
export { BedHardwareSerial, BedHardwareMQTT };
|
||||
// Create and export a default MQTT instance
|
||||
export const bedHardwareInstance = new BedHardware({
|
||||
type: 'mqtt',
|
||||
mqtt: {}
|
||||
});
|
||||
|
||||
export * from '../types/bedhardware';
|
Loading…
Add table
Add a link
Reference in a new issue