From b3965c4997d62fda6c2e9b21287cb8ec8aca75a9 Mon Sep 17 00:00:00 2001 From: Shrey birmiwal Date: Sat, 8 Feb 2025 19:28:07 -0600 Subject: [PATCH 1/4] Added backend --- backend/routers/firmware.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/backend/routers/firmware.py b/backend/routers/firmware.py index 571f5e1a9e..9814fc6621 100644 --- a/backend/routers/firmware.py +++ b/backend/routers/firmware.py @@ -4,6 +4,9 @@ import httpx from fastapi import APIRouter, HTTPException +from utils.notifications import send_notification +from database.notifications import get_token_only + router = APIRouter() @@ -69,6 +72,18 @@ async def get_latest_version(device: int): } +@router.get("/v1/firmware/battery-alert") +async def notify_low_batter(battery_percentage: int, uid: str): + + if battery_percentage < 20: + token = get_token_only(uid) + if token: + send_notification(token, 'Charge your OMI necklace🪫!','Your OMI necklace is running low on battery. Please charge it to avoid any interruptions.') + + return {'status': 'ok'} + + + def extract_key_value_pairs(markdown_content): key_value_pattern = re.compile(r'', re.DOTALL) key_value_match = key_value_pattern.search(markdown_content) @@ -88,3 +103,8 @@ def extract_key_value_pairs(markdown_content): key_value_map[key] = value return key_value_map + + + + + From fd00e4f358ac9501d546105c90d101e8716a9cef Mon Sep 17 00:00:00 2001 From: Shrey birmiwal Date: Mon, 10 Feb 2025 19:51:00 -0600 Subject: [PATCH 2/4] stuck --- app/lib/backend/http/api/hardware.dart | 36 +++++++++++++++++++++ app/lib/providers/device_provider.dart | 45 ++++++++++++++++++-------- 2 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 app/lib/backend/http/api/hardware.dart diff --git a/app/lib/backend/http/api/hardware.dart b/app/lib/backend/http/api/hardware.dart new file mode 100644 index 0000000000..f6617675eb --- /dev/null +++ b/app/lib/backend/http/api/hardware.dart @@ -0,0 +1,36 @@ +import 'dart:convert'; +import 'dart:ffi'; + +import 'package:flutter/material.dart'; +import 'package:friend_private/backend/http/shared.dart'; +import 'package:friend_private/env/env.dart'; + +Future saveFcmTokenServer( + {required String token, required String timeZone}) async { + var response = await makeApiCall( + url: '${Env.apiBaseUrl}v1/firmware/battery-alert', + headers: {'Content-Type': 'application/json'}, + method: 'POST', + body: jsonEncode({'fcm_token': token, 'time_zone': timeZone}), + ); + + debugPrint('saveToken: ${response?.body}'); + if (response?.statusCode == 200) { + debugPrint("Token saved successfully"); + } else { + debugPrint("Failed to save token"); + } +} + +Future low_battery_noti( + {required String uid, required int batteryLevel}) async { + var response = await makeApiCall( + url: '${Env.apiBaseUrl}/v1/firmware/battery-alert', + headers: {'Content-Type': 'application/json'}, + method: 'GET', + queryParameters: { + 'battery_percentage': batteryLevel.toString(), + 'uid': uid, + }, + ); +} diff --git a/app/lib/providers/device_provider.dart b/app/lib/providers/device_provider.dart index adcc88bf6f..292f9c754d 100644 --- a/app/lib/providers/device_provider.dart +++ b/app/lib/providers/device_provider.dart @@ -10,7 +10,8 @@ import 'package:friend_private/services/services.dart'; import 'package:friend_private/utils/analytics/mixpanel.dart'; import 'package:instabug_flutter/instabug_flutter.dart'; -class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption { +class DeviceProvider extends ChangeNotifier + implements IDeviceServiceSubsciption { CaptureProvider? captureProvider; bool isConnecting = false; @@ -44,10 +45,13 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption Future getDeviceInfo() async { if (connectedDevice != null) { - if (pairedDevice?.firmwareRevision != null && pairedDevice?.firmwareRevision != 'Unknown') { + if (pairedDevice?.firmwareRevision != null && + pairedDevice?.firmwareRevision != 'Unknown') { return; } - var connection = await ServiceManager.instance().device.ensureConnection(connectedDevice!.id); + var connection = await ServiceManager.instance() + .device + .ensureConnection(connectedDevice!.id); pairedDevice = await connectedDevice!.getDeviceInfo(connection); SharedPreferencesUtil().btDevice = pairedDevice!; } else { @@ -66,16 +70,19 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption void Function(int)? onBatteryLevelChange, }) async { { - var connection = await ServiceManager.instance().device.ensureConnection(deviceId); + var connection = + await ServiceManager.instance().device.ensureConnection(deviceId); if (connection == null) { return Future.value(null); } - return connection.getBleBatteryLevelListener(onBatteryLevelChange: onBatteryLevelChange); + return connection.getBleBatteryLevelListener( + onBatteryLevelChange: onBatteryLevelChange); } } Future> _getStorageList(String deviceId) async { - var connection = await ServiceManager.instance().device.ensureConnection(deviceId); + var connection = + await ServiceManager.instance().device.ensureConnection(deviceId); if (connection == null) { return []; } @@ -87,7 +94,8 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption if (deviceId.isEmpty) { return null; } - var connection = await ServiceManager.instance().device.ensureConnection(deviceId); + var connection = + await ServiceManager.instance().device.ensureConnection(deviceId); return connection?.device; } @@ -98,6 +106,7 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption connectedDevice!.id, onBatteryLevelChange: (int value) { batteryLevel = value; + // call teh low_battery_noti function in hardware.dart referecing the backend firmware.py @router.get("/v1/firmware/battery-alert") }, ); notifyListeners(); @@ -106,7 +115,8 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption Future periodicConnect(String printer) async { debugPrint("period connect"); _reconnectionTimer?.cancel(); - _reconnectionTimer = Timer.periodic(Duration(seconds: connectionCheckSeconds), (t) async { + _reconnectionTimer = + Timer.periodic(Duration(seconds: connectionCheckSeconds), (t) async { debugPrint("period connect..."); print(printer); print('seconds: $connectionCheckSeconds'); @@ -115,7 +125,8 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption if (SharedPreferencesUtil().btDevice.id.isEmpty) { return; } - print("isConnected: $isConnected, isConnecting: $isConnecting, connectedDevice: $connectedDevice"); + print( + "isConnected: $isConnected, isConnecting: $isConnecting, connectedDevice: $connectedDevice"); if ((!isConnected && connectedDevice == null)) { if (isConnecting) { return; @@ -127,7 +138,8 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption }); } - Future _scanAndConnectDevice({bool autoConnect = true, bool timeout = false}) async { + Future _scanAndConnectDevice( + {bool autoConnect = true, bool timeout = false}) async { var device = await _getConnectedDevice(); if (device != null) { return device; @@ -136,7 +148,9 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption int timeoutCounter = 0; while (true) { if (timeout && timeoutCounter >= 10) return null; - await ServiceManager.instance().device.discover(desirableDeviceId: SharedPreferencesUtil().btDevice.id); + await ServiceManager.instance() + .device + .discover(desirableDeviceId: SharedPreferencesUtil().btDevice.id); if (connectedDevice != null) { return connectedDevice; } @@ -270,11 +284,14 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption } @override - void onDeviceConnectionStateChanged(String deviceId, DeviceConnectionState state) async { - debugPrint("provider > device connection state changed...${deviceId}...${state}...${connectedDevice?.id}"); + void onDeviceConnectionStateChanged( + String deviceId, DeviceConnectionState state) async { + debugPrint( + "provider > device connection state changed...${deviceId}...${state}...${connectedDevice?.id}"); switch (state) { case DeviceConnectionState.connected: - var connection = await ServiceManager.instance().device.ensureConnection(deviceId); + var connection = + await ServiceManager.instance().device.ensureConnection(deviceId); if (connection == null) { return; } From b08c9ea146555d68948f8b219309515fb9cca8a3 Mon Sep 17 00:00:00 2001 From: Shrey Birmiwal Date: Tue, 11 Feb 2025 10:55:37 -0600 Subject: [PATCH 3/4] removed old changes --- app/lib/backend/http/api/hardware.dart | 14 ++------------ backend/routers/firmware.py | 11 ----------- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/app/lib/backend/http/api/hardware.dart b/app/lib/backend/http/api/hardware.dart index f6617675eb..4693bbb7f3 100644 --- a/app/lib/backend/http/api/hardware.dart +++ b/app/lib/backend/http/api/hardware.dart @@ -22,15 +22,5 @@ Future saveFcmTokenServer( } } -Future low_battery_noti( - {required String uid, required int batteryLevel}) async { - var response = await makeApiCall( - url: '${Env.apiBaseUrl}/v1/firmware/battery-alert', - headers: {'Content-Type': 'application/json'}, - method: 'GET', - queryParameters: { - 'battery_percentage': batteryLevel.toString(), - 'uid': uid, - }, - ); -} + + \ No newline at end of file diff --git a/backend/routers/firmware.py b/backend/routers/firmware.py index 9814fc6621..8b075d582b 100644 --- a/backend/routers/firmware.py +++ b/backend/routers/firmware.py @@ -72,17 +72,6 @@ async def get_latest_version(device: int): } -@router.get("/v1/firmware/battery-alert") -async def notify_low_batter(battery_percentage: int, uid: str): - - if battery_percentage < 20: - token = get_token_only(uid) - if token: - send_notification(token, 'Charge your OMI necklace🪫!','Your OMI necklace is running low on battery. Please charge it to avoid any interruptions.') - - return {'status': 'ok'} - - def extract_key_value_pairs(markdown_content): key_value_pattern = re.compile(r'', re.DOTALL) From dc1423b398a4c02a254d3c136fc6988d4df7cb84 Mon Sep 17 00:00:00 2001 From: Shrey Birmiwal Date: Tue, 11 Feb 2025 11:08:42 -0600 Subject: [PATCH 4/4] battery alert --- app/lib/backend/http/api/hardware.dart | 2 -- app/lib/providers/device_provider.dart | 21 +++++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/lib/backend/http/api/hardware.dart b/app/lib/backend/http/api/hardware.dart index 4693bbb7f3..4ac89b75e0 100644 --- a/app/lib/backend/http/api/hardware.dart +++ b/app/lib/backend/http/api/hardware.dart @@ -22,5 +22,3 @@ Future saveFcmTokenServer( } } - - \ No newline at end of file diff --git a/app/lib/providers/device_provider.dart b/app/lib/providers/device_provider.dart index 292f9c754d..9548d28e49 100644 --- a/app/lib/providers/device_provider.dart +++ b/app/lib/providers/device_provider.dart @@ -12,6 +12,8 @@ import 'package:instabug_flutter/instabug_flutter.dart'; class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption { + + CaptureProvider? captureProvider; bool isConnecting = false; @@ -23,6 +25,7 @@ class DeviceProvider extends ChangeNotifier int batteryLevel = -1; Timer? _reconnectionTimer; int connectionCheckSeconds = 4; + bool lowBatteryNotified = false; Timer? _disconnectNotificationTimer; @@ -106,7 +109,21 @@ class DeviceProvider extends ChangeNotifier connectedDevice!.id, onBatteryLevelChange: (int value) { batteryLevel = value; - // call teh low_battery_noti function in hardware.dart referecing the backend firmware.py @router.get("/v1/firmware/battery-alert") + + if (batteryLevel < 20) { + if (!lowBatteryNotified) { + NotificationService.instance.createNotification( + title: 'Charge your OMI necklace🪫!', + body: 'Your OMI necklace is running low on battery. Please charge it to avoid any interruptions.', + ); + lowBatteryNotified = true; + } + } else { + // Reset flag when battery charges above 20% + lowBatteryNotified = false; + } + + notifyListeners(); }, ); notifyListeners(); @@ -311,4 +328,4 @@ class DeviceProvider extends ChangeNotifier @override void onStatusChanged(DeviceServiceStatus status) {} -} +} \ No newline at end of file