private void handleDataFromEvent(PlcEventModel plcEventModel)
        {
            if (plcEventModel == null)
            {
                return;
            }
            var currentIO = PLCIOList.FirstOrDefault(io => io.Address.Equals(plcEventModel.Address));

            if (currentIO != null)
            {
                currentIO.Value = plcEventModel.Data;
            }
        }
        private void BtnDeleteButtonClicked(object sender, RoutedEventArgs e)
        {
            var toRemoveIOs = new List <PlcIO>();

            foreach (var selectedItem in datagridIOs.SelectedItems)
            {
                toRemoveIOs.Add(selectedItem as PlcIO);
            }
            foreach (var selectedItem in toRemoveIOs)
            {
                PLCIOList.Remove(selectedItem as PlcIO);
            }
            ShowNotification("Deleted selected IO.", "INFO", 3000);
        }
 private void StartPLCWatcher()
 {
     ShowNotification("Starting all jobs.", "INFO", 20000);
     _isWatcherStarted                  = true;
     btnStartPLCWatcher.Content         = "Stop";
     this.btnDeleteSelectedIO.IsEnabled = false;
     this.btnShowAddIODialog.IsEnabled  = false;
     this.btnSaveIOConfig.IsEnabled     = false;
     foreach (var io in PLCIOList)
     {
         io.Value = "Connecting...";
     }
     for (int i = 0; i < PLCIOList.Count; i++)
     {
         var io  = PLCIOList.ElementAt(i);
         var plc = _runningPLCList.Where(machine => machine.Address == io.Controller).FirstOrDefault();
         if (plc == null)
         {
             plc = new PLCMachine(io.Controller, io.CPUType.Type);
             plc.ListIO.Add(io);
             _runningPLCList.Add(plc);
         }
         else
         {
             plc.ListIO.Add(io);
         }
     }
     foreach (var plc in _runningPLCList)
     {
         new Thread(async() =>
         {
             var isOK = await plc.Start();
             if (!isOK)
             {
                 foreach (var io in plc.ListIO)
                 {
                     io.Value = "Timeout";
                     ShowNotification("Failed to start plc: " + plc.Address, "ERROR", 2000);
                 }
             }
             else
             {
                 ShowNotification("Success start plc: " + plc.Address, "INFO", 2000);
             }
         }).Start();
     }
 }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tb_Name.Text))
            {
                ShowNotification("Please input name of IO", "ERROR");
                return;
            }
            if (string.IsNullOrEmpty(tb_Address.Text))
            {
                ShowNotification("Please input address of IO", "ERROR");
                return;
            }
            if (comboboxValueType.SelectedItem == null)
            {
                ShowNotification("Please input address of IO", "ERROR");
                return;
            }
            if (comboboxCPUType.SelectedItem == null)
            {
                ShowNotification("Please select a controller!", "ERROR");
                return;
            }
            var selectController = comboboxCPUType.SelectedItem as PLCModel;
            var toAddIO          = new PlcIO()
            {
                Name       = tb_Name.Text,
                Value      = "N/A",
                Address    = tb_Address.Text,
                Type       = (comboboxValueType.SelectedItem as TypeModel).Type,
                Controller = selectController.Address,
                CPUType    = new CpuTypeModel()
                {
                    Type = selectController.CpuType
                },
                PlcId = selectController.Id
            };

            PLCIOList.Add(toAddIO);
            tb_Name.Text               = "";
            tb_Address.Text            = "";
            dialogViewAddIO.Visibility = Visibility.Collapsed;
            ShowNotification("Added new IO!", "INFO", 3000);
        }