コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            // sprawdzenie poprawności danych
            byte   deviceId    = (byte)numericUpDown1.Value;
            byte   ledsCount   = (byte)numericUpDown2.Value;
            string id          = _ledDeviceIndex < 0 ? Guid.NewGuid().ToString() : _configuration.LEDDevices[_ledDeviceIndex].Id;
            string description = textBox2.Text.Trim();

            textBox2.Text = description;

            if (description.Length == 0)
            {
                MessageBox.Show(this, string.Format("Nie podano opisu."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox2.Focus();
                return;
            }

            // sprawdzenie czy na tym interfejsie jest już urządzenie o takim ID
            if (_ledDeviceIndex == -1 || _configuration.LEDDevices[_ledDeviceIndex].DeviceId != deviceId)
            {
                if (_configuration.ExistsDevice(_interf, deviceId))
                {
                    MessageBox.Show(this, string.Format("Podany identyfikator urządzenia jest już używany na tym interfejsie."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    numericUpDown1.Focus();
                    return;
                }
            }

            // sprawdzenie poprawności identyfikatorów diod
            List <string> ids = new List <string>();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string id2 = (string)dataGridView1.Rows[i].Cells[1].Value;
                id2 = id2.Trim();
                if (id2.Length == 0)
                {
                    MessageBox.Show(this, string.Format("Nie podano identyfikatora dla diody '{0}'.", (i + 1)), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (ids.Contains(id2))
                {
                    MessageBox.Show(this, string.Format("Identyfikator '{0}' został użyty więcj niż jeden raz.", id2), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ids.Add(id2);
            }

            // sprawdzenie czy identyfikatory diod
            for (int i = 0; i < _configuration.LEDs.Length; i++)
            {
                LED led = (LED)_configuration.LEDs[i];
                if (led.LEDDevice.Id == id)
                {
                    continue;
                }

                if (ids.FindIndex(delegate(string o)
                {
                    return(o == led.ID);
                }) > -1)
                {
                    MessageBox.Show(this, string.Format("Identyfikator '{0}' jest już wykorzystywany.", led.ID), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            if (_ledDeviceIndex > -1)
            {
                LEDDevice dev = _configuration.LEDDevices[_ledDeviceIndex];
                dev.Description = description;
                dev.DeviceId    = deviceId;
                dev.LEDsCount   = ledsCount;

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    byte   dindex       = (byte)i;// byte.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()) - 1;
                    string did          = (string)dataGridView1.Rows[i].Cells[1].Value;
                    string ddescription = (string)dataGridView1.Rows[i].Cells[2].Value;

                    // aktualizacja ustawień wyświetlacza lub dodanie nowego
                    LED lcd3 = Array.Find <LED>(_configuration.LEDs, delegate(LED o)
                    {
                        return(o.LEDDevice == dev && o.Index == dindex);
                    });

                    if (lcd3 != null)
                    {
                        lcd3.ID          = did;
                        lcd3.Description = ddescription;
                    }
                    else
                    {
                        LED lcd4 = new LED()
                        {
                            Description = ddescription,
                            ID          = did,
                            Index       = dindex,
                            LEDDevice   = dev
                        };
                        List <LED> lcds2 = new List <LED>(_configuration.LEDs);
                        lcds2.Add(lcd4);
                        _configuration.LEDs = lcds2.ToArray();
                    }
                }

                // usunięcie znaków z obszarów
                List <LEDGroup> areas = new List <LEDGroup>();
                for (int j = 0; j < _configuration.LEDGroups.Length; j++)
                {
                    List <LED> ccc = new List <LED>(_configuration.LEDGroups[j].LEDs);
                    ccc.RemoveAll(delegate(LED o)
                    {
                        return(o.LEDDevice == dev && o.Index >= dataGridView1.Rows.Count);
                    });
                    if (ccc.Count > 0)
                    {
                        _configuration.LEDGroups[j].LEDs = ccc.ToArray();
                        areas.Add(_configuration.LEDGroups[j]);
                    }
                }
                _configuration.LEDGroups = areas.ToArray();

                // usunięcie diod
                List <LED> diodyOld = new List <LED>(_configuration.LEDs);
                diodyOld.RemoveAll(delegate(LED o)
                {
                    return(o.LEDDevice == dev && o.Index >= dataGridView1.Rows.Count);
                });
                _configuration.LEDs = diodyOld.ToArray();
            }
            else
            {
                // dodanie nowego urządzenia i wyświetlaczy
                LEDDevice dev = new LEDDevice()
                {
                    Description = description,
                    DeviceId    = deviceId,
                    Id          = id,
                    Interface   = _interf,
                    LEDsCount   = ledsCount
                };
                List <LEDDevice> devsAll = new List <LEDDevice>(_configuration.LEDDevices);
                devsAll.Add(dev);
                _configuration.LEDDevices = devsAll.ToArray();
                AddedLEDDevice            = dev;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    byte   dindex       = (byte)i;// byte.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                    string did          = (string)dataGridView1.Rows[i].Cells[1].Value;
                    string ddescription = (string)dataGridView1.Rows[i].Cells[2].Value;

                    LED lcd4 = new LED()
                    {
                        Description = ddescription,
                        ID          = did,
                        Index       = dindex,
                        LEDDevice   = dev
                    };
                    List <LED> lcds2 = new List <LED>(_configuration.LEDs);
                    lcds2.Add(lcd4);
                    _configuration.LEDs = lcds2.ToArray();
                }
            }

            DialogResult = DialogResult.OK;
            Close();
        }
コード例 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            // sprawdzenie poprawności danych
            byte   deviceId    = (byte)numericUpDown1.Value;
            byte   keysCount   = (byte)int.Parse((string)comboBox1.SelectedItem);
            string id          = _keysDeviceIndex < 0 ? Guid.NewGuid().ToString() : _configuration.KeysDevices[_keysDeviceIndex].Id;
            string description = textBox2.Text.Trim();

            textBox2.Text = description;
            byte delay           = (byte)numericUpDown2.Value;
            bool hardwareIndexes = checkBox1.Checked;

            if (description.Length == 0)
            {
                MessageBox.Show(this, string.Format("Nie podano opisu."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox2.Focus();
                return;
            }

            // sprawdzenie czy na tym interfejsie jest już urządzenie o takim ID
            if (_keysDeviceIndex == -1 || _configuration.KeysDevices[_keysDeviceIndex].DeviceId != deviceId)
            {
                if (_configuration.ExistsDevice(_interf, deviceId))
                {
                    MessageBox.Show(this, string.Format("Podany identyfikator urządzenia jest już używany na tym interfejsie."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    numericUpDown1.Focus();
                    return;
                }
            }

            // sprawdzenie poprawności identyfikatorów keys
            List <string> ids = new List <string>();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string id2 = (string)dataGridView1.Rows[i].Cells[1].Value;
                id2 = id2.Trim();
                if (id2.Length == 0)
                {
                    MessageBox.Show(this, string.Format("Nie podano identyfikatora dla wejścia '{0}'.", (i + 1)), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (ids.Contains(id2))
                {
                    MessageBox.Show(this, string.Format("Identyfikator '{0}' został użyty więcj niż jeden raz.", id2), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ids.Add(id2);
            }

            // sprawdzenie czy identyfikatory keys
            for (int i = 0; i < _configuration.Keys.Length; i++)
            {
                Key key = (Key)_configuration.Keys[i];
                if (key.KeysDevice.Id == id)
                {
                    continue;
                }

                if (ids.FindIndex(delegate(string o)
                {
                    return(o == key.ID);
                }) > -1)
                {
                    MessageBox.Show(this, string.Format("Identyfikator '{0}' jest już wykorzystywany.", key.ID), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            if (_keysDeviceIndex > -1)
            {
                KeysDevice dev = _configuration.KeysDevices[_keysDeviceIndex];
                dev.Description     = description;
                dev.DeviceId        = deviceId;
                dev.KeysCount       = keysCount;
                dev.Delay           = delay;
                dev.HardwareIndexes = hardwareIndexes;

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    byte   dindex       = (byte)i;// byte.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()) - 1;
                    string did          = (string)dataGridView1.Rows[i].Cells[1].Value;
                    string ddescription = (string)dataGridView1.Rows[i].Cells[2].Value;

                    // aktualizacja ustawień wyświetlacza lub dodanie nowego
                    Key lcd3 = Array.Find <Key>(_configuration.Keys, delegate(Key o)
                    {
                        return(o.KeysDevice == dev && o.Index == dindex);
                    });

                    if (lcd3 != null)
                    {
                        lcd3.ID          = did;
                        lcd3.Description = ddescription;
                    }
                    else
                    {
                        Key lcd4 = new Key()
                        {
                            Description = ddescription,
                            ID          = did,
                            Index       = dindex,
                            KeysDevice  = dev
                        };
                        List <Key> lcds2 = new List <Key>(_configuration.Keys);
                        lcds2.Add(lcd4);
                        _configuration.Keys = lcds2.ToArray();
                    }
                }

                // usunięcie encoderów
                List <Encoder> encoders = new List <Encoder>(_configuration.Encoders);
                encoders.RemoveAll(delegate(Encoder o)
                {
                    int index = o.Index * 2;
                    return(index >= keysCount);
                });
                _configuration.Encoders = encoders.ToArray();

                // usunięcie diod
                List <Key> diodyOld = new List <Key>(_configuration.Keys);
                diodyOld.RemoveAll(delegate(Key o)
                {
                    return(o.KeysDevice == dev && o.Index >= dataGridView1.Rows.Count);
                });
                _configuration.Keys = diodyOld.ToArray();
            }
            else
            {
                // dodanie nowego urządzenia i wyświetlaczy
                KeysDevice dev = new KeysDevice()
                {
                    Description     = description,
                    DeviceId        = deviceId,
                    Id              = id,
                    Interface       = _interf,
                    KeysCount       = keysCount,
                    Delay           = delay,
                    HardwareIndexes = hardwareIndexes
                };
                List <KeysDevice> devsAll = new List <KeysDevice>(_configuration.KeysDevices);
                devsAll.Add(dev);
                _configuration.KeysDevices = devsAll.ToArray();
                AddedKeysDevice            = dev;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    byte   dindex       = (byte)i;// byte.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                    string did          = (string)dataGridView1.Rows[i].Cells[1].Value;
                    string ddescription = (string)dataGridView1.Rows[i].Cells[2].Value;

                    Key lcd4 = new Key()
                    {
                        Description = ddescription,
                        ID          = did,
                        Index       = dindex,
                        KeysDevice  = dev
                    };
                    List <Key> lcds2 = new List <Key>(_configuration.Keys);
                    lcds2.Add(lcd4);
                    _configuration.Keys = lcds2.ToArray();
                }
            }

            DialogResult = DialogResult.OK;
            Close();
        }
コード例 #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            // sprawdzenie poprawności danych
            byte   deviceId    = (byte)numericUpDown1.Value;
            string id          = _lcdDeviceIndex < 0 ? Guid.NewGuid().ToString() : _configuration.LCDDevices[_lcdDeviceIndex].Id;
            string description = textBox2.Text.Trim();

            textBox2.Text = description;

            if (description.Length == 0)
            {
                MessageBox.Show(this, string.Format("Nie podano opisu."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox2.Focus();
                return;
            }

            // sprawdzenie czy na tym interfejsie jest już urządzenie o takim ID
            if (_lcdDeviceIndex == -1 || _configuration.LCDDevices[_lcdDeviceIndex].DeviceId != deviceId)
            {
                if (_configuration.ExistsDevice(_interf, deviceId))
                {
                    MessageBox.Show(this, string.Format("Podany identyfikator urządzenia jest już używany na tym interfejsie."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    numericUpDown1.Focus();
                    return;
                }
            }

            // sprawdzenie poprawności identyfikatorów wyświetlaczy
            List <string> ids = new List <string>();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string id2 = (string)dataGridView1.Rows[i].Cells[1].Value;
                id2 = id2.Trim();
                if (id2.Length == 0)
                {
                    MessageBox.Show(this, string.Format("Nie podano identyfikatora dla wyświetlacza '{0}'.", (i + 1)), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (ids.Contains(id2))
                {
                    MessageBox.Show(this, string.Format("Identyfikator '{0}' został użyty więcj niż jeden raz.", id2), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ids.Add(id2);
            }

            // sprawdzenie czy identyfikatory wyświetlaczy są unikalne
            for (int i = 0; i < _configuration.LCDs.Length; i++)
            {
                RS232LCD lcd = (RS232LCD)_configuration.LCDs[i];
                if (lcd.LCDDevice.Id == id)
                {
                    continue;
                }

                if (ids.FindIndex(delegate(string o)
                {
                    return(o == lcd.ID);
                }) > -1)
                {
                    MessageBox.Show(this, string.Format("Identyfikator '{0}' jest już wykorzystywany.", lcd.ID), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            if (_lcdDeviceIndex > -1)
            {
                LCDDevice dev = _configuration.LCDDevices[_lcdDeviceIndex];
                dev.Description = description;
                dev.DeviceId    = deviceId;

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    byte   dindex       = byte.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                    string did          = (string)dataGridView1.Rows[i].Cells[1].Value;
                    string ddescription = (string)dataGridView1.Rows[i].Cells[2].Value;
                    byte   drow         = byte.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
                    byte   dcolumn      = byte.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());

                    // aktualizacja ustawień wyświetlacza lub dodanie nowego
                    RS232LCD lcd3 = Array.Find <RS232LCD>(_configuration.LCDs, delegate(RS232LCD o)
                    {
                        return(o.LCDDevice == dev && o.Index == dindex);
                    });

                    if (lcd3 != null)
                    {
                        lcd3.ID          = did;
                        lcd3.Description = ddescription;
                        lcd3.Rows        = drow;
                        lcd3.Columns     = dcolumn;
                    }
                    else
                    {
                        RS232LCD lcd4 = new RS232LCD()
                        {
                            Columns     = dcolumn,
                            Description = ddescription,
                            ID          = did,
                            Index       = dindex,
                            LCDDevice   = dev,
                            Rows        = drow
                        };
                        List <RS232LCD> lcds2 = new List <RS232LCD>(_configuration.LCDs);
                        lcds2.Add(lcd4);
                        _configuration.LCDs = lcds2.ToArray();
                    }
                }

                // usunięcie wyświetlaczy
                List <RS232LCD> diodyOld = new List <RS232LCD>(_configuration.LCDs);
                diodyOld.RemoveAll(delegate(RS232LCD o)
                {
                    return(o.LCDDevice == dev && o.Index > dataGridView1.Rows.Count);
                });
                _configuration.LCDs = diodyOld.ToArray();

                // usunięcie znaków z obszarów
                List <LCDArea> areas = new List <LCDArea>();
                for (int j = 0; j < _configuration.Areas.Length; j++)
                {
                    List <LCDCharacter> ccc = new List <LCDCharacter>(_configuration.Areas[j].Characters);
                    int rem = ccc.RemoveAll(delegate(LCDCharacter o)
                    {
                        RS232LCD lcd = Array.Find <RS232LCD>(_configuration.LCDs, delegate(RS232LCD oo)
                        {
                            return(oo == o.LCD);
                        });
                        if (lcd != null)
                        {
                            return(o.Row >= lcd.Rows || o.Column >= lcd.Columns);
                        }
                        return(true);
                    });
                    if (rem > 0)
                    {
                        LCDReduction = true;
                    }
                    if (ccc.Count > 0)
                    {
                        _configuration.Areas[j].Characters = ccc.ToArray();
                        areas.Add(_configuration.Areas[j]);
                    }
                }
                _configuration.Areas = areas.ToArray();
            }
            else
            {
                // dodanie nowego urządzenia i wyświetlaczy
                LCDDevice dev = new LCDDevice()
                {
                    Description = description,
                    DeviceId    = deviceId,
                    Id          = id,
                    Interface   = _interf
                };
                List <LCDDevice> devsAll = new List <LCDDevice>(_configuration.LCDDevices);
                devsAll.Add(dev);
                _configuration.LCDDevices = devsAll.ToArray();
                AddedLCDDevice            = dev;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    byte   dindex       = byte.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                    string did          = (string)dataGridView1.Rows[i].Cells[1].Value;
                    string ddescription = (string)dataGridView1.Rows[i].Cells[2].Value;
                    byte   drow         = (byte)(int)dataGridView1.Rows[i].Cells[3].Value;
                    byte   dcolumn      = (byte)(int)dataGridView1.Rows[i].Cells[4].Value;

                    RS232LCD lcd4 = new RS232LCD()
                    {
                        Columns     = dcolumn,
                        Description = ddescription,
                        ID          = did,
                        Index       = dindex,
                        LCDDevice   = dev,
                        Rows        = drow
                    };
                    List <RS232LCD> lcds2 = new List <RS232LCD>(_configuration.LCDs);
                    lcds2.Add(lcd4);
                    _configuration.LCDs = lcds2.ToArray();
                }
            }

            DialogResult = DialogResult.OK;
            Close();
        }