public void RequestPlcDataToDB(string strJsonText)
    {
        try
        {
            if (!string.IsNullOrEmpty(strJsonText))
            {
                int           receiveNumber = strJsonText.Length;
                DataLogHelper logHelper     = new DataLogHelper();

                logHelper.SocketLog(strJsonText, receiveNumber);

                #region 对接收到的传感器日志数据进行转换操作
                try
                {
                    HNFactoryAutoSystem.Business.DeviceBusiness deviceBusiness = new HNFactoryAutoSystem.Business.DeviceBusiness();
                    SensorDataCollection datas = JsonConvert.DeserializeObject <SensorDataCollection>(strJsonText);
                    //将传感器读取的数据写入生产日志表
                    foreach (SensorData data in datas)
                    {
                        //判断值类型来提取传感器的状态信息
                        string  strDeviceStatus    = "P";
                        string  strSensorId        = data.SensorId;
                        string  strSensorValueType = data.ValueType;
                        decimal deParValue         = data.SensorValue;

                        deviceBusiness.AddDeviceProduceLog(strDeviceStatus, strSensorId, strSensorValueType, deParValue);
                    }
                }
                catch { }
                #endregion
            }
        }
        catch { }
    }
Ejemplo n.º 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                DataLogHelper           logHelper      = new DataLogHelper();
                Business.DeviceBusiness deviceBusiness = new Business.DeviceBusiness();
                string strText       = textBox1.Text;
                int    receiveNumber = strText.Length;
                if (!string.IsNullOrEmpty(strText))
                {
                    logHelper.SocketLog(strText, receiveNumber);

                    #region 对接收到的传感器日志数据进行转换操作
                    try
                    {
                        SensorDataCollection datas = JsonConvert.DeserializeObject <SensorDataCollection>(strText);
                        //将传感器读取的数据写入生产日志表
                        foreach (SensorData data in datas)
                        {
                            //判断值类型来提取传感器的状态信息
                            string  strDeviceStatus    = "P";
                            string  strSensorId        = data.SensorId;
                            string  strSensorValueType = data.ValueType;
                            decimal deParValue         = data.SensorValue;

                            deviceBusiness.AddDeviceProduceLog(strDeviceStatus, strSensorId, strSensorValueType, deParValue);
                        }
                    }
                    catch { }
                    #endregion
                }
            }
        }
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="clientSocket"></param>
        private void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;

            while (true)
            {
                try
                {
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    //实际接受到的有效字节数
                    int receiveNumber = myClientSocket.Receive(buffer);
                    if (receiveNumber == 0)
                    {
                        break;
                    }
                    //string strInfo = string.Format("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));

                    string strInfo = Encoding.UTF8.GetString(buffer, 0, receiveNumber);

                    string[] strTexts = strInfo.Split('|');

                    ////通过clientSocket接收数据
                    //int receiveNumber = myClientSocket.Receive(result);
                    //string strInfo = string.Format("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
                    ////MessageBox.Show(strInfo);
                    DataLogHelper           logHelper      = new DataLogHelper();
                    Business.DeviceBusiness deviceBusiness = new Business.DeviceBusiness();
                    foreach (string strText in strTexts)
                    {
                        if (!string.IsNullOrEmpty(strText))
                        {
                            logHelper.SocketLog(strText, receiveNumber);

                            #region 对接收到的传感器日志数据进行转换操作
                            try
                            {
                                SensorDataCollection datas = JsonConvert.DeserializeObject <SensorDataCollection>(strText);
                                //将传感器读取的数据写入生产日志表
                                foreach (SensorData data in datas)
                                {
                                    //判断值类型来提取传感器的状态信息
                                    string  strDeviceStatus    = "P";
                                    string  strSensorId        = data.SensorId;
                                    string  strSensorValueType = data.ValueType;
                                    decimal deParValue         = data.SensorValue;

                                    deviceBusiness.AddDeviceProduceLog(strDeviceStatus, strSensorId, strSensorValueType, deParValue);
                                }
                            }
                            catch { }
                            #endregion
                        }
                    }
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                    //ServerMainFormtxt_Log.AppendText(ex.Message + "\r \n");
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    break;
                }
            }
        }