mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
push water heater single control
This commit is contained in:
@ -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<TwoGangSwitchEvent, TwoGangSwitchState> {
|
||||
TwoGangSwitchBloc({required this.deviceId})
|
||||
: super(TwoGangSwitchInitial()) {
|
||||
TwoGangSwitchBloc({required this.deviceId}) : super(TwoGangSwitchInitial()) {
|
||||
on<TwoGangSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
|
||||
on<TwoGangSwitchControl>(_onControl);
|
||||
on<TwoGangSwitchFetchBatchEvent>(_onFetchBatchStatus);
|
||||
@ -89,7 +88,6 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
|
||||
if (code == 'switch_2') {
|
||||
deviceStatus = deviceStatus.copyWith(switch2: value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool _getValueByCode(String code) {
|
||||
@ -103,14 +101,13 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onFetchBatchStatus( TwoGangSwitchFetchBatchEvent event,
|
||||
Emitter< TwoGangSwitchState> emit) async {
|
||||
Future<void> _onFetchBatchStatus(TwoGangSwitchFetchBatchEvent event,
|
||||
Emitter<TwoGangSwitchState> 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()));
|
||||
|
@ -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<WaterHeaterEvent, WaterHeaterState> {
|
||||
StopScheduleEvent event,
|
||||
Emitter<WaterHeaterState> 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<WaterHeaterEvent, WaterHeaterState> {
|
||||
|
||||
FutureOr<void> _controlWaterHeater(
|
||||
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(
|
||||
deviceId: event.deviceId,
|
||||
value: event.isOn,
|
||||
code: event.code,
|
||||
value: event.value,
|
||||
oldValue: oldValue,
|
||||
emit: emit,
|
||||
);
|
||||
@ -75,8 +76,9 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
|
||||
Future<void> _runDebounce({
|
||||
required String deviceId,
|
||||
required bool value,
|
||||
required bool oldValue,
|
||||
required String code,
|
||||
required dynamic value,
|
||||
required dynamic oldValue,
|
||||
required Emitter<WaterHeaterState> emit,
|
||||
}) async {
|
||||
if (_timer != null) {
|
||||
@ -85,28 +87,47 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
|
||||
_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<WaterHeaterState> emit) {
|
||||
_updateLocalValue(oldValue);
|
||||
emit(WaterHeaterToggleState(isOn: oldValue));
|
||||
void _revertValueAndEmit(String deviceId, String code, dynamic oldValue,
|
||||
Emitter<WaterHeaterState> emit) {
|
||||
_updateLocalValue(code, oldValue, emit);
|
||||
emit(WaterHeaterDeviceStatusLoaded(deviceStatus));
|
||||
}
|
||||
|
||||
void _updateLocalValue(bool value) {
|
||||
deviceStatus = deviceStatus.copyWith(heaterSwitch: value);
|
||||
void _updateLocalValue(
|
||||
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(
|
||||
@ -114,22 +135,12 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
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<Status> 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()));
|
||||
}
|
||||
|
@ -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<Object?> get props => [isOn];
|
||||
List<Object?> get props => [value];
|
||||
}
|
||||
|
||||
final class UpdateScheduleEvent extends WaterHeaterEvent {
|
||||
|
@ -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<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 WaterHeaterLoadingState extends WaterHeaterState {}
|
||||
|
||||
final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState {
|
||||
final WaterHeaterStatusModel status;
|
||||
@ -46,15 +22,6 @@ final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState {
|
||||
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 String error;
|
||||
|
||||
@ -73,8 +40,6 @@ final class WaterHeaterBatchFailedState extends WaterHeaterState {
|
||||
List<Object?> get props => [error];
|
||||
}
|
||||
|
||||
final class WaterHeaterLoadingState extends WaterHeaterState {}
|
||||
|
||||
class WaterHeaterScheduleViewState extends WaterHeaterState {
|
||||
final ScheduleModes scheduleMode;
|
||||
final int hours;
|
||||
|
@ -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<Status> 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,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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<WaterHeaterBloc>().add(ToggleWaterHeaterEvent(
|
||||
deviceId: device.uuid!,
|
||||
isOn: value,
|
||||
code: 'switch_1',
|
||||
value: value,
|
||||
));
|
||||
},
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
context.read<WaterHeaterBloc>().add(const ShowScheduleViewEvent());
|
||||
// context.read<WaterHeaterBloc>().add(const ShowScheduleViewEvent());
|
||||
},
|
||||
child: DeviceControlsContainer(
|
||||
child: Column(
|
||||
|
Reference in New Issue
Block a user