mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 23:27:56 +00:00
131 lines
4.6 KiB
Dart
131 lines
4.6 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:syncrow_app/features/devices/bloc/acs_bloc/acs_event.dart';
|
|
import 'package:syncrow_app/features/devices/bloc/acs_bloc/acs_state.dart';
|
|
import 'package:syncrow_app/features/devices/model/ac_model.dart';
|
|
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
|
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
|
import 'package:syncrow_app/features/devices/model/status_model.dart';
|
|
import 'package:syncrow_app/services/api/devices_api.dart';
|
|
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
|
|
|
class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
|
final String acId;
|
|
late DeviceModel deviceModel;
|
|
late AcStatusModel deviceStatus;
|
|
|
|
ACsBloc({required this.acId}) : super(AcsInitialState()) {
|
|
on<AcsInitial>(_fetchAcsStatus);
|
|
on<IncreaseCoolToTemp>(_increaseCoolTo);
|
|
on<DecreaseCoolToTemp>(_decreaseCoolTo);
|
|
on<ChangeLock>(_changeLockValue);
|
|
on<ChangeAcMode>(_changeAcMode);
|
|
on<ChangeFanSpeed>(_changeFanSpeed);
|
|
}
|
|
|
|
void _fetchAcsStatus(AcsInitial event, Emitter<AcsState> emit) async {
|
|
emit(AcsLoadingState());
|
|
try {
|
|
var response = await DevicesAPI.getDeviceStatus(acId);
|
|
List<StatusModel> statusModelList = [];
|
|
for (var status in response['status']) {
|
|
statusModelList.add(StatusModel.fromJson(status));
|
|
}
|
|
deviceStatus = AcStatusModel.fromJson(statusModelList);
|
|
emit(GetAcStatusState(acStatusModel: deviceStatus));
|
|
} catch (e) {
|
|
emit(AcsFailedState(error: e.toString()));
|
|
return;
|
|
}
|
|
}
|
|
|
|
void _changeLockValue(ChangeLock event, Emitter<AcsState> emit) async {
|
|
emit(AcChangeLoading(acStatusModel: deviceStatus));
|
|
|
|
final lockValue = !event.lockBool;
|
|
try {
|
|
final response = await DevicesAPI.controlDevice(
|
|
DeviceControlModel(deviceId: acId, code: 'child_lock', value: lockValue), acId);
|
|
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.childLock = lockValue;
|
|
}
|
|
} catch (_) {}
|
|
emit(AcModifyingState(acStatusModel: deviceStatus));
|
|
}
|
|
|
|
void _increaseCoolTo(IncreaseCoolToTemp event, Emitter<AcsState> emit) async {
|
|
emit(AcChangeLoading(acStatusModel: deviceStatus));
|
|
double tempValue = event.value + 0.5;
|
|
int value = (tempValue * 10).toInt();
|
|
try {
|
|
final response = await DevicesAPI.controlDevice(
|
|
DeviceControlModel(deviceId: acId, code: 'temp_set', value: value), acId);
|
|
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.tempSet = value;
|
|
}
|
|
} catch (_) {}
|
|
emit(AcModifyingState(acStatusModel: deviceStatus));
|
|
}
|
|
|
|
void _decreaseCoolTo(DecreaseCoolToTemp event, Emitter<AcsState> emit) async {
|
|
emit(AcChangeLoading(acStatusModel: deviceStatus));
|
|
|
|
double tempValue = event.value - 0.5;
|
|
int value = (tempValue * 10).toInt();
|
|
try {
|
|
final response = await DevicesAPI.controlDevice(
|
|
DeviceControlModel(deviceId: acId, code: 'temp_set', value: value), acId);
|
|
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.tempSet = value;
|
|
}
|
|
} catch (_) {}
|
|
emit(AcModifyingState(acStatusModel: deviceStatus));
|
|
}
|
|
|
|
void _changeAcMode(ChangeAcMode event, Emitter<AcsState> emit) async {
|
|
emit(AcChangeLoading(acStatusModel: deviceStatus));
|
|
final tempMode = tempModesMap[getNextItem(tempModesMap, event.tempModes)]!;
|
|
try {
|
|
final response = await DevicesAPI.controlDevice(
|
|
DeviceControlModel(deviceId: acId, code: 'mode', value: getACModeString(tempMode)), acId);
|
|
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.modeString = getACModeString(tempMode);
|
|
deviceStatus.acMode = AcStatusModel.getACMode(getACModeString(tempMode));
|
|
}
|
|
} catch (_) {}
|
|
emit(AcModifyingState(acStatusModel: deviceStatus));
|
|
}
|
|
|
|
void _changeFanSpeed(ChangeFanSpeed event, Emitter<AcsState> emit) async {
|
|
emit(AcChangeLoading(acStatusModel: deviceStatus));
|
|
|
|
final fanSpeed = event.fanSpeeds;
|
|
final response = await DevicesAPI.controlDevice(
|
|
DeviceControlModel(deviceId: acId, code: 'level', value: getNextFanSpeedKey(fanSpeed)),
|
|
acId);
|
|
|
|
try {
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.fanSpeedsString = getNextFanSpeedKey(fanSpeed);
|
|
deviceStatus.acFanSpeed = AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed));
|
|
}
|
|
} catch (_) {}
|
|
emit(AcModifyingState(acStatusModel: deviceStatus));
|
|
}
|
|
|
|
String getACModeString(TempModes value) {
|
|
if (value == TempModes.cold) {
|
|
return 'cold';
|
|
} else if (value == TempModes.hot) {
|
|
return 'hot';
|
|
} else if (value == TempModes.wind) {
|
|
return 'wind';
|
|
} else {
|
|
return 'cold';
|
|
}
|
|
}
|
|
}
|