Skip to content

Commit

Permalink
Moving shelly package out
Browse files Browse the repository at this point in the history
  • Loading branch information
guyluz11 committed Oct 7, 2023
1 parent 76c5597 commit 2464cc5
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 1,001 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@ saved_cast_state.json
lib/infrastructure/core/constant_credentials.dart

# Web related
lib/generated_plugin_registrant.dart
lib/generated_plugin_registrant.dart

# Network tools
network_tools_db/*
2 changes: 2 additions & 0 deletions bin/cbj_hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import 'package:cbj_hub/infrastructure/shared_variables.dart';
import 'package:cbj_hub/infrastructure/system_commands/device_pin_manager.dart';
import 'package:cbj_hub/injection.dart';
import 'package:cbj_hub/utils.dart';
import 'package:network_tools/network_tools.dart';

Future<void> main(List<String> arguments) async {
// arguments[0] is the location of the project
configureNetworkTools('network_tools_db');
configureInjection(Env.prod);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import 'package:cbj_hub/infrastructure/devices/lifx/lifx_connector_conjector.dar
import 'package:cbj_hub/infrastructure/devices/philips_hue/philips_hue_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/shelly/shelly_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/sonoff_diy/sonoff_diy_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/switcher/switcher_api/switcher_discover.dart';
import 'package:cbj_hub/infrastructure/devices/switcher/switcher_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/tasmota/tasmota_ip/tasmota_ip_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/tuya_smart/tuya_smart_connector_conjector.dart';
Expand All @@ -35,6 +34,7 @@ import 'package:cbj_hub/injection.dart';
import 'package:cbj_hub/utils.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';
import 'package:network_tools/network_tools.dart';
import 'package:switcher_dart/switcher_dart.dart';

class CompaniesConnectorConjector {
static void updateAllDevicesReposWithDeviceChanges(
Expand Down
17 changes: 9 additions & 8 deletions lib/infrastructure/devices/esphome/esphome_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class EspHomeHelpers {
return tempEspHomeNodeDeviceId;
}

static Future<List<EspHomeDeviceEntityObject>> retreveOnlyNewEntities({
static Future<List<EspHomeDeviceEntityObject>> retrieveOnlyNewEntities({
required String mDnsName,
required String devicePassword,
String? espHomeDeviceNodeId,
Expand Down Expand Up @@ -98,7 +98,7 @@ class EspHomeHelpers {

/// Make sure we add only new entities
final List<EspHomeDeviceEntityObject> entitiesList =
await retreveOnlyNewEntities(
await retrieveOnlyNewEntities(
mDnsName: mDnsName,
devicePassword: devicePassword,
espHomeDeviceNodeId: espHomeDeviceNodeId,
Expand All @@ -111,12 +111,13 @@ class EspHomeHelpers {
final List<DeviceEntityAbstract> deviceEntityList = [];

final EspHomeNodeRedApi espHomeNodeRedApi = EspHomeNodeRedApi(
repository: getIt<NodeRedRepository>(),
nodeRedApiBaseTopic:
getIt<IMqttServerRepository>().getNodeRedApiBaseTopic(),
nodeRedDevicesTopic:
getIt<IMqttServerRepository>().getNodeRedDevicesTopicTypeName(),
nodeRedMqttBrokerNodeName: 'Cbj NodeRed plugs Api Broker');
repository: getIt<NodeRedRepository>(),
nodeRedApiBaseTopic:
getIt<IMqttServerRepository>().getNodeRedApiBaseTopic(),
nodeRedDevicesTopic:
getIt<IMqttServerRepository>().getNodeRedDevicesTopicTypeName(),
nodeRedMqttBrokerNodeName: 'Cbj NodeRed plugs Api Broker',
);

for (final EspHomeDeviceEntityObject espHomeDeviceEntityObject
in entitiesList) {
Expand Down
24 changes: 14 additions & 10 deletions lib/infrastructure/devices/ewelink/ewelink_connector_conjector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class EwelinkConnectorConjector implements AbstractCompanyConnectorConjector {
@override
Map<String, DeviceEntityAbstract> companyDevices = {};

Future<String> accountLogin(
Future<bool> accountLogin(
GenericEwelinkLoginDE loginDE,
) async {
try {
Expand All @@ -32,27 +32,31 @@ class EwelinkConnectorConjector implements AbstractCompanyConnectorConjector {
);

await ewelink!.getCredentials();
discoverNewDevices(activeHost: null);
} on EwelinkInvalidAccessToken {
logger.e('invalid access token');
return false;
} on EwelinkOfflineDeviceException {
logger.e('device is offline');
return false;
} catch (e) {
// ignore: unnecessary_brace_in_string_interps
logger.e('error: $e');
logger.e('EweLink error: $e');
return false;
}
return 'Success';
return true;
}

Future<void> discoverNewDevices({
required ActiveHost? activeHost,
}) async {
if (ewelink == null) {
await accountLogin(GenericEwelinkLoginDE.empty());
logger.w(
'eWeLink device got found but missing a email and password, please add '
'it in the app');
// return;
final bool lastRequest =
await accountLogin(GenericEwelinkLoginDE.empty());
if (!lastRequest) {
logger.w(
'eWeLink device got found but missing a email and password, please add '
'it in the app');
return;
}
}
final List<EwelinkDevice> devices = await ewelink!.getDevices();

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ class ShellyConnectorConjector implements AbstractCompanyConnectorConjector {
}
}

final List<DeviceEntityAbstract> espDevice =
final List<DeviceEntityAbstract> shellyDevice =
await ShellyHelpers.addDiscoverdDevice(
mDnsName: mDnsName,
ip: ip,
port: port,
uniqueDeviceId: tempCoreUniqueId,
);

if (espDevice.isEmpty) {
if (shellyDevice.isEmpty) {
return;
}

for (final DeviceEntityAbstract entityAsDevice in espDevice) {
for (final DeviceEntityAbstract entityAsDevice in shellyDevice) {
final DeviceEntityAbstract deviceToAdd =
CompaniesConnectorConjector.addDiscoverdDeviceToHub(entityAsDevice);

Expand Down
16 changes: 8 additions & 8 deletions lib/infrastructure/devices/shelly/shelly_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import 'package:cbj_hub/domain/generic_devices/abstract_device/device_entity_abs
import 'package:cbj_hub/domain/generic_devices/abstract_device/value_objects_core.dart';
import 'package:cbj_hub/domain/generic_devices/generic_rgbw_light_device/generic_rgbw_light_value_objects.dart';
import 'package:cbj_hub/domain/generic_devices/generic_switch_device/generic_switch_value_objects.dart';
import 'package:cbj_hub/infrastructure/devices/shelly/shelly_api/shelly_api_color_bulb.dart';
import 'package:cbj_hub/infrastructure/devices/shelly/shelly_light/shelly_light_entity.dart';
import 'package:cbj_hub/infrastructure/devices/shelly/shelly_relay_switch/shelly_relay_switch_entity.dart';
import 'package:cbj_hub/infrastructure/gen/cbj_hub_server/protoc_as_dart/cbj_hub_server.pbenum.dart';
import 'package:cbj_hub/utils.dart';
import 'package:color/color.dart';
import 'package:shelly/shelly.dart';

class ShellyHelpers {
static Future<List<DeviceEntityAbstract>> addDiscoverdDevice({
Expand All @@ -28,7 +28,7 @@ class ShellyHelpers {
final List<DeviceEntityAbstract> deviceEntityList = [];

try {
// TODO: shelly duo bulbe needs type that as the time of writing is
// TODO: shelly duo bulb needs type that as the time of writing is
// not supported, bulb + brightness + white temperature (not rgb).
// Lets create new type and add it.
if (mDnsName.contains('colorbulb')) {
Expand All @@ -46,14 +46,14 @@ class ShellyHelpers {
dynamic bulbLightProp = responseAsJson['lights'][0];

final String bulbMod = bulbLightProp['mode'] as String;
shellyApiDeviceAbstract.bulbeMode =
bulbMod == 'color' ? ShellyBulbeMode.colore : ShellyBulbeMode.white;
shellyApiDeviceAbstract.bulbMode =
bulbMod == 'color' ? ShellyBulbMode.colore : ShellyBulbMode.white;

final int brightness = bulbLightProp['brightness'] as int;
final int gain =
bulbLightProp['gain'] as int; // Brightness for color mod
final int currentBrightness =
shellyApiDeviceAbstract.bulbeMode == ShellyBulbeMode.colore
shellyApiDeviceAbstract.bulbMode == ShellyBulbMode.colore
? gain
: brightness;
final int temp = bulbLightProp['temp'] as int;
Expand Down Expand Up @@ -95,7 +95,7 @@ class ShellyHelpers {
deviceHostName: DeviceHostName(mDnsName.toLowerCase()),
deviceMdns: DeviceMdns(mDnsName),
deviceLastKnownIp: DeviceLastKnownIp(ip),
bulbeMode: shellyApiDeviceAbstract,
bulbMode: shellyApiDeviceAbstract,
deviceUniqueId: DeviceUniqueId('0'),
devicesMacAddress: DevicesMacAddress('0'),
entityKey: EntityKey('0'),
Expand All @@ -118,7 +118,7 @@ class ShellyHelpers {

dynamic bulbLightProp = responseAsJson['lights'][0];

shellyApiDeviceAbstract.bulbeMode = ShellyBulbeMode.white;
shellyApiDeviceAbstract.bulbMode = ShellyBulbMode.white;

final int brightness = bulbLightProp['brightness'] as int;

Expand Down Expand Up @@ -155,7 +155,7 @@ class ShellyHelpers {
deviceHostName: DeviceHostName(mDnsName.toLowerCase()),
deviceMdns: DeviceMdns(mDnsName),
deviceLastKnownIp: DeviceLastKnownIp(ip),
bulbeMode: shellyApiDeviceAbstract,
bulbMode: shellyApiDeviceAbstract,
deviceUniqueId: DeviceUniqueId('0'),
devicesMacAddress: DevicesMacAddress('0'),
entityKey: EntityKey('0'),
Expand Down
Loading

0 comments on commit 2464cc5

Please sign in to comment.