mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 14:47:55 +00:00
294 lines
9.9 KiB
Dart
294 lines
9.9 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.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;
|
|
AcStatusModel deviceStatus = AcStatusModel(
|
|
acSwitch: true,
|
|
modeString: 'hot',
|
|
tempSet: 300,
|
|
currentTemp: 315,
|
|
fanSpeedsString: 'low',
|
|
childLock: false);
|
|
List<AcStatusModel> deviceStatusList = [];
|
|
List<DeviceModel> devicesList = [];
|
|
bool allAcsPage = false;
|
|
bool allAcsOn = true;
|
|
bool allTempSame = true;
|
|
int globalTemp = 25;
|
|
|
|
ACsBloc({required this.acId}) : super(AcsInitialState()) {
|
|
on<AcsInitial>(_fetchAcsStatus);
|
|
on<AcSwitch>(_changeAcSwitch);
|
|
on<IncreaseCoolToTemp>(_increaseCoolTo);
|
|
on<DecreaseCoolToTemp>(_decreaseCoolTo);
|
|
on<ChangeLock>(_changeLockValue);
|
|
on<ChangeAcMode>(_changeAcMode);
|
|
on<ChangeFanSpeed>(_changeFanSpeed);
|
|
on<ChangeAllSwitch>(_changeAllAcSwitch);
|
|
on<IncreaseAllTemp>(_increaseAllTemp);
|
|
on<DecreaseAllTemp>(_decreaseAllTemp);
|
|
}
|
|
|
|
void _fetchAcsStatus(AcsInitial event, Emitter<AcsState> emit) async {
|
|
emit(AcsLoadingState());
|
|
try {
|
|
allAcsPage = event.allAcs;
|
|
if (event.allAcs) {
|
|
await _getAllAcs();
|
|
emit(GetAllAcsStatusState(
|
|
allAcsStatues: deviceStatusList,
|
|
allAcs: devicesList,
|
|
allOn: allAcsOn,
|
|
allTempSame: allTempSame,
|
|
temp: globalTemp));
|
|
} else {
|
|
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;
|
|
}
|
|
}
|
|
|
|
_getAllAcs() async {
|
|
deviceStatusList = [];
|
|
devicesList = [];
|
|
allAcsOn = true;
|
|
allTempSame = true;
|
|
devicesList = await DevicesAPI.getDeviceByGroupName(
|
|
HomeCubit.getInstance().selectedSpace?.id ?? '', 'AC');
|
|
|
|
for (int i = 0; i < devicesList.length; i++) {
|
|
var response = await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? '');
|
|
List<StatusModel> statusModelList = [];
|
|
for (var status in response['status']) {
|
|
statusModelList.add(StatusModel.fromJson(status));
|
|
}
|
|
deviceStatusList.add(AcStatusModel.fromJson(statusModelList));
|
|
}
|
|
if (deviceStatusList.isNotEmpty) {
|
|
int temp = deviceStatusList[0].tempSet;
|
|
deviceStatusList.firstWhere((element) {
|
|
if (!element.acSwitch) {
|
|
allAcsOn = false;
|
|
}
|
|
if (element.tempSet != temp) {
|
|
allTempSame = false;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
if (allTempSame) {
|
|
globalTemp = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
void _changeAcSwitch(AcSwitch event, Emitter<AcsState> emit) async {
|
|
emit(AcChangeLoading(acStatusModel: deviceStatus));
|
|
|
|
final acSwitchValue = !event.acSwitch;
|
|
try {
|
|
final response = await DevicesAPI.controlDevice(
|
|
DeviceControlModel(
|
|
deviceId: allAcsPage ? event.deviceId : acId, code: 'switch', value: acSwitchValue),
|
|
allAcsPage ? event.deviceId : acId);
|
|
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.acSwitch = acSwitchValue;
|
|
}
|
|
} catch (_) {}
|
|
if (allAcsPage) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
add(const AcsInitial(allAcs: true));
|
|
} else {
|
|
emit(AcModifyingState(acStatusModel: deviceStatus));
|
|
}
|
|
}
|
|
|
|
void _changeAllAcSwitch(ChangeAllSwitch event, Emitter<AcsState> emit) async {
|
|
emit(AcsLoadingState());
|
|
try {
|
|
if (deviceStatusList.length == devicesList.length) {
|
|
for (int i = 0; i < deviceStatusList.length; i++) {
|
|
await DevicesAPI.controlDevice(
|
|
DeviceControlModel(deviceId: devicesList[i].uuid, code: 'switch', value: event.value),
|
|
devicesList[i].uuid ?? '');
|
|
}
|
|
}
|
|
} catch (_) {}
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
add(const AcsInitial(allAcs: true));
|
|
}
|
|
|
|
void _increaseAllTemp(IncreaseAllTemp event, Emitter<AcsState> emit) async {
|
|
emit(AcsLoadingState());
|
|
try {
|
|
double tempValue = event.value + 0.5;
|
|
int value = (tempValue * 10).toInt();
|
|
if (deviceStatusList.length == devicesList.length) {
|
|
for (int i = 0; i < deviceStatusList.length; i++) {
|
|
await DevicesAPI.controlDevice(
|
|
DeviceControlModel(deviceId: devicesList[i].uuid, code: 'temp_set', value: value),
|
|
devicesList[i].uuid ?? '');
|
|
}
|
|
}
|
|
} catch (_) {}
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
add(const AcsInitial(allAcs: true));
|
|
}
|
|
|
|
void _decreaseAllTemp(DecreaseAllTemp event, Emitter<AcsState> emit) async {
|
|
emit(AcsLoadingState());
|
|
try {
|
|
double tempValue = event.value - 0.5;
|
|
int value = (tempValue * 10).toInt();
|
|
if (deviceStatusList.length == devicesList.length) {
|
|
for (int i = 0; i < deviceStatusList.length; i++) {
|
|
await DevicesAPI.controlDevice(
|
|
DeviceControlModel(deviceId: devicesList[i].uuid, code: 'temp_set', value: value),
|
|
devicesList[i].uuid ?? '');
|
|
}
|
|
}
|
|
} catch (_) {}
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
add(const AcsInitial(allAcs: true));
|
|
}
|
|
|
|
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: allAcsPage ? event.deviceId : acId, code: 'temp_set', value: value),
|
|
allAcsPage ? event.deviceId : acId);
|
|
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.tempSet = value;
|
|
}
|
|
} catch (_) {}
|
|
if (allAcsPage) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
add(const AcsInitial(allAcs: true));
|
|
} else {
|
|
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: allAcsPage ? event.deviceId : acId, code: 'temp_set', value: value),
|
|
allAcsPage ? event.deviceId : acId);
|
|
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.tempSet = value;
|
|
}
|
|
} catch (_) {}
|
|
if (allAcsPage) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
add(const AcsInitial(allAcs: true));
|
|
} else {
|
|
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: allAcsPage ? event.deviceId : acId,
|
|
code: 'mode',
|
|
value: getACModeString(tempMode)),
|
|
allAcsPage ? event.deviceId : acId);
|
|
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.modeString = getACModeString(tempMode);
|
|
deviceStatus.acMode = AcStatusModel.getACMode(getACModeString(tempMode));
|
|
}
|
|
} catch (_) {}
|
|
if (allAcsPage) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
add(const AcsInitial(allAcs: true));
|
|
} else {
|
|
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: allAcsPage ? event.deviceId : acId,
|
|
code: 'level',
|
|
value: getNextFanSpeedKey(fanSpeed)),
|
|
allAcsPage ? event.deviceId : acId);
|
|
|
|
try {
|
|
if (response['success'] ?? false) {
|
|
deviceStatus.fanSpeedsString = getNextFanSpeedKey(fanSpeed);
|
|
deviceStatus.acFanSpeed = AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed));
|
|
}
|
|
} catch (_) {}
|
|
if (allAcsPage) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
add(const AcsInitial(allAcs: true));
|
|
} else {
|
|
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';
|
|
}
|
|
}
|
|
}
|