From ca02e275c4adf8d131ed0a598d0e8ba6ebd281d1 Mon Sep 17 00:00:00 2001 From: Guy Luz Date: Sat, 13 Jan 2024 17:06:22 +0200 Subject: [PATCH] wip Hub support --- lib/domain/connections_service.dart | 24 +- lib/domain/i_hub_connection_repository.dart | 1 + .../app_connection_service.dart | 3 + .../demo_connection_service.dart | 3 + .../hub_connection_service.dart | 287 ++++++++++++++++++ .../none_connection_service.dart | 3 + .../hub_client/hub_connection_repository.dart | 54 ++-- lib/presentation/core/routes/app_router.dart | 1 + .../computer_connection_check_widget.dart | 5 +- .../configure_new_cbj_comp_widget.dart | 3 +- .../pages/comunication_method_page.dart | 61 ++++ .../pages/home_page/home_page.dart | 4 + lib/presentation/pages/plus_button.dart | 48 ++- 13 files changed, 437 insertions(+), 60 deletions(-) create mode 100644 lib/infrastructure/connection_service/hub_connection_service.dart create mode 100644 lib/presentation/pages/comunication_method_page.dart diff --git a/lib/domain/connections_service.dart b/lib/domain/connections_service.dart index 0c4a1e42..b3b31239 100644 --- a/lib/domain/connections_service.dart +++ b/lib/domain/connections_service.dart @@ -1,10 +1,24 @@ import 'dart:async'; import 'dart:collection'; +import 'dart:io'; import 'package:cbj_integrations_controller/integrations_controller.dart'; +import 'package:connectivity_plus/connectivity_plus.dart'; +import 'package:cybearjinni/infrastructure/core/injection.dart'; +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:grpc/grpc.dart'; +import 'package:location/location.dart'; +import 'package:network_info_plus/network_info_plus.dart'; +import 'package:network_tools_flutter/network_tools_flutter.dart'; +import 'package:permission_handler/permission_handler.dart' + as permission_handler; +import 'package:rxdart/rxdart.dart'; part 'package:cybearjinni/infrastructure/connection_service/app_connection_service.dart'; +part 'package:cybearjinni/infrastructure/connection_service/hub_connection_service.dart'; part 'package:cybearjinni/infrastructure/connection_service/demo_connection_service.dart'; part 'package:cybearjinni/infrastructure/connection_service/none_connection_service.dart'; @@ -30,13 +44,16 @@ abstract interface class ConnectionsService { if (connectionType == _currentConnectionType) { return; } - final ConnectionsService? oldInstance = _instance; + _instance?.dispose(); + switch (connectionType) { case ConnectionType.appAsHub: _instance = _AppConnectionService(); _currentConnectionType = ConnectionType.appAsHub; - case ConnectionType.demo: case ConnectionType.hub: + _currentConnectionType = ConnectionType.hub; + _instance = _HubConnectionService(); + case ConnectionType.demo: case ConnectionType.remotePipes: _instance = _DemoConnectionService(); _currentConnectionType = ConnectionType.demo; @@ -45,11 +62,12 @@ abstract interface class ConnectionsService { _instance = _NoneConnectionService(); _currentConnectionType = ConnectionType.none; } - oldInstance?.dispose(); } static ConnectionType getCurrentConnectionType() => _currentConnectionType; + Future connect(); + Future searchDevices(); void setEntityState(ActionObject action); diff --git a/lib/domain/i_hub_connection_repository.dart b/lib/domain/i_hub_connection_repository.dart index 40cd72a8..a61938f9 100644 --- a/lib/domain/i_hub_connection_repository.dart +++ b/lib/domain/i_hub_connection_repository.dart @@ -5,6 +5,7 @@ import 'package:cbj_integrations_controller/integrations_controller.dart'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:cybearjinni/domain/i_local_db_repository.dart'; import 'package:cybearjinni/infrastructure/core/injection.dart'; +import 'package:cybearjinni/infrastructure/core/logger.dart'; import 'package:dartz/dartz.dart'; import 'package:flutter/foundation.dart' show kIsWeb; import 'package:location/location.dart'; diff --git a/lib/infrastructure/connection_service/app_connection_service.dart b/lib/infrastructure/connection_service/app_connection_service.dart index 620e997a..67e6f712 100644 --- a/lib/infrastructure/connection_service/app_connection_service.dart +++ b/lib/infrastructure/connection_service/app_connection_service.dart @@ -61,4 +61,7 @@ class _AppConnectionService implements ConnectionsService { @override Future> getVendors() async => IcSynchronizer().getVendors(); + + @override + Future connect() async => true; } diff --git a/lib/infrastructure/connection_service/demo_connection_service.dart b/lib/infrastructure/connection_service/demo_connection_service.dart index f23916c0..72c6522c 100644 --- a/lib/infrastructure/connection_service/demo_connection_service.dart +++ b/lib/infrastructure/connection_service/demo_connection_service.dart @@ -59,4 +59,7 @@ class _DemoConnectionService implements ConnectionsService { @override Future> getVendors() async => IcSynchronizer().getVendors(); + + @override + Future connect() async => true; } diff --git a/lib/infrastructure/connection_service/hub_connection_service.dart b/lib/infrastructure/connection_service/hub_connection_service.dart new file mode 100644 index 00000000..78a6d603 --- /dev/null +++ b/lib/infrastructure/connection_service/hub_connection_service.dart @@ -0,0 +1,287 @@ +part of 'package:cybearjinni/domain/connections_service.dart'; + +class _HubConnectionService implements ConnectionsService { + _HubConnectionService() { + if (currentEnvApp == EnvApp.prod) { + hubPort = 50055; + } else { + hubPort = 60055; + } + } + + /// Port to connect to the cbj hub, will change according to the current + /// running environment + late int hubPort; + + String? hubIp; + String? networkBssid; + String? networkName; + + ClientChannel? channel; + CbjHubClient? stub; + + StreamController>? entitiesStream; + StreamController>? areasStream; + + BehaviorSubject hubMessagesToApp = + BehaviorSubject(); + + // StreamController appMessagesToHub = + // StreamController(); + + BehaviorSubject appMessagesToHub = BehaviorSubject(); + + @override + Future dispose() async { + entitiesStream?.close(); + } + + @override + Future> get getAllEntities async { + appMessagesToHub.sink + .add(ClientStatusRequests(sendingType: SendingType.firstConnection)); + await for (final RequestsAndStatusFromHub message + in hubMessagesToApp.stream) { + logger.i('message from hub is $message'); + } + return HashMap(); + } + + @override + Future> get getAllAreas async => HashMap(); + + @override + Future searchDevices() async { + try { + final Either locationRequest = + await askLocationPermissionAndLocationOn(); + + if (locationRequest.isLeft()) { + return locationRequest; + } + + logger.i('searchForHub'); + String? appDeviceIp; + if (await Connectivity().checkConnectivity() == ConnectivityResult.wifi && + !kIsWeb) { + final NetworkInfo networkInfo = NetworkInfo(); + networkName = await networkInfo.getWifiName(); + appDeviceIp = await networkInfo.getWifiIP(); + networkBssid = await networkInfo.getWifiBSSID(); + } else { + return false; + // if (deviceIpOnTheNetwork == null) { + // // Issue https://github.com/CyBear-Jinni/cbj_app/issues/256 + // return left( + // const HubFailures + // .findingHubWhenConnectedToEthernetCableIsNotSupported(), + // ); + // } + + // currentDeviceIP = deviceIpOnTheNetwork; + // networkBSSID = 'no:Network:Bssid:Found'; + // networkName = 'noNetworkNameFound'; + // if (isThatTheIpOfTheHub != null && isThatTheIpOfTheHub) { + // return insertHubInfo( + // networkIp: currentDeviceIP, + // networkBSSID: networkBSSID, + // networkName: networkName, + // ); + // } + } + if (appDeviceIp == null) { + return false; + } + final String subnet = + appDeviceIp.substring(0, appDeviceIp.lastIndexOf('.')); + + logger.i('Hub Search subnet IP $subnet'); + + // TODO: Search for hub + final Stream devicesWithPort = + HostScanner.scanDevicesForSinglePort( + subnet, + hubPort, + + /// TODO: return this settings when can use with the await for loop + // resultsInIpAscendingOrder: false, + timeout: const Duration(milliseconds: 600), + ); + + await for (final ActiveHost activeHost in devicesWithPort) { + logger.i('Found Cbj Hub device: ${activeHost.address}'); + hubIp = activeHost.address; + return true; + // if (networkBSSID != null && networkName != null) { + // return insertHubInfo( + // networkIp: activeHost.address, + // networkBSSID: networkBSSID, + // networkName: networkName, + // ); + // } + } + } catch (e) { + logger.w('Exception searchForHub\n$e'); + } + } + + @override + void setEntityState(ActionObject action) {} + + @override + Stream> watchEntities() { + entitiesStream?.close(); + + entitiesStream = StreamController.broadcast(); + return entitiesStream!.stream; + } + + @override + Stream> watchAreas() { + areasStream?.close(); + + areasStream = StreamController.broadcast(); + return areasStream!.stream; + } + + @override + Future setNewArea(AreaEntity area) async {} + + @override + Future setEtitiesToArea(String areaId, HashSet entities) async {} + + @override + Future addScene(SceneCbjEntity scene) async {} + + @override + Future> getScenes() async => HashMap(); + + @override + Future activateScene(String id) async {} + + @override + Future loginVendor(VendorLoginEntity value) async {} + + @override + Future> getVendors() async => + IcSynchronizer().getVendors(); + + @override + Future connect() async { + searchDevices(); + if (hubIp == null) { + return false; + } + + connectDirectlyToHub(); + + return true; + } + + Future> askLocationPermissionAndLocationOn() async { + final Location location = Location(); + + bool serviceEnabled; + PermissionStatus permissionGranted; + + int permissionCounter = 0; + int disabledCounter = 0; + + // Get location permission is not supported on Linux + if (Platform.isLinux || Platform.isWindows) { + return right(unit); + } + + while (true) { + permissionGranted = await location.hasPermission(); + if (permissionGranted == PermissionStatus.denied) { + permissionGranted = await location.requestPermission(); + if (permissionGranted != PermissionStatus.granted) { + logger.e('Permission to use location is denied'); + await Future.delayed(const Duration(seconds: 10)); + + permissionCounter++; + if (permissionCounter > 5) { + permission_handler.openAppSettings(); + } else if (permissionCounter > 7) { + return const Left(HubFailures.unexpected()); + } + continue; + } + } + + serviceEnabled = await location.serviceEnabled(); + if (!serviceEnabled) { + serviceEnabled = await location.requestService(); + if (!serviceEnabled) { + disabledCounter++; + if (disabledCounter > 2) { + return const Left(HubFailures.unexpected()); + } + logger.w('Location is disabled'); + await Future.delayed(const Duration(seconds: 5)); + continue; + } + } + break; + } + return right(unit); + } + + /// Connect directly to the Hub if possible + Future connectDirectlyToHub() async { + if (hubIp == null) { + return; + } + + // Socket? testHubConnection; + // try { + // testHubConnection = await Socket.connect( + // hubIP, + // hubPort, + // timeout: const Duration(milliseconds: 500), + // ); + // await testHubConnection.close(); + // testHubConnection.destroy(); + // testHubConnection = null; + // } catch (e) { + // await testHubConnection?.close(); + // testHubConnection?.destroy(); + // } + + try { + // // TODO: DELTE + // await HubClient.createStreamWithHub( + // hubIp!, + // hubPort, + // ); + + channel = ClientChannel( + hubIp!, + port: hubPort, + options: + const ChannelOptions(credentials: ChannelCredentials.insecure()), + ); + + channel!.onConnectionStateChanged.listen((event) { + logger.i('gRPC connection state $event'); + }); + + stub = CbjHubClient(channel!); + + final ResponseStream response = + stub!.clientTransferEntities( + appMessagesToHub.stream, + ); + + // appMessagesToHub.sink + // .add(ClientStatusRequests(sendingType: SendingType.firstConnection)); + + hubMessagesToApp.addStream(response); + await Future.delayed(const Duration(seconds: 3)); + } catch (e) { + logger.e('Caught error while stream with hub\n$e'); + await channel?.shutdown(); + } + } +} diff --git a/lib/infrastructure/connection_service/none_connection_service.dart b/lib/infrastructure/connection_service/none_connection_service.dart index d3a8f8d1..8e8555db 100644 --- a/lib/infrastructure/connection_service/none_connection_service.dart +++ b/lib/infrastructure/connection_service/none_connection_service.dart @@ -58,4 +58,7 @@ class _NoneConnectionService implements ConnectionsService { @override Future> getVendors() async => []; + + @override + Future connect() async => true; } diff --git a/lib/infrastructure/hub_client/hub_connection_repository.dart b/lib/infrastructure/hub_client/hub_connection_repository.dart index 2c2b24a4..629ca963 100644 --- a/lib/infrastructure/hub_client/hub_connection_repository.dart +++ b/lib/infrastructure/hub_client/hub_connection_repository.dart @@ -43,7 +43,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { try { connectivityResult = await Connectivity().checkConnectivity(); } catch (e) { - icLogger.w('Cant check connectivity this is probably PC, error\n$e'); + logger.w('Cant check connectivity this is probably PC, error\n$e'); } // Last Number of bssid can change fix?, need to check if more numbers @@ -59,7 +59,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { wifiBSSIDWithoutLastNumber = wifiBSSID?.substring(0, wifiBSSID.lastIndexOf(':')); } catch (e) { - icLogger.w("Can't get WiFi BSSID"); + logger.w("Can't get WiFi BSSID"); } final Either remotePipesInformation = await ILocalDbRepository.instance.getRemotePipesDnsName(); @@ -81,14 +81,14 @@ class _HubConnectionRepository implements IHubConnectionRepository { (kIsWeb && savedWifiBssidWithoutLastNumber == 'no:Network:Bssid')) { (await openAndroidWifiSettingIfPossible()).fold( (l) { - icLogger + logger .w('No way to establish connection with the Hub, WiFi or location' ' permission is closed for here'); return; }, (r) async {}, ); - icLogger.i('Connect using direct connection to Hub'); + logger.i('Connect using direct connection to Hub'); await connectDirectlyToHub(); return; @@ -126,7 +126,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { networkName: hubNetworkName!, ).toDomain(); } catch (e) { - icLogger.e('Crashed while setting Hub info from local db\n$e'); + logger.e('Crashed while setting Hub info from local db\n$e'); } } @@ -134,7 +134,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { try { connectivityResult = await Connectivity().checkConnectivity(); } catch (e) { - icLogger.w('Cant check connectivity this is probably PC, error\n$e'); + logger.w('Cant check connectivity this is probably PC, error\n$e'); } // Last Number of bssid can change fix?, need to check if more numbers @@ -158,7 +158,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { savedWifiBssidWithoutLastNumber != null && wifiBSSIDWithoutLastNumber != null && savedWifiBssidWithoutLastNumber == wifiBSSIDWithoutLastNumber) { - icLogger.i('Connect using direct connection to Hub'); + logger.i('Connect using direct connection to Hub'); if (IHubConnectionRepository.hubEntity?.lastKnownIp.getOrCrash() != null) { @@ -191,16 +191,16 @@ class _HubConnectionRepository implements IHubConnectionRepository { } return right(compHubInfo); } catch (e) { - icLogger.e('Error getting hubInfo\n$e'); + logger.e('Error getting hubInfo\n$e'); return left(const HubFailures.unexpected()); } // return; } else { - icLogger.i('Connect using Remote Pipes'); + logger.i('Connect using Remote Pipes'); return (await ILocalDbRepository.instance.getRemotePipesDnsName()).fold( (l) { - icLogger.e('Cant find local Remote Pipes Dns name'); + logger.e('Cant find local Remote Pipes Dns name'); return left(const HubFailures.unexpected()); }, (r) async { try { @@ -212,7 +212,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { } return right(compHubInfo); } catch (e) { - icLogger.e('Error getting hubInfo\n$e'); + logger.e('Error getting hubInfo\n$e'); return left(const HubFailures.unexpected()); } }); @@ -250,7 +250,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { ResourceRecordQuery.addressIPv4(mDnsName), )) { deviceIp = record.address.address; - icLogger.i('Found address (${record.address}).'); + logger.i('Found address (${record.address}).'); } // await for (final IPAddressResourceRecord record @@ -261,7 +261,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { client.stop(); - icLogger.t('Done.'); + logger.t('Done.'); return deviceIp; } @@ -279,7 +279,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { return locationRequest; } - icLogger.i('searchForHub'); + logger.i('searchForHub'); String? currentDeviceIP; String? networkBSSID; @@ -314,7 +314,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { final String subnet = currentDeviceIP!.substring(0, currentDeviceIP.lastIndexOf('.')); - icLogger.i('Hub Search subnet IP $subnet'); + logger.i('Hub Search subnet IP $subnet'); // TODO: Search for hub // final Stream devicesWithPort = @@ -328,7 +328,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { // ); // await for (final ActiveHost activeHost in devicesWithPort) { - // icLogger.i('Found Cbj Hub device: ${activeHost.address}'); + // logger.i('Found Cbj Hub device: ${activeHost.address}'); // if (networkBSSID != null && networkName != null) { // return insertHubInfo( // networkIp: activeHost.address, @@ -338,7 +338,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { // } // } } catch (e) { - icLogger.w('Exception searchForHub\n$e'); + logger.w('Exception searchForHub\n$e'); } await Future.delayed(const Duration(seconds: 5)); return left(const HubFailures.cantFindHubInNetwork()); @@ -346,7 +346,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { @override Future saveHubIP(String hubIP) async { - icLogger.w('saveHubIP'); + logger.w('saveHubIP'); } Future> askLocationPermissionAndLocationOn() async { @@ -368,7 +368,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { if (permissionGranted == PermissionStatus.denied) { permissionGranted = await location.requestPermission(); if (permissionGranted != PermissionStatus.granted) { - icLogger.e('Permission to use location is denied'); + logger.e('Permission to use location is denied'); await Future.delayed(const Duration(seconds: 10)); permissionCounter++; @@ -389,7 +389,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { if (disabledCounter > 2) { return const Left(HubFailures.unexpected()); } - icLogger.w('Location is disabled'); + logger.w('Location is disabled'); await Future.delayed(const Duration(seconds: 5)); continue; } @@ -420,8 +420,8 @@ class _HubConnectionRepository implements IHubConnectionRepository { lastKnownIp: hubDtos.lastKnownIp, )) .fold( - (l) => icLogger.e('Cant find local Remote Pipes Dns name'), - (r) => icLogger.i('Found CyBear Jinni Hub'), + (l) => logger.e('Cant find local Remote Pipes Dns name'), + (r) => logger.i('Found CyBear Jinni Hub'), ); return right(unit); } @@ -457,7 +457,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { networkName: hubNetworkName!, ).toDomain(); } catch (e) { - icLogger.e('Crashed while setting Hub info from local db\n$e'); + logger.e('Crashed while setting Hub info from local db\n$e'); } } @@ -508,7 +508,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { (l) async { (await openAndroidWifiSettingIfPossible()).fold( (l) { - icLogger.w( + logger.w( 'No way to establish connection with the Hub, WiFi or location' ' permission is closed'); }, @@ -518,7 +518,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { ); }, (r) { - icLogger.i('Connect using Remote Pipes'); + logger.i('Connect using Remote Pipes'); HubClient.createStreamWithHub(r, 50056); tryAgainConnectToTheHubOnceMore = 0; }, @@ -556,7 +556,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { // } // } } else { - icLogger.w( + logger.w( 'Will ask the user to open WiFi and gps to try local connection', ); final bool wifiEnabled = await WiFiForIoTPlugin.isEnabled(); @@ -568,7 +568,7 @@ class _HubConnectionRepository implements IHubConnectionRepository { } (await askLocationPermissionAndLocationOn()).fold((l) { - icLogger.e( + logger.e( 'User does not allow opening location and does not have remote pipes info', ); }, (r) async { diff --git a/lib/presentation/core/routes/app_router.dart b/lib/presentation/core/routes/app_router.dart index 903ba0d9..beda6cc6 100644 --- a/lib/presentation/core/routes/app_router.dart +++ b/lib/presentation/core/routes/app_router.dart @@ -32,6 +32,7 @@ class AppRouter extends $AppRouter { AutoRoute(page: EntitiesInNetworkRoute.page), AutoRoute(page: EntitiesInAreaRoute.page), AutoRoute(page: LoginVendorRoute.page), + AutoRoute(page: ComunicationMethodRoute.page), // Pages from packages AutoRoute(page: SmartCameraContainerRoute.page), diff --git a/lib/presentation/pages/add_new_devices_process/computer_connection_check/widgets/computer_connection_check_widget.dart b/lib/presentation/pages/add_new_devices_process/computer_connection_check/widgets/computer_connection_check_widget.dart index c5de27c4..8e734e0f 100644 --- a/lib/presentation/pages/add_new_devices_process/computer_connection_check/widgets/computer_connection_check_widget.dart +++ b/lib/presentation/pages/add_new_devices_process/computer_connection_check/widgets/computer_connection_check_widget.dart @@ -7,6 +7,7 @@ import 'package:cybearjinni/domain/cbj_comp/i_cbj_comp_repository.dart'; import 'package:cybearjinni/domain/manage_network/i_manage_network_repository.dart'; import 'package:cybearjinni/domain/manage_network/manage_network_entity.dart'; import 'package:cybearjinni/domain/security_bear/i_security_bear_connection_repository.dart'; +import 'package:cybearjinni/infrastructure/core/logger.dart'; import 'package:cybearjinni/presentation/atoms/atoms.dart'; import 'package:cybearjinni/presentation/pages/add_new_devices_process/configure_new_cbj_comp/widgets/configure_new_cbj_comp_widget.dart'; import 'package:dartz/dartz.dart' as dartz; @@ -61,7 +62,7 @@ class _ComputerConnectionCheckWidgetState } bool isConnectedToWifi = false; - icLogger.i('Waiting for user to get connected to WiFi'); + logger.i('Waiting for user to get connected to WiFi'); while (true) { isConnectedToWifi = await WiFiForIoTPlugin.isConnected(); @@ -124,7 +125,7 @@ class _ComputerConnectionCheckWidgetState deviceE..cbjEntityName = CbjEntityName(deviceName), ); } catch (e) { - icLogger.w("Can't add unsupported device"); + logger.w("Can't add unsupported device"); } }); final CbjCompEntity compUpdatedData = cbjCompEntity.copyWith( diff --git a/lib/presentation/pages/add_new_devices_process/configure_new_cbj_comp/widgets/configure_new_cbj_comp_widget.dart b/lib/presentation/pages/add_new_devices_process/configure_new_cbj_comp/widgets/configure_new_cbj_comp_widget.dart index d892863e..ee1affc7 100644 --- a/lib/presentation/pages/add_new_devices_process/configure_new_cbj_comp/widgets/configure_new_cbj_comp_widget.dart +++ b/lib/presentation/pages/add_new_devices_process/configure_new_cbj_comp/widgets/configure_new_cbj_comp_widget.dart @@ -8,6 +8,7 @@ import 'package:cybearjinni/domain/manage_network/i_manage_network_repository.da import 'package:cybearjinni/domain/manage_network/manage_network_entity.dart'; import 'package:cybearjinni/domain/security_bear/i_security_bear_connection_repository.dart'; import 'package:cybearjinni/domain/security_bear/security_bear_failures.dart'; +import 'package:cybearjinni/infrastructure/core/logger.dart'; import 'package:cybearjinni/presentation/atoms/atoms.dart'; import 'package:cybearjinni/presentation/core/routes/app_router.gr.dart'; import 'package:cybearjinni/presentation/molecules/molecules.dart'; @@ -112,7 +113,7 @@ class _ConfigureNewCbjCompWidgetsState deviceE..cbjEntityName = CbjEntityName(deviceName), ); } catch (e) { - icLogger.w("Can't add unsupported device"); + logger.w("Can't add unsupported device"); } }); final CbjCompEntity compUpdatedData = cbjCompEntity.copyWith( diff --git a/lib/presentation/pages/comunication_method_page.dart b/lib/presentation/pages/comunication_method_page.dart new file mode 100644 index 00000000..63c8ba6c --- /dev/null +++ b/lib/presentation/pages/comunication_method_page.dart @@ -0,0 +1,61 @@ +import 'package:auto_route/auto_route.dart'; +import 'package:cybearjinni/domain/connections_service.dart'; +import 'package:cybearjinni/presentation/atoms/atoms.dart'; +import 'package:cybearjinni/presentation/core/routes/app_router.gr.dart'; +import 'package:cybearjinni/presentation/organisms/organisms.dart'; +import 'package:flutter/material.dart'; + +@RoutePage() +class ComunicationMethodPage extends StatelessWidget { + @override + Widget build(BuildContext context) { + return PageOrganism( + pageName: 'Comunication type', + child: Column( + children: [ + const TextAtom('Chose comunication Method'), + TextAtom( + 'Current comunication: ${ConnectionsService.getCurrentConnectionType()}', + ), + Expanded( + child: Column( + children: [ + ButtonWidgetAtom( + variant: ButtonVariant.primary, + text: 'App as a Hub', + onPressed: () => ConnectionsService.setCurrentConnectionType( + ConnectionType.appAsHub, + ), + ), + ButtonWidgetAtom( + variant: ButtonVariant.primary, + text: 'Hub', + onPressed: () { + ConnectionsService.setCurrentConnectionType( + ConnectionType.hub, + ); + ConnectionsService.instance.connect(); + }, + ), + ButtonWidgetAtom( + variant: ButtonVariant.primary, + text: 'Demo', + onPressed: () => ConnectionsService.setCurrentConnectionType( + ConnectionType.demo, + ), + ), + ], + ), + ), + ButtonWidgetAtom( + variant: ButtonVariant.primary, + text: 'Insert Remote Pipes', + onPressed: () { + context.router.push(const RemotePipesRoute()); + }, + ), + ], + ), + ); + } +} diff --git a/lib/presentation/pages/home_page/home_page.dart b/lib/presentation/pages/home_page/home_page.dart index de48cccd..13b50c09 100644 --- a/lib/presentation/pages/home_page/home_page.dart +++ b/lib/presentation/pages/home_page/home_page.dart @@ -210,7 +210,11 @@ class _HomePageState extends State { GestureDetector( onTap: () async { await context.router.push(const PlusButtonRoute()); + areas = null; + entities = null; _initialzeAreas(); + _initialzeEntities(); + initializedScenes(); }, child: CircleAvatar( diff --git a/lib/presentation/pages/plus_button.dart b/lib/presentation/pages/plus_button.dart index cc95ceb5..0d8d0369 100644 --- a/lib/presentation/pages/plus_button.dart +++ b/lib/presentation/pages/plus_button.dart @@ -94,31 +94,6 @@ class PlusButtonPage extends StatelessWidget { }, ), ), - if (ConnectionsService.getCurrentConnectionType() == - ConnectionType.hub) ...[ - const SizedBox(height: 1), - ColoredBox( - color: Colors.blue, - child: ListTile( - leading: FaIcon( - FontAwesomeIcons.globe, - color: colorScheme.background, - ), - title: TextAtom( - 'Add Remote Control Support', - style: TextStyle( - color: Theme.of(context) - .textTheme - .bodyLarge! - .color, - ), - ), - onTap: () { - context.router.push(const RemotePipesRoute()); - }, - ), - ), - ], const SizedBox(height: 1), ColoredBox( color: Colors.purple.withOpacity(0.7), @@ -185,9 +160,28 @@ class PlusButtonPage extends StatelessWidget { color: Colors.white, child: Column( children: [ - const SizedBox( - height: 1, + const SizedBox(height: 1), + ColoredBox( + color: Colors.blue, + child: ListTile( + leading: FaIcon( + FontAwesomeIcons.globe, + color: colorScheme.background, + ), + title: TextAtom( + 'Comunication Method', + style: TextStyle( + color: + Theme.of(context).textTheme.bodyLarge!.color, + ), + ), + onTap: () { + context.router + .push(const ComunicationMethodRoute()); + }, + ), ), + const SizedBox(height: 1), ColoredBox( color: Colors.pink.withOpacity(0.9), child: ListTile(