import { useBLEContext } from '../context/BLEContext'; /** * Hook to access BLE functionality * Provides easy access to BLE operations and state */ export const useBLE = () => { const context = useBLEContext(); return { // State bluetoothEnabled: context.bluetoothEnabled, isScanning: context.isScanning, discoveredDevices: context.discoveredDevices, // Multi-device support connectedDevices: context.connectedDevices, getDeviceState: context.getDeviceState, isDeviceConnected: context.isDeviceConnected, getConnectedDeviceIds: context.getConnectedDeviceIds, // Legacy single device support (for backward compatibility) connectedDeviceId: context.connectedDeviceId, isInfusionRunning: context.isInfusionRunning, // Actions startScan: context.startScan, stopScan: context.stopScan, connectToDevice: context.connectToDevice, disconnectDevice: context.disconnectDevice, requestPermissions: context.requestPermissions, // Infusion control startInfusion: context.startInfusion, stopInfusion: context.stopInfusion, setInfusionLevel: context.setInfusionLevel, // Characteristic operations readCharacteristic: context.readCharacteristic, writeCharacteristic: context.writeCharacteristic, // User role management setUserRole: context.setUserRole, getUserRole: context.getUserRole, }; }; /** * Hook to access characteristic notifications */ export const useBLENotifications = () => { const context = useBLEContext(); return { ff21Notification: context.ff21Notification, ff31Notification: context.ff31Notification, ff41Notification: context.ff41Notification, ff02Notification: context.ff02Notification, }; }; /** * Hook to get connection status */ export const useBLEConnection = () => { const context = useBLEContext(); return { isConnected: context.connectedDeviceId !== null, connectedDeviceId: context.connectedDeviceId, disconnect: context.disconnectDevice, }; }; /** * Hook to access discovered devices */ export const useBLEDevices = () => { const context = useBLEContext(); return { devices: context.discoveredDevices, isScanning: context.isScanning, startScan: context.startScan, stopScan: context.stopScan, }; }; /** * Hook for infusion control */ export const useBLEInfusion = () => { const context = useBLEContext(); return { isInfusionRunning: context.isInfusionRunning, startInfusion: context.startInfusion, stopInfusion: context.stopInfusion, setInfusionLevel: context.setInfusionLevel, connectedDeviceId: context.connectedDeviceId, }; }; /** * Hook for characteristic operations */ export const useBLECharacteristics = () => { const context = useBLEContext(); return { readCharacteristic: context.readCharacteristic, writeCharacteristic: context.writeCharacteristic, connectedDeviceId: context.connectedDeviceId, }; }; /** * Hook for multi-device management * Provides access to all connected devices and their states */ export const useBLEMultiDevice = () => { const context = useBLEContext(); return { connectedDevices: context.connectedDevices, getDeviceState: context.getDeviceState, isDeviceConnected: context.isDeviceConnected, getConnectedDeviceIds: context.getConnectedDeviceIds, connectToDevice: context.connectToDevice, disconnectDevice: context.disconnectDevice, }; }; /** * Hook for specific device notifications * @param deviceId - The device ID to get notifications for */ export const useBLEDeviceNotifications = (deviceId: string | null) => { const context = useBLEContext(); if (!deviceId) { return { ff21Notification: null, ff31Notification: null, ff41Notification: null, ff02Notification: null, }; } const deviceState = context.getDeviceState(deviceId); return { ff21Notification: deviceState?.notifications.ff21 || null, ff31Notification: deviceState?.notifications.ff31 || null, ff41Notification: deviceState?.notifications.ff41 || null, ff02Notification: deviceState?.notifications.ff02 || null, }; }; /** * Hook for specific device infusion control * @param deviceId - The device ID to control infusion for */ export const useBLEDeviceInfusion = (deviceId: string | null) => { const context = useBLEContext(); const deviceState = deviceId ? context.getDeviceState(deviceId) : undefined; return { deviceId, isConnected: deviceId ? context.isDeviceConnected(deviceId) : false, isInfusionRunning: deviceState?.isInfusionRunning || false, infusionLevel: deviceState?.infusionLevel || null, startInfusion: deviceId ? () => context.startInfusion(deviceId) : async () => { throw new Error('No device selected'); }, stopInfusion: deviceId ? () => context.stopInfusion(deviceId) : async () => { throw new Error('No device selected'); }, setInfusionLevel: deviceId ? (level: number) => context.setInfusionLevel(deviceId, level) : async () => { throw new Error('No device selected'); }, }; };