Beispiel #1
0
        private void OpenDevices()
        {
            if (Working)
            {
                return;
            }

            button1.Enabled = false;

            // ustawienie interfejsu
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                listView1.Items[i].SubItems[1].Text = "";
            }
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].Cells[2].Value           = false.ToString();
                dataGridView1.Rows[i].Cells[2].Style.BackColor = dataGridView1.Columns[2].DefaultCellStyle.BackColor;
            }
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                dataGridView2.Rows[i].Cells[2].Value = false;
            }
            for (int i = 0; i < dataGridView3.Rows.Count; i++)
            {
                dataGridView3.Rows[i].Cells[2].Value = "";
            }
            for (int i = 0; i < dataGridView4.Rows.Count; i++)
            {
                dataGridView4.Rows[i].Cells[3].Value = "";
            }

            // ustawienie słuchaczy
            foreach (DigitalInput di in _configuration.DigitalInputs)
            {
                di.RegisterListener(di.ID, _inputsEvent);
            }

            Working = true;

            // uruchomienie urządzeń
            _usedDevice.Clear();

            Dictionary <Device, List <InputVariable> > inputsDevice = new Dictionary <Device, List <InputVariable> >();

            // sprawdzenie czy są subskrypcje na enkodery
            for (int i = 0; i < _configuration.Encoders.Length; i++)
            {
                Encoder e = _configuration.Encoders[i];
                if (e.LeftInput.IsSubscribed || e.RightInput.IsSubscribed)
                {
                    e.LeftInput.Module = _skalarkiIO;
                    e.LeftInput.Reset();
                    e.RightInput.Module = _skalarkiIO;
                    e.RightInput.Reset();
                    EncoderInput ei = new EncoderInput(e.LeftInput, e.RightInput);
                    _encoders.Add(ei);
                    // dodanie enkodera jako zmiennej do śledzenia
                    if (ei.UseAsInputVariable)
                    {
                        ei.Module = _skalarkiIO;
                        ei.Reset();
                        Device d = ei.Device.Extension ? ei.Device.Parent : ei.Device;
                        if (!inputsDevice.ContainsKey(d))
                        {
                            inputsDevice.Add(d, new List <InputVariable>());
                        }
                        inputsDevice[d].Add(ei);
                    }
                }
            }

            List <InputVariable> _inputs = new List <InputVariable>();

            _inputs.AddRange(_configuration.DigitalInputs);

            for (int i = 0; i < _inputs.Count; i++)
            {
                if (_encoders.Find(delegate(EncoderInput o)
                {
                    return(o.LeftInput == _inputs[i] || o.RightInput == _inputs[i]);
                }) != null)
                {
                    continue;
                }
                if (!_inputs[i].IsSubscribed)
                {
                    continue;
                }
                _inputs[i].Module = _skalarkiIO;
                _inputs[i].Reset();
                Device d = _inputs[i].Device.Extension ? _inputs[i].Device.Parent : _inputs[i].Device;
                if (!inputsDevice.ContainsKey(d))
                {
                    inputsDevice.Add(d, new List <InputVariable>());
                }
                inputsDevice[d].Add(_inputs[i]);
            }
            if (inputsDevice.Count > 0)
            {
                _readingThreads = new Thread[inputsDevice.Count];
                int i = 0;
                foreach (KeyValuePair <Device, List <InputVariable> > kvp in inputsDevice)
                {
                    kvp.Key.PrepareForReading();
                    _readingThreads[i]      = new Thread(new ParameterizedThreadStart(kvp.Key.ReadingMethod));
                    _readingThreads[i].Name = "Wątek [" + i.ToString() + "] - Czytanie z urządzenia: " + kvp.Key.Id;
                    _readingThreads[i].Start(new object[] { this, kvp.Value.ToArray() });
                    i++;
                }
            }

            List <OutputVariable> _outputs = new List <OutputVariable>();

            _outputs.AddRange(_configuration.DigitalOutputs);
            _outputs.AddRange(_configuration.LED7DisplayOutputs);
            foreach (Device d in _configuration.Devices)
            {
                _outputs.AddRange(d.DeviceOutputVariables);
            }

            foreach (OutputVariable kvp in _outputs)
            {
                kvp.Reset();
                if (kvp is IDevices)
                {
                    Device [] ds = ((IDevices)kvp).MainDevices;
                    for (int i = 0; i < ds.Length; i++)
                    {
                        if (!_usedDevice.Contains(ds[i]))
                        {
                            _usedDevice.Add(ds[i]);
                        }
                    }
                }
                else
                {
                    if (!_usedDevice.Contains(kvp.Device.MainDevice))
                    {
                        _usedDevice.Add(kvp.Device.MainDevice);
                    }
                }
            }

            foreach (Device d in _usedDevice)
            {
                if (inputsDevice.ContainsKey(d))
                {
                    d.Open(this, true);
                }
                else
                {
                    d.Open(this, false);
                }
            }

            button2.Enabled = true;
        }
        public void Start(HomeSimCockpitSDK.StartStopType startType)
        {
            if (Working)
            {
                return;
            }

            Stop(startType);
            _working = true;
            _usedDevice.Clear();

            Dictionary <Device, List <InputVariable> > inputsDevice = new Dictionary <Device, List <InputVariable> >();

            // sprawdzenie czy są subskrypcje na enkodery
            for (int i = 0; i < _configuration.Encoders.Length; i++)
            {
                Encoder e = _configuration.Encoders[i];
                if (e.LeftInput.IsSubscribed || e.RightInput.IsSubscribed)
                {
                    e.LeftInput.Module = this;
                    e.LeftInput.Reset();
                    e.RightInput.Module = this;
                    e.RightInput.Reset();
                    EncoderInput ei = new EncoderInput(e.LeftInput, e.RightInput);
                    _encoders.Add(ei);
                    // dodanie enkodera jako zmiennej do śledzenia
                    if (ei.UseAsInputVariable)
                    {
                        ei.Module = this;
                        ei.Reset();
                        Device d = ei.Device.Extension ? ei.Device.Parent : ei.Device;
                        if (!inputsDevice.ContainsKey(d))
                        {
                            inputsDevice.Add(d, new List <InputVariable>());
                        }
                        inputsDevice[d].Add(ei);
                    }
                }
            }

            for (int i = 0; i < _inputs.Length; i++)
            {
                if (_inputs[i].IsSubscribed)
                {
                    if (_encoders.Find(delegate(EncoderInput o)
                    {
                        return(o.LeftInput == _inputs[i] || o.RightInput == _inputs[i]);
                    }) != null)
                    {
                        continue;
                    }

                    _inputs[i].Module = this;
                    _inputs[i].Reset();
                    Device d = _inputs[i].Device.Extension ? _inputs[i].Device.Parent : _inputs[i].Device;
                    if (!inputsDevice.ContainsKey(d))
                    {
                        inputsDevice.Add(d, new List <InputVariable>());
                    }
                    inputsDevice[d].Add(_inputs[i]);
                }
            }
            if (inputsDevice.Count > 0)
            {
                _readingThreads = new Thread[inputsDevice.Count];
                int i = 0;
                foreach (KeyValuePair <Device, List <InputVariable> > kvp in inputsDevice)
                {
                    kvp.Key.PrepareForReading();
                    _readingThreads[i]      = new Thread(new ParameterizedThreadStart(kvp.Key.ReadingMethod));
                    _readingThreads[i].Name = "Wątek [" + i.ToString() + "] - Czytanie z urządzenia: " + kvp.Key.Id;
                    _readingThreads[i].Start(new object[] { this, kvp.Value.ToArray() });
                    i++;
                }
            }

            foreach (KeyValuePair <string, OutputVariable> kvp in _outputVariables)
            {
                kvp.Value.Reset();
                if (kvp.Value is IDevices)
                {
                    Device [] ds = ((IDevices)kvp.Value).MainDevices;
                    for (int i = 0; i < ds.Length; i++)
                    {
                        if (!_usedDevice.Contains(ds[i]))
                        {
                            _usedDevice.Add(ds[i]);
                        }
                    }
                }
                else
                {
                    if (!_usedDevice.Contains(kvp.Value.Device.MainDevice))
                    {
                        _usedDevice.Add(kvp.Value.Device.MainDevice);
                    }
                }
            }

            foreach (Device d in _usedDevice)
            {
                if (inputsDevice.ContainsKey(d))
                {
                    d.Open(this, true);
                }
                else
                {
                    d.Open(this, false);
                }
            }
        }