diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs | 290 |
1 files changed, 274 insertions, 16 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs index 5f2dcd00d..e0d99d0f0 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs @@ -25,17 +25,30 @@ using Tango.Core.Threading; using Tango.MachineStudio.RML.ViewModels; using Tango.Settings; using Tango.MachineStudio.RML.Models; +using Tango.BL.ActionLogs; +using Tango.MachineStudio.Common.Authentication; +using Tango.BL.DTO; namespace Tango.MachineStudio.MachineDesigner.ViewModels { public class MainViewVM : StudioViewModel<IMainView> { private INotificationProvider _notification; + private IActionLogManager _actionLogManager; + private IAuthenticationProvider _authentication; private ActionTimer _machines_action_timer; private ActionTimer _dispensers_action_timer; + private MachineDTO _machineBeforeSave; #region Properties + private bool _isNewMachine; + public bool IsNewMachine + { + get { return _isNewMachine; } + set { _isNewMachine = value; RaisePropertyChangedAuto(); } + } + private ObservablesStaticCollections _machinesAdapter; public ObservablesStaticCollections MachinesAdapter { @@ -115,6 +128,21 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels set { _selectedSpool = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } + private Site _selectedSite; + public Site SelectedSite + { + get { return _selectedSite; } + set { _selectedSite = value; RaisePropertyChangedAuto(); } + } + + private List<Site> _sites; + public List<Site> Sites + { + get { return _sites; } + set { _sites = value; RaisePropertyChangedAuto(); } + } + + private ColorCalibrationViewVM _colorCalibrationViewVM; public ColorCalibrationViewVM ColorCalibrationViewVM { @@ -123,7 +151,6 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels } private HardwareConfigurationViewVM _hardwareConfigurationViewVM; - public HardwareConfigurationViewVM HardwareConfigurationViewVM { get { return _hardwareConfigurationViewVM; } @@ -134,6 +161,19 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels } } + private MachineUpdatesViewVM _machineUpdatesViewVM; + public MachineUpdatesViewVM MachineUpdatesViewVM + { + get { return _machineUpdatesViewVM; } + set { _machineUpdatesViewVM = value; RaisePropertyChangedAuto(); } + } + + private TupViewVM _tupViewVM; + public TupViewVM TupViewVM + { + get { return _tupViewVM; } + set { _tupViewVM = value; RaisePropertyChangedAuto(); } + } #endregion @@ -188,6 +228,17 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// Gets or sets the clone machine command. /// </summary> public RelayCommand CloneMachineCommand { get; set; } + + /// <summary> + /// Gets or sets the reset device registration command. + /// </summary> + public RelayCommand ResetDeviceRegistrationCommand { get; set; } + + /// <summary> + /// Gets or sets the make prototype command. + /// </summary> + public RelayCommand MakePrototypeCommand { get; set; } + #endregion #region Constructors @@ -200,10 +251,12 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels /// <summary> /// Initializes a new instance of the <see cref="MainViewVM"/> class. /// </summary> - public MainViewVM(INotificationProvider notification) + public MainViewVM(INotificationProvider notification, IAuthenticationProvider authentication, IActionLogManager actionLogManager) { MachinesAdapter = new ObservablesStaticCollections(ObservablesContext.CreateDefault()); _notification = notification; + _authentication = authentication; + _actionLogManager = actionLogManager; _machines_action_timer = new ActionTimer(TimeSpan.FromMilliseconds(200)); _dispensers_action_timer = new ActionTimer(TimeSpan.FromMilliseconds(200)); @@ -218,7 +271,13 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels AddSpoolCommand = new RelayCommand(AddNewSpool); RemoveSpoolCommand = new RelayCommand(RemoveSpool, () => SelectedSpool != null); CloneMachineCommand = new RelayCommand(CloneMachine, () => SelectedMachine != null); + ResetDeviceRegistrationCommand = new RelayCommand(ResetDeviceRegistration); + MakePrototypeCommand = new RelayCommand(MakePrototype); + + MachineUpdatesViewVM = new MachineUpdatesViewVM(_notification); + TupViewVM = new TupViewVM(_notification); } + #endregion #region Application Ready @@ -239,7 +298,7 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels { Task.Factory.StartNew(() => { - ActiveMachineAdapter.Dispensers = ActiveMachineAdapter.Context.Dispensers.Where(x => x.SerialNumber.ToLower().StartsWith(DispensersFilter.ToLower())).ToSynchronizedObservableCollection(); + ActiveMachineAdapter.Dispensers = ActiveMachineAdapter.Context.Dispensers.Where(x => x.SerialNumber.ToLower().StartsWith(DispensersFilter.ToLower())).OrderBy(x => x.SerialNumber).ToSynchronizedObservableCollection(); }); }); } @@ -388,8 +447,10 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels InvalidateRelayCommands(); } - private async void LoadSelectedMachine(bool newMachine = false, bool clone = false, MachineVersion selectedVersion = null) + private async void LoadSelectedMachine(bool newMachine = false, bool clone = false, MachineCreationDialogVM machineCreationDialogVM = null) { + IsNewMachine = false; + using (_notification.PushTaskItem("Loading machine details...")) { try @@ -421,33 +482,140 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels ActiveMachineAdapter.MachineVersions = (await ActiveMachineAdapter.Context.MachineVersions.ToListAsync()).ToObservableCollection(); ActiveMachineAdapter.Organizations = (await ActiveMachineAdapter.Context.Organizations.ToListAsync()).ToObservableCollection(); + bool initHwConfig = true; + + Configuration machineConfigBeforeClone = null; + if (!newMachine) { ActiveMachine = (await new MachineBuilder(ActiveMachineAdapter.Context).Set(SelectedMachine.Guid).WithOrganization().WithConfiguration().WithSpools().BuildAsync()); + _machineBeforeSave = MachineDTO.FromObservable(ActiveMachine); + if (clone) { + machineConfigBeforeClone = ActiveMachine.Configuration; + + IsNewMachine = true; ActiveMachine = ActiveMachine.Clone(); - ActiveMachine.Name = ""; - ActiveMachine.SerialNumber = ""; + ActiveMachine.Name = machineCreationDialogVM.Name; + ActiveMachine.SerialNumber = machineCreationDialogVM.SerialNumber; + ActiveMachine.IsDeviceRegistered = false; + ActiveMachine.DeviceId = null; + ActiveMachine.DeviceName = null; ActiveMachineAdapter.Context.Machines.Add(ActiveMachine); } } else { - if (selectedVersion == null) + IsNewMachine = true; + + if (machineCreationDialogVM.SelectedMachineVersion == null) { ActiveMachine = new Machine(); ActiveMachine.Configuration = new Configuration(); + ActiveMachine.SerialNumber = machineCreationDialogVM.SerialNumber; + ActiveMachine.Name = machineCreationDialogVM.Name; ActiveMachineAdapter.Context.Machines.Add(ActiveMachine); } else { - ActiveMachine = selectedVersion.CreatePrototypeMachine(ActiveMachineAdapter.Context); - ActiveMachineAdapter.Context.Machines.Add(ActiveMachine); + try + { + initHwConfig = false; + ActiveMachine = machineCreationDialogVM.SelectedMachineVersion.CreatePrototypeMachine(ActiveMachineAdapter.Context); + ActiveMachine.SerialNumber = machineCreationDialogVM.SerialNumber; + ActiveMachine.Name = machineCreationDialogVM.Name; + ActiveMachineAdapter.Context.Machines.Add(ActiveMachine); + + HardwareConfigurationViewVM = new HardwareConfigurationViewVM(_notification); + var version = await new HardwareVersionBuilder(ActiveMachineAdapter.Context).Set(ActiveMachine.Configuration.HardwareVersionGuid).WithHardwareComponents().BuildAsync(); + HardwareConfigurationViewVM.Init(ActiveMachine.Configuration); + } + catch (Exception ex) + { + _notification.ShowError($"Invalid machine version prototype.\n{ex.FlattenMessage()}"); + View.NavigateTo(MachineDesignerNavigationView.MachinesView); + return; + } } } + if ((newMachine || clone) && machineCreationDialogVM.GenerateDispensers) + { + for (int i = 0; i < 8; i++) + { + var serial = machineCreationDialogVM.SerialNumber + "-" + (i + 1); + + var existingDispenser = await ActiveMachineAdapter.Context.Dispensers.Include(x => x.IdsPacks).SingleAsync(x => x.SerialNumber == serial); + + if (existingDispenser != null) + { + if (existingDispenser.IsInstalled) + { + _notification.ShowError($"Dispenser '{serial}' already exists. Cannot create machine."); + return; + } + else + { + if (!_notification.ShowErrorQuestion($"Dispenser '{serial}' already exists and is not installed. Do you wish to assign the existing dispenser to this machine?")) + { + return; + } + } + } + + Dispenser dispenser = new Dispenser(); + + if (existingDispenser == null) + { + dispenser.SerialNumber = serial; + + if (newMachine) + { + dispenser.NlPerPulse = machineCreationDialogVM.DispenserFactor; + dispenser.DispenserTypeGuid = ActiveMachineAdapter.DispenserTypes.First().Guid; + } + else + { + var packBefore = machineConfigBeforeClone.NoneEmptyIdsPacks.SingleOrDefault(x => x.PackIndex == i); + + if (packBefore != null) + { + dispenser.NlPerPulse = packBefore.Dispenser.NlPerPulse; + dispenser.DispenserTypeGuid = packBefore.Dispenser.DispenserType.Guid; + } + else + { + continue; + } + } + + dispenser.ProductionDate = DateTime.UtcNow; + + ActiveMachineAdapter.Context.Dispensers.Add(dispenser); + } + else + { + dispenser = existingDispenser; + } + + var idsPack = ActiveMachine.Configuration.NoneEmptyIdsPacks.SingleOrDefault(x => x.PackIndex == i); + + if (idsPack != null) + { + idsPack.Dispenser = dispenser; + idsPack.DispenserGuid = dispenser.Guid; + } + } + } + + var sites = await ActiveMachineAdapter.Context.Sites.ToListAsync(); + sites.Insert(0, new Site() { Name = "NONE", ID = -1 }); + + Sites = sites; + SelectedSite = Sites.SingleOrDefault(x => x.Guid == ActiveMachine.SiteGuid); + ColorCalibrationViewVM = new ColorCalibrationViewVM(_notification, ActiveMachine, _activeMachineAdapter.Context) { Rmls = ActiveMachineAdapter.Rmls, @@ -455,8 +623,17 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels SelectedRML = ActiveMachineAdapter.Rmls.FirstOrDefault(), }; - HardwareConfigurationViewVM = new HardwareConfigurationViewVM(_notification); - HardwareConfigurationViewVM.Init(ActiveMachine.Configuration); + if (initHwConfig) + { + HardwareConfigurationViewVM = new HardwareConfigurationViewVM(_notification); + HardwareConfigurationViewVM.Init(ActiveMachine.Configuration); + } + + if (!IsNewMachine) + { + await MachineUpdatesViewVM.Init(ActiveMachine, ActiveMachineAdapter.Context); + TupViewVM.Init(ActiveMachine); + } ActiveMachine.Configuration.HardwareVersionChanged += Configuration_HardwareVersionChanged; @@ -485,6 +662,11 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels { try { + if (HardwareConfigurationViewVM == null) + { + HardwareConfigurationViewVM = new HardwareConfigurationViewVM(_notification); + } + version = await new HardwareVersionBuilder(ActiveMachineAdapter.Context).Set(version.Guid).WithHardwareComponents().BuildAsync(); HardwareConfigurationViewVM.Init(ActiveMachine.Configuration); } @@ -620,15 +802,25 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels ActiveMachine.ConfigurationGuid = ActiveMachine.Configuration.Guid; ActiveMachine.LastUpdated = DateTime.UtcNow; - ActiveMachine.ProductionDate = DateTime.UtcNow; + ActiveMachine.SiteGuid = SelectedSite == null ? null : (SelectedSite.ID == -1 ? null : SelectedSite.Guid); ColorCalibrationViewVM.Save(); var hwConfig = HardwareConfigurationViewVM.GetResultingHardwareConfiguration(); ActiveMachine.Configuration.SetHardwareConfiguration(hwConfig); - await ActiveMachineAdapter.Context.SaveChangesAsync(); + if (IsNewMachine) + { + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.MachineCreated, _authentication.CurrentUser, ActiveMachine.Name, ActiveMachine, "New machine created using Machine Studio."); + } + else + { + var machineAfterDTO = MachineDTO.FromObservable(ActiveMachine); + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.MachineSaved, _authentication.CurrentUser, _machineBeforeSave.Name, _machineBeforeSave, machineAfterDTO, "Machine saved using Machine Studio."); + _machineBeforeSave = machineAfterDTO; + } + if (SelectedMachine != null) { await SelectedMachine.Reload(MachinesAdapter.Context); @@ -652,16 +844,23 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels private void AddNewMachine() { MachineCreationDialogVM vm = new MachineCreationDialogVM(); + vm.IsNewMachine = true; vm.MachineVersions = MachinesAdapter.MachineVersions.ToList(); _notification.ShowModalDialog<MachineCreationDialogVM, Views.MachineCreationDialog>(vm, (x) => { + if (MachinesAdapter.Context.Machines.Any(y => y.SerialNumber == vm.SerialNumber || y.Name.ToLower() == vm.Name.ToLower())) + { + _notification.ShowError("Machine serial number or name already exists."); + return; + } + if (vm.SelectedMachineVersion != null && String.IsNullOrWhiteSpace(vm.SelectedMachineVersion.PrototypeMachineData)) { _notification.ShowError("The selected version does not contain any prototype machine data."); return; } - LoadSelectedMachine(true, false, vm.SelectedMachineVersion); + LoadSelectedMachine(true, false, vm); }, () => { }); } @@ -675,7 +874,9 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels { IsFree = false; await SelectedMachine.DeleteCascadeAsync(MachinesAdapter.Context); + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.MachineDeleted, _authentication.CurrentUser, SelectedMachine.Name, SelectedMachine, "Machine deleted using Machine Studio."); MachinesAdapter.Context.Machines.Remove(SelectedMachine); + MachinesAdapter.Machines.Remove(SelectedMachine); } catch (Exception ex) { @@ -690,11 +891,68 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels } } + private void ResetDeviceRegistration() + { + if (_notification.ShowQuestion("Are you sure you wish to reset this machine device registration?")) + { + ActiveMachine.IsDeviceRegistered = false; + ActiveMachine.DeviceId = null; + ActiveMachine.DeviceName = null; + } + } + + private async void MakePrototype() + { + if (ActiveMachine.MachineVersion == null) + { + _notification.ShowError("Machine version must be selected in order to make a prototype."); + return; + } + + if (_notification.ShowQuestion($"Are you sure you wish to make this machine configuration as a prototype for version '{ActiveMachine.MachineVersion.Name}' ?")) + { + using (_notification.PushTaskItem($"Making prototype machine for '{ActiveMachine.MachineVersion.Name}'...")) + { + try + { + IsFree = false; + + using (var db = ObservablesContext.CreateDefault()) + { + var machineVersion = await db.MachineVersions.SingleOrDefaultAsync(x => x.Guid == ActiveMachine.MachineVersionGuid); + await machineVersion.ApplyPrototypeMachine(ActiveMachine, db); + await db.SaveChangesAsync(); + } + } + catch (Exception ex) + { + _notification.ShowError($"Error making machine version prototype\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } + } + } + } + #endregion private void CloneMachine() { - LoadSelectedMachine(false, true); + MachineCreationDialogVM vm = new MachineCreationDialogVM(); + vm.IsNewMachine = false; + vm.MachineVersions = MachinesAdapter.MachineVersions.ToList(); + _notification.ShowModalDialog<MachineCreationDialogVM, Views.MachineCreationDialog>(vm, (x) => + { + if (MachinesAdapter.Context.Machines.Any(y => y.SerialNumber == vm.SerialNumber || y.Name.ToLower() == vm.Name.ToLower())) + { + _notification.ShowError("Machine serial number or name already exists."); + return; + } + + LoadSelectedMachine(false, true, vm); + }, () => { }); } private void AddNewSpool() @@ -725,7 +983,7 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels try { IsFree = false; - MachinesAdapter.Machines = MachinesAdapter.Context.Machines.Where(x => x.SerialNumber.StartsWith(Filter)).Include(x => x.Organization).Include(x => x.MachineVersion).ToSynchronizedObservableCollection(); + MachinesAdapter.Machines = MachinesAdapter.Context.Machines.Where(x => x.SerialNumber.StartsWith(Filter)).Include(x => x.Organization).Include(x => x.MachineVersion).OrderBy(x => x.SerialNumber).ToSynchronizedObservableCollection(); } catch { |
