Exemplo n.º 1
0
        public MainViewModel()
        {
            SerialPortService = new SerialPortService();

            SerialPortService.DataReceived += (s) =>
            {
                var com = s.Trim().Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
                InGoingComm = com + Environment.NewLine + InGoingComm;

                //Check Commands
                analyseReturnCommand(com);
            };
            SerialPortService.DataSend += (s) =>
            {
                OutGoingComm = s + Environment.NewLine + OutGoingComm;
            };

            #region SerialPortConfiguration
            this.PortNames = new CollectionView(SerialPortService.AvailableCOMPorts);

            this.PortName = SerialPortService.PortName;

            Messenger.Default.Register <PropertyChangedMessage <string> >(this, (a) =>
            {
                if (a.PropertyName == "PortName")
                {
                    SerialPortService.PortName = a.NewValue;
                }
            });
            #endregion

            #region Commands
            Start = new RelayCommand(() =>
            {
                SerialPortService.Start();
                OutGoingComm = String.Empty;
                InGoingComm  = String.Empty;
            }, () =>
            {
                return(!SerialPortService.ComIsOpen);
            });
            Stop = new RelayCommand(() =>
            {
                SerialPortService.Stop();
            }, () =>
            {
                return(SerialPortService.ComIsOpen);
            });
            RegnerToggle = new RelayCommand(() =>
            {
                if (!RegnerOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "REGNER", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "REGNER", Action = "OFF"
                    });
                }
            });
            SprueherToggle = new RelayCommand(() =>
            {
                if (!SprueherOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "SPRUEHER", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "SPRUEHER", Action = "OFF"
                    });
                }
            });
            TropferToggle = new RelayCommand(() =>
            {
                if (!TropferOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "TROPFER", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "TROPFER", Action = "OFF"
                    });
                }
            });
            ManualToggle = new RelayCommand(() =>
            {
                if (!ManualOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "MANUAL", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "MANUAL", Action = "OFF"
                    });
                }
            });
            OnRelaisToggle = new RelayCommand(() =>
            {
                if (!OnRelaisOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "ONRELAIS", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "ONRELAIS", Action = "OFF"
                    });
                }
            });
            OneRelaisToggle = new RelayCommand(() =>
            {
                if (!OneRelaisOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "ONERELAIS", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "ONERELAIS", Action = "OFF"
                    });
                }
            });
            TwoRelaisToggle = new RelayCommand(() =>
            {
                if (!TwoRelaisOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "TWORELAIS", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "TWORELAIS", Action = "OFF"
                    });
                }
            });
            ThreeRelaisToggle = new RelayCommand(() =>
            {
                if (!ThreeRelaisOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "THREERELAIS", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "THREERELAIS", Action = "OFF"
                    });
                }
            });
            VentilRelaisToggle = new RelayCommand(() =>
            {
                if (!VentilRelaisOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "VENTILRELAIS", Action = "WATER"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "VENTILRELAIS", Action = "ZISTERNE"
                    });
                }
            });
            ManualRelaisToggle = new RelayCommand(() =>
            {
                if (!ManualRelaisOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "MANUALRELAIS", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "MANUALRELAIS", Action = "OFF"
                    });
                }
            });
            PumpeRelaisToggle = new RelayCommand(() =>
            {
                if (!PumpeRelaisOn)
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "PUMPERELAIS", Action = "ON"
                    });
                }
                else
                {
                    SerialPortService.SendData(new CommunicationModel()
                    {
                        Command = "PUMPERELAIS", Action = "OFF"
                    });
                }
            });
            #endregion
        }