diff --git a/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart b/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart index 98ce8071..7db9d56c 100644 --- a/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart +++ b/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart @@ -8,8 +8,7 @@ import 'package:syncrow_web/pages/device_managment/two_gang_switch/models/two_ga import 'package:syncrow_web/services/devices_mang_api.dart'; class TwoGangSwitchBloc extends Bloc { - TwoGangSwitchBloc({required this.deviceId}) - : super(TwoGangSwitchInitial()) { + TwoGangSwitchBloc({required this.deviceId}) : super(TwoGangSwitchInitial()) { on(_onFetchDeviceStatus); on(_onControl); on(_onFetchBatchStatus); @@ -89,7 +88,6 @@ class TwoGangSwitchBloc extends Bloc { if (code == 'switch_2') { deviceStatus = deviceStatus.copyWith(switch2: value); } - } bool _getValueByCode(String code) { @@ -103,14 +101,13 @@ class TwoGangSwitchBloc extends Bloc { } } - Future _onFetchBatchStatus( TwoGangSwitchFetchBatchEvent event, - Emitter< TwoGangSwitchState> emit) async { + Future _onFetchBatchStatus(TwoGangSwitchFetchBatchEvent event, + Emitter emit) async { emit(TwoGangSwitchLoading()); try { final status = await DevicesManagementApi().getDeviceStatus(event.deviceId); - deviceStatus = - TwoGangStatusModel.fromJson(event.deviceId, status.status); + deviceStatus = TwoGangStatusModel.fromJson(event.deviceId, status.status); emit(TwoGangSwitchStatusLoaded(deviceStatus)); } catch (e) { emit(TwoGangSwitchError(e.toString())); diff --git a/lib/pages/device_managment/water_heater/bloc/water_heater_bloc.dart b/lib/pages/device_managment/water_heater/bloc/water_heater_bloc.dart index 01507097..43372b2d 100644 --- a/lib/pages/device_managment/water_heater/bloc/water_heater_bloc.dart +++ b/lib/pages/device_managment/water_heater/bloc/water_heater_bloc.dart @@ -1,9 +1,9 @@ import 'dart:async'; - import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart'; import 'package:syncrow_web/pages/device_managment/water_heater/models/water_heater_status_model.dart'; +import 'package:syncrow_web/services/devices_mang_api.dart'; part 'water_heater_event.dart'; part 'water_heater_state.dart'; @@ -46,10 +46,10 @@ class WaterHeaterBloc extends Bloc { StopScheduleEvent event, Emitter emit, ) { - if (state is WaterHeaterScheduleState) { - final currentState = state as WaterHeaterScheduleState; - emit(WaterHeaterScheduleState( - scheduleType: currentState.scheduleType, + if (state is WaterHeaterScheduleViewState) { + final currentState = state as WaterHeaterScheduleViewState; + emit(WaterHeaterScheduleViewState( + scheduleMode: currentState.scheduleMode, hours: currentState.hours, minutes: currentState.minutes, isActive: false, @@ -59,15 +59,16 @@ class WaterHeaterBloc extends Bloc { FutureOr _controlWaterHeater( ToggleWaterHeaterEvent event, Emitter emit) async { - final oldValue = deviceStatus.heaterSwitch; + final oldValue = _getValueByCode(event.code); - _updateLocalValue(event.isOn); + _updateLocalValue(event.code, event.value, emit); - emit(WaterHeaterToggleState(isOn: event.isOn)); + emit(WaterHeaterDeviceStatusLoaded(deviceStatus)); await _runDebounce( deviceId: event.deviceId, - value: event.isOn, + code: event.code, + value: event.value, oldValue: oldValue, emit: emit, ); @@ -75,8 +76,9 @@ class WaterHeaterBloc extends Bloc { Future _runDebounce({ required String deviceId, - required bool value, - required bool oldValue, + required String code, + required dynamic value, + required dynamic oldValue, required Emitter emit, }) async { if (_timer != null) { @@ -85,28 +87,47 @@ class WaterHeaterBloc extends Bloc { _timer = Timer(const Duration(milliseconds: 500), () async { try { - //// TODO: implement control + final status = await DevicesManagementApi().deviceControl( + deviceId, + Status(code: code, value: value), + ); - // final status = await DevicesManagementApi().deviceControl( - // deviceId, Status(value: value, code: 'heaterSwitch')); - - // if (!status) { - // _revertValueAndEmit(deviceId, oldValue, emit); - // } + if (!status) { + _revertValueAndEmit(deviceId, code, oldValue, emit); + } } catch (e) { - _revertValueAndEmit(deviceId, oldValue, emit); + _revertValueAndEmit(deviceId, code, oldValue, emit); } }); } - void _revertValueAndEmit( - String deviceId, bool oldValue, Emitter emit) { - _updateLocalValue(oldValue); - emit(WaterHeaterToggleState(isOn: oldValue)); + void _revertValueAndEmit(String deviceId, String code, dynamic oldValue, + Emitter emit) { + _updateLocalValue(code, oldValue, emit); + emit(WaterHeaterDeviceStatusLoaded(deviceStatus)); } - void _updateLocalValue(bool value) { - deviceStatus = deviceStatus.copyWith(heaterSwitch: value); + void _updateLocalValue( + String code, dynamic value, Emitter emit) { + switch (code) { + case 'switch_1': + if (value is bool) { + deviceStatus = deviceStatus.copyWith(heaterSwitch: value); + } + break; + default: + break; + } + } + + dynamic _getValueByCode(String code) { + switch (code) { + case 'switch_1': + return deviceStatus.heaterSwitch; + + default: + return null; + } } FutureOr _fetchWaterHeaterStatus( @@ -114,22 +135,12 @@ class WaterHeaterBloc extends Bloc { emit(WaterHeaterLoadingState()); try { - // final status = - // await DevicesManagementApi().getDeviceStatus(event.deviceId); - // deviceStatus = - // WaterHeaterStatusModel.fromJson(event.deviceId, status.status); + final status = + await DevicesManagementApi().getDeviceStatus(event.deviceId); + deviceStatus = + WaterHeaterStatusModel.fromJson(event.deviceId, status.status); - final List fakeStatusList = [ - Status(code: 'switch', value: true), - Status(code: 'schedule_mode', value: 'countdown'), - Status(code: 'countdown_hours', value: 6), - Status(code: 'countdown_minutes', value: 23), - ]; - - final fakeWaterHeaterStatus = - WaterHeaterStatusModel.fromJson(event.deviceId, fakeStatusList); - - emit(WaterHeaterDeviceStatusLoaded(fakeWaterHeaterStatus)); + emit(WaterHeaterDeviceStatusLoaded(deviceStatus)); } catch (e) { emit(WaterHeaterFailedState(error: e.toString())); } diff --git a/lib/pages/device_managment/water_heater/bloc/water_heater_event.dart b/lib/pages/device_managment/water_heater/bloc/water_heater_event.dart index 2f508a35..4c1354f9 100644 --- a/lib/pages/device_managment/water_heater/bloc/water_heater_event.dart +++ b/lib/pages/device_managment/water_heater/bloc/water_heater_event.dart @@ -8,13 +8,15 @@ sealed class WaterHeaterEvent extends Equatable { } final class ToggleWaterHeaterEvent extends WaterHeaterEvent { - final bool isOn; + final dynamic value; final String deviceId; + final String code; - const ToggleWaterHeaterEvent({required this.isOn, required this.deviceId}); + const ToggleWaterHeaterEvent( + {required this.value, required this.deviceId, required this.code}); @override - List get props => [isOn]; + List get props => [value]; } final class UpdateScheduleEvent extends WaterHeaterEvent { diff --git a/lib/pages/device_managment/water_heater/bloc/water_heater_state.dart b/lib/pages/device_managment/water_heater/bloc/water_heater_state.dart index 349be2be..a4216e44 100644 --- a/lib/pages/device_managment/water_heater/bloc/water_heater_state.dart +++ b/lib/pages/device_managment/water_heater/bloc/water_heater_state.dart @@ -11,31 +11,7 @@ sealed class WaterHeaterState extends Equatable { final class WaterHeaterInitial extends WaterHeaterState {} -final class WaterHeaterToggleState extends WaterHeaterState { - final bool isOn; - - const WaterHeaterToggleState({required this.isOn}); - - @override - List get props => [isOn]; -} - -final class WaterHeaterScheduleState extends WaterHeaterState { - final ScheduleModes scheduleType; - final int hours; - final int minutes; - final bool isActive; - - const WaterHeaterScheduleState({ - required this.scheduleType, - required this.hours, - required this.minutes, - required this.isActive, - }); - - @override - List get props => [scheduleType, hours, minutes, isActive]; -} +final class WaterHeaterLoadingState extends WaterHeaterState {} final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState { final WaterHeaterStatusModel status; @@ -46,15 +22,6 @@ final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState { List get props => [status]; } -final class WaterHeaterBatchStatusLoaded extends WaterHeaterState { - final WaterHeaterStatusModel status; - - const WaterHeaterBatchStatusLoaded(this.status); - - @override - List get props => [status]; -} - final class WaterHeaterFailedState extends WaterHeaterState { final String error; @@ -73,8 +40,6 @@ final class WaterHeaterBatchFailedState extends WaterHeaterState { List get props => [error]; } -final class WaterHeaterLoadingState extends WaterHeaterState {} - class WaterHeaterScheduleViewState extends WaterHeaterState { final ScheduleModes scheduleMode; final int hours; diff --git a/lib/pages/device_managment/water_heater/models/water_heater_status_model.dart b/lib/pages/device_managment/water_heater/models/water_heater_status_model.dart index b4a02a02..3cf29a96 100644 --- a/lib/pages/device_managment/water_heater/models/water_heater_status_model.dart +++ b/lib/pages/device_managment/water_heater/models/water_heater_status_model.dart @@ -5,38 +5,46 @@ enum ScheduleModes { countdown, schedule, circulate, inching } class WaterHeaterStatusModel { final String uuid; final bool heaterSwitch; - final String scheduleModeString; final int countdownHours; final int countdownMinutes; final ScheduleModes scheduleMode; + final String relayStatus; + final String cycleTiming; WaterHeaterStatusModel({ required this.uuid, required this.heaterSwitch, - required this.scheduleModeString, required this.countdownHours, required this.countdownMinutes, - }) : scheduleMode = getScheduleMode(scheduleModeString); + required this.relayStatus, + required this.cycleTiming, + required this.scheduleMode, + }); factory WaterHeaterStatusModel.fromJson(String id, List jsonList) { - late bool heaterSwitch; - late String scheduleMode; - late int countdownHours; - late int countdownMinutes; + late bool heaterSwitch = false; + late int countdownHours = 0; + late int countdownMinutes = 0; + late String relayStatus = ''; + late String cycleTiming = ''; + late ScheduleModes scheduleMode = ScheduleModes.countdown; for (var status in jsonList) { switch (status.code) { - case 'switch': + case 'switch_1': heaterSwitch = status.value ?? false; break; - case 'schedule_mode': - scheduleMode = status.value ?? 'countdown'; - break; - case 'countdown_hours': + case 'countdown_1': countdownHours = status.value ?? 0; break; - case 'countdown_minutes': - countdownMinutes = status.value ?? 0; + case 'relay_status': + relayStatus = status.value ?? 'memory'; + break; + case 'cycle_timing': + cycleTiming = status.value ?? ''; + break; + case 'switch_inching': + scheduleMode = getScheduleMode(status.value ?? 'countdown'); break; } } @@ -44,25 +52,31 @@ class WaterHeaterStatusModel { return WaterHeaterStatusModel( uuid: id, heaterSwitch: heaterSwitch, - scheduleModeString: scheduleMode, countdownHours: countdownHours, countdownMinutes: countdownMinutes, + relayStatus: relayStatus, + cycleTiming: cycleTiming, + scheduleMode: scheduleMode, ); } WaterHeaterStatusModel copyWith({ String? uuid, bool? heaterSwitch, - String? scheduleModeString, int? countdownHours, int? countdownMinutes, + String? relayStatus, + String? cycleTiming, + ScheduleModes? scheduleMode, }) { return WaterHeaterStatusModel( uuid: uuid ?? this.uuid, heaterSwitch: heaterSwitch ?? this.heaterSwitch, - scheduleModeString: scheduleModeString ?? this.scheduleModeString, countdownHours: countdownHours ?? this.countdownHours, countdownMinutes: countdownMinutes ?? this.countdownMinutes, + relayStatus: relayStatus ?? this.relayStatus, + cycleTiming: cycleTiming ?? this.cycleTiming, + scheduleMode: scheduleMode ?? this.scheduleMode, ); } diff --git a/lib/pages/device_managment/water_heater/view/water_heater_device_control.dart b/lib/pages/device_managment/water_heater/view/water_heater_device_control.dart index 2d975370..b1cc82d5 100644 --- a/lib/pages/device_managment/water_heater/view/water_heater_device_control.dart +++ b/lib/pages/device_managment/water_heater/view/water_heater_device_control.dart @@ -58,19 +58,20 @@ class WaterHeaterDeviceControl extends StatelessWidget children: [ ToggleWidget( deviceId: device.uuid!, - code: 'water_heater', - value: false, + code: 'switch_1', + value: status.heaterSwitch, label: 'Water Heater', onChange: (value) { context.read().add(ToggleWaterHeaterEvent( deviceId: device.uuid!, - isOn: value, + code: 'switch_1', + value: value, )); }, ), GestureDetector( onTap: () { - context.read().add(const ShowScheduleViewEvent()); + // context.read().add(const ShowScheduleViewEvent()); }, child: DeviceControlsContainer( child: Column(