public override async Task <Result> ExportAsync(string fileName, CancellationToken cancellationToken) { using (var context = new ZvsContext(EntityContextConnection)) { var existingTriggers = await context.DeviceValueTriggers .ToListAsync(cancellationToken); var backupTriggers = new List <TriggerBackup>(); foreach (var o in existingTriggers) { var trigger = new TriggerBackup { Name = o.Name, isEnabled = o.IsEnabled, DeviceValueName = o.DeviceValue.Name, NodeNumber = o.DeviceValue.Device.NodeNumber, StoredCommand = await StoredCmdBackup.ConvertToBackupCommand(o), Operator = (int?)o.Operator, Value = o.Value }; backupTriggers.Add(trigger); } var saveResult = await SaveAsXmlToDiskAsync(backupTriggers, fileName); if (saveResult.HasError) { return(Result.ReportError(saveResult.Message)); } return(Result.ReportSuccessFormat("Exported {0} triggers to {1}", backupTriggers.Count, Path.GetFileName(fileName))); } }
public async static Task <StoredCmdBackup> ConvertToBackupCommand(IStoredCommand m) { if (m == null) { return(null); } var bcmd = new StoredCmdBackup(); if (m.Command is BuiltinCommand) { bcmd.CommandType = CommandTypes.Builtin; } else if (m.Command is DeviceTypeCommand) { bcmd.CommandType = CommandTypes.DeviceType; using (var context = new ZvsContext()) { int dId = int.TryParse(m.Argument2, out dId) ? dId : 0; var d = await context.Devices.FirstOrDefaultAsync(o => o.Id == dId); if (d != null) { bcmd.NodeNumber = d.NodeNumber; } } } else { var command = m.Command as DeviceCommand; if (command != null) { bcmd.CommandType = CommandTypes.Device; bcmd.NodeNumber = command.Device.NodeNumber; } else if (m.Command is JavaScriptCommand) { bcmd.CommandType = CommandTypes.JavaScript; } } if (m.Command != null) { bcmd.UniqueIdentifier = m.Command.UniqueIdentifier; } bcmd.Argument = m.Argument; return(bcmd); }
public async override Task <Result> ExportAsync(string fileName, CancellationToken cancellationToken) { var cmdCount = 0; using (var context = new ZvsContext(EntityContextConnection)) { var existingScenes = await context.Scenes .Include(o => o.Commands) .ToListAsync(cancellationToken); var backupScenes = new List <SceneBackup>(); foreach (var s in existingScenes) { var sceneBackup = new SceneBackup { Name = s.Name }; foreach (var scmd in s.Commands) { var sceneCmdBackup = new SceneCMDBackup { Order = scmd.SortOrder, StoredCommand = await StoredCmdBackup.ConvertToBackupCommand(scmd) }; sceneBackup.Commands.Add(sceneCmdBackup); cmdCount++; } backupScenes.Add(sceneBackup); } var saveResult = await SaveAsXmlToDiskAsync(backupScenes, fileName); if (saveResult.HasError) { return(Result.ReportError(saveResult.Message)); } return(Result.ReportSuccessFormat("Exported {0} scenes with {1} scene commands to {2}", backupScenes.Count, cmdCount, Path.GetFileName(fileName))); } }
public async override Task <Result> ExportAsync(string fileName, CancellationToken cancellationToken) { using (var context = new ZvsContext(EntityContextConnection)) { var existingSTs = await context.ScheduledTasks .ToListAsync(cancellationToken); var backupSTs = new List <ScheduledTaskBackup>(); foreach (var t in existingSTs) { var task = new ScheduledTaskBackup { StoredCommand = await StoredCmdBackup.ConvertToBackupCommand(t), startTime = t.StartTime, Frequency = (int)t.TaskType, Name = t.Name, isEnabled = t.IsEnabled, DaysOfMonthToActivate = (int)t.DaysOfMonthToActivate, DaysOfWeekToActivate = (int)t.DaysOfWeekToActivate, RecurDays = t.RepeatIntervalInDays, RecurMonth = t.RepeatIntervalInMonths, RecurSeconds = t.RepeatIntervalInSeconds, RecurWeeks = t.RepeatIntervalInWeeks, sortOrder = t.SortOrder }; backupSTs.Add(task); } var saveResult = await SaveAsXmlToDiskAsync(backupSTs, fileName); if (saveResult.HasError) { return(Result.ReportError(saveResult.Message)); } return(Result.ReportSuccessFormat("Exported {0} scheduled tasks to {1}", backupSTs.Count, Path.GetFileName(fileName))); } }
public async static Task<StoredCmdBackup> ConvertToBackupCommand(IStoredCommand m) { if (m == null) return null; var bcmd = new StoredCmdBackup(); if (m.Command is BuiltinCommand) bcmd.CommandType = CommandTypes.Builtin; else if (m.Command is DeviceTypeCommand) { bcmd.CommandType = CommandTypes.DeviceType; using (var context = new ZvsContext()) { int dId = int.TryParse(m.Argument2, out dId) ? dId : 0; var d = await context.Devices.FirstOrDefaultAsync(o => o.Id == dId); if (d != null) bcmd.NodeNumber = d.NodeNumber; } } else { var command = m.Command as DeviceCommand; if (command != null) { bcmd.CommandType = CommandTypes.Device; bcmd.NodeNumber = command.Device.NodeNumber; } else if (m.Command is JavaScriptCommand) bcmd.CommandType = CommandTypes.JavaScript; } if (m.Command != null) bcmd.UniqueIdentifier = m.Command.UniqueIdentifier; bcmd.Argument = m.Argument; return bcmd; }
public async override Task <Result> ImportAsync(string fileName, CancellationToken cancellationToken) { var result = await ReadAsXmlFromDiskAsync <List <SceneBackup> >(fileName); if (result.HasError) { return(Result.ReportError(result.Message)); } var skippedCount = 0; var newScene = new List <Scene>(); var importedCmdCount = 0; using (var context = new ZvsContext(EntityContextConnection)) { var existingScenes = await context.Scenes.ToListAsync(cancellationToken); foreach (var backupScene in result.Data) { if (existingScenes.Any(o => o.Name == backupScene.Name)) { skippedCount++; continue; } var s = new Scene { Name = backupScene.Name }; foreach (var backupSceneCmd in backupScene.Commands) { var sc = await StoredCmdBackup.RestoreStoredCommandAsync(context, backupSceneCmd.StoredCommand, cancellationToken); if (sc == null) { continue; } s.Commands.Add(new SceneStoredCommand { Argument = sc.Argument, Argument2 = sc.Argument2, CommandId = sc.CommandId, SortOrder = backupSceneCmd.Order }); importedCmdCount++; } newScene.Add(s); } context.Scenes.AddRange(newScene); if (newScene.Count > 0) { var saveResult = await context.TrySaveChangesAsync(cancellationToken); if (saveResult.HasError) { return(Result.ReportError(saveResult.Message)); } } } return(Result.ReportSuccess( $"Imported {newScene.Count} scenes with {importedCmdCount} scene commands. Skipped {skippedCount} scene from {Path.GetFileName(fileName)}")); }
public override async Task <Result> ImportAsync(string fileName, CancellationToken cancellationToken) { var result = await ReadAsXmlFromDiskAsync <List <TriggerBackup> >(fileName); if (result.HasError) { return(Result.ReportError(result.Message)); } var skippedCount = 0; var newTriggers = new List <DeviceValueTrigger>(); using (var context = new ZvsContext(EntityContextConnection)) { var existingTriggers = await context.DeviceValueTriggers.ToListAsync(cancellationToken); var existingDeviceValues = await context.DeviceValues .Include(o => o.Device) .ToListAsync(cancellationToken); foreach (var backupTrigger in result.Data) { if (existingTriggers.Any(o => o.Name == backupTrigger.Name)) { skippedCount++; continue; } var dv = existingDeviceValues.FirstOrDefault(o => o.Device.NodeNumber == backupTrigger.NodeNumber && o.Name == backupTrigger.DeviceValueName); var cmd = await StoredCmdBackup.RestoreStoredCommandAsync(context, backupTrigger.StoredCommand, cancellationToken); if (dv == null || cmd == null) { continue; } var dvTrigger = new DeviceValueTrigger { Name = backupTrigger.Name, DeviceValue = dv, IsEnabled = backupTrigger.isEnabled }; dvTrigger.Name = backupTrigger.Name; dvTrigger.Argument = cmd.Argument; dvTrigger.Argument2 = cmd.Argument2; dvTrigger.CommandId = cmd.CommandId; if (backupTrigger.Operator != null) { dvTrigger.Operator = (TriggerOperator)backupTrigger.Operator; } dvTrigger.Value = backupTrigger.Value; newTriggers.Add(dvTrigger); } context.DeviceValueTriggers.AddRange(newTriggers); if (newTriggers.Count > 0) { var saveResult = await context.TrySaveChangesAsync(cancellationToken); if (saveResult.HasError) { return(Result.ReportError(saveResult.Message)); } foreach (var cmd in newTriggers) { cmd.SetDescription(); await cmd.SetTargetObjectNameAsync(context); cmd.SetTriggerDescription(); } var r = await context.TrySaveChangesAsync(cancellationToken); if (r.HasError) { return(Result.ReportError(r.Message)); } } } return (Result.ReportSuccess( $"Imported {newTriggers.Count} triggers, skipped {skippedCount} from {Path.GetFileName(fileName)}")); }
/// <summary> /// Return null if failed to restore command. /// </summary> /// <param name="context"></param> /// <param name="backupStoredCmd"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async static Task<IStoredCommand> RestoreStoredCommandAsync(ZvsContext context, StoredCmdBackup backupStoredCmd, CancellationToken cancellationToken) { if (backupStoredCmd == null) return null; switch (backupStoredCmd.CommandType) { case CommandTypes.Device: case CommandTypes.DeviceType: { var storedCommand = new StoredCommand(); Command c = null; if (backupStoredCmd.CommandType == CommandTypes.Device) { c = await context.DeviceCommands .FirstOrDefaultAsync(o => o.UniqueIdentifier == backupStoredCmd.UniqueIdentifier && o.Device.NodeNumber == backupStoredCmd.NodeNumber, cancellationToken); } if (backupStoredCmd.CommandType == CommandTypes.DeviceType) { c = await context.DeviceTypeCommands .FirstOrDefaultAsync(o => o.UniqueIdentifier == backupStoredCmd.UniqueIdentifier && o.DeviceType.Devices.Any(p => p.NodeNumber == backupStoredCmd.NodeNumber), cancellationToken); var dId = await context.Devices .Where(o => o.NodeNumber == backupStoredCmd.NodeNumber) .Select(o => o.Id) .FirstOrDefaultAsync(cancellationToken); storedCommand.Argument2 = dId.ToString(); } if (c == null) return null; storedCommand.Argument = backupStoredCmd.Argument; storedCommand.CommandId = c.Id; return storedCommand; } case CommandTypes.Builtin: case CommandTypes.JavaScript: { Command c = null; if (backupStoredCmd.CommandType == CommandTypes.Builtin) c = await context.BuiltinCommands.FirstOrDefaultAsync(o => o.UniqueIdentifier == backupStoredCmd.UniqueIdentifier, cancellationToken); if (backupStoredCmd.CommandType == CommandTypes.JavaScript) c = await context.JavaScriptCommands.FirstOrDefaultAsync(o => o.UniqueIdentifier == backupStoredCmd.UniqueIdentifier, cancellationToken); if (c == null) return null; var sc = new StoredCommand {Argument = backupStoredCmd.Argument, CommandId = c.Id}; return sc; } } return null; }
public async override Task <Result> ImportAsync(string fileName, CancellationToken cancellationToken) { var result = await ReadAsXmlFromDiskAsync <List <ScheduledTaskBackup> >(fileName); if (result.HasError) { return(Result.ReportError(result.Message)); } var skippedCount = 0; var newSTs = new List <DataModel.ScheduledTask>(); using (var context = new ZvsContext(EntityContextConnection)) { var existingScheduledTasks = await context.ScheduledTasks .ToListAsync(cancellationToken); foreach (var scheduledTaskBackup in result.Data) { if (existingScheduledTasks.Any(o => o.Name == scheduledTaskBackup.Name)) { skippedCount++; continue; } var cmd = await StoredCmdBackup.RestoreStoredCommandAsync(context, scheduledTaskBackup.StoredCommand, cancellationToken); if (cmd == null) { continue; } var task = new DataModel.ScheduledTask { Argument = cmd.Argument, Argument2 = cmd.Argument2, CommandId = cmd.CommandId, Name = scheduledTaskBackup.Name, IsEnabled = scheduledTaskBackup.isEnabled, RepeatIntervalInDays = scheduledTaskBackup.RecurDays ?? 0, RepeatIntervalInMonths = scheduledTaskBackup.RecurMonth ?? 0, RepeatIntervalInSeconds = scheduledTaskBackup.RecurSeconds ?? 0, RepeatIntervalInWeeks = scheduledTaskBackup.RecurWeeks ?? 0, SortOrder = scheduledTaskBackup.sortOrder, StartTime = scheduledTaskBackup.startTime ?? DateTime.Now }; if (scheduledTaskBackup.Frequency != null) { task.TaskType = (ScheduledTaskType)scheduledTaskBackup.Frequency; } if (scheduledTaskBackup.DaysOfMonthToActivate != null) { task.DaysOfMonthToActivate = (DaysOfMonth)scheduledTaskBackup.DaysOfMonthToActivate; } if (scheduledTaskBackup.DaysOfWeekToActivate != null) { task.DaysOfWeekToActivate = (DaysOfWeek)scheduledTaskBackup.DaysOfWeekToActivate; } newSTs.Add(task); } context.ScheduledTasks.AddRange(newSTs); if (newSTs.Count > 0) { var saveResult = await context.TrySaveChangesAsync(cancellationToken); if (saveResult.HasError) { return(Result.ReportError(saveResult.Message)); } foreach (var task in newSTs) { task.SetDescription(); await task.SetTargetObjectNameAsync(context); } var r = await context.TrySaveChangesAsync(cancellationToken); if (r.HasError) { return(Result.ReportError(r.Message)); } } } return(Result.ReportSuccess( $"Imported {newSTs.Count} scheduled tasks, skipped {skippedCount} from {Path.GetFileName(fileName)}")); }
/// <summary> /// Return null if failed to restore command. /// </summary> /// <param name="context"></param> /// <param name="backupStoredCmd"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async static Task <IStoredCommand> RestoreStoredCommandAsync(ZvsContext context, StoredCmdBackup backupStoredCmd, CancellationToken cancellationToken) { if (backupStoredCmd == null) { return(null); } switch (backupStoredCmd.CommandType) { case CommandTypes.Device: case CommandTypes.DeviceType: { var storedCommand = new StoredCommand(); Command c = null; if (backupStoredCmd.CommandType == CommandTypes.Device) { c = await context.DeviceCommands .FirstOrDefaultAsync(o => o.UniqueIdentifier == backupStoredCmd.UniqueIdentifier && o.Device.NodeNumber == backupStoredCmd.NodeNumber, cancellationToken); } if (backupStoredCmd.CommandType == CommandTypes.DeviceType) { c = await context.DeviceTypeCommands .FirstOrDefaultAsync(o => o.UniqueIdentifier == backupStoredCmd.UniqueIdentifier && o.DeviceType.Devices.Any(p => p.NodeNumber == backupStoredCmd.NodeNumber), cancellationToken); var dId = await context.Devices .Where(o => o.NodeNumber == backupStoredCmd.NodeNumber) .Select(o => o.Id) .FirstOrDefaultAsync(cancellationToken); storedCommand.Argument2 = dId.ToString(); } if (c == null) { return(null); } storedCommand.Argument = backupStoredCmd.Argument; storedCommand.CommandId = c.Id; return(storedCommand); } case CommandTypes.Builtin: case CommandTypes.JavaScript: { Command c = null; if (backupStoredCmd.CommandType == CommandTypes.Builtin) { c = await context.BuiltinCommands.FirstOrDefaultAsync(o => o.UniqueIdentifier == backupStoredCmd.UniqueIdentifier, cancellationToken); } if (backupStoredCmd.CommandType == CommandTypes.JavaScript) { c = await context.JavaScriptCommands.FirstOrDefaultAsync(o => o.UniqueIdentifier == backupStoredCmd.UniqueIdentifier, cancellationToken); } if (c == null) { return(null); } var sc = new StoredCommand { Argument = backupStoredCmd.Argument, CommandId = c.Id }; return(sc); } } return(null); }