diff --git a/lib/domain/connections_service.dart b/lib/domain/connections_service.dart index f077a512b..05e55b684 100644 --- a/lib/domain/connections_service.dart +++ b/lib/domain/connections_service.dart @@ -10,7 +10,6 @@ import 'package:cybearjinni/infrastructure/core/logger.dart'; import 'package:cybearjinni/infrastructure/network_utilities_flutter.dart'; import 'package:dartz/dartz.dart'; import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; import 'package:grpc/grpc.dart'; import 'package:location/location.dart'; import 'package:network_info_plus/network_info_plus.dart'; diff --git a/lib/domain/i_local_db_repository.dart b/lib/domain/i_local_db_repository.dart deleted file mode 100644 index af3283501..000000000 --- a/lib/domain/i_local_db_repository.dart +++ /dev/null @@ -1,34 +0,0 @@ -import 'package:cbj_integrations_controller/integrations_controller.dart'; -import 'package:cybearjinni/infrastructure/core/logger.dart'; -import 'package:cybearjinni/infrastructure/isar_local_db/isar_objects/home_entity_isar_model.dart'; -import 'package:cybearjinni/infrastructure/isar_local_db/isar_objects/hub_entity_isar_model.dart'; -import 'package:cybearjinni/infrastructure/isar_local_db/isar_objects/remote_pipes_isar_model.dart'; -import 'package:dartz/dartz.dart'; -import 'package:isar/isar.dart'; -import 'package:path_provider/path_provider.dart'; - -part 'package:cybearjinni/infrastructure/isar_local_db/isar_repository.dart'; - -abstract interface class ILocalDbRepository { - static ILocalDbRepository? _instance; - - static ILocalDbRepository get instance { - return _instance ??= _IsarRepository(); - } - - Future asyncConstructor(); - - Future> saveHubEntity({ - required String hubNetworkBssid, - required String networkName, - required String lastKnownIp, - }); - - Future> getRemotePipesDnsName(); - - Future> getHubEntityNetworkBssid(); - - Future> getHubEntityNetworkName(); - - Future> getHubEntityLastKnownIp(); -} diff --git a/lib/domain/manage_network/i_manage_network_repository.dart b/lib/domain/manage_network/i_manage_network_repository.dart index a5207c970..29fb092ff 100644 --- a/lib/domain/manage_network/i_manage_network_repository.dart +++ b/lib/domain/manage_network/i_manage_network_repository.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:cbj_integrations_controller/integrations_controller.dart'; import 'package:cybearjinni/domain/home_user/home_user_failures.dart'; import 'package:cybearjinni/domain/manage_network/manage_network_entity.dart'; import 'package:cybearjinni/domain/manage_network/manage_network_value_objects.dart'; @@ -7,6 +8,7 @@ import 'package:cybearjinni/infrastructure/core/logger.dart'; import 'package:dartz/dartz.dart'; import 'package:flutter/services.dart'; import 'package:kt_dart/collection.dart'; +import 'package:location/location.dart' as location; import 'package:network_info_plus/network_info_plus.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:wifi_iot/wifi_iot.dart'; @@ -22,6 +24,8 @@ abstract interface class IManageNetworkRepository { static ManageNetworkEntity? manageWiFiEntity; + Future loadWifi(); + Future> doesWiFiEnabled(); Stream>> diff --git a/lib/infrastructure/app_commands.dart b/lib/infrastructure/app_commands.dart index b92ee6ccf..d89682c28 100644 --- a/lib/infrastructure/app_commands.dart +++ b/lib/infrastructure/app_commands.dart @@ -4,11 +4,7 @@ import 'package:cbj_integrations_controller/integrations_controller.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:path_provider/path_provider.dart'; -class AppCommands implements IPhoneCommandsD { - AppCommands() { - IPhoneCommandsD.instance = this; - } - +class AppCommands extends SystemCommandsBaseClassD { @override Future getAllEtcReleaseFilesText() { // TODO: implement getAllEtcReleaseFilesText @@ -62,7 +58,7 @@ class AppCommands implements IPhoneCommandsD { } @override - Future getLocalDbPath(Future currentUserName) async { + Future getLocalDbPath() async { final Directory appDocDirectory = await getApplicationDocumentsDirectory(); return appDocDirectory.path; } @@ -86,8 +82,8 @@ class AppCommands implements IPhoneCommandsD { } @override - Future suspendComputer() { - // TODO: implement suspendComputer - throw UnimplementedError(); - } + Future suspendComputer() async => null; + + @override + Future getRaspberryPiDeviceVersion() async => null; } diff --git a/lib/infrastructure/isar_local_db/isar_objects/home_entity_isar_model.dart b/lib/infrastructure/isar_local_db/isar_objects/home_entity_isar_model.dart deleted file mode 100644 index d56475dd8..000000000 --- a/lib/infrastructure/isar_local_db/isar_objects/home_entity_isar_model.dart +++ /dev/null @@ -1,10 +0,0 @@ -import 'package:isar/isar.dart'; - -part 'home_entity_isar_model.g.dart'; - -@collection -class HomeEntityIsarModel { - Id id = Isar.autoIncrement; - - late String homeId; -} diff --git a/lib/infrastructure/isar_local_db/isar_objects/hub_entity_isar_model.dart b/lib/infrastructure/isar_local_db/isar_objects/hub_entity_isar_model.dart deleted file mode 100644 index f3fbd7888..000000000 --- a/lib/infrastructure/isar_local_db/isar_objects/hub_entity_isar_model.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'package:isar/isar.dart'; - -part 'hub_entity_isar_model.g.dart'; - -@collection -class HubEntityIsarModel { - Id id = Isar.autoIncrement; - - late String hubNetworkBssid; - late String networkName; - late String lastKnownIp; -} diff --git a/lib/infrastructure/isar_local_db/isar_objects/remote_pipes_isar_model.dart b/lib/infrastructure/isar_local_db/isar_objects/remote_pipes_isar_model.dart deleted file mode 100644 index 8b0944700..000000000 --- a/lib/infrastructure/isar_local_db/isar_objects/remote_pipes_isar_model.dart +++ /dev/null @@ -1,10 +0,0 @@ -import 'package:isar/isar.dart'; - -part 'remote_pipes_isar_model.g.dart'; - -@collection -class RemotePipesIsarModel { - Id id = Isar.autoIncrement; - - late String domainName; -} diff --git a/lib/infrastructure/isar_local_db/isar_repository.dart b/lib/infrastructure/isar_local_db/isar_repository.dart deleted file mode 100644 index 127a91753..000000000 --- a/lib/infrastructure/isar_local_db/isar_repository.dart +++ /dev/null @@ -1,137 +0,0 @@ -part of 'package:cybearjinni/domain/i_local_db_repository.dart'; - -class _IsarRepository implements ILocalDbRepository { - late Isar isar; - @override - Future asyncConstructor() async { - final dir = await getApplicationDocumentsDirectory(); - isar = await Isar.open( - [ - RemotePipesIsarModelSchema, - HubEntityIsarModelSchema, - HomeEntityIsarModelSchema, - ], - directory: dir.path, - ); - } - - @override - Future> saveHubEntity({ - required String hubNetworkBssid, - required String networkName, - required String lastKnownIp, - }) async { - try { - final HubEntityIsarModel hubEntityIsarModel = HubEntityIsarModel() - ..hubNetworkBssid = hubNetworkBssid - ..networkName = networkName - ..lastKnownIp = lastKnownIp; - - await isar.writeTxn(() async { - await isar.hubEntityIsarModels.clear(); - await isar.hubEntityIsarModels.put(hubEntityIsarModel); - }); - - logger.i('Hub entity got saved to local storage'); - } catch (e) { - logger.e('Error saving Hub entity to local storage: $e'); - - return left(const LocalDbFailures.unexpected()); - } - return right(unit); - } - - @override - Future> getRemotePipesDnsName() async { - try { - final List remotePipesIsarModelFromDb = - await isar.remotePipesIsarModels.where().findAll(); - - if (remotePipesIsarModelFromDb.isNotEmpty) { - final String remotePipesDnsName = - remotePipesIsarModelFromDb[0].domainName; - logger.i( - 'Remote pipes domain name is: ' - '${remotePipesIsarModelFromDb[0].domainName}', - ); - - return right(remotePipesDnsName); - } - - logger.i("Didn't find any remote pipes in the local DB"); - } catch (e) { - logger.e('Local DB isar getRemotePipesDnsName error: $e'); - } - return left(const LocalDbFailures.unexpected()); - } - - @override - Future> getHubEntityLastKnownIp() async { - try { - final List hubEntityIsarModelFromDb = - await isar.hubEntityIsarModels.where().findAll(); - - if (hubEntityIsarModelFromDb.isNotEmpty) { - final String hubLastKnownIp = hubEntityIsarModelFromDb[0].lastKnownIp; - logger.i( - 'Hub entity lastKnownIp is: ' - '${hubEntityIsarModelFromDb[0].lastKnownIp}', - ); - - return right(hubLastKnownIp); - } - - logger.i("Didn't find any Hub entity in the local DB"); - } catch (e) { - logger.e('Local DB isar getHubEntityLastKnownIp error: $e'); - } - return left(const LocalDbFailures.unexpected()); - } - - @override - Future> getHubEntityNetworkBssid() async { - try { - final List hubEntityIsarModelFromDb = - await isar.hubEntityIsarModels.where().findAll(); - - if (hubEntityIsarModelFromDb.isNotEmpty) { - final String hubNetworkBssid = - hubEntityIsarModelFromDb[0].hubNetworkBssid; - logger.i( - 'Hub entity network bssid is: ' - '${hubEntityIsarModelFromDb[0].hubNetworkBssid}', - ); - - return right(hubNetworkBssid); - } - - logger.i("Didn't find any Hub entity in the local DB"); - } catch (e) { - logger.e('Local DB isar getHubEntityNetworkBssid error: $e'); - } - return left(const LocalDbFailures.unexpected()); - } - - @override - Future> getHubEntityNetworkName() async { - try { - final List hubEntityIsarModelFromDb = - await isar.hubEntityIsarModels.where().findAll(); - - if (hubEntityIsarModelFromDb.isNotEmpty) { - final String hubNetworkName = hubEntityIsarModelFromDb[0].networkName; - logger.i( - 'Hub entity network name is: ' - '${hubEntityIsarModelFromDb[0].networkName}', - ); - - return right(hubNetworkName); - } - - logger.i("Didn't find any Hub entity in the local DB"); - } catch (e) { - logger.e('Local DB isar getHubEntityNetworkName error: $e'); - } - return left(const LocalDbFailures.unexpected()); - } -} diff --git a/lib/infrastructure/manage_wifi_repository.dart b/lib/infrastructure/manage_wifi_repository.dart index 6dd1e6aea..ec716c99e 100644 --- a/lib/infrastructure/manage_wifi_repository.dart +++ b/lib/infrastructure/manage_wifi_repository.dart @@ -2,6 +2,7 @@ part of 'package:cybearjinni/domain/manage_network/i_manage_network_repository.d class _ManageWiFiRepository implements IManageNetworkRepository { final NetworkSecurity networkSecurity = NetworkSecurity.WPA; + NetworkObject? network; @override Future> doesWiFiEnabled() async { @@ -125,4 +126,63 @@ class _ManageWiFiRepository implements IManageNetworkRepository { } return wifiName; } + + @override + Future loadWifi() async { + // TODO: Get bssid + final NetworkInfo info = NetworkInfo(); + final PermissionStatus locationStatus = await Permission.location.status; + if (locationStatus.isDenied) { + await Permission.locationWhenInUse.request(); + } + if (await Permission.location.isRestricted) { + await openAppSettings(); + } + + final bool isWifiEnabled = await WiFiForIoTPlugin.isEnabled(); + final bool isWifiConnected = await WiFiForIoTPlugin.isConnected(); + + if (!isWifiEnabled || !isWifiConnected) { + logger.w('Not connected to WiFi'); + exit(0); + } + String bssid; + if (await Permission.location.isGranted) { + final String? bssidTemp = await info.getWifiBSSID(); + if (bssidTemp == null || bssidTemp == "02:00:00:00:00:00") { + logger.w( + 'Location is not on or user-allowed approximate location instead of precise location', + ); + exit(0); + } + bssid = bssidTemp; + } else { + logger.w('Missing location permission'); + exit(0); + } + final String? ssid = await WiFiForIoTPlugin.getSSID(); + final List? ipSplit = (await WiFiForIoTPlugin.getIP())?.split('.'); + String? subNet; + if (ipSplit != null) { + subNet = ipSplit.sublist(0, ipSplit.length - 1).join(); + } + final location.LocationData locationData = + await location.Location().getLocation(); + if (ssid == null || subNet == null) { + logger.w('Ssid or subnet is null'); + exit(0); + } + + final NetworkObject network = NetworkObject( + bssid: bssid, + ssid: ssid, + subNet: subNet, + longitude: locationData.longitude, + latitude: locationData.latitude, + remotePipe: null, + type: null, + ); + NetworksManager().addNetwork(network); + NetworksManager().setCurrentNetwork(network.uniqueId); + } } diff --git a/lib/infrastructure/mqtt.dart b/lib/infrastructure/mqtt.dart index 27a67a14a..e0541a15e 100644 --- a/lib/infrastructure/mqtt.dart +++ b/lib/infrastructure/mqtt.dart @@ -69,10 +69,6 @@ class MqttServerRepository extends IMqttServerRepository { final DeviceEntityBase deviceEntityBase = entityFromTheApp.toDomain(); deviceEntityBase.entityStateGRPC = EntityState.state(EntityStateGRPC.waitingInComp); - - /// Sends directly to device connector conjecture - // ISavedDevicesRepo.instance.addOrUpdateFromMqtt(deviceEntityBase); - return; } else if (entityFromTheApp is DeviceEntityBase) { entityFromTheApp.entityStateGRPC = diff --git a/lib/presentation/pages/splash_page.dart b/lib/presentation/pages/splash_page.dart index 6c528ec64..7ec2d5c52 100644 --- a/lib/presentation/pages/splash_page.dart +++ b/lib/presentation/pages/splash_page.dart @@ -2,9 +2,8 @@ import 'dart:io'; import 'package:auto_route/auto_route.dart'; import 'package:cbj_integrations_controller/integrations_controller.dart'; -import 'package:cbj_smart_device_flutter/commands/flutter_commands.dart'; import 'package:cybearjinni/domain/connections_service.dart'; -import 'package:cybearjinni/domain/i_local_db_repository.dart'; +import 'package:cybearjinni/domain/manage_network/i_manage_network_repository.dart'; import 'package:cybearjinni/infrastructure/app_commands.dart'; import 'package:cybearjinni/infrastructure/mqtt.dart'; import 'package:cybearjinni/presentation/atoms/atoms.dart'; @@ -28,24 +27,16 @@ class _SplashPageState extends State { } Future initilizeApp() async { - // TODO: can we remove + SystemCommandsBaseClassD.instance = AppCommands(); await Hive.initFlutter(); - AppCommands(); - // TODO: can we remove - await Future.value([ - IDbRepository.instance.initializeDb(isFlutter: true), - ILocalDbRepository.instance.asyncConstructor(), - // ISavedDevicesRepo.instance.setUpAllFromDb(), - ]); - // TODO: can we remove + IcSynchronizer.initializeIntegrationsController(); + await IManageNetworkRepository.instance.loadWifi(); + ConnectionsService.setCurrentConnectionType(ConnectionType.appAsHub); + _navigate(); + + // TODO: Only here so that app will not crash MqttServerRepository(); - // TODO: Same as App Command? - PhoneCommandsD(); - SystemCommandsManager(); - // TODO: can we remove NodeRedRepository(); - ConnectionsService.instance; - _navigate(); } void _navigate() {