WellNuo/services/ble/MockBLEManager.ts
Sergei 2b2bd88726 Add BLE cleanup on user logout
Implement comprehensive BLE cleanup functionality that properly
disconnects all devices and releases resources when user logs out.

Changes:
- Add cleanup() method to BLEManager and MockBLEManager
- Update IBLEManager interface to include cleanup
- Add cleanupBLE() to BLEContext to disconnect all devices
- Implement callback mechanism in api.ts for BLE cleanup on logout
- Wire up BLE cleanup in app layout to trigger on logout
- Add unit tests for BLE cleanup functionality

This ensures no BLE connections remain active after logout,
preventing resource leaks and potential connection issues.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-29 10:57:43 -08:00

130 lines
3.3 KiB
TypeScript

// Mock BLE Manager для iOS Simulator (Bluetooth недоступен)
import { IBLEManager, WPDevice, WiFiNetwork, WiFiStatus } from './types';
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
export class MockBLEManager implements IBLEManager {
private connectedDevices = new Set<string>();
private mockDevices: WPDevice[] = [
{
id: 'mock-743',
name: 'WP_497_81a14c',
mac: '142B2F81A14C',
rssi: -55,
wellId: 497,
},
{
id: 'mock-769',
name: 'WP_523_81aad4',
mac: '142B2F81AAD4',
rssi: -67,
wellId: 523,
},
];
async scanDevices(): Promise<WPDevice[]> {
console.log('[MockBLE] Scanning for devices...');
await delay(2000); // Simulate scan delay
return this.mockDevices;
}
stopScan(): void {
console.log('[MockBLE] Scan stopped');
}
async connectDevice(deviceId: string): Promise<boolean> {
console.log(`[MockBLE] Connecting to ${deviceId}...`);
await delay(1000);
this.connectedDevices.add(deviceId);
return true;
}
async disconnectDevice(deviceId: string): Promise<void> {
console.log(`[MockBLE] Disconnecting ${deviceId}`);
await delay(500);
this.connectedDevices.delete(deviceId);
}
isDeviceConnected(deviceId: string): boolean {
return this.connectedDevices.has(deviceId);
}
async sendCommand(deviceId: string, command: string): Promise<string> {
console.log(`[MockBLE] Sending command: ${command}`);
await delay(500);
// Simulate responses
if (command === 'pin|7856') {
return 'pin|ok';
}
if (command === 'w') {
return 'mac,142b2f81a14c|w|3|FrontierTower,-55|HomeNetwork,-67|TP-Link_5G,-75';
}
if (command === 'a') {
return 'mac,142b2f81a14c|a|FrontierTower,-67';
}
if (command.startsWith('W|')) {
return 'mac,142b2f81a14c|W|ok';
}
return 'ok';
}
async getWiFiList(deviceId: string): Promise<WiFiNetwork[]> {
console.log(`[MockBLE] Getting WiFi list for ${deviceId}`);
await delay(1500);
return [
{ ssid: 'FrontierTower', rssi: -55 },
{ ssid: 'HomeNetwork', rssi: -67 },
{ ssid: 'TP-Link_5G', rssi: -75 },
{ ssid: 'Office-WiFi', rssi: -80 },
];
}
async setWiFi(
deviceId: string,
ssid: string,
password: string
): Promise<boolean> {
console.log(`[MockBLE] Setting WiFi: ${ssid}`);
await delay(2000);
return true;
}
async getCurrentWiFi(deviceId: string): Promise<WiFiStatus | null> {
console.log(`[MockBLE] Getting current WiFi for ${deviceId}`);
await delay(1000);
return {
ssid: 'FrontierTower',
rssi: -67,
connected: true,
};
}
async rebootDevice(deviceId: string): Promise<void> {
console.log(`[MockBLE] Rebooting ${deviceId}`);
await delay(500);
this.connectedDevices.delete(deviceId);
}
/**
* Cleanup all BLE connections and state
* Should be called on app logout to properly release resources
*/
async cleanup(): Promise<void> {
console.log('[MockBLE] Cleanup: disconnecting all devices');
// Disconnect all connected devices
const deviceIds = Array.from(this.connectedDevices);
for (const deviceId of deviceIds) {
await this.disconnectDevice(deviceId);
}
this.connectedDevices.clear();
console.log('[MockBLE] Cleanup: complete');
}
}