aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ConfigurationViewModel.cs
blob: bfcc60bcff00e65bd161a6c97b3f282c7933c24b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.FSE.Common.Navigation;
using Tango.FSE.MachineConfiguration.Navigation;

namespace Tango.FSE.MachineConfiguration
{
    public class ConfigurationViewModel : ModularNavigationFSEViewModel<ConfigurationView>
    {
    }
}
/ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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();
            }
        }
    }
}