diff --git a/adapter/mqtt.ts b/adapter/mqtt.ts index d007af3..b81be99 100644 --- a/adapter/mqtt.ts +++ b/adapter/mqtt.ts @@ -132,6 +132,7 @@ class MQTT { private onMessage(topic: string, message: Buffer): void { const msg = message.toString(); + console.log(`Received message on topic ${topic}:`, msg); this.subscriptions.forEach(sub => { if (this.matchTopic(sub.topic, topic)) { sub.callback(topic, msg); diff --git a/hooks/useBedPressureData.ts b/hooks/useBedPressureData.ts index a00822f..5518c98 100644 --- a/hooks/useBedPressureData.ts +++ b/hooks/useBedPressureData.ts @@ -1,21 +1,6 @@ import { useEffect } from 'react' import { useBedPressureStore, SensorData } from '@/stores/bedPressureStore' -// Mock data generator -const generateTimeSeriesData = (hours = 24) => { - const data = [] - const now = new Date() - - for (let i = hours * 60; i >= 0; i -= 5) { - const time = new Date(now.getTime() - i * 60 * 1000) - data.push({ - time: time.toLocaleTimeString("en-US", { hour12: false }), - timestamp: time.getTime(), - value: Math.floor(Math.random() * 4096 + Math.sin(i / 60) * 500 + 2000), // 0-4095 range - }) - } - return data -} export function useBedPressureData() { const { @@ -40,8 +25,8 @@ export function useBedPressureData() { sensorConfig.forEach((sensor) => { initialData[sensor.id] = { ...sensor, - currentValue: Math.floor(Math.random() * 1000 + 1000), // Start with baseline analog value (1000-2000) - data: generateTimeSeriesData(), + currentValue: 0, + data: [], status: "normal", } }) diff --git a/services/BedHardwareSerial.ts.disabled b/services/BedHardwareSerial.ts.disabled deleted file mode 100644 index 1bf4537..0000000 --- a/services/BedHardwareSerial.ts.disabled +++ /dev/null @@ -1,149 +0,0 @@ -import { SerialPort } from 'serialport'; -import { ReadlineParser } from '@serialport/parser-readline'; -import { EventEmitter } from 'events'; -import { IBedHardware, PinState, PinChange } from '../types/bedhardware'; - -export class BedHardwareSerial extends EventEmitter implements IBedHardware { - private serialPort: SerialPort | null = null; - private parser: ReadlineParser | null = null; - private pinStates: Map = new Map(); - private connectionState: boolean = false; - - constructor(private portPath: string, private baudRate: number = 9600) { - super(); - } - - async connect(): Promise { - try { - this.serialPort = new SerialPort({ - path: this.portPath, - baudRate: this.baudRate, - autoOpen: false - }); - - this.parser = new ReadlineParser({ delimiter: '\n' }); - this.serialPort.pipe(this.parser); - - // Setup event handlers - this.serialPort.on('open', () => { - this.connectionState = true; - this.emit('connected'); - console.log('Serial port opened'); - }); - - this.serialPort.on('error', (error) => { - this.emit('error', error); - console.error('Serial port error:', error); - }); - - this.serialPort.on('close', () => { - this.connectionState = false; - this.emit('disconnected'); - console.log('Serial port closed'); - }); - - this.parser.on('data', (data: string) => { - this.handleSerialData(data.trim()); - }); - - // Open the port - await new Promise((resolve, reject) => { - this.serialPort!.open((error) => { - if (error) { - reject(error); - } else { - resolve(); - } - }); - }); - - } catch (error) { - throw new Error(`Failed to connect to ${this.portPath}: ${error}`); - } - } - - async disconnect(): Promise { - if (this.serialPort && this.serialPort.isOpen) { - await new Promise((resolve) => { - this.serialPort!.close(() => { - resolve(); - }); - }); - } - this.serialPort = null; - this.parser = null; - this.connectionState = false; - } - - private handleSerialData(data: string): void { - const parts = data.split(':'); - - if (parts[0] === 'INIT') { - if (parts[1] === 'START') { - this.emit('initialized'); - console.log('Arduino initialization started'); - } else if (parts.length >= 3) { - // INIT:PIN:STATE format - const pin = parseInt(parts[1]); - const state = parseInt(parts[2]); - - if (!isNaN(pin) && !isNaN(state)) { - const pinState: PinState = { - pin, - state, - name: `PIN${pin}`, - timestamp: new Date() - }; - - this.pinStates.set(pin, pinState); - this.emit('pinInitialized', pinState); - } - } - } else if (parts[0] === 'CHANGE' && parts.length >= 4) { - // CHANGE:PIN:PREVIOUS_STATE:CURRENT_STATE format - const pin = parseInt(parts[1]); - const previousState = parseInt(parts[2]); - const currentState = parseInt(parts[3]); - - if (!isNaN(pin) && !isNaN(previousState) && !isNaN(currentState)) { - const pinChange: PinChange = { - pin, - previousState, - currentState, - timestamp: new Date() - }; - - // Update stored pin state - const pinState: PinState = { - pin, - state: currentState, - name: `PIN${pin}`, - timestamp: new Date() - }; - - this.pinStates.set(pin, pinState); - - this.emit('pinChanged', pinChange); - this.emit(`pin${pin}Changed`, pinChange); - } - } - } - - getPinState(pin: number): PinState | undefined { - return this.pinStates.get(pin); - } - - getAllPinStates(): PinState[] { - return Array.from(this.pinStates.values()); - } - - isConnected(): boolean { - return this.connectionState && this.serialPort?.isOpen === true; - } - - // Static method to list available serial ports - static async listPorts(): Promise { - const ports = await SerialPort.list(); - return ports.map(port => port.path); - } -} \ No newline at end of file