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 _availablePortNames; public List AvailablePortNames { get { return _availablePortNames; } set { _availablePortNames = value; RaisePropertyChangedAuto(); } } private ObservableCollection _pumps; public ObservableCollection 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(); Pumps = new ObservableCollection(); 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().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(); } } } }