mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
Update Automation Status
This commit is contained in:
@ -0,0 +1,124 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/auth/model/project_model.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/automation_scene_trigger_bloc/automation_scene_trigger_event.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/automation_scene_trigger_bloc/automation_scene_trigger_status.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/routine_model.dart';
|
||||
import 'package:syncrow_web/services/routines_api.dart';
|
||||
|
||||
class AutomationSceneTriggerBloc extends Bloc<AutomationSceneTriggerEvent, AutomationSceneTriggerStatus> {
|
||||
AutomationSceneTriggerBloc() : super(AutomationSceneInitial()) {
|
||||
// on<LoadScenes>(_onLoadScenes);
|
||||
// on<LoadAutomation>(_onLoadAutomation);
|
||||
on<SceneTrigger>(_onSceneTrigger);
|
||||
on<UpdateAutomationStatus>(_onUpdateAutomationStatus);
|
||||
}
|
||||
|
||||
List<ScenesModel> scenes = [];
|
||||
List<ScenesModel> automationList = [];
|
||||
|
||||
// Future<void> _onLoadScenes(LoadScenes event, Emitter<AutomationSceneTriggerStatus> emit) async {
|
||||
// emit(SceneLoading());
|
||||
|
||||
// try {
|
||||
// Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
// if (event.unitId.isNotEmpty) {
|
||||
// scenes = await SceneApi.getScenesByUnitId(event.unitId,
|
||||
// event.unit.community.uuid, project?.uuid ?? TempConst.projectIdDev,
|
||||
// showInDevice: event.showInDevice);
|
||||
// emit(SceneLoaded(scenes, automationList));
|
||||
// } else {
|
||||
// emit(const SceneError(message: 'Unit ID is empty'));
|
||||
// }
|
||||
// } catch (e) {
|
||||
// emit(const SceneError(message: 'Something went wrong'));
|
||||
// }
|
||||
// }
|
||||
|
||||
// Future<void> _onLoadAutomation(
|
||||
// LoadAutomation event, Emitter<AutomationSceneTriggerStatus> emit) async {
|
||||
// emit(SceneLoading());
|
||||
|
||||
// try {
|
||||
// Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
// if (event.unitId.isNotEmpty) {
|
||||
// automationList = await SceneApi.getAutomationByUnitId(
|
||||
// event.unitId, event.communityId, project?.uuid ?? '');
|
||||
// emit(SceneLoaded(scenes, automationList));
|
||||
// } else {
|
||||
// emit(const SceneError(message: 'Unit ID is empty'));
|
||||
// }
|
||||
// } catch (e) {
|
||||
// emit(const SceneError(message: 'Something went wrong'));
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<void> _onSceneTrigger(
|
||||
SceneTrigger event, Emitter<AutomationSceneTriggerStatus> emit) async {
|
||||
final currentState = state;
|
||||
if (currentState is AutomationSceneLoaded) {
|
||||
emit(AutomationSceneLoaded(
|
||||
currentState.scenes,
|
||||
currentState.automationList,
|
||||
loadingSceneId: event.sceneId,
|
||||
));
|
||||
|
||||
try {
|
||||
final success = await SceneApi.triggerScene(event.sceneId);
|
||||
if (success) {
|
||||
emit(SceneTriggerSuccess(event.name));
|
||||
emit(AutomationSceneLoaded(currentState.scenes, currentState.automationList));
|
||||
} else {
|
||||
emit(const AutomationSceneError(message: 'Something went wrong'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const AutomationSceneError(message: 'Something went wrong'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onUpdateAutomationStatus(
|
||||
UpdateAutomationStatus event, Emitter<AutomationSceneTriggerStatus> emit) async {
|
||||
final currentState = state;
|
||||
if (currentState is AutomationSceneLoaded) {
|
||||
final newLoadingStates =
|
||||
Map<String, bool>.from(currentState.loadingStates)
|
||||
..[event.automationId] = true;
|
||||
|
||||
emit(AutomationSceneLoaded(
|
||||
currentState.scenes,
|
||||
currentState.automationList,
|
||||
loadingStates: newLoadingStates,
|
||||
));
|
||||
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final success = await SceneApi.updateAutomationStatus(
|
||||
event.automationId,
|
||||
event.automationStatusUpdate,
|
||||
project?.uuid ?? '');
|
||||
if (success) {
|
||||
automationList = await SceneApi.getAutomationByUnitId(
|
||||
event.automationStatusUpdate.spaceUuid,
|
||||
event.communityId,
|
||||
project?.uuid ?? '');
|
||||
newLoadingStates[event.automationId] = false;
|
||||
emit(AutomationSceneLoaded(
|
||||
currentState.scenes,
|
||||
automationList,
|
||||
loadingStates: newLoadingStates,
|
||||
));
|
||||
} else {
|
||||
emit(const AutomationSceneError(message: 'Something went wrong'));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const AutomationSceneError(message: 'Something went wrong'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/automation_scene_trigger_bloc/automation_status_update.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/routine_bloc/routine_bloc.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart';
|
||||
|
||||
abstract class AutomationSceneTriggerEvent extends Equatable {
|
||||
const AutomationSceneTriggerEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class AutomationSceneScenes extends AutomationSceneTriggerEvent {
|
||||
final String unitId;
|
||||
final bool showInDevice;
|
||||
final SpaceModel unit;
|
||||
|
||||
const AutomationSceneScenes(this.unitId, this.unit, {this.showInDevice = false});
|
||||
|
||||
@override
|
||||
List<Object> get props => [unitId, showInDevice];
|
||||
}
|
||||
|
||||
class AutomationSceneAutomation extends AutomationSceneTriggerEvent {
|
||||
final String unitId;
|
||||
final String communityId;
|
||||
|
||||
|
||||
const AutomationSceneAutomation(this.unitId, this.communityId);
|
||||
|
||||
@override
|
||||
List<Object> get props => [unitId, communityId];
|
||||
}
|
||||
|
||||
class SceneTrigger extends AutomationSceneTriggerEvent {
|
||||
final String sceneId;
|
||||
final String name;
|
||||
|
||||
const SceneTrigger(this.sceneId, this.name);
|
||||
|
||||
@override
|
||||
List<Object> get props => [sceneId];
|
||||
}
|
||||
|
||||
//updateAutomationStatus
|
||||
class UpdateAutomationStatus extends AutomationSceneTriggerEvent {
|
||||
final String automationId;
|
||||
final AutomationStatusUpdate automationStatusUpdate;
|
||||
final String communityId;
|
||||
|
||||
const UpdateAutomationStatus({required this.automationStatusUpdate, required this.automationId, required this.communityId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [automationStatusUpdate];
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/routine_model.dart';
|
||||
|
||||
abstract class AutomationSceneTriggerStatus extends Equatable {
|
||||
const AutomationSceneTriggerStatus();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AutomationSceneInitial extends AutomationSceneTriggerStatus {}
|
||||
|
||||
class AutomationSceneLoading extends AutomationSceneTriggerStatus {}
|
||||
|
||||
class AutomationSceneLoaded extends AutomationSceneTriggerStatus {
|
||||
final List<ScenesModel> scenes;
|
||||
final List<ScenesModel> automationList;
|
||||
final String? loadingSceneId;
|
||||
final Map<String, bool> loadingStates;
|
||||
|
||||
const AutomationSceneLoaded(this.scenes, this.automationList,
|
||||
{this.loadingSceneId, this.loadingStates = const {}});
|
||||
|
||||
@override
|
||||
List<Object?> get props =>
|
||||
[scenes, loadingSceneId, automationList, loadingStates];
|
||||
}
|
||||
|
||||
class AutomationSceneError extends AutomationSceneTriggerStatus {
|
||||
final String message;
|
||||
|
||||
const AutomationSceneError({required this.message});
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
|
||||
class SceneTriggerSuccess extends AutomationSceneTriggerStatus {
|
||||
final String sceneName;
|
||||
|
||||
const SceneTriggerSuccess(this.sceneName);
|
||||
|
||||
@override
|
||||
List<Object> get props => [sceneName];
|
||||
}
|
||||
|
||||
class UpdateAutomationStatusLoading extends AutomationSceneTriggerStatus {
|
||||
const UpdateAutomationStatusLoading();
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
class AutomationStatusUpdate {
|
||||
final String spaceUuid;
|
||||
final bool isEnable;
|
||||
|
||||
AutomationStatusUpdate({
|
||||
required this.spaceUuid,
|
||||
required this.isEnable,
|
||||
});
|
||||
|
||||
factory AutomationStatusUpdate.fromRawJson(String str) =>
|
||||
AutomationStatusUpdate.fromJson(json.decode(str));
|
||||
|
||||
String toRawJson() => json.encode(toJson());
|
||||
|
||||
factory AutomationStatusUpdate.fromJson(Map<String, dynamic> json) =>
|
||||
AutomationStatusUpdate(
|
||||
spaceUuid: json["spaceUuid"],
|
||||
isEnable: json["isEnable"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"spaceUuid": spaceUuid,
|
||||
"isEnable": isEnable,
|
||||
};
|
||||
|
||||
factory AutomationStatusUpdate.fromMap(Map<String, dynamic> map) =>
|
||||
AutomationStatusUpdate(
|
||||
spaceUuid: map["spaceUuid"],
|
||||
isEnable: map["isEnable"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"spaceUuid": spaceUuid,
|
||||
"isEnable": isEnable,
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user