Skip to content

Commit

Permalink
Added db support
Browse files Browse the repository at this point in the history
  • Loading branch information
guyluz11 committed Feb 2, 2024
1 parent 9c14294 commit df38738
Show file tree
Hide file tree
Showing 36 changed files with 248 additions and 229 deletions.
1 change: 1 addition & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extensions:
17 changes: 10 additions & 7 deletions lib/domain/connections_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ abstract interface class ConnectionsService {
static ConnectionsService? _instance;

static ConnectionsService get instance {
return _instance ??= _AppConnectionService();
return _instance ??= _NoneConnectionService();
}

static ConnectionType _currentConnectionType = ConnectionType.appAsHub;
static ConnectionType _currentConnectionType = ConnectionType.none;

static void setCurrentConnectionType(ConnectionType connectionType) {
static void setCurrentConnectionType({
required String networkBssid,
required ConnectionType connectionType,
}) {
if (connectionType == _currentConnectionType) {
return;
}
Expand All @@ -52,13 +55,13 @@ abstract interface class ConnectionsService {

switch (connectionType) {
case ConnectionType.appAsHub:
_instance = _AppConnectionService();
_instance = _AppConnectionService(networkBssid);
case ConnectionType.hub:
_instance = _HubConnectionService();
_instance = _HubConnectionService(networkBssid);
case ConnectionType.remotePipes:
_instance = _RemotePipesConnectionService();
_instance = _RemotePipesConnectionService(networkBssid);
case ConnectionType.demo:
_instance = _DemoConnectionService();
_instance = _DemoConnectionService(networkBssid);
case ConnectionType.none:
_instance = _NoneConnectionService();
}
Expand Down
18 changes: 10 additions & 8 deletions lib/infrastructure/connection_service/app_connection_service.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
part of 'package:cybearjinni/domain/connections_service.dart';

class _AppConnectionService implements ConnectionsService {
_AppConnectionService() {
searchDevicesInstance = SearchDevices();
searchDevices();
}
_AppConnectionService(this.networkBssid);

late SearchDevices searchDevicesInstance;
String networkBssid;
SearchDevices? searchDevicesInstance;

@override
Future<HashMap<String, DeviceEntityBase>> get getEntities async =>
Expand All @@ -17,8 +15,12 @@ class _AppConnectionService implements ConnectionsService {
IcSynchronizer().getAreas();

@override
Future searchDevices() =>
searchDevicesInstance.startSearchIsolate(NetworkUtilitiesFlutter());
Future searchDevices() async =>
searchDevicesInstance = (searchDevicesInstance ?? SearchDevices())
..startSearchIsolate(
networkUtilitiesType: NetworkUtilitiesFlutter(),
systemCommands: SystemCommandsBaseClassD.instance,
);

@override
Stream<MapEntry<String, DeviceEntityBase>> watchEntities() =>
Expand All @@ -33,7 +35,7 @@ class _AppConnectionService implements ConnectionsService {
IcSynchronizer().setEntitiesState(action);

@override
Future dispose() async => searchDevicesInstance.dispose();
Future dispose() async => searchDevicesInstance?.dispose();

@override
Future setNewArea(AreaEntity area) async {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
part of 'package:cybearjinni/domain/connections_service.dart';

class _DemoConnectionService implements ConnectionsService {
_DemoConnectionService(this.networkBssid);

String networkBssid;

StreamController<MapEntry<String, DeviceEntityBase>>? entitiesStream;
StreamController<MapEntry<String, AreaEntity>>? areasStream;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
part of 'package:cybearjinni/domain/connections_service.dart';

class _HubConnectionService implements ConnectionsService {
_HubConnectionService() {
_HubConnectionService(this.networkBssid) {
if (currentEnvApp == EnvApp.prod) {
hubPort = 50055;
} else {
hubPort = 60055;
}
}

String networkBssid;

/// 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;
Expand Down Expand Up @@ -166,7 +167,6 @@ class _HubConnectionService implements ConnectionsService {
final NetworkInfo networkInfo = NetworkInfo();
networkName = await networkInfo.getWifiName();
appDeviceIp = await networkInfo.getWifiIP();
networkBssid = await networkInfo.getWifiBSSID();
} else {
return false;
// if (deviceIpOnTheNetwork == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
part of 'package:cybearjinni/domain/connections_service.dart';

class _RemotePipesConnectionService implements ConnectionsService {
_RemotePipesConnectionService(this.networkBssid);

/// Port to connect to the cbj hub, will change according to the current
/// running environment
int hubPort = 50051;

String? address;
String? networkBssid;
String? networkName;
String networkBssid;

ClientChannel? channel;
CbjHubClient? stub;
Expand Down
45 changes: 36 additions & 9 deletions lib/infrastructure/manage_wifi_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class _ManageWiFiRepository implements IManageNetworkRepository {
}
} on PlatformException catch (e) {
logger.e('Failed to get Wifi Name\n$e');
// wifiName = "Failed to get Wifi Name";
} catch (exception) {
logger.e(exception.toString());
}
Expand All @@ -129,8 +128,31 @@ class _ManageWiFiRepository implements IManageNetworkRepository {

@override
Future loadWifi() async {
// TODO: Get bssid
final NetworkInfo info = NetworkInfo();

if (Platform.isLinux) {
final String? bssid = await info.getWifiBSSID();
final String? wifiName = await info.getWifiName();
final String? ip = await info.getWifiIP();
if (bssid == null || wifiName == null || ip == null) {
return;
}
final String subnet = ipToSubnet(ip);

final NetworkObject network = NetworkObject(
bssid: bssid,
ssid: wifiName,
subNet: subnet,
longitude: null,
latitude: null,
remotePipe: null,
type: null,
);
NetworksManager().addNetwork(network);
NetworksManager().setCurrentNetwork(network.uniqueId);
return;
}

final PermissionStatus locationStatus = await Permission.location.status;
if (locationStatus.isDenied) {
await Permission.locationWhenInUse.request();
Expand All @@ -146,6 +168,7 @@ class _ManageWiFiRepository implements IManageNetworkRepository {
logger.w('Not connected to WiFi');
exit(0);
}

String bssid;
if (await Permission.location.isGranted) {
final String? bssidTemp = await info.getWifiBSSID();
Expand All @@ -161,18 +184,17 @@ class _ManageWiFiRepository implements IManageNetworkRepository {
exit(0);
}
final String? ssid = await WiFiForIoTPlugin.getSSID();
final List<String>? ipSplit = (await WiFiForIoTPlugin.getIP())?.split('.');
String? subNet;
if (ipSplit != null) {
subNet = ipSplit.sublist(0, ipSplit.length - 1).join();
}
final String? ip = await WiFiForIoTPlugin.getIP();
final location.LocationData locationData =
await location.Location().getLocation();
if (ssid == null || subNet == null) {
logger.w('Ssid or subnet is null');

if (ssid == null || ip == null) {
logger.w('Ssid is null');
exit(0);
}

final String subNet = ipToSubnet(ip);

final NetworkObject network = NetworkObject(
bssid: bssid,
ssid: ssid,
Expand All @@ -185,4 +207,9 @@ class _ManageWiFiRepository implements IManageNetworkRepository {
NetworksManager().addNetwork(network);
NetworksManager().setCurrentNetwork(network.uniqueId);
}

String ipToSubnet(String ip) {
final List<String> ipSplit = ip.split('.');
return ipSplit.sublist(0, ipSplit.length - 1).join();
}
}
2 changes: 1 addition & 1 deletion lib/presentation/molecules/devices/ac_molecule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _AcMoleculeState extends State<AcMolecule> {

void setEntityState(EntityActions action) {
final HashSet<String> entitiesId =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/molecules/devices/blind_molecule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class _BlindMoleculeState extends State<BlindMolecule> {

void setEntityState(EntityActions action) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/molecules/devices/boiler_molecule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _BoilerMoleculeState extends State<BoilerMolecule> {

void setEntityState(EntityActions action) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class _DimmableLightMoleculeState extends State<DimmableLightMolecule> {
HashMap<ActionValues, dynamic>? value,
}) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
4 changes: 2 additions & 2 deletions lib/presentation/molecules/devices/light_card_molecule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class LightCardMolecule extends StatelessWidget {

if (value) {
// await IDeviceRepository.instance.turnOnDevices(
// devicesId: [tempDeviceEntity.deviceCbjUniqueId.getOrCrash()],
// devicesId: [tempDeviceEntity.entitiyCbjUniqueId.getOrCrash()],
// );
} else {
// await IDeviceRepository.instance.turnOffDevices(
// devicesId: [tempDeviceEntity.deviceCbjUniqueId.getOrCrash()],
// devicesId: [tempDeviceEntity.entitiyCbjUniqueId.getOrCrash()],
// );
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/molecules/devices/light_molecule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LightMolecule extends StatelessWidget {

void setEntityState(EntityActions action) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
4 changes: 2 additions & 2 deletions lib/presentation/molecules/devices/rgb_light_molecule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class _RgbwLightMoleculeState extends State<RgbwLightMolecule> {
HashMap<ActionValues, dynamic>? value,
}) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down Expand Up @@ -202,7 +202,7 @@ class _LightColorMods extends State<LightColorMods> {
HashMap<ActionValues, dynamic>? value,
}) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class _SmartComputerMoleculeState extends State<SmartComputerMolecule> {
}

void suspendComputer(BuildContext context) {
final String deviceId = widget.entity.getCbjDeviceId;
final String deviceId = widget.entity.getCbjEntityId;
_suspendAllSmartComputers([deviceId]);
}

void shutdownComputer(BuildContext context) {
final String deviceId = widget.entity.getCbjDeviceId;
final String deviceId = widget.entity.getCbjEntityId;
_shutdownAllSmartComputers([deviceId]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class _SmartPlugsMoleculeState extends State<SmartPlugsMolecule> {

void setEntityState(EntityActions action) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/molecules/devices/smart_tv_molecule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class _SmartTvMoleculeState extends State<SmartTvMolecule> {
HashMap<ActionValues, dynamic>? value,
}) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/molecules/devices/switch_molecule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class _SwitchMoleculeState extends State<SwitchMolecule> {

void setEntityState(EntityActions action) {
final HashSet<String> uniqueIdByVendor =
HashSet.from([widget.entity.deviceCbjUniqueId.getOrCrash()]);
HashSet.from([widget.entity.entitiyCbjUniqueId.getOrCrash()]);

ConnectionsService.instance.setEntityState(
RequestActionObject(
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/pages/add_action_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class _AddActionPageState extends State<AddActionPage> {
items:
widget.entities.values.map<DropdownMenuItem<String>>((e) {
return DropdownMenuItem<String>(
value: e.getCbjDeviceId,
value: e.getCbjEntityId,
child: TextAtom(e.cbjEntityName.getOrCrash()!),
);
}).toList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import 'dart:collection';

import 'package:adaptive_action_sheet/adaptive_action_sheet.dart';
import 'package:auto_route/auto_route.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:cbj_integrations_controller/integrations_controller.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/core/snack_bar_service.dart';
import 'package:cybearjinni/presentation/molecules/molecules.dart';
import 'package:cybearjinni/presentation/pages/add_action_page.dart';
import 'package:cybearjinni/presentation/pages/add_bindings/widgets/binding_action_widget.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:hexcolor/hexcolor.dart';
Expand Down Expand Up @@ -111,10 +111,22 @@ class _AddBindingPageState extends State<AddBindingPage> {

return Container(
margin: const EdgeInsets.symmetric(vertical: 1),
child: BindingActionWidget(
entity: currentDevice.entity,
propertyToChange: currentDevice.property,
actionToChange: currentDevice.action,
child: ColoredBox(
color: Colors.blue.withOpacity(0.3),
child: ListTile(
leading: const FaIcon(
FontAwesomeIcons.lightbulb,
color: Colors.yellow,
),
title: AutoSizeText(
'${currentDevice.entity.cbjEntityName.getOrCrash()!} - ${currentDevice.property.name}',
maxLines: 2,
),
trailing: AutoSizeText(
currentDevice.action.name,
style: const TextStyle(color: Colors.black),
),
),
),
);
},
Expand Down
Loading

0 comments on commit df38738

Please sign in to comment.