aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.ContinuousPumpsActivation.UI/ViewModels/MainWindowVM.cs
blob: 0258932a0098c7eed529c7bac896c6ecfae38587 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.SharedUI;
using System.IO.Ports;
using System.Management;
using Tango.ContinuousPumpsActivation.UI.Model;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Threading;
using Tango.Core.Commands;

namespace Tango.ContinuousPumpsActivation.UI.ViewModels
{
    public class MainWindowVM: ViewModel
    {
        private DateTime _startTime;
        
        private List<string> _availablePortNames;

        public List<string> AvailablePortNames
        {
            get { return _availablePortNames; }
            set { _availablePortNames = value;
                RaisePropertyChangedAuto();
            }
        }

        private ObservableCollection<PumpModel> _pumps;

        public ObservableCollection<PumpModel> Pumps
        {
            get { return _pumps; }
            set { _pumps = value;
                RaisePropertyChangedAuto();
            }
        }

        public DispatcherTimer DispTimer { get; set; }

        private string _timer;

        public string Timer
        {
            get { return _timer; }
            set { _timer = value;
                RaisePropertyChangedAuto();
            }
        }

        #region Commands

        public RelayCommand SetCommand { get; set; }
        public RelayCommand StopCommand { get; set; }
        
        #endregion

        public MainWindowVM()
        {
            AvailablePortNames = new List<string>();
            Pumps = new ObservableCollection<PumpModel>();
            InitPorts();
            CreatePumps();
            DispTimer = new DispatcherTimer();
            DispTimer.Interval = TimeSpan.FromSeconds(1);
            DispTimer.Tick += timer_Tick;

            SetCommand = new RelayCommand(SetActivation);
            StopCommand = new RelayCommand(Stop);
        }

        private void Stop(object obj)
        {
            DispTimer.Stop();
            Timer = "00:00:00";
            foreach (var pump in Pumps)
            {
                pump.StopPump();
            }
        }

        private void SetActivation(object obj)
        {
            DispTimer.Start();
            _startTime = DateTime.Now;
            foreach (var pump in Pumps)
            {
                pump.StartPump();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Timer = (DateTime.Now - _startTime).ToString(@"hh\:mm\:ss");
        }

        public void InitPorts()
        {
            AvailablePortNames.Clear();
            using (var searcher = new ManagementObjectSearcher
                ("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
            {
                string[] portnames = SerialPort.GetPortNames();
                var tports = searcher.Get().Cast<ManagementBaseObject>().Select(p => p["Caption"].ToString()).ToList();
                foreach(var t in tports)
                {
                    if(t.Contains("High-Speed USB Serial Port"))
                    {
                        int Start, End;
                        Start = t.IndexOf("(", 0);
                        End = t.IndexOf(")", Start);
                        var curCom =  t.Substring(Start +1, End - Start -1);
                        var comName = portnames.Where(x => x.Contains(curCom)).FirstOrDefault();
                        AvailablePortNames.Add(comName);
                    }
                }
            }
            //var ports = SerialPort.GetPortNames();
            //foreach( var portName in ports)
            //{
            //    SerialPort port = new SerialPort(portName);
            //    if (portName.Contains("High-Speed"))
            //    {
            //        AvailablePortNames.Add(portName);
            //    }
            //}
        }
        public void CreatePumps()
        {
            int index = 0;
            Pumps.Add(new PumpModel($"Pump #{index}", AvailablePortNames.Count > index ? AvailablePortNames[index] : "", Colors.Cyan, AvailablePortNames));
            index++;
            Pumps.Add(new PumpModel($"Pump #{index} - Low", AvailablePortNames.Count > index ? AvailablePortNames[index] : "", Colors.Magenta, AvailablePortNames));
            index++;
            Pumps.Add(new PumpModel($"Pump #{index} - High", AvailablePortNames.Count > index ? AvailablePortNames[index] : "", Colors.Magenta, AvailablePortNames));
            index++;
            Pumps.Add(new PumpModel($"Pump #{index}", AvailablePortNames.Count > index ? AvailablePortNames[index] : "", Colors.Yellow, AvailablePortNames));
            index++;
            Pumps.Add(new PumpModel($"Pump #{index}", AvailablePortNames.Count > index ? AvailablePortNames[index] : "", Colors.Transparent, AvailablePortNames));
            //foreach (var pump in Pumps)
            //{
            //    pump.SetAndOpenPort();
            //}
        }

        public void ClosePorts()
        {
            foreach (var pump in Pumps)
            {
                pump.ClosePort();
            }
        }
    }
}