Exemplo n.º 1
0
        /// <summary>
        /// Creates device and adds it to devices List
        /// </summary>
        /// <param name="devices"></param>
        /// <param name="deviceType"></param>
        /// <param name="deviceInfo"></param>
        /// <param name="deviceNumber"></param>
        /// <param name="WriteLog"></param>
        private static void CreateDevice(ref List <Device> devices, string deviceType, Dictionary <string, string> deviceInfo, ref int deviceNumber, Action <string, int> WriteLog)
        {
            string id = "Device " + deviceNumber;
            string portStr;

            switch (deviceType)
            {
            case "Reader":

                string beepStr;
                string antPositionXStr;
                string antPositionYStr;
                string qValueStr;
                string sessionStr;
                string targetStr;
                string powerStr;
                string freqMinStr;
                string freqMaxStr;

                #region Validate Reader with all necessary parameters

                if (!deviceInfo.TryGetValue("port", out portStr))
                {
                    throw new Exception("\tDevice config -> Reader with port value not found");
                }
                else if (!deviceInfo.TryGetValue("beep", out beepStr))
                {
                    throw new Exception("\tDevice config -> Reader with beep value not found");
                }
                else if (!deviceInfo.TryGetValue("antenna_pos_x (m)", out antPositionXStr))
                {
                    throw new Exception("\tDevice config -> Reader with antenna_pos_x value not found");
                }
                else if (!deviceInfo.TryGetValue("antenna_pos_y (m)", out antPositionYStr))
                {
                    throw new Exception("\tDevice config -> Reader with antenna_pos_y value not found");
                }
                else if (!deviceInfo.TryGetValue("q_value", out qValueStr))
                {
                    throw new Exception("\tDevice config -> Reader with q_value not found");
                }
                else if (!deviceInfo.TryGetValue("session", out sessionStr))
                {
                    throw new Exception("\tDevice config -> Reader with session value not found");
                }
                else if (!deviceInfo.TryGetValue("target", out targetStr))
                {
                    throw new Exception("\tDevice config -> Reader with target value not found");
                }
                else if (!deviceInfo.TryGetValue("power (dBm)", out powerStr))
                {
                    throw new Exception("\tDevice config -> Reader with power value not found");
                }
                else if (!deviceInfo.TryGetValue("freq_min (MHz)", out freqMinStr))
                {
                    throw new Exception("\tDevice config -> Reader with freq_min value not found");
                }
                else if (!deviceInfo.TryGetValue("freq_max (MHz)", out freqMaxStr))
                {
                    throw new Exception("\tDevice config -> Reader with freq_max value not found");
                }

                decimal minFreq = Convert.ToDecimal(freqMinStr);
                decimal maxFreq = Convert.ToDecimal(freqMaxStr);

                if (!RegionFrequencyConverter.ValidFrequency(minFreq))
                {
                    throw new Exception("\tDevice config -> Reader with invalid freq_min value");
                }
                else if (!RegionFrequencyConverter.ValidFrequency(maxFreq))
                {
                    throw new Exception("\tDevice config -> Reader with invalid freq_max value");
                }
                else if (maxFreq < minFreq)
                {
                    throw new Exception("\tDevice config -> Reader cannot have freq_max < freq_min");
                }

                #endregion

                #region create UHFReader

                bool beep = beepStr == "1" ? true : false;

                decimal pos_x = Convert.ToDecimal(antPositionXStr);
                decimal pos_y = Convert.ToDecimal(antPositionYStr);

                Antenna ant = new Antenna(deviceNumber, pos_x, pos_y);

                UHFReader reader = new UHFReader(id, portStr, ant, beep, Convert.ToByte(powerStr), minFreq, maxFreq, WriteLog);

                reader.InvParams.QValue  = Convert.ToByte(qValueStr);
                reader.InvParams.Session = Convert.ToByte(sessionStr);
                reader.InvParams.Target  = Convert.ToByte(targetStr);

                devices.Add(reader);

                #endregion

                deviceNumber++;

                break;

            case "Laser":

                string directionStr;
                string offsetStr;

                #region Validate Laser with all necessary parameters

                if (!deviceInfo.TryGetValue("port", out portStr))
                {
                    throw new Exception("\tDevice config -> Laser with port value not found");
                }
                else if (!deviceInfo.TryGetValue("direction", out directionStr))
                {
                    throw new Exception("\tDevice config -> Laser with direction value not found");
                }
                else if (!deviceInfo.TryGetValue("offset (m)", out offsetStr))
                {
                    throw new Exception("\tDevice config -> Laser with offset value not found");
                }

                #endregion

                #region create LaserModule

                decimal offset = Convert.ToDecimal(offsetStr);

                if (directionStr == "X")
                {
                    devices.Add(new LaserModule(id, portStr, LaserDirection.X, offset, WriteLog));
                }
                else if (directionStr == "Y")
                {
                    devices.Add(new LaserModule(id, portStr, LaserDirection.Y, offset, WriteLog));
                }

                #endregion

                deviceNumber++;

                break;

            case "Servo":

                #region Validate Servo with all necessary parameters

                if (!deviceInfo.TryGetValue("port", out portStr))
                {
                    throw new Exception("\tDevice config -> Servo with port value not found");
                }

                #endregion

                #region create Servo

                Servo servo = new Servo(id, portStr, WriteLog);

                devices.Add(servo);

                #endregion

                deviceNumber++;

                break;
            }
        }
Exemplo n.º 2
0
        private void GenerateUIForDevices(ref Panel panelDevices, List <Device> devices)
        {
            for (int i = 0; i < devices.Count; i++)
            {
                int deviceNumber = i + 1;

                GroupBox Gb = new GroupBox
                {
                    Name     = "GroupBoxDevice" + deviceNumber,
                    Text     = devices[i].ID,
                    Location = new Point(0, 89 * i),
                    Size     = new Size(800, 83)
                };

                #region generate controls for groupbox

                Label LabelType = new Label
                {
                    Name     = "labelType",
                    Text     = "Type:",
                    Location = new Point(6, 22),
                    Size     = new Size(34, 13)
                };

                Label LabelPort = new Label
                {
                    Name     = "labelPort",
                    Text     = "Port:",
                    Location = new Point(6, 50),
                    Size     = new Size(29, 13)
                };

                Label LabelBaudRate = new Label
                {
                    Name     = "labelBaudRate",
                    Text     = "Baud rate:",
                    Location = new Point(105, 22),
                    Size     = new Size(56, 13)
                };

                string  deviceType  = devices[i] is UHFReader ? "Reader" : devices[i] is LaserModule ? "Laser" : "Servo";
                TextBox textBoxType = new TextBox
                {
                    Name     = "textBoxType",
                    Text     = deviceType,
                    Location = new Point(41, 19),
                    Size     = new Size(60, 21),
                    ReadOnly = true
                };

                ComboBox ComboBoxPort = new ComboBox
                {
                    Name          = "comboBoxPort",
                    Location      = new Point(41, 47),
                    Size          = new Size(60, 21),
                    DropDownStyle = ComboBoxStyle.DropDownList
                };
                ComboBoxPort.Items.AddRange(UICollections.DevicePorts);
                ComboBoxPort.SelectedItem          = devices[i].PortStr;
                ComboBoxPort.SelectedIndexChanged += new EventHandler(ComboBoxPort_SelectedIndexChanged);

                ComboBox ComboBoxBaudRate = new ComboBox
                {
                    Name          = "comboBoxBaudRate",
                    Location      = new Point(167, 19),
                    Size          = new Size(77, 21),
                    DropDownStyle = ComboBoxStyle.DropDownList,
                    Enabled       = false
                };
                ComboBoxBaudRate.Items.AddRange(UICollections.DeviceBaudRates);
                ComboBoxBaudRate.SelectedItem          = devices[i].BaudRateStr;
                ComboBoxBaudRate.SelectedIndexChanged += new EventHandler(ComboBoxBaudRate_SelectedIndexChanged);

                Button ButtonConnection = new Button
                {
                    Name     = "buttonConnection",
                    Text     = "Connect",
                    Location = new Point(108, 46),
                    Size     = new Size(78, 24)
                };
                ButtonConnection.Click += new EventHandler(ButtonConnection_Click);

                //------

                Gb.Controls.Add(LabelType);
                Gb.Controls.Add(LabelPort);
                Gb.Controls.Add(LabelBaudRate);

                Gb.Controls.Add(textBoxType);

                Gb.Controls.Add(ComboBoxPort);
                Gb.Controls.Add(ComboBoxBaudRate);

                Gb.Controls.Add(ButtonConnection);

                #endregion

                if (devices[i] is UHFReader)
                {
                    #region generate UHFReader especific controls

                    Label LabelAntX = new Label
                    {
                        Name      = "labelAntX",
                        Text      = "Antenna x (m):",
                        Location  = new Point(276, 22),
                        Size      = new Size(83, 13),
                        TextAlign = ContentAlignment.MiddleRight
                    };

                    Label LabelAntY = new Label
                    {
                        Name      = "labelAntY",
                        Text      = "Antenna y (m):",
                        Location  = new Point(276, 48),
                        Size      = new Size(83, 13),
                        TextAlign = ContentAlignment.MiddleRight
                    };

                    Label LabelFrequencyMin = new Label
                    {
                        Name      = "labelFreqMin",
                        Text      = "Freq. min. (MHz):",
                        Location  = new Point(480, 22),
                        Size      = new Size(100, 13),
                        TextAlign = ContentAlignment.MiddleRight
                    };

                    Label LabelFrequencyMax = new Label
                    {
                        Name      = "labelFreqMax",
                        Text      = "Freq. Max. (MHz):",
                        Location  = new Point(480, 50),
                        Size      = new Size(100, 13),
                        TextAlign = ContentAlignment.MiddleRight
                    };

                    Label LabelPower = new Label
                    {
                        Name      = "labelPower",
                        Text      = "Power (dBm):",
                        Location  = new Point(680, 50),
                        Size      = new Size(70, 13),
                        TextAlign = ContentAlignment.MiddleRight
                    };

                    TextBox TextBoxAntX = new TextBox
                    {
                        Name     = "textBoxAntX",
                        Location = new Point(365, 19),
                        Size     = new Size(77, 20),
                        Text     = (devices[i] as UHFReader).Antenna.Position.X.ToString(),
                        ReadOnly = true
                    };

                    TextBox TextBoxAntY = new TextBox
                    {
                        Name     = "textBoxAntY",
                        Location = new Point(365, 45),
                        Size     = new Size(77, 20),
                        Text     = (devices[i] as UHFReader).Antenna.Position.Y.ToString(),
                        ReadOnly = true
                    };

                    CheckBox CheckBoxBeep = new CheckBox
                    {
                        Name     = "checkBoxBeep",
                        Text     = "Beep",
                        Location = new Point(193, 51),
                        Size     = new Size(51, 17)
                    };
                    CheckBoxBeep.Click  += new EventHandler(CheckBoxBeep_Click);
                    CheckBoxBeep.Enabled = false;

                    ComboBox ComboBoxFrequencyMin = new ComboBox
                    {
                        Name          = "comboBoxFrequencyMin",
                        Location      = new Point(580, 19),
                        Size          = new Size(55, 21),
                        DropDownStyle = ComboBoxStyle.DropDownList,
                        Enabled       = false
                    };
                    ComboBoxFrequencyMin.Items.AddRange(RegionFrequencyConverter.GetFrequencies());
                    ComboBoxFrequencyMin.SelectedItem = Convert.ToString((devices[i] as UHFReader).MinFrequency);

                    ComboBox ComboBoxFrequencyMax = new ComboBox
                    {
                        Name          = "comboBoxFrequencyMax",
                        Location      = new Point(580, 47),
                        Size          = new Size(55, 21),
                        DropDownStyle = ComboBoxStyle.DropDownList,
                        Enabled       = false
                    };
                    ComboBoxFrequencyMax.Items.AddRange(RegionFrequencyConverter.GetFrequencies());
                    ComboBoxFrequencyMax.SelectedItem = Convert.ToString((devices[i] as UHFReader).MaxFrequency);

                    ComboBox ComboBoxPower = new ComboBox
                    {
                        Name          = "comboBoxPower",
                        Location      = new Point(750, 47),
                        Size          = new Size(40, 21),
                        DropDownStyle = ComboBoxStyle.DropDownList
                    };
                    ComboBoxPower.Items.AddRange(UICollections.DevicePower);
                    ComboBoxPower.SelectedItem          = Convert.ToString((devices[i] as UHFReader).PowerDbm);
                    ComboBoxPower.SelectedIndexChanged += new EventHandler(ComboBoxPower_SelectedIndexChanged);

                    Gb.Controls.Add(LabelAntX);
                    Gb.Controls.Add(LabelAntY);
                    Gb.Controls.Add(LabelPower);
                    Gb.Controls.Add(TextBoxAntX);
                    Gb.Controls.Add(TextBoxAntY);
                    Gb.Controls.Add(CheckBoxBeep);
                    Gb.Controls.Add(ComboBoxPower);

                    Gb.Controls.Add(LabelFrequencyMin);
                    Gb.Controls.Add(LabelFrequencyMax);
                    Gb.Controls.Add(ComboBoxFrequencyMin);
                    Gb.Controls.Add(ComboBoxFrequencyMax);

                    #endregion
                }
                else if (devices[i] is LaserModule)
                {
                    #region generate LaserModule especific controls

                    Label LabelDirection = new Label
                    {
                        Name      = "labelDirection",
                        Text      = "Direction:",
                        Location  = new Point(276, 22),
                        Size      = new Size(83, 13),
                        TextAlign = ContentAlignment.MiddleRight
                    };

                    ComboBox ComboBoxDirection = new ComboBox
                    {
                        Name          = "comboBoxDirection",
                        Location      = new Point(365, 19),
                        Size          = new Size(77, 20),
                        DropDownStyle = ComboBoxStyle.DropDownList
                    };
                    ComboBoxDirection.Items.AddRange(UICollections.LaserDirection);
                    ComboBoxDirection.SelectedIndex         = (int)(devices[i] as LaserModule).Direction;
                    ComboBoxDirection.SelectedIndexChanged += new EventHandler(ComboBoxDirection_SelectedIndexChanged);

                    Button ButtonGetLaserStatus = new Button
                    {
                        Name     = "buttonGetLaserStatus",
                        Text     = "Status",
                        Location = new Point(276, 46),
                        Size     = new Size(78, 24)
                    };
                    ButtonGetLaserStatus.Click += new EventHandler(ButtonGetLaserStatus_Click);

                    TextBox TextBoxLaserStatus = new TextBox
                    {
                        Name     = "textBoxLaserStatus",
                        Location = new Point(365, 48),
                        Size     = new Size(77, 20),
                        ReadOnly = true
                    };

                    Button ButtonLaserOnOff = new Button
                    {
                        Name     = "buttonLaserOnOff",
                        Text     = "L (OFF)",
                        Location = new Point(195, 46),
                        Size     = new Size(50, 24)
                    };
                    ButtonLaserOnOff.Click += new EventHandler(ButtonLaserOnOff_Click);

                    Label LabelOffset = new Label
                    {
                        Name      = "labelOffset",
                        Text      = "Offset (m):",
                        Location  = new Point(497, 22),
                        Size      = new Size(83, 13),
                        TextAlign = ContentAlignment.MiddleRight
                    };

                    TextBox TextBoxLaserOffset = new TextBox
                    {
                        Name     = "textBoxLaserOffset",
                        Location = new Point(580, 20),
                        Size     = new Size(77, 20),
                        ReadOnly = true
                    };
                    TextBoxLaserOffset.Text = Convert.ToString((devices[i] as LaserModule).Offset);

                    Gb.Controls.Add(LabelDirection);
                    Gb.Controls.Add(LabelOffset);
                    Gb.Controls.Add(ComboBoxDirection);
                    Gb.Controls.Add(ButtonGetLaserStatus);
                    Gb.Controls.Add(TextBoxLaserStatus);
                    Gb.Controls.Add(TextBoxLaserOffset);

                    Gb.Controls.Add(ButtonLaserOnOff);

                    #endregion
                }
                else if (devices[i] is Servo)
                {
                    //TODO
                }

                panelDevices.Controls.Add(Gb);
            }
        }