mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
changing ac bloc
This commit is contained in:
@ -1,16 +1,31 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/ac/bloc/ac_event.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/ac/bloc/ac_state.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/ac/model/ac_model.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
||||
import 'package:syncrow_web/services/devices_mang_api.dart';
|
||||
|
||||
|
||||
class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
AcStatusModel deviceStatus = AcStatusModel(
|
||||
uuid: '',
|
||||
acSwitch: true,
|
||||
modeString: 'hot',
|
||||
tempSet: 300,
|
||||
currentTemp: 315,
|
||||
fanSpeedsString: 'low',
|
||||
childLock: false,
|
||||
);
|
||||
|
||||
AcBloc() : super(AcsInitialState()) {
|
||||
on<AcFetchDeviceStatus>(_onFetchAcStatus);
|
||||
|
||||
on<AcControl>(_onAcControl);
|
||||
on<AcSwitch>(_changeAcSwitch);
|
||||
on<IncreaseCoolToTemp>(_increaseCoolTo);
|
||||
on<DecreaseCoolToTemp>(_decreaseCoolTo);
|
||||
on<ChangeLock>(_changeLockValue);
|
||||
on<ChangeAcMode>(_changeAcMode);
|
||||
on<ChangeFanSpeed>(_changeFanSpeed);
|
||||
}
|
||||
|
||||
FutureOr<void> _onFetchAcStatus(
|
||||
@ -18,11 +33,121 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
emit(AcsLoadingState());
|
||||
try {
|
||||
final status =
|
||||
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||
emit(ACStatusLoaded(status));
|
||||
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||
deviceStatus = AcStatusModel.fromJson(status.productUuid, status.status);
|
||||
emit(ACStatusLoaded(deviceStatus));
|
||||
} catch (e) {
|
||||
emit(AcsFailedState( error: e.toString()));
|
||||
emit(AcsFailedState(error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _onAcControl(AcControl event, Emitter<AcsState> emit) async {
|
||||
final oldValue = _getValueByCode(event.code);
|
||||
|
||||
_updateLocalValue(event.code, event.value);
|
||||
emit(ACStatusLoaded(deviceStatus));
|
||||
|
||||
try {
|
||||
final status = Status(code: event.code, value: event.value);
|
||||
final response =
|
||||
await DevicesManagementApi().deviceControl(event.deviceId, status);
|
||||
|
||||
if (!response) {
|
||||
_updateLocalValue(event.code, oldValue);
|
||||
emit(ACStatusLoaded(deviceStatus));
|
||||
emit(AcsFailedState(error: 'Failed to control the device.'));
|
||||
}
|
||||
} catch (e) {
|
||||
_updateLocalValue(event.code, oldValue);
|
||||
emit(ACStatusLoaded(deviceStatus));
|
||||
emit(AcsFailedState(error: 'Error controlling the device: $e'));
|
||||
}
|
||||
}
|
||||
|
||||
void _updateLocalValue(String code, dynamic value) {
|
||||
switch (code) {
|
||||
case 'switch':
|
||||
deviceStatus.acSwitch = value;
|
||||
break;
|
||||
case 'temp_set':
|
||||
deviceStatus.tempSet = value;
|
||||
break;
|
||||
case 'mode':
|
||||
deviceStatus.modeString = value;
|
||||
deviceStatus.acMode = AcStatusModel.getACMode(value);
|
||||
break;
|
||||
case 'level':
|
||||
deviceStatus.fanSpeedsString = value;
|
||||
deviceStatus.acFanSpeed = AcStatusModel.getFanSpeed(value);
|
||||
break;
|
||||
case 'child_lock':
|
||||
deviceStatus.childLock = value;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dynamic _getValueByCode(String code) {
|
||||
switch (code) {
|
||||
case 'switch':
|
||||
return deviceStatus.acSwitch;
|
||||
case 'temp_set':
|
||||
return deviceStatus.tempSet;
|
||||
case 'mode':
|
||||
return deviceStatus.modeString;
|
||||
case 'level':
|
||||
return deviceStatus.fanSpeedsString;
|
||||
case 'child_lock':
|
||||
return deviceStatus.childLock;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _changeAcSwitch(AcSwitch event, Emitter<AcsState> emit) async {
|
||||
final newValue = !event.acSwitch;
|
||||
add(AcControl(deviceId: event.deviceId, code: 'switch', value: newValue));
|
||||
}
|
||||
|
||||
FutureOr<void> _increaseCoolTo(
|
||||
IncreaseCoolToTemp event, Emitter<AcsState> emit) async {
|
||||
final newValue = (event.value * 10).toInt() + 5;
|
||||
if (_isValidTemperature(newValue)) {
|
||||
add(AcControl(
|
||||
deviceId: event.deviceId, code: 'temp_set', value: newValue));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _decreaseCoolTo(
|
||||
DecreaseCoolToTemp event, Emitter<AcsState> emit) async {
|
||||
final newValue = (event.value * 10).toInt() - 5;
|
||||
if (_isValidTemperature(newValue)) {
|
||||
add(AcControl(
|
||||
deviceId: event.deviceId, code: 'temp_set', value: newValue));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _changeLockValue(
|
||||
ChangeLock event, Emitter<AcsState> emit) async {
|
||||
final newValue = !event.lockBool;
|
||||
add(AcControl(
|
||||
deviceId: event.deviceId, code: 'child_lock', value: newValue));
|
||||
}
|
||||
|
||||
FutureOr<void> _changeAcMode(
|
||||
ChangeAcMode event, Emitter<AcsState> emit) async {
|
||||
final newValue = AcStatusModel.getACMode(event.tempModes.name);
|
||||
add(AcControl(deviceId: event.deviceId, code: 'mode', value: newValue));
|
||||
}
|
||||
|
||||
FutureOr<void> _changeFanSpeed(
|
||||
ChangeFanSpeed event, Emitter<AcsState> emit) async {
|
||||
final newValue = AcStatusModel.getFanSpeed(event.fanSpeeds.name);
|
||||
add(AcControl(deviceId: event.deviceId, code: 'level', value: newValue));
|
||||
}
|
||||
|
||||
bool _isValidTemperature(int value) {
|
||||
return value >= 200 && value <= 300;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user