mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-16 01:56:24 +00:00
Pulled latest changes
This commit is contained in:
@ -3,12 +3,17 @@ import 'dart:async';
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
|
||||
import 'package:syncrow_web/pages/routiens/models/create_scene_and_autoamtion/create_automation_model.dart';
|
||||
import 'package:syncrow_web/pages/routiens/models/create_scene_and_autoamtion/create_scene_model.dart';
|
||||
import 'package:syncrow_web/pages/routiens/models/delay/delay_fucntions.dart';
|
||||
import 'package:syncrow_web/pages/routiens/models/device_functions.dart';
|
||||
import 'package:syncrow_web/pages/routiens/models/routine_details_model.dart';
|
||||
import 'package:syncrow_web/pages/routiens/models/routine_model.dart';
|
||||
import 'package:syncrow_web/services/devices_mang_api.dart';
|
||||
import 'package:syncrow_web/services/routines_api.dart';
|
||||
import 'package:syncrow_web/utils/constants/assets.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
part 'routine_event.dart';
|
||||
part 'routine_state.dart';
|
||||
@ -34,11 +39,11 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
on<ResetRoutineState>(_onResetRoutineState);
|
||||
on<GetSceneDetails>(_onGetSceneDetails);
|
||||
on<GetAutomationDetails>(_onGetAutomationDetails);
|
||||
// on<InitializeRoutineState>(_onInitializeRoutineState);
|
||||
on<DeleteScene>(_deleteScene);
|
||||
on<DeleteAutomation>(_deleteAutomation);
|
||||
// on<RemoveFunction>(_onRemoveFunction);
|
||||
// on<ClearFunctions>(_onClearFunctions);
|
||||
// on<DeleteAutomation>(_deleteAutomation);
|
||||
on<FetchDevicesInRoutine>(_fetchDevices);
|
||||
on<UpdateScene>(_onUpdateScene);
|
||||
on<UpdateAutomation>(_onUpdateAutomation);
|
||||
}
|
||||
|
||||
void _onAddToIfContainer(AddToIfContainer event, Emitter<RoutineState> emit) {
|
||||
@ -411,20 +416,246 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
emit(state.copyWith(routineName: event.name));
|
||||
}
|
||||
|
||||
(List<Map<String, dynamic>>, List<Map<String, dynamic>>, Map<String, List<DeviceFunctionData>>)
|
||||
_createCardData(
|
||||
List<RoutineAction> actions,
|
||||
List<RoutineCondition>? conditions,
|
||||
Map<String, List<DeviceFunctionData>> currentFunctions,
|
||||
bool isTabToRun,
|
||||
) {
|
||||
final ifItems = isTabToRun
|
||||
? [
|
||||
{
|
||||
'entityId': 'tab_to_run',
|
||||
'uniqueCustomId': const Uuid().v4(),
|
||||
'deviceId': 'tab_to_run',
|
||||
'title': 'Tab to run',
|
||||
'productType': 'tab_to_run',
|
||||
'imagePath': Assets.tabToRun,
|
||||
}
|
||||
]
|
||||
: conditions?.map((condition) {
|
||||
final matchingDevice = state.devices.firstWhere(
|
||||
(device) => device.uuid == condition.entityId,
|
||||
orElse: () => AllDevicesModel(
|
||||
uuid: condition.entityId,
|
||||
name: condition.entityId,
|
||||
productType: condition.entityType,
|
||||
),
|
||||
);
|
||||
|
||||
final cardData = {
|
||||
'entityId': condition.entityId,
|
||||
'uniqueCustomId': const Uuid().v4(),
|
||||
'deviceId': condition.entityId,
|
||||
'title': matchingDevice.name ?? condition.entityId,
|
||||
'productType': condition.entityType,
|
||||
'imagePath': matchingDevice.getDefaultIcon(condition.entityType),
|
||||
};
|
||||
|
||||
final functions = matchingDevice.functions;
|
||||
|
||||
for (var function in functions) {
|
||||
if (function.code == condition.expr.statusCode) {
|
||||
currentFunctions[cardData['uniqueCustomId'] ?? ''] = [
|
||||
DeviceFunctionData(
|
||||
entityId: condition.entityId,
|
||||
functionCode: condition.expr.statusCode,
|
||||
value: condition.expr.statusValue,
|
||||
operationName: function.operationName,
|
||||
),
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return cardData;
|
||||
}).toList() ??
|
||||
[];
|
||||
|
||||
final thenItems = actions.map((action) {
|
||||
final matchingDevice = state.devices.firstWhere(
|
||||
(device) => device.uuid == action.entityId,
|
||||
orElse: () => AllDevicesModel(
|
||||
uuid: action.entityId,
|
||||
name: action.entityId,
|
||||
productType: action.productType,
|
||||
),
|
||||
);
|
||||
|
||||
final cardData = {
|
||||
'entityId': action.entityId,
|
||||
'uniqueCustomId': const Uuid().v4(),
|
||||
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
||||
'title': action.actionExecutor == 'delay' ? 'Delay' : (matchingDevice.name ?? 'Device'),
|
||||
'productType': action.productType,
|
||||
'imagePath': matchingDevice.getDefaultIcon(action.productType),
|
||||
};
|
||||
|
||||
final functions = matchingDevice.functions;
|
||||
|
||||
if (action.executorProperty != null && action.actionExecutor != 'delay') {
|
||||
final functionCode = action.executorProperty!.functionCode;
|
||||
for (var function in functions) {
|
||||
if (function.code == functionCode) {
|
||||
currentFunctions[cardData['uniqueCustomId'] ?? ''] = [
|
||||
DeviceFunctionData(
|
||||
entityId: action.entityId,
|
||||
functionCode: functionCode ?? '',
|
||||
value: action.executorProperty!.functionValue,
|
||||
operationName: function.operationName,
|
||||
),
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (action.actionExecutor == 'delay') {
|
||||
final delayFunction = DelayFunction(
|
||||
deviceId: action.entityId,
|
||||
deviceName: 'Delay',
|
||||
);
|
||||
currentFunctions[cardData['uniqueCustomId'] ?? ''] = [
|
||||
DeviceFunctionData(
|
||||
entityId: action.entityId,
|
||||
functionCode: 'delay',
|
||||
value: action.executorProperty?.delaySeconds ?? 0,
|
||||
operationName: delayFunction.operationName,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return cardData;
|
||||
}).toList();
|
||||
|
||||
return (thenItems, ifItems, currentFunctions);
|
||||
}
|
||||
|
||||
Future<void> _onGetSceneDetails(GetSceneDetails event, Emitter<RoutineState> emit) async {
|
||||
try {
|
||||
emit(state.copyWith(
|
||||
isLoading: true,
|
||||
isTabToRun: event.isTabToRun,
|
||||
isUpdate: true,
|
||||
sceneId: event.sceneId,
|
||||
isAutomation: false));
|
||||
isLoading: true,
|
||||
isTabToRun: event.isTabToRun,
|
||||
isUpdate: true,
|
||||
sceneId: event.sceneId,
|
||||
isAutomation: false,
|
||||
ifItems: [],
|
||||
thenItems: [],
|
||||
));
|
||||
|
||||
final sceneDetails = await SceneApi.getSceneDetails(event.sceneId);
|
||||
add(InitializeRoutineState(sceneDetails));
|
||||
|
||||
final List<Map<String, dynamic>> thenItems;
|
||||
final List<Map<String, dynamic>> ifItems;
|
||||
final Map<String, List<DeviceFunctionData>> updatedFunctions =
|
||||
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||
|
||||
if (event.isTabToRun) {
|
||||
thenItems = sceneDetails.actions.map((action) {
|
||||
AllDevicesModel? matchingDevice;
|
||||
for (var device in state.devices) {
|
||||
if (device.uuid == action.entityId) {
|
||||
matchingDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
final cardData = {
|
||||
'entityId': action.entityId,
|
||||
'uniqueCustomId': const Uuid().v4(),
|
||||
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
||||
'title': action.actionExecutor == 'delay'
|
||||
? 'Delay'
|
||||
: action.type == 'automation'
|
||||
? action.name ?? 'Automation'
|
||||
: (matchingDevice?.name ?? 'Device'),
|
||||
'productType': action.productType,
|
||||
'functions': matchingDevice?.functions,
|
||||
'imagePath': matchingDevice?.getDefaultIcon(action.productType),
|
||||
'device': matchingDevice ?? null,
|
||||
'name': action.name,
|
||||
'type': action.type,
|
||||
};
|
||||
if (action.type == 'automation') {
|
||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
||||
DeviceFunctionData(
|
||||
entityId: action.entityId,
|
||||
functionCode: 'rule',
|
||||
value: action.actionExecutor,
|
||||
operationName: action.name ?? 'Automation',
|
||||
),
|
||||
];
|
||||
} else if (action.executorProperty != null && action.actionExecutor != 'delay') {
|
||||
final functions = matchingDevice?.functions;
|
||||
final functionCode = action.executorProperty!.functionCode;
|
||||
for (var function in functions ?? []) {
|
||||
if (function.code == functionCode) {
|
||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
||||
DeviceFunctionData(
|
||||
entityId: action.entityId,
|
||||
functionCode: functionCode ?? '',
|
||||
value: action.executorProperty!.functionValue,
|
||||
operationName: function.operationName,
|
||||
),
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (action.actionExecutor == 'delay') {
|
||||
final delayFunction = DelayFunction(
|
||||
deviceId: action.entityId,
|
||||
deviceName: 'Delay',
|
||||
);
|
||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
||||
DeviceFunctionData(
|
||||
entityId: action.entityId,
|
||||
functionCode: 'delay',
|
||||
value: action.executorProperty?.delaySeconds ?? 0,
|
||||
operationName: delayFunction.operationName,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return cardData;
|
||||
}).toList();
|
||||
|
||||
ifItems = [
|
||||
{
|
||||
'entityId': 'tab_to_run',
|
||||
'uniqueCustomId': const Uuid().v4(),
|
||||
'deviceId': 'tab_to_run',
|
||||
'title': 'Tab to run',
|
||||
'productType': 'tab_to_run',
|
||||
'imagePath': Assets.tabToRun,
|
||||
}
|
||||
];
|
||||
} else {
|
||||
final result = _createCardData(
|
||||
sceneDetails.actions,
|
||||
sceneDetails.conditions,
|
||||
updatedFunctions,
|
||||
false,
|
||||
);
|
||||
thenItems = result.$1;
|
||||
ifItems = result.$2;
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
routineName: sceneDetails.name,
|
||||
selectedIcon: sceneDetails.iconId,
|
||||
selectedAutomationOperator: sceneDetails.decisionExpr,
|
||||
effectiveTime: sceneDetails.effectiveTime,
|
||||
isAutomation: false,
|
||||
isTabToRun: event.isTabToRun,
|
||||
thenItems: thenItems,
|
||||
ifItems: ifItems,
|
||||
selectedFunctions: updatedFunctions,
|
||||
sceneId: sceneDetails.sceneId,
|
||||
));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: 'Failed to load scene details',
|
||||
errorMessage: 'Failed to load scene details: $e',
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -434,99 +665,147 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
try {
|
||||
emit(state.copyWith(
|
||||
isLoading: true,
|
||||
isAutomation: event.isAutomation,
|
||||
automationId: event.automationId,
|
||||
isTabToRun: false,
|
||||
isUpdate: true,
|
||||
isTabToRun: false,
|
||||
automationId: event.automationId,
|
||||
isAutomation: true,
|
||||
ifItems: [],
|
||||
thenItems: [],
|
||||
));
|
||||
|
||||
final automationDetails = await SceneApi.getAutomationDetails(event.automationId);
|
||||
add(InitializeRoutineState(automationDetails));
|
||||
|
||||
final List<Map<String, dynamic>> thenItems;
|
||||
final List<Map<String, dynamic>> ifItems;
|
||||
final Map<String, List<DeviceFunctionData>> updatedFunctions =
|
||||
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
|
||||
|
||||
ifItems = automationDetails.conditions?.map((condition) {
|
||||
late AllDevicesModel? matchingDevice;
|
||||
for (var device in state.devices) {
|
||||
if (device.uuid == condition.entityId) {
|
||||
matchingDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
final cardData = {
|
||||
'entityId': condition.entityId,
|
||||
'uniqueCustomId': const Uuid().v4(),
|
||||
'deviceId': condition.expr.statusCode == 'delay' ? 'delay' : condition.entityId,
|
||||
'title': condition.expr.statusCode == 'delay'
|
||||
? 'Delay'
|
||||
: (matchingDevice?.name ?? 'Device'),
|
||||
'productType': condition.productType,
|
||||
'functions': matchingDevice?.functions ?? [],
|
||||
'imagePath': matchingDevice?.getDefaultIcon(condition.productType) ?? '',
|
||||
'device': matchingDevice,
|
||||
};
|
||||
|
||||
final functions = matchingDevice?.functions ?? [];
|
||||
for (var function in functions) {
|
||||
if (function.code == condition.expr.statusCode) {
|
||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
||||
DeviceFunctionData(
|
||||
entityId: condition.entityId,
|
||||
functionCode: condition.expr.statusCode,
|
||||
value: condition.expr.statusValue,
|
||||
operationName: function.operationName,
|
||||
),
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return cardData;
|
||||
}).toList() ??
|
||||
[];
|
||||
|
||||
// Create then items from actions
|
||||
thenItems = automationDetails.actions.map((action) {
|
||||
final matchingDevice = state.devices.firstWhere(
|
||||
(device) => device.uuid == action.entityId,
|
||||
orElse: () => AllDevicesModel(
|
||||
uuid: action.entityId,
|
||||
name: action.entityId,
|
||||
productType: action.productType,
|
||||
),
|
||||
);
|
||||
|
||||
final cardData = {
|
||||
'entityId': action.entityId,
|
||||
'uniqueCustomId': const Uuid().v4(),
|
||||
'deviceId': action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
||||
'title': action.actionExecutor == 'delay' ? 'Delay' : (matchingDevice.name ?? 'Device'),
|
||||
'productType': action.productType,
|
||||
'functions': matchingDevice.functions,
|
||||
'imagePath': matchingDevice.getDefaultIcon(action.productType),
|
||||
'device': matchingDevice,
|
||||
};
|
||||
|
||||
if (action.executorProperty != null && action.actionExecutor != 'delay') {
|
||||
final functions = matchingDevice.functions;
|
||||
final functionCode = action.executorProperty!.functionCode;
|
||||
for (var function in functions) {
|
||||
if (function.code == functionCode) {
|
||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
||||
DeviceFunctionData(
|
||||
entityId: action.entityId,
|
||||
functionCode: functionCode ?? '',
|
||||
value: action.executorProperty!.functionValue,
|
||||
operationName: function.operationName,
|
||||
),
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (action.actionExecutor == 'delay') {
|
||||
final delayFunction = DelayFunction(
|
||||
deviceId: action.entityId,
|
||||
deviceName: 'Delay',
|
||||
);
|
||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
||||
DeviceFunctionData(
|
||||
entityId: action.entityId,
|
||||
functionCode: 'delay',
|
||||
value: action.executorProperty?.delaySeconds ?? 0,
|
||||
operationName: delayFunction.operationName,
|
||||
),
|
||||
];
|
||||
} else if (action.actionExecutor == 'rule_disable' ||
|
||||
action.actionExecutor == 'rule_enable') {
|
||||
updatedFunctions[cardData['uniqueCustomId'].toString()] = [
|
||||
DeviceFunctionData(
|
||||
entityId: action.entityId,
|
||||
functionCode: 'automation',
|
||||
value: action.actionExecutor,
|
||||
operationName: action.name ?? 'Automation',
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return cardData;
|
||||
}).toList();
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
routineName: automationDetails.name,
|
||||
selectedAutomationOperator: automationDetails.decisionExpr,
|
||||
effectiveTime: automationDetails.effectiveTime,
|
||||
isAutomation: true,
|
||||
thenItems: thenItems,
|
||||
ifItems: ifItems,
|
||||
selectedFunctions: updatedFunctions,
|
||||
automationId: automationDetails.automationId,
|
||||
));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: 'Failed to load automation details',
|
||||
errorMessage: 'Failed to load automation details: $e',
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// void _onInitializeRoutineState(
|
||||
// InitializeRoutineState event, Emitter<RoutineState> emit) {
|
||||
// final routineDetails = event.routineDetails;
|
||||
|
||||
// // Convert actions to draggable cards for the THEN container
|
||||
// final thenItems = routineDetails.actions.map((action) {
|
||||
// final Map<String, dynamic> cardData = {
|
||||
// 'entityId': action.entityId,
|
||||
// 'uniqueCustomId': const Uuid().v4(),
|
||||
// 'deviceId':
|
||||
// action.actionExecutor == 'delay' ? 'delay' : action.entityId,
|
||||
// 'title': action.actionExecutor == 'delay' ? 'Delay' : 'Device',
|
||||
// // fix this
|
||||
// 'imagePath':
|
||||
// action.actionExecutor == 'delay' ? Assets.delay : Assets.logo,
|
||||
// };
|
||||
|
||||
// // Add functions to selectedFunctions
|
||||
// if (action.executorProperty != null) {
|
||||
// final functions = <DeviceFunctionData>[
|
||||
// DeviceFunctionData(
|
||||
// entityId: action.entityId,
|
||||
// functionCode: action.executorProperty!.functionCode ?? '',
|
||||
// value: action.executorProperty!.functionValue,
|
||||
|
||||
// /// fix this
|
||||
// operationName: action.executorProperty?.functionCode ?? ''),
|
||||
// ];
|
||||
// state.selectedFunctions[cardData['uniqueCustomId']] = functions;
|
||||
// }
|
||||
|
||||
// return cardData;
|
||||
// }).toList();
|
||||
|
||||
// // Convert conditions to draggable cards for the IF container
|
||||
// final ifItems = routineDetails.conditions?.map((condition) {
|
||||
// final Map<String, dynamic> cardData = {
|
||||
// 'entityId': condition.entityId,
|
||||
// 'uniqueCustomId': const Uuid().v4(),
|
||||
// 'deviceId': condition.entityId,
|
||||
|
||||
// /// fix this
|
||||
// 'title': 'Device',
|
||||
|
||||
// /// fix this
|
||||
// 'imagePath': Assets.logo,
|
||||
// };
|
||||
|
||||
// // Add functions to selectedFunctions
|
||||
// final functions = <DeviceFunctionData>[
|
||||
// DeviceFunctionData(
|
||||
// entityId: condition.entityId,
|
||||
// functionCode: condition.expr.statusCode,
|
||||
// value: condition.expr.statusValue,
|
||||
// condition: condition.expr.comparator,
|
||||
// operationName: condition.expr.comparator,
|
||||
// ),
|
||||
// ];
|
||||
// state.selectedFunctions[cardData['uniqueCustomId']] = functions;
|
||||
|
||||
// return cardData;
|
||||
// }).toList() ??
|
||||
// [];
|
||||
|
||||
// emit(state.copyWith(
|
||||
// isLoading: false,
|
||||
// routineName: routineDetails.name,
|
||||
// selectedIcon: routineDetails.iconId,
|
||||
// selectedAutomationOperator: routineDetails.decisionExpr,
|
||||
// effectiveTime: routineDetails.effectiveTime,
|
||||
// isAutomation: routineDetails.conditions != null,
|
||||
// isTabToRun: routineDetails.conditions == null,
|
||||
// thenItems: thenItems,
|
||||
// ifItems: ifItems,
|
||||
// selectedFunctions: Map.from(state.selectedFunctions),
|
||||
// ));
|
||||
// }
|
||||
|
||||
RoutineState _resetState() {
|
||||
return const RoutineState(
|
||||
ifItems: [],
|
||||
@ -555,7 +834,12 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
FutureOr<void> _deleteScene(DeleteScene event, Emitter<RoutineState> emit) {
|
||||
try {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
SceneApi.deleteScene(unitUuid: spaceId, sceneId: event.sceneId);
|
||||
if (state.isTabToRun) {
|
||||
SceneApi.deleteScene(unitUuid: spaceId, sceneId: event.id);
|
||||
} else {
|
||||
SceneApi.deleteAutomation(unitUuid: spaceId, automationId: event.id);
|
||||
}
|
||||
|
||||
add(const LoadScenes(spaceId, communityId));
|
||||
add(const LoadAutomation(spaceId));
|
||||
emit(_resetState());
|
||||
@ -567,17 +851,173 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _deleteAutomation(DeleteAutomation event, Emitter<RoutineState> emit) {
|
||||
// FutureOr<void> _deleteAutomation(DeleteAutomation event, Emitter<RoutineState> emit) {
|
||||
// try {
|
||||
// emit(state.copyWith(isLoading: true));
|
||||
// add(const LoadAutomation(spaceId));
|
||||
// add(const LoadScenes(spaceId, communityId));
|
||||
// emit(_resetState());
|
||||
// } catch (e) {
|
||||
// emit(state.copyWith(
|
||||
// isLoading: false,
|
||||
// errorMessage: 'Failed to delete automation',
|
||||
// ));
|
||||
// }
|
||||
// }
|
||||
|
||||
FutureOr<void> _fetchDevices(FetchDevicesInRoutine event, Emitter<RoutineState> emit) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
final devices = await DevicesManagementApi().fetchDevices();
|
||||
|
||||
emit(state.copyWith(isLoading: false, devices: devices));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _onUpdateScene(UpdateScene event, Emitter<RoutineState> emit) async {
|
||||
try {
|
||||
// Check if first action is delay
|
||||
if (_isFirstActionDelay(state.thenItems)) {
|
||||
emit(state.copyWith(
|
||||
errorMessage: 'Cannot have delay as the first action',
|
||||
isLoading: false,
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
emit(state.copyWith(isLoading: true));
|
||||
SceneApi.deleteAutomation(unitUuid: spaceId, automationId: event.automationId);
|
||||
add(const LoadAutomation(spaceId));
|
||||
add(const LoadScenes(spaceId, communityId));
|
||||
emit(_resetState());
|
||||
|
||||
final actions = state.thenItems.expand((item) {
|
||||
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||
return functions.map((function) {
|
||||
if (function.functionCode == 'automation') {
|
||||
return CreateSceneAction(
|
||||
entityId: function.entityId,
|
||||
actionExecutor: function.value,
|
||||
executorProperty: null,
|
||||
);
|
||||
}
|
||||
|
||||
if (item['deviceId'] == 'delay') {
|
||||
return CreateSceneAction(
|
||||
entityId: function.entityId,
|
||||
actionExecutor: 'delay',
|
||||
executorProperty: CreateSceneExecutorProperty(
|
||||
functionCode: '',
|
||||
functionValue: '',
|
||||
delaySeconds: int.tryParse(function.value.toString()) ?? 0,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return CreateSceneAction(
|
||||
entityId: function.entityId,
|
||||
actionExecutor: 'device_issue',
|
||||
executorProperty: CreateSceneExecutorProperty(
|
||||
functionCode: function.functionCode,
|
||||
functionValue: function.value,
|
||||
delaySeconds: 0,
|
||||
),
|
||||
);
|
||||
});
|
||||
}).toList();
|
||||
|
||||
final createSceneModel = CreateSceneModel(
|
||||
spaceUuid: state.sceneId ?? '',
|
||||
iconId: state.selectedIcon ?? '',
|
||||
showInDevice: true,
|
||||
sceneName: state.routineName ?? '',
|
||||
decisionExpr: 'and',
|
||||
actions: actions,
|
||||
);
|
||||
|
||||
final result = await SceneApi.updateScene(createSceneModel, state.sceneId ?? '');
|
||||
if (result['success']) {
|
||||
emit(_resetState());
|
||||
add(const LoadScenes(spaceId, communityId));
|
||||
} else {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: result['message'],
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: 'Failed to delete automation',
|
||||
errorMessage: 'Something went wrong',
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _onUpdateAutomation(UpdateAutomation event, Emitter<RoutineState> emit) async {
|
||||
if (_isFirstActionDelay(state.thenItems)) {
|
||||
emit(state.copyWith(
|
||||
errorMessage: 'Cannot have delay as the first action',
|
||||
isLoading: false,
|
||||
));
|
||||
return;
|
||||
}
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
final conditions = state.ifItems
|
||||
.map((item) {
|
||||
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||
if (functions.isEmpty) return null;
|
||||
|
||||
final function = functions.first;
|
||||
return Condition(
|
||||
code: state.ifItems.indexOf(item) + 1,
|
||||
entityId: function.entityId,
|
||||
entityType: item['productType'],
|
||||
expr: ConditionExpr(
|
||||
statusCode: function.functionCode,
|
||||
statusValue: function.value,
|
||||
comparator: function.condition ?? '==',
|
||||
),
|
||||
);
|
||||
})
|
||||
.whereType<Condition>()
|
||||
.toList();
|
||||
|
||||
final actions = state.thenItems
|
||||
.map((item) {
|
||||
final functions = state.selectedFunctions[item['uniqueCustomId']] ?? [];
|
||||
if (functions.isEmpty) return null;
|
||||
|
||||
final function = functions.first;
|
||||
return AutomationAction(
|
||||
entityId: function.entityId,
|
||||
actionExecutor: function.actionExecutor,
|
||||
executorProperty: ExecutorProperty(
|
||||
functionCode: function.functionCode,
|
||||
functionValue: function.value,
|
||||
delaySeconds:
|
||||
function.functionCode == 'delay' ? (function.value as num).toInt() : null,
|
||||
),
|
||||
);
|
||||
})
|
||||
.whereType<AutomationAction>()
|
||||
.toList();
|
||||
|
||||
final createAutomationModel = CreateAutomationModel(
|
||||
spaceUuid: spaceId,
|
||||
automationName: state.routineName ?? '',
|
||||
decisionExpr: state.selectedAutomationOperator,
|
||||
effectiveTime: state.effectiveTime ?? EffectiveTime(start: '', end: '', loops: ''),
|
||||
conditions: conditions,
|
||||
actions: actions,
|
||||
);
|
||||
|
||||
await SceneApi.updateAutomation(createAutomationModel, state.automationId ?? '');
|
||||
|
||||
add(const LoadAutomation(spaceId));
|
||||
emit(state.copyWith(isLoading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: 'Failed to update automation: $e',
|
||||
));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user