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,5 +1,5 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { BedHardware, PinState, PinChange } from '@/services/BedHardware';
|
||||
import { bedHardwareInstance, PinState, PinChange } from '@/services/BedHardware';
|
||||
import { SensorDataStorage, SensorDataPoint } from '@/services/SensorDataStorage';
|
||||
import { SensorConfig } from '@/types/sensor';
|
||||
|
||||
|
@ -44,7 +44,6 @@ SENSOR_CONFIG.forEach(sensor => {
|
|||
}
|
||||
});
|
||||
|
||||
let bedHardware: BedHardware | null = null;
|
||||
const sensorDataStorage = SensorDataStorage.getInstance();
|
||||
const sensorData: Record<string, {
|
||||
id: string;
|
||||
|
@ -108,41 +107,33 @@ function generateTimeSeriesData(hours = 1) {
|
|||
|
||||
// Initialize hardware connection
|
||||
async function initializeHardware() {
|
||||
if (bedHardware && isHardwareConnected) return;
|
||||
if (isHardwareConnected) return;
|
||||
|
||||
try {
|
||||
// Try to find available serial ports
|
||||
const availablePorts = await BedHardware.listPorts();
|
||||
const portPath = availablePorts.find(port =>
|
||||
port.includes('ttyUSB') || port.includes('ttyACM') || port.includes('cu.usbmodem')
|
||||
) || '/dev/ttyUSB0'; // Default fallback
|
||||
|
||||
bedHardware = new BedHardware(portPath, 9600);
|
||||
|
||||
bedHardware.on('connected', () => {
|
||||
bedHardwareInstance.on('connected', () => {
|
||||
console.log('BedHardware connected');
|
||||
isHardwareConnected = true;
|
||||
});
|
||||
|
||||
bedHardware.on('disconnected', () => {
|
||||
bedHardwareInstance.on('disconnected', () => {
|
||||
console.log('BedHardware disconnected');
|
||||
isHardwareConnected = false;
|
||||
});
|
||||
|
||||
bedHardware.on('pinChanged', (change: PinChange) => {
|
||||
bedHardwareInstance.on('pinChanged', (change: PinChange) => {
|
||||
updateSensorFromPin(change.pin, change.currentState);
|
||||
});
|
||||
|
||||
bedHardware.on('pinInitialized', (pinState: PinState) => {
|
||||
bedHardwareInstance.on('pinInitialized', (pinState: PinState) => {
|
||||
updateSensorFromPin(pinState.pin, pinState.state);
|
||||
});
|
||||
|
||||
bedHardware.on('error', (error) => {
|
||||
bedHardwareInstance.on('error', (error) => {
|
||||
console.error('BedHardware error:', error);
|
||||
isHardwareConnected = false;
|
||||
});
|
||||
|
||||
await bedHardware.connect();
|
||||
await bedHardwareInstance.connect();
|
||||
} catch (error) {
|
||||
console.warn('Failed to connect to hardware, using mock data:', error);
|
||||
isHardwareConnected = false;
|
||||
|
@ -283,7 +274,7 @@ export async function GET() {
|
|||
}
|
||||
|
||||
// Initialize hardware if not already done
|
||||
if (!bedHardware) {
|
||||
if (!isHardwareConnected) {
|
||||
await initializeHardware();
|
||||
}
|
||||
|
||||
|
@ -291,8 +282,8 @@ export async function GET() {
|
|||
updateMockSensorData();
|
||||
|
||||
// If hardware is connected, get current pin states
|
||||
if (isHardwareConnected && bedHardware) {
|
||||
const pinStates = bedHardware.getAllPinStates();
|
||||
if (isHardwareConnected) {
|
||||
const pinStates = bedHardwareInstance.getAllPinStates();
|
||||
for (const pinState of pinStates) {
|
||||
await updateSensorFromPin(pinState.pin, pinState.state);
|
||||
}
|
||||
|
@ -332,9 +323,8 @@ export async function POST(request: NextRequest) {
|
|||
}
|
||||
|
||||
if (body.action === 'disconnect') {
|
||||
if (bedHardware) {
|
||||
await bedHardware.disconnect();
|
||||
bedHardware = null;
|
||||
if (bedHardwareInstance) {
|
||||
await bedHardwareInstance.disconnect();
|
||||
isHardwareConnected = false;
|
||||
}
|
||||
return NextResponse.json({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue