Exemple #1
0
        private void timerSendDataToServer_Tick(object sender, EventArgs e)
        {
            string urlLineState   = serverIIS + ":9030/write/line-state";
            string urlSensorValue = serverIIS + ":9030/values";

            string json = string.Empty;

            if (!client.Connected)
            {
                lineStateInsertList.Add(
                    new LineStateInsertModel()
                {
                    connectionStringName = cnnString,
                    dtFrom   = _dateTimeService.UnixTimeNow(),
                    idLine   = 1,
                    idState  = 100,
                    typeInfo = "2"
                });

                currLineState = 100;
            }

            if (lineStateInsertList.Count > 0)
            {
                json = JsonConvert.SerializeObject(lineStateInsertList);
                byte[] body = Encoding.UTF8.GetBytes(json);

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlLineState);

                req.Method        = "POST";
                req.ContentType   = "application/json";
                req.ContentLength = body.Length;
                req.Timeout       = 10000;

                bool lineStateListInsertOk = true;

                //var request = new Request();

                try
                {
                    using (Stream stream = req.GetRequestStream())
                    {
                        stream.Write(body, 0, body.Length);
                        stream.Close();
                    }

                    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                    {
                        response.Close();
                    }

                    //request.Execute(urlLineState, lineStateInsertList, "POST");
                }
                catch (Exception ex)
                {
                    listBox1.Items.Add(string.Format("{0:dd.MM.yyyy HH:mm:ss}", DateTime.Now) + ": Ошибка отправки данных (line-state) на сервер: " + ex.Message);
                    lineStateListInsertOk = false;
                }

                if (lineStateListInsertOk)
                {
                    //listBox1.Items.Add(DateTime.Now + ": Отправка данных (line-state) на сервер. PLC IP:" + plc.Ip + " Rack:" + plc.Rack + " Slot:" + plc.Slot);
                    lineStateInsertList.Clear();
                }
            }

            if (sensorValueInsert.counterValue.Count > 0)
            {
                json = JsonConvert.SerializeObject(sensorValueInsert);
                byte[] body = Encoding.UTF8.GetBytes(json);

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlSensorValue);

                req.Method        = "POST";
                req.ContentType   = "application/json";
                req.ContentLength = body.Length;
                req.Timeout       = 10000;


                bool sensorValueInsertOk = true;

                //var request = new Request();

                try
                {
                    using (Stream stream = req.GetRequestStream())
                    {
                        stream.Write(body, 0, body.Length);
                        stream.Close();
                    }

                    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                    {
                        response.Close();
                    }

                    //request.Execute(urlSensorValue, sensorValueInsert, "POST");
                }
                catch (Exception ex)
                {
                    listBox1.Items.Add(string.Format("{0:dd.MM.yyyy HH:mm:ss}", DateTime.Now) + ": Ошибка отправки данных (values) на сервер: " + ex.Message);
                    sensorValueInsertOk = false;
                }

                if (sensorValueInsertOk)
                {
                    //listBox1.Items.Add(DateTime.Now + ": Отправка данных (values) на сервер. PLC IP:" + plc.Ip + " Rack:" + plc.Rack + " Slot:" + plc.Slot);
                    sensorValueInsert.counterValue.Clear();
                }
            }
        }