示例#1
0
        /// <summary>
        /// 返回服务器端接收的数据,此处主要将一个设备下的所有IO表数据统一获取后在一次性上传,
        /// </summary>
        /// <param name="server"></param>
        /// <param name="comm"></param>
        /// <param name="device"></param>
        /// <param name="para">为null,此参数不传递,根据用户驱动需要</param>
        /// <param name="receivedatas">接收的全部数据,要求在驱动中进行一次读取后返回</param>

        public static void CDriverDll_OnDataReceived(IO_SERVER server, IO_COMMUNICATION comm, IO_DEVICE device, byte[] receivedatas, string date, object sender)
        {
            //解析数据
            var analysisTask = Task.Run(() =>
            {
                try
                {
                    if (device != null && device.DeviceDrive != null)
                    {
                        //清理已经接收完成的数据
                        for (int i = 0; i < device.IOParas.Count; i++)
                        {
                            device.IOParas[i].IORealData = null;
                        }

                        device.GetedValueDate = DateTime.Now;
                        device.ReceiveBytes   = receivedatas;
                        #region 循环解析实时数据接收的每个参数

                        ScadaDeviceKernel Driver = (ScadaDeviceKernel)device.DeviceDrive;
                        for (int i = 0; i < device.IOParas.Count; i++)
                        {
                            #region 解析开关量 模拟量 字符常量 数据
                            try
                            {
                                if (device.IOParas[i].IO_POINTTYPE == "模拟量" || device.IOParas[i].IO_POINTTYPE == "开关量" || device.IOParas[i].IO_POINTTYPE == "字符串量")
                                {
                                    try
                                    {
                                        Driver.InitKernel(server, comm, device, device.IOParas[i], device.DriverInfo);
                                        IOData recdata = Driver.AnalysisData(server, comm, device, device.IOParas[i], receivedatas, Convert.ToDateTime(date), sender);
                                        if (recdata != null)
                                        {
                                            device.IOParas[i].IORealData = recdata;
                                        }
                                    }
                                    catch
                                    {
                                        device.IOParas[i].IORealData = null;
                                    }
                                }
                            }
                            catch
                            {
                            }
                            #endregion
                            #region 解析关系数据库值
                            try
                            {
                                if (device.IOParas[i].IO_POINTTYPE == "关系数据库值" && device.IOParas[i].IO_DATASOURCE.Trim() != "")
                                {
                                    RelationalDatabase rlation = new RelationalDatabase(device.IOParas[i].IO_DATASOURCE);
                                    string sql  = rlation.GetSql();
                                    string conn = rlation.ConnectString;
                                    switch (rlation.Database_Type)
                                    {
                                    case "SQL Server":
                                        {
                                            try
                                            {
                                                if (conn != "" && sql != "")
                                                {
                                                    DbHelperSQL sqlHealp      = new DbHelperSQL();
                                                    sqlHealp.connectionString = conn;
                                                    DataSet ds = sqlHealp.Query(sql);
                                                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                                                    {
                                                        device.IOParas[i].IORealData = new IOData();
                                                        device.IOParas[i].IORealData.CommunicationID = device.IO_COMM_ID;
                                                        device.IOParas[i].IORealData.ServerID        = device.IO_SERVER_ID;
                                                        device.IOParas[i].IORealData.ID           = device.IO_DEVICE_ID;
                                                        device.IOParas[i].IORealData.ParaName     = device.IOParas[i].IO_NAME;
                                                        device.IOParas[i].IORealData.ParaString   = device.IOParas[i].IO_PARASTRING;
                                                        device.IOParas[i].IORealData.ParaValue    = ds.Tables[0].Rows[0]["value"].ToString();
                                                        device.IOParas[i].IORealData.QualityStamp = QualityStamp.GOOD;
                                                        device.IOParas[i].IORealData.Date         = Convert.ToDateTime(ds.Tables[0].Rows[0]["datetime"].ToString());
                                                    }
                                                }
                                            }
                                            catch
                                            {
                                                device.IOParas[i].IORealData = null;
                                            }
                                        }
                                        break;

                                    case "ORACLE":
                                        {
                                            try
                                            {
                                                if (conn != "" && sql != "")
                                                {
                                                    DbHelperOra oracleHealp      = new DbHelperOra();
                                                    oracleHealp.connectionString = conn;
                                                    DataSet ds = oracleHealp.Query(sql);
                                                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                                                    {
                                                        device.IOParas[i].IORealData = new IOData();
                                                        device.IOParas[i].IORealData.CommunicationID = device.IO_COMM_ID;
                                                        device.IOParas[i].IORealData.ServerID        = device.IO_SERVER_ID;
                                                        device.IOParas[i].IORealData.ID           = device.IO_DEVICE_ID;
                                                        device.IOParas[i].IORealData.ParaName     = device.IOParas[i].IO_NAME;
                                                        device.IOParas[i].IORealData.ParaString   = device.IOParas[i].IO_PARASTRING;
                                                        device.IOParas[i].IORealData.ParaValue    = ds.Tables[0].Rows[0]["value"].ToString();
                                                        device.IOParas[i].IORealData.QualityStamp = QualityStamp.GOOD;
                                                        device.IOParas[i].IORealData.Date         = Convert.ToDateTime(ds.Tables[0].Rows[0]["datetime"].ToString());
                                                    }
                                                }
                                            }
                                            catch
                                            {
                                                device.IOParas[i].IORealData = null;
                                            }
                                        }
                                        break;

                                    case "MySql":
                                        {
                                            try
                                            {
                                                if (conn != "" && sql != "")
                                                {
                                                    DbHelperMySQL mysqlHealp    = new DbHelperMySQL();
                                                    mysqlHealp.connectionString = conn;
                                                    DataSet ds = mysqlHealp.Query(sql);
                                                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                                                    {
                                                        device.IOParas[i].IORealData = new IOData();
                                                        device.IOParas[i].IORealData.CommunicationID = device.IO_COMM_ID;
                                                        device.IOParas[i].IORealData.ServerID        = device.IO_SERVER_ID;
                                                        device.IOParas[i].IORealData.ID           = device.IO_DEVICE_ID;
                                                        device.IOParas[i].IORealData.ParaName     = device.IOParas[i].IO_NAME;
                                                        device.IOParas[i].IORealData.ParaString   = device.IOParas[i].IO_PARASTRING;
                                                        device.IOParas[i].IORealData.ParaValue    = ds.Tables[0].Rows[0]["value"].ToString();
                                                        device.IOParas[i].IORealData.QualityStamp = QualityStamp.GOOD;
                                                        device.IOParas[i].IORealData.Date         = Convert.ToDateTime(ds.Tables[0].Rows[0]["datetime"].ToString());
                                                        double d = 0;
                                                        if (double.TryParse(device.IOParas[i].IORealData.ParaValue, out d))
                                                        {
                                                            device.IOParas[i].IORealData.DataType = typeof(double);
                                                        }
                                                        else
                                                        {
                                                            device.IOParas[i].IORealData.DataType = typeof(string);
                                                        }
                                                    }
                                                }
                                            }
                                            catch
                                            {
                                                device.IOParas[i].IORealData = null;
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                            catch
                            {
                            }
                            #endregion
                            #region 解析计算值包含公式计算的

                            //

                            try
                            {
                                if (device.IOParas[i].IO_POINTTYPE == "计算值")
                                {
                                    if (device.IOParas[i].IO_FORMULA.Trim() != "")
                                    {
                                        device.IOParas[i].IORealData = new IOData();
                                        try
                                        {
                                            device.IOParas[i].IORealData.QualityStamp = QualityStamp.GOOD;
                                            device.IOParas[i].IORealData.Date         = DateTime.Now;
                                            device.IOParas[i].IORealData.ParaName     = device.IOParas[i].IO_NAME;
                                            device.IOParas[i].IORealData.DataType     = typeof(double);
                                            //替换关键字为数值
                                            string formula = device.IOParas[i].IO_FORMULA;
                                            foreach (IO_PARA para in device.IOParas)
                                            {
                                                if (device.IOParas[i].IO_POINTTYPE != "字符串量" && device.IOParas[i].IO_POINTTYPE != "计算值" && device.IOParas[i].IORealData != null)
                                                {
                                                    if (device.IOParas[i].IORealData.QualityStamp == QualityStamp.GOOD && device.IOParas[i].IORealData.ParaValue != "-9999" && device.IOParas[i].IORealData.ParaValue != "")
                                                    {
                                                        formula = formula.Replace(device.IOParas[i].IO_NAME, device.IOParas[i].IORealData.ParaValue);
                                                    }
                                                }
                                            }
                                            if (formula != "")
                                            {
                                                //解析数学公式
                                                device.IOParas[i].IORealData.ParaValue = AnalyzeCalculate.Calculate(device.IOParas[i].IO_FORMULA);
                                                double d = 0;
                                                if (double.TryParse(device.IOParas[i].IORealData.ParaValue, out d))
                                                {
                                                    device.IOParas[i].IORealData.DataType = typeof(double);
                                                }
                                                else
                                                {
                                                    device.IOParas[i].IORealData.DataType = typeof(string);
                                                }
                                            }

                                            else
                                            {
                                                device.IOParas[i].IORealData.ParaValue    = "-9999";
                                                device.IOParas[i].IORealData.QualityStamp = QualityStamp.BAD;
                                            }
                                        }
                                        catch
                                        {
                                            device.IOParas[i].IORealData.QualityStamp = QualityStamp.BAD;
                                            device.IOParas[i].IORealData.Date         = DateTime.Now;
                                            device.IOParas[i].IORealData.ParaName     = device.IOParas[i].IO_NAME;
                                        }
                                    }
                                    else
                                    {
                                        device.IOParas[i].IORealData = null;
                                    }
                                }
                            }
                            catch
                            {
                            }
                            #endregion
                            #region 进行量程转换
                            try
                            {
                                if (device.IOParas[i].IO_POINTTYPE == "模拟量")
                                {
                                    if (device.IOParas[i].IORealData != null && device.IOParas[i].IO_ENABLERANGECONVERSION == 1 && device.IOParas[i].IORealData.QualityStamp == QualityStamp.GOOD)
                                    {
                                        if (device.IOParas[i].IORealData.ParaValue != "" && device.IOParas[i].IORealData.ParaValue != "-9999")
                                        {
                                            string value = ConvertParaTypeValue(device.IOParas[i].GetParaValueType(), device.IOParas[i].IORealData.ParaValue, double.Parse(device.IOParas[i].IO_RANGEMAX), double.Parse(device.IOParas[i].IO_RANGEMIN), double.Parse(device.IOParas[i].IO_MAXVALUE), double.Parse(device.IOParas[i].IO_MINVALUE));
                                            device.IOParas[i].IORealData.ParaValue = value;
                                        }
                                    }
                                }
                            }
                            catch
                            {
                            }

                            #endregion

                            #region 常量值
                            try
                            {
                                if (device.IOParas[i].IO_POINTTYPE == "常量值")
                                {
                                    device.IOParas[i].IORealData = new IOData()
                                    {
                                        CommunicationID = device.IOParas[i].IO_COMM_ID,
                                        DataType        = typeof(string),
                                        Date            = device.GetedValueDate,
                                        ParaName        = device.IOParas[i].IO_NAME,
                                        ParaString      = device.IOParas[i].IO_PARASTRING,
                                        ParaValue       = device.IOParas[i].IO_INITALVALUE,
                                        QualityStamp    = QualityStamp.GOOD,
                                        ServerID        = device.IOParas[i].IO_SERVER_ID
                                    };
                                }
                            }
                            catch
                            {
                            }

                            #endregion
                        }
                        #endregion
                        #region 将解析后的数据上传到数据中心服务器上
                        IO_DEVICE newDevice = device.Copy();
                        try
                        {
                            //将接收到的数据保存到实时缓存,主要用于批量上传,提高传输效率
                            receiveRealCache.Push(new ReceiveCacheObject()
                            {
                                DataString = RealDataDBUtility.GetRealDataCacheString(device)
                            });
                            MonitorFormManager.ShowMonitorUploadListView(server, comm, device, "上传成功");
                        }
                        catch (Exception emx)
                        {
                            ThrowExceptionToMain(emx);
                        }
                        //在事件接收窗体中显示接收的采集数据
                        if (OnMonitorReceive != null)
                        {
                            OnMonitorReceive(server, comm, newDevice, receivedatas);
                        }

                        #endregion
                        #region 计算报警并上传
                        try
                        {
                            Task.Run(() =>
                            {
                                List <IO_PARAALARM> res = new List <IO_PARAALARM>();
                                //将接收到的数据保存到实时缓存,主要用于批量上传,提高传输效率
                                receiveRealCache.Push(new AlarmCacheObject()
                                {
                                    DataString = RealDataDBUtility.GetAlarmCacheString(device, out res)
                                });
                                for (int i = 0; i < res.Count; i++)
                                {
                                    MonitorFormManager.MonitorIODataAlarmShowView(server, comm, device, res[i], "上传成功");
                                }
                                res.Clear();
                                res = null;
                            });
                        }
                        catch (Exception emx)
                        {
                            ThrowExceptionToMain(emx);
                        }
                        #endregion
                    }
                }
                catch (Exception emx)
                {
                    ThrowExceptionToMain(emx);
                }
                if (receivedatas != null && receivedatas.Length > 0)
                {
                    AddLogToMainLog(device.IO_DEVICE_NAME + "接收到数据 DATA=" + CVT.ByteToHexStr(receivedatas));
                }
            });
        }