aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/ConfigurationViewVM.cs
blob: 61eef2bdc3e2bce9e31992da40d2e83910855b94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.FSE.Common;
using Tango.FSE.Common.AutoComplete;
using Tango.FSE.Common.SQL;
using Tango.FSE.MachineConfiguration.Messages;
using static Tango.FSE.BL.Services.MachineConfigurationService;

namespace Tango.FSE.MachineConfiguration.ViewModels
{
    public class ConfigurationViewVM : FSEViewModel
    {
        [TangoInject]
        private IRemoteSqlProvider RemoteSqlProvider { get; set; }

        private MachineEditingComposition _editingComposition;
        public MachineEditingComposition EditingComposition
        {
            get { return _editingComposition; }
            set { _editingComposition = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand SaveCommand { get; set; }
        public RelayCommand ResetCountersCommand { get; set; }
        public RelayCommand ResetDeviceRegistrationCommand { get; set; }


        public ConfigurationViewVM()
        {
            RegisterForMessage<EditingCompositionLoadedMessage>(OnEditingCompositionLoaded);
            SaveCommand = new RelayCommand(SaveConfiguration);
            ResetCountersCommand = new RelayCommand(ResetCounters);
            ResetDeviceRegistrationCommand = new RelayCommand(ResetDeviceRegistration);
        }

        private void OnEditingCompositionLoaded(EditingCompositionLoadedMessage msg)
        {
            using (NotificationProvider.PushTaskItem("Loading machine configuration..."))
            {
                EditingComposition = msg.EditingComposition;
                EditingComposition.Machine.ForceVersionUpdateChanged += (x, value) => { if (value) EditingComposition.Machine.SuspendVersionUpdate = false; };
                EditingComposition.Machine.SuspendVersionUpdateChanged += (x, value) => { if (value) EditingComposition.Machine.ForceVersionUpdate = false; };
            }
        }

        private async void SaveConfiguration()
        {
            using (NotificationProvider.PushTaskItem("Saving machine configuration..."))
            {
                EditingComposition = await Services.MachineConfigurationService.SaveMachineEditingComposition(EditingComposition);
                await NotificationProvider.ShowSuccess("Machine configuration saved successfully.");
            }
        }

        private async void ResetCounters()
        {
            if (!MachineProvider.IsPPCAvailable)
            {
                await NotificationProvider.ShowError("Resetting the machine counters requires an active connection to this machine.");
                return;
            }

            if (!await NotificationProvider.ShowWarningQuestion("Resetting the machine counters will delete the entire job runs history. Are you sure?", "RESET COUNTERS", "CANCEL")) return;

            using (var task = NotificationProvider.PushTaskItem("Resetting machine counters..."))
            {
                task.UpdateProgress("Resetting local machine counters...");

                var result = await RemoteSqlProvider.ExecuteSqlCommandAsync(new RemoteSqlCommand()
                {
                    Mode = RemoteSqlCommandMode.Local,
                    Timeout = 30,
                    SQL = "DELETE FROM JOB_RUNS"
                });

                int localJobRuns = result.LocalAffectedRecords;

                task.UpdateProgress("Resetting global machine counters...");

                int remoteJobRuns = await Services.MachineConfigurationService.ResetCounters(EditingComposition.Machine.Guid);

                await NotificationProvider.ShowSuccess("Machine counters deleted successfully.");
            }
        }

        private async void ResetDeviceRegistration()
        {
            if (!await NotificationProvider.ShowWarningQuestion("Resetting the machine's device registration will allow other panel PCs to perform a machine setup for this machine. Are you sure?", "RESET", "CANCEL")) return;

            EditingComposition.Machine.IsDeviceRegistered = false;
            EditingComposition.Machine.DeviceId = null;
            EditingComposition.Machine.DeviceName = null;
        }
    }
}