Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 | // Real BLE Manager для физических устройств
import { BleManager, Device } from 'react-native-ble-plx';
import { Platform } from 'react-native';
import {
IBLEManager,
WPDevice,
WiFiNetwork,
WiFiStatus,
BLE_CONFIG,
BLE_COMMANDS,
BLEConnectionState,
BLEDeviceConnection,
BLEEventListener,
BLEConnectionEvent,
SensorHealthMetrics,
SensorHealthStatus,
WiFiSignalQuality,
CommunicationHealth,
BulkOperationResult,
BulkWiFiResult,
} from './types';
import { requestBLEPermissions, checkBluetoothEnabled } from './permissions';
import base64 from 'react-native-base64';
export class RealBLEManager implements IBLEManager {
private _manager: BleManager | null = null;
private connectedDevices = new Map<string, Device>();
private connectionStates = new Map<string, BLEDeviceConnection>();
private eventListeners: BLEEventListener[] = [];
private scanning = false;
private connectingDevices = new Set<string>(); // Track devices currently being connected
// Health monitoring state
private sensorHealthMetrics = new Map<string, SensorHealthMetrics>();
private communicationStats = new Map<string, CommunicationHealth>();
// Lazy initialization to prevent crash on app startup
private get manager(): BleManager {
if (!this._manager) {
this._manager = new BleManager();
}
return this._manager;
}
// Empty constructor - using lazy initialization for BleManager
/**
* Update connection state and notify listeners
*/
private updateConnectionState(
deviceId: string,
state: BLEConnectionState,
deviceName?: string,
error?: string
): void {
const existing = this.connectionStates.get(deviceId);
const now = Date.now();
const connection: BLEDeviceConnection = {
deviceId,
deviceName: deviceName || existing?.deviceName || deviceId,
state,
error,
connectedAt: state === BLEConnectionState.CONNECTED ? now : existing?.connectedAt,
lastActivity: now,
};
this.connectionStates.set(deviceId, connection);
this.emitEvent(deviceId, 'state_changed', { state, error });
}
/**
* Emit event to all registered listeners
*/
private emitEvent(deviceId: string, event: BLEConnectionEvent, data?: any): void {
this.eventListeners.forEach((listener) => {
try {
listener(deviceId, event, data);
} catch {
// Listener error should not crash the app
}
});
}
/**
* Get current connection state for a device
*/
getConnectionState(deviceId: string): BLEConnectionState {
const connection = this.connectionStates.get(deviceId);
return connection?.state || BLEConnectionState.DISCONNECTED;
}
/**
* Get all active connections
*/
getAllConnections(): Map<string, BLEDeviceConnection> {
return new Map(this.connectionStates);
}
/**
* Add event listener
*/
addEventListener(listener: BLEEventListener): void {
if (!this.eventListeners.includes(listener)) {
this.eventListeners.push(listener);
}
}
/**
* Remove event listener
*/
removeEventListener(listener: BLEEventListener): void {
const index = this.eventListeners.indexOf(listener);
if (index > -1) {
this.eventListeners.splice(index, 1);
}
}
async scanDevices(): Promise<WPDevice[]> {
// Check permissions with graceful fallback
const permissionStatus = await requestBLEPermissions();
if (!permissionStatus.granted) {
throw new Error(permissionStatus.error || 'Bluetooth permissions not granted');
}
// Check Bluetooth state
const bluetoothStatus = await checkBluetoothEnabled(this.manager);
if (!bluetoothStatus.enabled) {
throw new Error(bluetoothStatus.error || 'Bluetooth is disabled. Please enable it in settings.');
}
const foundDevices = new Map<string, WPDevice>();
return new Promise((resolve, reject) => {
this.scanning = true;
this.manager.startDeviceScan(
null,
{ allowDuplicates: false },
(error, device) => {
if (error) {
this.scanning = false;
reject(error);
return;
}
if (device && device.name?.startsWith(BLE_CONFIG.DEVICE_NAME_PREFIX)) {
// Parse well_id from name (WP_497_81a14c -> 497)
const wellIdMatch = device.name.match(/WP_(\d+)_/);
const wellId = wellIdMatch ? parseInt(wellIdMatch[1], 10) : undefined;
// Extract MAC from device name (last part after underscore)
const macMatch = device.name.match(/_([a-fA-F0-9]{6})$/);
const mac = macMatch ? macMatch[1].toUpperCase() : '';
foundDevices.set(device.id, {
id: device.id,
name: device.name,
mac: mac,
rssi: device.rssi || -100,
wellId,
});
}
}
);
// Stop scan after timeout
setTimeout(() => {
this.stopScan();
resolve(Array.from(foundDevices.values()));
}, BLE_CONFIG.SCAN_TIMEOUT);
});
}
stopScan(): void {
if (this.scanning) {
this.manager.stopDeviceScan();
this.scanning = false;
}
}
async connectDevice(deviceId: string): Promise<boolean> {
try {
// Check if connection is already in progress
if (this.connectingDevices.has(deviceId)) {
throw new Error('Connection already in progress for this device');
}
// Check if already connected
const existingDevice = this.connectedDevices.get(deviceId);
if (existingDevice) {
const isConnected = await existingDevice.isConnected();
if (isConnected) {
this.updateConnectionState(deviceId, BLEConnectionState.READY, existingDevice.name || undefined);
this.emitEvent(deviceId, 'ready');
return true;
}
// Device was in map but disconnected, remove it
this.connectedDevices.delete(deviceId);
this.updateConnectionState(deviceId, BLEConnectionState.DISCONNECTED);
}
// Mark device as connecting
this.connectingDevices.add(deviceId);
// Update state to CONNECTING
this.updateConnectionState(deviceId, BLEConnectionState.CONNECTING);
// Step 0: Check permissions (required for Android 12+)
const permissionStatus = await requestBLEPermissions();
if (!permissionStatus.granted) {
const error = permissionStatus.error || 'Bluetooth permissions not granted';
this.updateConnectionState(deviceId, BLEConnectionState.ERROR, undefined, error);
this.emitEvent(deviceId, 'connection_failed', { error });
throw new Error(error);
}
// Step 0.5: Check Bluetooth is enabled
const bluetoothStatus = await checkBluetoothEnabled(this.manager);
if (!bluetoothStatus.enabled) {
const error = bluetoothStatus.error || 'Bluetooth is disabled. Please enable it in settings.';
this.updateConnectionState(deviceId, BLEConnectionState.ERROR, undefined, error);
this.emitEvent(deviceId, 'connection_failed', { error });
throw new Error(error);
}
const device = await this.manager.connectToDevice(deviceId, {
timeout: 10000, // 10 second timeout
});
// Update state to CONNECTED
this.updateConnectionState(deviceId, BLEConnectionState.CONNECTED, device.name || undefined);
// Update state to DISCOVERING
this.updateConnectionState(deviceId, BLEConnectionState.DISCOVERING, device.name || undefined);
await device.discoverAllServicesAndCharacteristics();
// Request larger MTU for Android (default is 23 bytes which is too small)
if (Platform.OS === 'android') {
try {
await device.requestMTU(512);
} catch {
// MTU request may fail on some devices - continue anyway
}
}
this.connectedDevices.set(deviceId, device);
// Update state to READY
this.updateConnectionState(deviceId, BLEConnectionState.READY, device.name || undefined);
this.emitEvent(deviceId, 'ready');
return true;
} catch (error: any) {
const errorMessage = error?.message || 'Connection failed';
this.updateConnectionState(deviceId, BLEConnectionState.ERROR, undefined, errorMessage);
this.emitEvent(deviceId, 'connection_failed', { error: errorMessage });
return false;
} finally {
// Always remove from connecting set when done (success or failure)
this.connectingDevices.delete(deviceId);
}
}
async disconnectDevice(deviceId: string): Promise<void> {
const device = this.connectedDevices.get(deviceId);
if (device) {
try {
// Update state to DISCONNECTING
this.updateConnectionState(deviceId, BLEConnectionState.DISCONNECTING);
// Cancel any pending operations before disconnecting
// This helps prevent Android NullPointerException in monitor callbacks
await device.cancelConnection();
// Update state to DISCONNECTED
this.updateConnectionState(deviceId, BLEConnectionState.DISCONNECTED);
this.emitEvent(deviceId, 'disconnected');
} catch {
// Log but don't throw - device may already be disconnected
this.updateConnectionState(deviceId, BLEConnectionState.DISCONNECTED);
this.emitEvent(deviceId, 'disconnected');
} finally {
this.connectedDevices.delete(deviceId);
}
} else {
// Not in connected devices map, just update state
this.updateConnectionState(deviceId, BLEConnectionState.DISCONNECTED);
this.emitEvent(deviceId, 'disconnected');
}
}
isDeviceConnected(deviceId: string): boolean {
return this.connectedDevices.has(deviceId);
}
/**
* Update communication stats for a device
*/
private updateCommunicationStats(deviceKey: string, success: boolean, responseTime: number): void {
const existing = this.communicationStats.get(deviceKey);
const now = Date.now();
if (!existing) {
this.communicationStats.set(deviceKey, {
successfulCommands: success ? 1 : 0,
failedCommands: success ? 0 : 1,
averageResponseTime: responseTime,
lastSuccessfulCommand: success ? now : 0,
lastFailedCommand: success ? undefined : now,
});
} else {
const totalCommands = existing.successfulCommands + existing.failedCommands;
const newAverage =
(existing.averageResponseTime * totalCommands + responseTime) / (totalCommands + 1);
this.communicationStats.set(deviceKey, {
successfulCommands: existing.successfulCommands + (success ? 1 : 0),
failedCommands: existing.failedCommands + (success ? 0 : 1),
averageResponseTime: newAverage,
lastSuccessfulCommand: success ? now : existing.lastSuccessfulCommand,
lastFailedCommand: success ? existing.lastFailedCommand : now,
});
}
}
async sendCommand(deviceId: string, command: string): Promise<string> {
const startTime = Date.now();
const device = this.connectedDevices.get(deviceId);
if (!device) {
throw new Error('Device not connected');
}
// Verify device is still connected
try {
const isConnected = await device.isConnected();
if (!isConnected) {
this.connectedDevices.delete(deviceId);
throw new Error('Device disconnected');
}
} catch {
throw new Error('Failed to verify connection');
}
// Generate unique transaction ID to prevent Android null pointer issues
const transactionId = `cmd_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
return new Promise(async (resolve, reject) => {
let responseReceived = false;
let subscription: any = null;
let timeoutId: ReturnType<typeof setTimeout> | null = null;
const cleanup = () => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
if (subscription) {
try {
subscription.remove();
} catch {
// Ignore errors during cleanup - device may already be disconnected
}
subscription = null;
}
};
// Safe reject wrapper to handle null error messages (Android BLE crash fix)
const safeReject = (error: any) => {
if (responseReceived) return;
// Extract error code (numeric or string)
const errorCode = error?.errorCode || error?.code || 'BLE_ERROR';
// Ignore "Operation was cancelled" (code 2) - this is expected when we cleanup
// This happens when subscription is removed but BLE still tries to send callback
if (errorCode === 2 || errorCode === 'OperationCancelled' ||
(error?.message && error.message.includes('cancelled'))) {
return;
}
responseReceived = true;
cleanup();
// Track failed command
const responseTime = Date.now() - startTime;
const deviceKey = `${deviceId}`;
this.updateCommunicationStats(deviceKey, false, responseTime);
// Ensure error has a valid message (fixes Android NullPointerException)
const errorMessage = error?.message || error?.reason || 'BLE operation failed';
reject(new Error(`[${errorCode}] ${errorMessage}`));
};
try {
// Subscribe to notifications with explicit transactionId
subscription = device.monitorCharacteristicForService(
BLE_CONFIG.SERVICE_UUID,
BLE_CONFIG.CHAR_UUID,
(error, characteristic) => {
// Wrap callback in try-catch to prevent crashes
try {
if (error) {
const errCode = (error as any)?.errorCode;
// errorCode 2 = "Operation was cancelled" — normal BLE cleanup, not a real error
if (errCode === 2) {
} else {
// Notification error
}
safeReject(error);
return;
}
if (characteristic?.value) {
const decoded = base64.decode(characteristic.value);
if (!responseReceived) {
responseReceived = true;
cleanup();
// Track successful command
const responseTime = Date.now() - startTime;
const deviceKey = `${deviceId}`;
this.updateCommunicationStats(deviceKey, true, responseTime);
resolve(decoded);
}
}
} catch (callbackError: any) {
safeReject(callbackError);
}
},
transactionId // Explicit transaction ID prevents Android null pointer
);
// Send command
const encoded = base64.encode(command);
await device.writeCharacteristicWithResponseForService(
BLE_CONFIG.SERVICE_UUID,
BLE_CONFIG.CHAR_UUID,
encoded
);
// Timeout
timeoutId = setTimeout(() => {
if (!responseReceived) {
safeReject(new Error('Command timeout'));
}
}, BLE_CONFIG.COMMAND_TIMEOUT);
} catch (error: any) {
safeReject(error);
}
});
}
async getWiFiList(deviceId: string): Promise<WiFiNetwork[]> {
// Step 1: Unlock device
const unlockResponse = await this.sendCommand(deviceId, BLE_COMMANDS.PIN_UNLOCK);
if (!unlockResponse.includes('ok')) {
throw new Error('Failed to unlock device');
}
// Step 2: Get WiFi list
const listResponse = await this.sendCommand(deviceId, BLE_COMMANDS.GET_WIFI_LIST);
// Parse response: "mac,XXXXXX|w|COUNT|SSID1,RSSI1|SSID2,RSSI2|..."
const parts = listResponse.split('|');
if (parts.length < 3) {
throw new Error('Invalid WiFi list response');
}
const count = parseInt(parts[2], 10);
if (count < 0) {
if (count === -1) {
throw new Error('WiFi scan in progress, please wait');
}
if (count === -2) {
return []; // No networks found
}
}
// Use Map to deduplicate by SSID, keeping the strongest signal
const networksMap = new Map<string, WiFiNetwork>();
for (let i = 3; i < parts.length; i++) {
const [ssid, rssiStr] = parts[i].split(',');
if (ssid && rssiStr) {
const trimmedSsid = ssid.trim();
const rssi = parseInt(rssiStr, 10);
// Skip empty SSIDs
if (!trimmedSsid) continue;
// Keep the one with strongest signal if duplicate
const existing = networksMap.get(trimmedSsid);
if (!existing || rssi > existing.rssi) {
networksMap.set(trimmedSsid, {
ssid: trimmedSsid,
rssi: rssi,
});
}
}
}
// Convert to array and sort by signal strength (strongest first)
return Array.from(networksMap.values()).sort((a, b) => b.rssi - a.rssi);
}
async setWiFi(deviceId: string, ssid: string, password: string): Promise<boolean> {
// Step 1: Unlock device
const unlockResponse = await this.sendCommand(deviceId, BLE_COMMANDS.PIN_UNLOCK);
if (!unlockResponse.includes('ok')) {
throw new Error(`Device unlock failed: ${unlockResponse}`);
}
// Step 1.5: Check if already connected to the target WiFi
// This prevents "W|fail" when sensor uses old saved credentials
try {
const statusResponse = await this.sendCommand(deviceId, BLE_COMMANDS.GET_WIFI_STATUS);
// Parse: "mac,XXXXXX|a|SSID,RSSI" or "mac,XXXXXX|a|,0" (not connected)
const parts = statusResponse.split('|');
if (parts.length >= 3) {
const [currentSsid] = parts[2].split(',');
if (currentSsid && currentSsid.trim().toLowerCase() === ssid.toLowerCase()) {
return true;
}
}
} catch {
// Ignore status check errors - continue with WiFi config
}
// Step 2: Set WiFi credentials
const command = `${BLE_COMMANDS.SET_WIFI}|${ssid},${password}`;
const setResponse = await this.sendCommand(deviceId, command);
// Parse response: "mac,XXXXXX|W|ok" or "mac,XXXXXX|W|fail" or other errors
if (setResponse.includes('|W|ok')) {
return true;
}
// WiFi config failed - check if sensor is still connected (using old credentials)
if (setResponse.includes('|W|fail')) {
try {
const recheckResponse = await this.sendCommand(deviceId, BLE_COMMANDS.GET_WIFI_STATUS);
const parts = recheckResponse.split('|');
if (parts.length >= 3) {
const [currentSsid, rssiStr] = parts[2].split(',');
const rssi = parseInt(rssiStr, 10);
// If connected to target SSID (using old credentials), consider it success
if (currentSsid && currentSsid.trim().toLowerCase() === ssid.toLowerCase() && rssi < 0) {
return true;
}
}
} catch {
// Ignore recheck errors - password was rejected
}
throw new Error('WiFi credentials rejected by sensor. Check password.');
}
if (setResponse.includes('timeout') || setResponse.includes('Timeout')) {
throw new Error('Sensor did not respond to WiFi config. Try again.');
}
// Unknown error - include raw response for debugging
throw new Error(`WiFi config failed: ${setResponse.substring(0, 100)}`);
}
async getCurrentWiFi(deviceId: string): Promise<WiFiStatus | null> {
// Step 1: Unlock device
const unlockResponse = await this.sendCommand(deviceId, BLE_COMMANDS.PIN_UNLOCK);
if (!unlockResponse.includes('ok')) {
throw new Error('Failed to unlock device');
}
// Step 2: Get current WiFi status
const statusResponse = await this.sendCommand(deviceId, BLE_COMMANDS.GET_WIFI_STATUS);
// Parse response: "mac,XXXXXX|a|SSID,RSSI" or "mac,XXXXXX|a|,0" (not connected)
const parts = statusResponse.split('|');
if (parts.length < 3) {
return null;
}
const [ssid, rssiStr] = parts[2].split(',');
if (!ssid || ssid.trim() === '') {
return null; // Not connected
}
return {
ssid: ssid.trim(),
rssi: parseInt(rssiStr, 10),
connected: true,
};
}
async rebootDevice(deviceId: string): Promise<void> {
// Step 1: Unlock device
await this.sendCommand(deviceId, BLE_COMMANDS.PIN_UNLOCK);
// Step 2: Reboot (device will disconnect)
await this.sendCommand(deviceId, BLE_COMMANDS.REBOOT);
// Remove from connected devices
this.connectedDevices.delete(deviceId);
}
/**
* Calculate WiFi signal quality from RSSI value
*/
private getWiFiSignalQuality(rssi: number | null): WiFiSignalQuality {
if (rssi === null) return WiFiSignalQuality.UNKNOWN;
if (rssi >= -50) return WiFiSignalQuality.EXCELLENT;
if (rssi >= -60) return WiFiSignalQuality.GOOD;
if (rssi >= -70) return WiFiSignalQuality.FAIR;
return WiFiSignalQuality.WEAK;
}
/**
* Calculate overall sensor health status
*/
private calculateOverallHealth(
connectionStatus: 'online' | 'warning' | 'offline',
wifiQuality: WiFiSignalQuality,
commHealth: CommunicationHealth
): SensorHealthStatus {
// Critical: offline or very poor communication
if (connectionStatus === 'offline') {
return SensorHealthStatus.CRITICAL;
}
// Calculate communication success rate
const totalCommands = commHealth.successfulCommands + commHealth.failedCommands;
const successRate = totalCommands > 0 ? commHealth.successfulCommands / totalCommands : 1;
// Poor: warning status or high failure rate or weak WiFi
if (
connectionStatus === 'warning' ||
successRate < 0.5 ||
wifiQuality === WiFiSignalQuality.WEAK
) {
return SensorHealthStatus.POOR;
}
// Fair: moderate success rate or fair WiFi
if (successRate < 0.8 || wifiQuality === WiFiSignalQuality.FAIR) {
return SensorHealthStatus.FAIR;
}
// Good: high success rate and good WiFi
if (successRate >= 0.9 && wifiQuality === WiFiSignalQuality.GOOD) {
return SensorHealthStatus.GOOD;
}
// Excellent: perfect or near-perfect performance
return SensorHealthStatus.EXCELLENT;
}
/**
* Get sensor health metrics for a specific device
* Attempts to connect to the device via BLE and query its WiFi status
*/
async getSensorHealth(wellId: number, mac: string): Promise<SensorHealthMetrics | null> {
try {
const deviceKey = `WP_${wellId}_${mac.slice(-6).toLowerCase()}`;
// Try to find device via BLE scan
const devices = await this.scanDevices();
const device = devices.find((d) => d.wellId === wellId && d.mac === mac);
if (!device) {
// Device not found via BLE - return null or cached metrics
return this.sensorHealthMetrics.get(deviceKey) || null;
}
// Connect to device
const connected = await this.connectDevice(device.id);
if (!connected) {
return this.sensorHealthMetrics.get(deviceKey) || null;
}
// Get WiFi status
let wifiStatus: WiFiStatus | null = null;
try {
wifiStatus = await this.getCurrentWiFi(device.id);
} catch {
// WiFi status query failed - continue with partial data
}
// Get communication stats
const commStats = this.communicationStats.get(deviceKey) || {
successfulCommands: 0,
failedCommands: 0,
averageResponseTime: 0,
lastSuccessfulCommand: Date.now(),
};
// Calculate metrics
const now = Date.now();
const lastSeenMinutesAgo = 0; // Just connected
const wifiRssi = wifiStatus?.rssi || null;
const wifiQuality = this.getWiFiSignalQuality(wifiRssi);
const connectionStatus: 'online' | 'warning' | 'offline' =
lastSeenMinutesAgo < 5 ? 'online' : lastSeenMinutesAgo < 60 ? 'warning' : 'offline';
const overallHealth = this.calculateOverallHealth(connectionStatus, wifiQuality, commStats);
// Get connection state
const connState = this.connectionStates.get(device.id);
const metrics: SensorHealthMetrics = {
deviceId: device.id,
deviceName: deviceKey,
overallHealth,
connectionStatus,
lastSeen: new Date(),
lastSeenMinutesAgo,
wifiSignalQuality: wifiQuality,
wifiRssi,
wifiSsid: wifiStatus?.ssid || null,
wifiConnected: wifiStatus?.connected || false,
communication: commStats,
bleRssi: device.rssi,
bleConnectionAttempts: 1,
bleConnectionFailures: 0,
lastHealthCheck: now,
lastBleConnection: connState?.connectedAt,
};
// Cache metrics
this.sensorHealthMetrics.set(deviceKey, metrics);
// Disconnect after health check
await this.disconnectDevice(device.id);
return metrics;
} catch {
return null;
}
}
/**
* Get all cached sensor health metrics
*/
getAllSensorHealth(): Map<string, SensorHealthMetrics> {
return new Map(this.sensorHealthMetrics);
}
/**
* Cleanup all BLE connections and state
* Should be called on app logout to properly release resources
*/
async cleanup(): Promise<void> {
// Stop any ongoing scan
if (this.scanning) {
this.stopScan();
}
// Disconnect all connected devices
const deviceIds = Array.from(this.connectedDevices.keys());
for (const deviceId of deviceIds) {
try {
await this.disconnectDevice(deviceId);
} catch {
// Continue cleanup even if one device fails
}
}
// Clear the maps and sets
this.connectedDevices.clear();
this.connectionStates.clear();
this.connectingDevices.clear();
// Clear health monitoring data
this.sensorHealthMetrics.clear();
this.communicationStats.clear();
// Clear event listeners
this.eventListeners = [];
}
/**
* Bulk disconnect multiple devices
* Useful for cleanup or batch operations
*/
async bulkDisconnect(deviceIds: string[]): Promise<BulkOperationResult[]> {
const results: BulkOperationResult[] = [];
for (const deviceId of deviceIds) {
const device = this.connectedDevices.get(deviceId);
const deviceName = device?.name || deviceId;
try {
await this.disconnectDevice(deviceId);
results.push({
deviceId,
deviceName,
success: true,
});
} catch (error: any) {
results.push({
deviceId,
deviceName,
success: false,
error: error?.message || 'Disconnect failed',
});
}
}
return results;
}
/**
* Bulk reboot multiple devices
* Useful for applying settings changes to multiple sensors
*/
async bulkReboot(deviceIds: string[]): Promise<BulkOperationResult[]> {
const results: BulkOperationResult[] = [];
for (const deviceId of deviceIds) {
const device = this.connectedDevices.get(deviceId);
const deviceName = device?.name || deviceId;
try {
// Connect if not already connected
if (!this.isDeviceConnected(deviceId)) {
const connected = await this.connectDevice(deviceId);
if (!connected) {
throw new Error('Could not connect to device');
}
}
// Reboot
await this.rebootDevice(deviceId);
results.push({
deviceId,
deviceName,
success: true,
});
} catch (error: any) {
results.push({
deviceId,
deviceName,
success: false,
error: error?.message || 'Reboot failed',
});
}
}
return results;
}
/**
* Bulk WiFi configuration for multiple devices
* Configures all devices with the same WiFi credentials sequentially
*/
async bulkSetWiFi(
devices: Array<{ id: string; name: string }>,
ssid: string,
password: string,
onProgress?: (deviceId: string, status: 'connecting' | 'configuring' | 'rebooting' | 'success' | 'error', error?: string) => void
): Promise<BulkWiFiResult[]> {
const results: BulkWiFiResult[] = [];
for (const device of devices) {
const { id: deviceId, name: deviceName } = device;
try {
// Step 1: Connect
onProgress?.(deviceId, 'connecting');
const connected = await this.connectDevice(deviceId);
if (!connected) {
throw new Error('Could not connect to device');
}
// Step 2: Set WiFi
onProgress?.(deviceId, 'configuring');
await this.setWiFi(deviceId, ssid, password);
// Step 3: Reboot
onProgress?.(deviceId, 'rebooting');
await this.rebootDevice(deviceId);
// Success
onProgress?.(deviceId, 'success');
results.push({
deviceId,
deviceName,
success: true,
});
} catch (error: any) {
const errorMessage = error?.message || 'WiFi configuration failed';
onProgress?.(deviceId, 'error', errorMessage);
results.push({
deviceId,
deviceName,
success: false,
error: errorMessage,
});
}
}
return results;
}
}
|