private async void Window_Loaded_1(object sender, RoutedEventArgs e) { Trigger = await _context.DeviceValueTriggers .Include(o => o.DeviceValue) .Include(o => o.DeviceValue.Device) .FirstOrDefaultAsync(o => o.Id == _deviceValueTriggerId) ?? new DeviceValueTrigger { Name = "New Trigger" }; //EAGER LOAD await _context.Devices .Include(o => o.Values) .ToListAsync(); await _context.Scenes.ToListAsync(); var deviceViewSource = ((System.Windows.Data.CollectionViewSource)(FindResource("deviceViewSource"))); // Load data by setting the CollectionViewSource.Source property: deviceViewSource.Source = _context.Devices.Local; OperatorCmboBx.ItemsSource = Enum.GetValues(typeof(TriggerOperator)); OperatorCmboBx.SelectedIndex = 0; //Set presets if (Trigger != null && Trigger.DeviceValue != null) { if (Trigger.DeviceValue.Device != null) DeviceCmboBx.SelectedItem = Trigger.DeviceValue.Device; if (Trigger.DeviceValue != null) ValueCmboBx.SelectedItem = Trigger.DeviceValue; } try { OperatorCmboBx.Text = Enum.GetName(typeof(TriggerOperator), Trigger.Operator); } catch (Exception ex) { Console.WriteLine(ex.Message); } if (Trigger.Value != null) ValueTxtBx.Text = Trigger.Value; }
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)}"); }