示例#1
0
        public MainForm()
        {
            InitializeComponent();

            DBControl.OpenConnection();
            _dateTimeService = new DateTimeService();

            InitializeUserData();
        }
示例#2
0
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("Закрыть программу?", "Предупреждение", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
            {
                DisconnectBtn.PerformClick();

                DBControl.CloseConnection();
            }
            else
            {
                e.Cancel = true;
            }
        }
示例#3
0
        private void InitializeUserData()
        {
            DataSet dsPLC    = new DataSet();
            DataSet dsSensor = new DataSet();
            DataSet dsDB     = new DataSet();

            strSQL = "SELECT id, name, ip, rack, slot, type, serverId, cnnString FROM plc WHERE id = 1";
            dsPLC  = DBControl.Select(strSQL);

            if (dsPLC == null || dsPLC.Tables[0].Rows.Count == 0)
            {
                listBox1.Items.Add("В БД нет PLC");
            }
            else
            {
                foreach (DataRow p in dsPLC.Tables[0].Rows)
                {
                    plc.Id        = Convert.ToInt32(p["id"].ToString());
                    plc.Name      = p["name"].ToString().Trim();
                    plc.Ip        = p["ip"].ToString().Trim();
                    plc.Rack      = Convert.ToInt32(p["rack"].ToString());
                    plc.Slot      = Convert.ToInt32(p["slot"].ToString());
                    plc.Type      = Convert.ToInt32(p["type"].ToString());
                    plc.CnnString = p["cnnString"].ToString().Trim();
                }

                listBox1.Items.Add(string.Format("{0:dd.MM.yyyy HH:mm:ss}", DateTime.Now) + " PLC:[" + plc.Name + "] " + plc.Ip + "; cnn:" + plc.CnnString);

                strSQL   = "SELECT sensor_id, tag, (SELECT number FROM datablock WHERE id = 1) as db, address, deadband, isLine, idLine, typeLine FROM sensor WHERE plc_id = 1";
                dsSensor = DBControl.Select(strSQL);

                if (dsSensor == null || dsSensor.Tables[0].Rows.Count == 0)
                {
                    listBox1.Items.Add("В БД нет сенсоров для PLC: " + plc.Name);
                }
                else
                {
                    foreach (DataRow s in dsSensor.Tables[0].Rows)
                    {
                        sensorList.Add(
                            new Model.Sensor
                        {
                            SensorId = Convert.ToInt32(s["sensor_id"].ToString()),
                            Tag      = s["tag"].ToString().Trim(),
                            PlcId    = plc.Id,
                            Db       = Convert.ToInt32(s["db"].ToString()),
                            Address  = s["address"].ToString().Trim(),
                            Deadband = Convert.ToDouble(s["deadband"].ToString()),
                            IsLine   = Convert.ToInt32(s["isLine"].ToString()),
                            IdLine   = Convert.ToInt32(s["idLine"].ToString()),
                            typeLine = s["typeLine"].ToString()
                        }
                            );
                    }

                    strSQL = "SELECT number, size FROM datablock WHERE id = 1";
                    dsDB   = DBControl.Select(strSQL);

                    foreach (DataRow db in dsDB.Tables[0].Rows)
                    {
                        dbNumber = Convert.ToInt32(db["number"].ToString());
                        dbSize   = Convert.ToInt32(db["size"].ToString());
                    }

                    sensorValueInsert = new SensorValueInsertModel()
                    {
                        connectionStringName = plc.CnnString.Trim(),
                        counterValue         = new List <Model.SensorValueModel>()
                    };

                    sensorValId1AvgInsert = new SensorValueInsertModel()
                    {
                        connectionStringName = plc.CnnString.Trim(),
                        counterValue         = new List <Model.SensorValueModel>()
                    };

                    sensorValId2AvgInsert = new SensorValueInsertModel()
                    {
                        connectionStringName = plc.CnnString.Trim(),
                        counterValue         = new List <Model.SensorValueModel>()
                    };

                    //sensorValue2mList.Add(new SensorValueModel());
                    //lineStateInsertList.Add(new LineStateInsertModel());

                    client = new S7Client();

                    Buffer = new byte[65536];

                    isDataExist = true;
                }
            }
            dsPLC.Clear();
            dsPLC.Dispose();
            dsSensor.Clear();
            dsSensor.Dispose();
            dsDB.Clear();
            dsDB.Dispose();
        }