Exemple #1
0
        /// <summary>
        /// 从采集站端上传报警值
        /// </summary>
        /// <param name="server"></param>
        /// <param name="comm"></param>
        /// <param name="device"></param>
        /// <returns></returns>
        public static bool UploadAlarm(List <AlarmCacheObject> receiveCaches)
        {
            bool   result     = false;
            string sendString = "";

            receiveCaches.ForEach(delegate(AlarmCacheObject p) {
                sendString += p.DataString + "^";
            });

            try
            {
                Scada.AsyncNetTcp.TcpData tcpData = new Scada.AsyncNetTcp.TcpData();
                byte[] simuBytes = tcpData.StringToTcpByte(sendString, Scada.AsyncNetTcp.ScadaTcpOperator.报警值);



                if (IOMonitorManager.TcpClient != null && IOMonitorManager.TcpClient.IsClientConnected)
                {
                    IOMonitorManager.TcpClient.Send(new ArraySegment <byte>(simuBytes));
                    result = true;
                }
            }
            catch (Exception ex)
            {
                result = false;
                //写入错误日志,并将错误日志返回的日志窗体
                MonitorFormManager.DisplyException(ex);
            }
            receiveCaches.Clear();
            receiveCaches = null;
            sendString    = "";
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// 设备状态变化返回的事件
        /// </summary>
        /// <param name="server"></param>
        /// <param name="comm"></param>
        /// <param name="device"></param>
        /// <param name="para"></param>
        /// <param name="tag"></param>

        public static void CDriverDll_DeviceStatusChanged(IO_SERVER server, IO_COMMUNICATION comm, IO_DEVICE device, IO_PARA para, object tag)
        {
            var analysisTask = Task.Run(() =>
            {
                if (tag != null)
                {
                    if (tag.ToString() == "1")
                    {
                        //设备上线不需要通知,如果下线,则在系统日志中进行显示
                        device.IO_DEVICE_STATUS = 1;
                        MonitorFormManager.RefreshIOStatus(device);
                    }
                    else
                    {
                        AddLogToMainLog("设备" + device.IO_DEVICE_NAME + " 下线 ");
                        device.IO_DEVICE_STATUS = 0;
                        MonitorFormManager.RefreshIOStatus(device);
                    }
                }
            });
        }
Exemple #3
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));
                }
            });
        }