push water heater single control

This commit is contained in:
ashrafzarkanisala
2024-09-18 00:30:03 +03:00
parent 185d94d800
commit 7d5b5340db
6 changed files with 97 additions and 107 deletions

View File

@ -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'; import 'package:syncrow_web/services/devices_mang_api.dart';
class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> { class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
TwoGangSwitchBloc({required this.deviceId}) TwoGangSwitchBloc({required this.deviceId}) : super(TwoGangSwitchInitial()) {
: super(TwoGangSwitchInitial()) {
on<TwoGangSwitchFetchDeviceEvent>(_onFetchDeviceStatus); on<TwoGangSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
on<TwoGangSwitchControl>(_onControl); on<TwoGangSwitchControl>(_onControl);
on<TwoGangSwitchFetchBatchEvent>(_onFetchBatchStatus); on<TwoGangSwitchFetchBatchEvent>(_onFetchBatchStatus);
@ -89,7 +88,6 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
if (code == 'switch_2') { if (code == 'switch_2') {
deviceStatus = deviceStatus.copyWith(switch2: value); deviceStatus = deviceStatus.copyWith(switch2: value);
} }
} }
bool _getValueByCode(String code) { bool _getValueByCode(String code) {
@ -103,14 +101,13 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
} }
} }
Future<void> _onFetchBatchStatus( TwoGangSwitchFetchBatchEvent event, Future<void> _onFetchBatchStatus(TwoGangSwitchFetchBatchEvent event,
Emitter< TwoGangSwitchState> emit) async { Emitter<TwoGangSwitchState> emit) async {
emit(TwoGangSwitchLoading()); emit(TwoGangSwitchLoading());
try { try {
final status = final status =
await DevicesManagementApi().getDeviceStatus(event.deviceId); await DevicesManagementApi().getDeviceStatus(event.deviceId);
deviceStatus = deviceStatus = TwoGangStatusModel.fromJson(event.deviceId, status.status);
TwoGangStatusModel.fromJson(event.deviceId, status.status);
emit(TwoGangSwitchStatusLoaded(deviceStatus)); emit(TwoGangSwitchStatusLoaded(deviceStatus));
} catch (e) { } catch (e) {
emit(TwoGangSwitchError(e.toString())); emit(TwoGangSwitchError(e.toString()));

View File

@ -1,9 +1,9 @@
import 'dart:async'; import 'dart:async';
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.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/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/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_event.dart';
part 'water_heater_state.dart'; part 'water_heater_state.dart';
@ -46,10 +46,10 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
StopScheduleEvent event, StopScheduleEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) { ) {
if (state is WaterHeaterScheduleState) { if (state is WaterHeaterScheduleViewState) {
final currentState = state as WaterHeaterScheduleState; final currentState = state as WaterHeaterScheduleViewState;
emit(WaterHeaterScheduleState( emit(WaterHeaterScheduleViewState(
scheduleType: currentState.scheduleType, scheduleMode: currentState.scheduleMode,
hours: currentState.hours, hours: currentState.hours,
minutes: currentState.minutes, minutes: currentState.minutes,
isActive: false, isActive: false,
@ -59,15 +59,16 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
FutureOr<void> _controlWaterHeater( FutureOr<void> _controlWaterHeater(
ToggleWaterHeaterEvent event, Emitter<WaterHeaterState> emit) async { ToggleWaterHeaterEvent event, Emitter<WaterHeaterState> 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( await _runDebounce(
deviceId: event.deviceId, deviceId: event.deviceId,
value: event.isOn, code: event.code,
value: event.value,
oldValue: oldValue, oldValue: oldValue,
emit: emit, emit: emit,
); );
@ -75,8 +76,9 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
Future<void> _runDebounce({ Future<void> _runDebounce({
required String deviceId, required String deviceId,
required bool value, required String code,
required bool oldValue, required dynamic value,
required dynamic oldValue,
required Emitter<WaterHeaterState> emit, required Emitter<WaterHeaterState> emit,
}) async { }) async {
if (_timer != null) { if (_timer != null) {
@ -85,28 +87,47 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
_timer = Timer(const Duration(milliseconds: 500), () async { _timer = Timer(const Duration(milliseconds: 500), () async {
try { try {
//// TODO: implement control final status = await DevicesManagementApi().deviceControl(
deviceId,
Status(code: code, value: value),
);
// final status = await DevicesManagementApi().deviceControl( if (!status) {
// deviceId, Status(value: value, code: 'heaterSwitch')); _revertValueAndEmit(deviceId, code, oldValue, emit);
}
// if (!status) {
// _revertValueAndEmit(deviceId, oldValue, emit);
// }
} catch (e) { } catch (e) {
_revertValueAndEmit(deviceId, oldValue, emit); _revertValueAndEmit(deviceId, code, oldValue, emit);
} }
}); });
} }
void _revertValueAndEmit( void _revertValueAndEmit(String deviceId, String code, dynamic oldValue,
String deviceId, bool oldValue, Emitter<WaterHeaterState> emit) { Emitter<WaterHeaterState> emit) {
_updateLocalValue(oldValue); _updateLocalValue(code, oldValue, emit);
emit(WaterHeaterToggleState(isOn: oldValue)); emit(WaterHeaterDeviceStatusLoaded(deviceStatus));
} }
void _updateLocalValue(bool value) { void _updateLocalValue(
deviceStatus = deviceStatus.copyWith(heaterSwitch: value); String code, dynamic value, Emitter<WaterHeaterState> 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<void> _fetchWaterHeaterStatus( FutureOr<void> _fetchWaterHeaterStatus(
@ -114,22 +135,12 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
emit(WaterHeaterLoadingState()); emit(WaterHeaterLoadingState());
try { try {
// final status = final status =
// await DevicesManagementApi().getDeviceStatus(event.deviceId); await DevicesManagementApi().getDeviceStatus(event.deviceId);
// deviceStatus = deviceStatus =
// WaterHeaterStatusModel.fromJson(event.deviceId, status.status); WaterHeaterStatusModel.fromJson(event.deviceId, status.status);
final List<Status> fakeStatusList = [ emit(WaterHeaterDeviceStatusLoaded(deviceStatus));
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));
} catch (e) { } catch (e) {
emit(WaterHeaterFailedState(error: e.toString())); emit(WaterHeaterFailedState(error: e.toString()));
} }

View File

@ -8,13 +8,15 @@ sealed class WaterHeaterEvent extends Equatable {
} }
final class ToggleWaterHeaterEvent extends WaterHeaterEvent { final class ToggleWaterHeaterEvent extends WaterHeaterEvent {
final bool isOn; final dynamic value;
final String deviceId; 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 @override
List<Object?> get props => [isOn]; List<Object?> get props => [value];
} }
final class UpdateScheduleEvent extends WaterHeaterEvent { final class UpdateScheduleEvent extends WaterHeaterEvent {

View File

@ -11,31 +11,7 @@ sealed class WaterHeaterState extends Equatable {
final class WaterHeaterInitial extends WaterHeaterState {} final class WaterHeaterInitial extends WaterHeaterState {}
final class WaterHeaterToggleState extends WaterHeaterState { final class WaterHeaterLoadingState extends WaterHeaterState {}
final bool isOn;
const WaterHeaterToggleState({required this.isOn});
@override
List<Object?> 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<Object?> get props => [scheduleType, hours, minutes, isActive];
}
final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState { final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState {
final WaterHeaterStatusModel status; final WaterHeaterStatusModel status;
@ -46,15 +22,6 @@ final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState {
List<Object?> get props => [status]; List<Object?> get props => [status];
} }
final class WaterHeaterBatchStatusLoaded extends WaterHeaterState {
final WaterHeaterStatusModel status;
const WaterHeaterBatchStatusLoaded(this.status);
@override
List<Object?> get props => [status];
}
final class WaterHeaterFailedState extends WaterHeaterState { final class WaterHeaterFailedState extends WaterHeaterState {
final String error; final String error;
@ -73,8 +40,6 @@ final class WaterHeaterBatchFailedState extends WaterHeaterState {
List<Object?> get props => [error]; List<Object?> get props => [error];
} }
final class WaterHeaterLoadingState extends WaterHeaterState {}
class WaterHeaterScheduleViewState extends WaterHeaterState { class WaterHeaterScheduleViewState extends WaterHeaterState {
final ScheduleModes scheduleMode; final ScheduleModes scheduleMode;
final int hours; final int hours;

View File

@ -5,38 +5,46 @@ enum ScheduleModes { countdown, schedule, circulate, inching }
class WaterHeaterStatusModel { class WaterHeaterStatusModel {
final String uuid; final String uuid;
final bool heaterSwitch; final bool heaterSwitch;
final String scheduleModeString;
final int countdownHours; final int countdownHours;
final int countdownMinutes; final int countdownMinutes;
final ScheduleModes scheduleMode; final ScheduleModes scheduleMode;
final String relayStatus;
final String cycleTiming;
WaterHeaterStatusModel({ WaterHeaterStatusModel({
required this.uuid, required this.uuid,
required this.heaterSwitch, required this.heaterSwitch,
required this.scheduleModeString,
required this.countdownHours, required this.countdownHours,
required this.countdownMinutes, required this.countdownMinutes,
}) : scheduleMode = getScheduleMode(scheduleModeString); required this.relayStatus,
required this.cycleTiming,
required this.scheduleMode,
});
factory WaterHeaterStatusModel.fromJson(String id, List<Status> jsonList) { factory WaterHeaterStatusModel.fromJson(String id, List<Status> jsonList) {
late bool heaterSwitch; late bool heaterSwitch = false;
late String scheduleMode; late int countdownHours = 0;
late int countdownHours; late int countdownMinutes = 0;
late int countdownMinutes; late String relayStatus = '';
late String cycleTiming = '';
late ScheduleModes scheduleMode = ScheduleModes.countdown;
for (var status in jsonList) { for (var status in jsonList) {
switch (status.code) { switch (status.code) {
case 'switch': case 'switch_1':
heaterSwitch = status.value ?? false; heaterSwitch = status.value ?? false;
break; break;
case 'schedule_mode': case 'countdown_1':
scheduleMode = status.value ?? 'countdown';
break;
case 'countdown_hours':
countdownHours = status.value ?? 0; countdownHours = status.value ?? 0;
break; break;
case 'countdown_minutes': case 'relay_status':
countdownMinutes = status.value ?? 0; relayStatus = status.value ?? 'memory';
break;
case 'cycle_timing':
cycleTiming = status.value ?? '';
break;
case 'switch_inching':
scheduleMode = getScheduleMode(status.value ?? 'countdown');
break; break;
} }
} }
@ -44,25 +52,31 @@ class WaterHeaterStatusModel {
return WaterHeaterStatusModel( return WaterHeaterStatusModel(
uuid: id, uuid: id,
heaterSwitch: heaterSwitch, heaterSwitch: heaterSwitch,
scheduleModeString: scheduleMode,
countdownHours: countdownHours, countdownHours: countdownHours,
countdownMinutes: countdownMinutes, countdownMinutes: countdownMinutes,
relayStatus: relayStatus,
cycleTiming: cycleTiming,
scheduleMode: scheduleMode,
); );
} }
WaterHeaterStatusModel copyWith({ WaterHeaterStatusModel copyWith({
String? uuid, String? uuid,
bool? heaterSwitch, bool? heaterSwitch,
String? scheduleModeString,
int? countdownHours, int? countdownHours,
int? countdownMinutes, int? countdownMinutes,
String? relayStatus,
String? cycleTiming,
ScheduleModes? scheduleMode,
}) { }) {
return WaterHeaterStatusModel( return WaterHeaterStatusModel(
uuid: uuid ?? this.uuid, uuid: uuid ?? this.uuid,
heaterSwitch: heaterSwitch ?? this.heaterSwitch, heaterSwitch: heaterSwitch ?? this.heaterSwitch,
scheduleModeString: scheduleModeString ?? this.scheduleModeString,
countdownHours: countdownHours ?? this.countdownHours, countdownHours: countdownHours ?? this.countdownHours,
countdownMinutes: countdownMinutes ?? this.countdownMinutes, countdownMinutes: countdownMinutes ?? this.countdownMinutes,
relayStatus: relayStatus ?? this.relayStatus,
cycleTiming: cycleTiming ?? this.cycleTiming,
scheduleMode: scheduleMode ?? this.scheduleMode,
); );
} }

View File

@ -58,19 +58,20 @@ class WaterHeaterDeviceControl extends StatelessWidget
children: [ children: [
ToggleWidget( ToggleWidget(
deviceId: device.uuid!, deviceId: device.uuid!,
code: 'water_heater', code: 'switch_1',
value: false, value: status.heaterSwitch,
label: 'Water Heater', label: 'Water Heater',
onChange: (value) { onChange: (value) {
context.read<WaterHeaterBloc>().add(ToggleWaterHeaterEvent( context.read<WaterHeaterBloc>().add(ToggleWaterHeaterEvent(
deviceId: device.uuid!, deviceId: device.uuid!,
isOn: value, code: 'switch_1',
value: value,
)); ));
}, },
), ),
GestureDetector( GestureDetector(
onTap: () { onTap: () {
context.read<WaterHeaterBloc>().add(const ShowScheduleViewEvent()); // context.read<WaterHeaterBloc>().add(const ShowScheduleViewEvent());
}, },
child: DeviceControlsContainer( child: DeviceControlsContainer(
child: Column( child: Column(