Skip to content

Commit

Permalink
Merge pull request #60 from guyluz11/dev
Browse files Browse the repository at this point in the history
Add files for xiaomi #57 and closed #59
  • Loading branch information
guyluz11 committed Sep 13, 2021
2 parents 6d0288c + 747e9e2 commit 32cabfe
Show file tree
Hide file tree
Showing 18 changed files with 376 additions and 18 deletions.
6 changes: 6 additions & 0 deletions lib/infrastructure/devices/companys_connector_conjector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:cbj_hub/infrastructure/devices/esphome/esphome_connector_conject
import 'package:cbj_hub/infrastructure/devices/google/google_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/switcher/switcher_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/tasmota/tasmota_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/xiaomi_io/xiaomi_io_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/devices/yeelight/yeelight_connector_conjector.dart';
import 'package:cbj_hub/infrastructure/gen/cbj_hub_server/protoc_as_dart/cbj_hub_server.pbgrpc.dart';
import 'package:cbj_hub/injection.dart';
Expand All @@ -30,6 +31,9 @@ class CompanysConnectorConjector {
} else if (deviceVendor == VendorsAndServices.google.toString()) {
GoogleConnectorConjector()
.manageHubRequestsForDevice(deviceEntityAbstract);
} else if (deviceVendor == VendorsAndServices.xiaomiMiio.toString()) {
XiaomiIoConnectorConjector()
.manageHubRequestsForDevice(deviceEntityAbstract);
} else {
print('Cannot send device changes to its repo, company not supported');
}
Expand Down Expand Up @@ -69,6 +73,8 @@ class CompanysConnectorConjector {
SwitcherConnectorConjector.companyDevices.addEntries([devicesEntry]);
} else if (deviceVendor == VendorsAndServices.google.toString()) {
GoogleConnectorConjector.companyDevices.addEntries([devicesEntry]);
} else if (deviceVendor == VendorsAndServices.xiaomiMiio.toString()) {
XiaomiIoConnectorConjector.companyDevices.addEntries([devicesEntry]);
} else {
print('Cannot add device entity to its repo, type not supported');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:cbj_hub/domain/generic_devices/abstract_device/core_failures.dar
import 'package:cbj_hub/domain/generic_devices/abstract_device/device_entity_abstract.dart';
import 'package:cbj_hub/infrastructure/devices/google/chrome_cast/chrome_cast_entity.dart';
import 'package:cbj_hub/infrastructure/generic_devices/abstract_device/abstract_company_connector_conjector.dart';
import 'package:cbj_hub/utils.dart';
import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
import 'package:multicast_dns/multicast_dns.dart';
Expand Down Expand Up @@ -44,7 +45,7 @@ class GoogleConnectorConjector implements AbstractCompanyConnectorConjector {
if (device is ChromeCastEntity) {
device.executeDeviceAction(googleDE);
} else {
print('Google device type does not exist');
logger.i('Google device type does not exist');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';

import 'package:cbj_hub/domain/generic_devices/abstract_device/core_failures.dart';
import 'package:cbj_hub/domain/generic_devices/abstract_device/device_entity_abstract.dart';
import 'package:cbj_hub/infrastructure/devices/philips%20hue/philips_hue_e26/philips_hue_e26_entity.dart';
import 'package:cbj_hub/infrastructure/devices/philips_hue/philips_hue_e26/philips_hue_e26_entity.dart';
import 'package:cbj_hub/infrastructure/generic_devices/abstract_device/abstract_company_connector_conjector.dart';
import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:cbj_hub/domain/generic_devices/abstract_device/core_failures.dart';
import 'package:cbj_hub/domain/generic_devices/abstract_device/value_objects_core.dart';
import 'package:cbj_hub/infrastructure/devices/philips%20hue/philips_hue_device_validators.dart';
import 'package:cbj_hub/infrastructure/devices/philips_hue/philips_hue_device_validators.dart';
import 'package:dartz/dartz.dart';

/// PhilipsHue device unique address that came withe the device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:cbj_hub/domain/generic_devices/abstract_device/value_objects_cor
import 'package:cbj_hub/domain/generic_devices/device_type_enums.dart';
import 'package:cbj_hub/domain/generic_devices/generic_rgbw_light_device/generic_rgbw_light_entity.dart';
import 'package:cbj_hub/domain/generic_devices/generic_rgbw_light_device/generic_rgbw_light_value_objects.dart';
import 'package:cbj_hub/infrastructure/devices/philips%20hue/philips_hue_device_value_objects.dart';
import 'package:cbj_hub/infrastructure/devices/philips_hue/philips_hue_device_value_objects.dart';
import 'package:cbj_hub/infrastructure/gen/cbj_hub_server/protoc_as_dart/cbj_hub_server.pbgrpc.dart';
import 'package:dartz/dartz.dart';
import 'package:yeedart/yeedart.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:cbj_hub/domain/generic_devices/abstract_device/device_entity_abstract.dart';
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/infrastructure/devices/philips%20hue/philips_hue_device_value_objects.dart';
import 'package:cbj_hub/infrastructure/devices/philips%20hue/philips_hue_e26/philips_hue_e26_entity.dart';
import 'package:cbj_hub/infrastructure/devices/philips_hue/philips_hue_device_value_objects.dart';
import 'package:cbj_hub/infrastructure/devices/philips_hue/philips_hue_e26/philips_hue_e26_entity.dart';
import 'package:cbj_hub/infrastructure/gen/cbj_hub_server/protoc_as_dart/cbj_hub_server.pbgrpc.dart';
import 'package:yeedart/yeedart.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class SwitcherDiscover {
static Stream<SwitcherApiObject> discover20002Devices() async* {
try {
final RawDatagramSocket socket20002 = await RawDatagramSocket.bind(
InternetAddress.anyIPv4, SWITCHER_UDP_PORT);
InternetAddress.anyIPv4,
SWITCHER_UDP_PORT,
);

await for (final event in socket20002) {
final Datagram? datagram = socket20002.receive();
Expand All @@ -27,7 +29,9 @@ class SwitcherDiscover {
static Stream<SwitcherApiObject> discover20003Devices() async* {
try {
final RawDatagramSocket socket20003 = await RawDatagramSocket.bind(
InternetAddress.anyIPv4, SWITCHER_UDP_PORT2);
InternetAddress.anyIPv4,
SWITCHER_UDP_PORT2,
);

await for (final event in socket20003) {
final Datagram? datagram = socket20003.receive();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'dart:async';

import 'package:cbj_hub/domain/generic_devices/abstract_device/core_failures.dart';
import 'package:cbj_hub/domain/generic_devices/abstract_device/device_entity_abstract.dart';
import 'package:cbj_hub/infrastructure/devices/xiaomi_io/xiaomi_io_gpx3021gl/xiaomi_io_gpx3021gl_entity.dart';
import 'package:cbj_hub/infrastructure/generic_devices/abstract_device/abstract_company_connector_conjector.dart';
import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';

@singleton
class XiaomiIoConnectorConjector implements AbstractCompanyConnectorConjector {
XiaomiIoConnectorConjector() {
_discoverNewDevices();
}

@override
static Map<String, DeviceEntityAbstract> companyDevices = {};

Future<void> _discoverNewDevices() async {
// await MiIoCommandRunner().run(args);
// final InternetAddress internetAddress = InternetAddress('192.168.31.255');
// final MiIo miIo = MiIo.instance;
// miIo.discover(internetAddress).listen((event) {
// print('event $event');
// });
// MiIo m = MiIo();
// MiIoDevice miDevice = MiIoDevice(address: internetAddress);
// miio discover --ip 192.168.1.255
}

@override
Future<Either<CoreFailure, Unit>> create(DeviceEntityAbstract xiaomi_io) {
// TODO: implement create
throw UnimplementedError();
}

@override
Future<Either<CoreFailure, Unit>> delete(DeviceEntityAbstract xiaomi_io) {
// TODO: implement delete
throw UnimplementedError();
}

@override
Future<void> initiateHubConnection() {
// TODO: implement initiateHubConnection
throw UnimplementedError();
}

@override
Future<void> manageHubRequestsForDevice(
DeviceEntityAbstract xiaomiDE,
) async {
final DeviceEntityAbstract? device = companyDevices[xiaomiDE.getDeviceId()];

if (device is XiaomiIoGpx4021GlEntity) {
device.executeDeviceAction(xiaomiDE);
} else {
print('XiaomiIo device type does not exist');
}
}

@override
Future<Either<CoreFailure, Unit>> updateDatabase(
{required String pathOfField,
required Map<String, dynamic> fieldsToUpdate,
String? forceUpdateLocation}) async {
// TODO: implement updateDatabase
throw UnimplementedError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:cbj_hub/domain/generic_devices/abstract_device/core_failures.dart';
import 'package:dartz/dartz.dart';

Either<CoreFailure<String>, String> validateXiaomiIoIdNotEmpty(String input) {
if (input != null) {
return right(input);
} else {
return left(CoreFailure.empty(failedValue: input));
}
}

Either<CoreFailure<String>, String> validateXiaomiIoPortNotEmpty(
String input,
) {
if (input != null) {
return right(input);
} else {
return left(CoreFailure.empty(failedValue: input));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:cbj_hub/domain/generic_devices/abstract_device/core_failures.dart';
import 'package:cbj_hub/domain/generic_devices/abstract_device/value_objects_core.dart';
import 'package:cbj_hub/infrastructure/devices/xiaomi_io/xiaomi_io_device_validators.dart';
import 'package:dartz/dartz.dart';

/// XiaomiIo device unique address that came withe the device
class XiaomiIoDeviceId extends ValueObjectCore<String> {
factory XiaomiIoDeviceId(String? input) {
assert(input != null);
return XiaomiIoDeviceId._(
validateXiaomiIoIdNotEmpty(input!),
);
}

const XiaomiIoDeviceId._(this.value);

@override
final Either<CoreFailure<String>, String> value;
}

/// XiaomiIo communication port
class XiaomiIoPort extends ValueObjectCore<String> {
factory XiaomiIoPort(String? input) {
assert(input != null);
return XiaomiIoPort._(
validateXiaomiIoPortNotEmpty(input!),
);
}

const XiaomiIoPort._(this.value);

@override
final Either<CoreFailure<String>, String> value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import 'dart:async';

import 'package:cbj_hub/domain/generic_devices/abstract_device/core_failures.dart';
import 'package:cbj_hub/domain/generic_devices/abstract_device/device_entity_abstract.dart';
import 'package:cbj_hub/domain/generic_devices/abstract_device/value_objects_core.dart';
import 'package:cbj_hub/domain/generic_devices/device_type_enums.dart';
import 'package:cbj_hub/domain/generic_devices/generic_rgbw_light_device/generic_rgbw_light_entity.dart';
import 'package:cbj_hub/domain/generic_devices/generic_rgbw_light_device/generic_rgbw_light_value_objects.dart';
import 'package:cbj_hub/infrastructure/devices/xiaomi_io/xiaomi_io_device_value_objects.dart';
import 'package:cbj_hub/infrastructure/gen/cbj_hub_server/protoc_as_dart/cbj_hub_server.pbgrpc.dart';
import 'package:dartz/dartz.dart';
import 'package:yeedart/yeedart.dart';

class XiaomiIoGpx4021GlEntity extends GenericRgbwLightDE {
XiaomiIoGpx4021GlEntity({
required CoreUniqueId uniqueId,
required CoreUniqueId roomId,
required DeviceDefaultName defaultName,
required DeviceRoomName roomName,
required DeviceState deviceStateGRPC,
required DeviceStateMassage stateMassage,
required DeviceSenderDeviceOs senderDeviceOs,
required DeviceSenderDeviceModel senderDeviceModel,
required DeviceSenderId senderId,
required DeviceCompUuid compUuid,
required DevicePowerConsumption powerConsumption,
required GenericRgbwLightSwitchState lightSwitchState,
required GenericRgbwLightColorAlpha lightColorAlpha,
required GenericRgbwLightColorHue lightColorHue,
required GenericRgbwLightColorSaturation lightColorSaturation,
required GenericRgbwLightColorValue lightColorValue,
required this.xiaomi_ioDeviceId,
required this.xiaomi_ioPort,
this.deviceMdnsName,
this.lastKnownIp,
required GenericRgbwLightColorTemperature lightColorTemperature,
required GenericRgbwLightBrightness lightBrightness,
}) : super(
uniqueId: uniqueId,
defaultName: defaultName,
roomId: roomId,
lightSwitchState: lightSwitchState,
roomName: roomName,
deviceStateGRPC: deviceStateGRPC,
stateMassage: stateMassage,
senderDeviceOs: senderDeviceOs,
senderDeviceModel: senderDeviceModel,
senderId: senderId,
// TODO: change when implementing philips hue
deviceVendor: DeviceVendor(
VendorsAndServices.vendorsAndServicesNotSupported.toString()),
compUuid: compUuid,
powerConsumption: powerConsumption,
lightColorTemperature: lightColorTemperature,
lightBrightness: lightBrightness,
lightColorAlpha: lightColorAlpha,
lightColorHue: lightColorHue,
lightColorSaturation: lightColorSaturation,
lightColorValue: lightColorValue,
);

/// XiaomiIo device unique id that came withe the device
XiaomiIoDeviceId? xiaomi_ioDeviceId;

/// XiaomiIo communication port
XiaomiIoPort? xiaomi_ioPort;

DeviceLastKnownIp? lastKnownIp;

DeviceMdnsName? deviceMdnsName;

/// XiaomiIo package object require to close previews request before new one
Device? xiaomi_ioPackageObject;

/// Please override the following methods
@override
Future<Either<CoreFailure, Unit>> executeDeviceAction(
DeviceEntityAbstract newEntity,
) async {
if (newEntity is! GenericRgbwLightDE) {
return left(
const CoreFailure.actionExcecuter(
failedValue: 'Not the correct type',
),
);
}

if (newEntity.lightSwitchState!.getOrCrash() !=
lightSwitchState!.getOrCrash()) {
final DeviceActions? actionToPreform = EnumHelper.stringToDeviceAction(
newEntity.lightSwitchState!.getOrCrash(),
);

if (actionToPreform.toString() != lightSwitchState!.getOrCrash()) {
if (actionToPreform == DeviceActions.on) {
(await turnOnLight()).fold(
(l) => print('Error turning xiaomi_io light on'),
(r) => print('Light turn on success'));
} else if (actionToPreform == DeviceActions.off) {
(await turnOffLight()).fold(
(l) => print('Error turning xiaomi_io light off'),
(r) => print('Light turn off success'),
);
} else {
print('actionToPreform is not set correctly on XiaomiIo Gpx4021Gl');
}
}
}

return right(unit);
}

@override
Future<Either<CoreFailure, Unit>> turnOnLight() async {
lightSwitchState = GenericRgbwLightSwitchState(DeviceActions.on.toString());
try {
return left(const CoreFailure.unexpected());
} catch (e) {
return left(const CoreFailure.unexpected());
}
}

@override
Future<Either<CoreFailure, Unit>> turnOffLight() async {
lightSwitchState =
GenericRgbwLightSwitchState(DeviceActions.off.toString());
try {
return left(const CoreFailure.unexpected());
} catch (e) {
return left(const CoreFailure.unexpected());
}
}

@override
Future<Either<CoreFailure, Unit>> adjustBrightness(String brightness) async {
print('Please override this method in the non generic implementation');
return left(
const CoreFailure.actionExcecuter(
failedValue: 'Action does not exist',
),
);
}

@override
Future<Either<CoreFailure, Unit>> changeColorTemperature({
required String lightColorAlphaNewValue,
required String lightColorHueNewValue,
required String lightColorSaturationNewValue,
required String lightColorValueNewValue,
}) async {
print('Please override this method in the non generic implementation');
return left(
const CoreFailure.actionExcecuter(
failedValue: 'Action does not exist',
),
);
}
}
Loading

0 comments on commit 32cabfe

Please sign in to comment.