Ejemplo n.º 1
0
        /// <summary>
        /// 新增采集点信息
        /// </summary>
        /// <param name="configName">配置信息名称</param>
        /// <param name="Config">配置信息</param>
        private void AddAllDataDict(string configName, PbConfig Config)
        {
            pb_config_rtds config_rtds = (pb_config_rtds)Config;

            foreach (var tag in config_rtds.TagInfors)
            {
                pb_data_sensor dataObject = new pb_data_sensor();
                dataObject.Name    = tag.Name;
                dataObject.Type    = pb_data_type.EnumInt32;
                dataObject.Size    = 0;
                dataObject.Value   = ByteString.CopyFromUtf8("");
                dataObject.Time    = 0;
                dataObject.Quality = 128;
                dataObject.Status  = pb_data_status.Nonexist;
                dataObject.Unit    = pb_data_unit.NoneUnit;
                if (!AllDataDict.ContainsKey(dataObject.Name))
                {
                    AllDataDict.Add(dataObject.Name, dataObject);
                }

                // 建立configName与配置点的关系
                if (!TagAndConfigDict.ContainsKey(dataObject.Name))
                {
                    List <string> ConfigNameList = new List <string>();
                    TagAndConfigDict.Add(dataObject.Name, ConfigNameList);
                    TagAndConfigDict[dataObject.Name].Add(configName);
                }
                else
                {
                    TagAndConfigDict[dataObject.Name].Add(configName);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="pbDataType">数据类型</param>
        /// <param name="pbData">数据信息</param>
        /// <param name="dataName">绑定数据别名</param>
        /// <returns>1:成功 -1:失败 -2:请求点不在集合中</returns>
        private int bind_data(string pbDataType, byte[] pbData, string dataName)
        {
            int result = 1;
            pb_data_sensor_list data_sensor_list = pb_data_sensor_list.Parser.ParseFrom(pbData);

            log.Debug(string.Format("server received:command:{0}, pbDataType:{1}, dataName:{2}, {3} tags in bind_data", "bind_data", pbDataType.ToLower(), dataName, data_sensor_list.PbDataSensors.Count));

            if (pbDataType.ToLower() != "pb_data_sensor_list")
            {
                result = (int)eErrorCode.BIND_PARAM_ERR;
                log.Debug(string.Format("bind_data error:params {0} ≠ pb_data_sensor_list", pbDataType));
            }
            else
            {
                // 判断绑定数据是否为空
                if (data_sensor_list.PbDataSensors.Count > 0)
                {
                    // 判断请求点是否在集合中
                    foreach (var data_sensor in data_sensor_list.PbDataSensors)
                    {
                        if (!RDB.AllDataDict.ContainsKey(data_sensor.Name))
                        {
                            result = (int)eErrorCode.BIND_NOTIN_ERR;
                            log.Debug("bind_data error:the tag " + data_sensor.Name + " not exist");
                            break;
                        }
                    }
                    if (result != 0x03090203)
                    {
                        Hashtable PbList = new Hashtable();
                        foreach (var data_sensor in data_sensor_list.PbDataSensors)
                        {
                            pb_data_sensor dataSensor = (pb_data_sensor)RDB.AllDataDict[data_sensor.Name];
                            if (!PbList.Contains(dataSensor.Name))
                            {
                                PbList.Add(dataSensor.Name, dataSensor);
                            }
                        }
                        // 绑定dataName与AllDataDict
                        if (!dataDict.ContainsKey(dataName))
                        {
                            dataDict.Add(dataName, PbList);
                        }
                        else
                        {
                            dataDict[dataName] = PbList;
                        }
                    }
                }
                else
                {
                    result = (int)eErrorCode.BIND_NOTAG_ERR;
                    log.Debug("bind_data error:bind data is empty");
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 初始化配置
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="name"></param>
        /// <param name="N"></param>
        /// <returns>1:成功 -1:失败</returns>
        public int Init(string ip, string name, string N)
        {
            int result = 1;

            // 校验name是否为s_rtds
            if (name.ToLower() != "s_rtds")
            {
                result = (int)eErrorCode.INIT_PARAM_ERR;
                log.Debug(string.Format("init error:zmq start failed,because {0}", "name not match"));
            }
            else
            {
                if (int.Parse(N) == 1)
                {
                    try
                    {
                        string port = (40000 + int.Parse(N) * 1000 + 9).ToString();
                        // 完成ZMQ的bind完成ZMQ监听 此处ip地址、端口号后续增加校验
                        string address = "tcp://" + ip + ":" + port;
                        serverSocket = new ResponseSocket();
                        serverSocket.Bind(address);

                        log.Info("zmq start success");

                        // 初始化rtds
                        rdb = new RDB();

                        // DataList增加富岛实时数据服务状态(s_rtds_status_N)并更新为1
                        pb_data_sensor s_rtds_stt = new pb_data_sensor();
                        s_rtds_stt.Name  = "s_rtds_status_" + N;
                        s_rtds_stt.Value = ByteString.CopyFromUtf8("1");
                        s_rtds_stt.Time  = rdb.GetCurrentTimeUnix();
                        if (!RDB.AllDataDict.ContainsKey("s_rtds_status_" + N))
                        {
                            RDB.AllDataDict.Add(s_rtds_stt.Name, s_rtds_stt);
                        }

                        Hashtable      Statu      = new Hashtable();
                        pb_data_sensor dataSensor = (pb_data_sensor)RDB.AllDataDict[s_rtds_stt.Name];
                        if (!Statu.ContainsKey(dataSensor.Name))
                        {
                            Statu.Add(dataSensor.Name, dataSensor);
                        }
                        else
                        {
                            Statu[dataSensor.Name] = dataSensor;
                        }
                        if (!dataDict.Contains(dataSensor.Name))
                        {
                            dataDict.Add("all_status", Statu);
                        }
                        else
                        {
                            dataDict["all_status"] = Statu;
                        }
                    }
                    catch (Exception ex)
                    {
                        result = (int)eErrorCode.INIT_ZMQ_ERR;
                        log.Debug(string.Format("init error:zmq start failed,because {0}", ex.Message.ToString()));
                    }
                }
                else
                {
                    result = (int)eErrorCode.INIT_PARAM_ERR;
                    log.Debug("init error:zmq start failed,because param N is not 1");
                }
            }
            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 开始读取数据并更新内存AllDataDict
        /// </summary>
        /// <param name="configObj">配置项名称</param>
        private void Work(object configObj)
        {
            string configName = configObj.ToString();
            // 获取数据更新周期
            int cycle = 0;

            foreach (KeyValuePair <string, PbConfig> kvp in AllConfigDict)
            {
                if (kvp.Key == configName)
                {
                    cycle = ((pb_config_rtds)kvp.Value).UpdateCycle;
                    break;
                }
            }
            while (GetThreadStt(configName))
            {
                var now = DateTime.Now;
                try
                {
                    pb_data_sensor_list pbDataList = new pb_data_sensor_list();
                    foreach (KeyValuePair <string, List <string> > kvp in TagAndConfigDict)
                    {
                        if (kvp.Value.Contains(configName))
                        {
                            pb_data_sensor dataObject = new pb_data_sensor();
                            dataObject.Name = kvp.Key;

                            pbDataList.PbDataSensors.Add(dataObject);
                        }
                    }

                    MemoryStream mStream = new MemoryStream();
                    pbDataList.WriteTo(mStream);

                    // 定义这个c#中用来接收c++返回数据的指针类型
                    IntPtr unmanaged_ptr = IntPtr.Zero;
                    int    length        = Rtds.rtds_get_cur_data(mStream.ToArray(), mStream.ToArray().Length, out unmanaged_ptr);

                    if (length > 0)
                    {
                        byte[] managed_data = new byte[length];
                        // 将非托管内存拷贝成托管内存,才能在c#里面使用
                        Marshal.Copy(unmanaged_ptr, managed_data, 0, length);

                        // 遍历返回标签点数据 并更新到内存中
                        pb_data_sensor_list data_sensor_list = pb_data_sensor_list.Parser.ParseFrom(managed_data);
                        foreach (pb_data_sensor pb_data_sensor in data_sensor_list.PbDataSensors)
                        {
                            if (AllDataDict.ContainsKey(pb_data_sensor.Name))
                            {
                                ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Type    = pb_data_sensor.Type;
                                ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Size    = pb_data_sensor.Size;
                                ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Value   = pb_data_sensor.Value;
                                ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Time    = pb_data_sensor.Time;
                                ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Quality = pb_data_sensor.Quality;
                                ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Status  = pb_data_sensor.Status;
                                ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Unit    = pb_data_sensor.Unit;
                            }
                        }
                        // 清理C++内存空间
                        Rtds.rtds_release_mem(out unmanaged_ptr);
                    }
                    else
                    {
                        log.Debug(string.Format("GetData Error:calling rtds_get_cur_data method,method returns data length:{0}", length));
                    }
                    // 实时更新状态 为已运行 3
                    SetRtdsStatus("3");
                }
                catch (Exception ex)
                {
                    // 实时更新状态 为异常 4
                    SetRtdsStatus("4");
                    log.Debug(string.Format("GetData Error:update cache data failed,because {0}", ex.Message.ToString()));
                }
                // 如果数据更新周期为0 则只取一次数据
                if (cycle == 0)
                {
                    break;
                }
                else
                {
                    var      cur = DateTime.Now;
                    TimeSpan ts  = cur - now;
                    if (ts.TotalMilliseconds < cycle * 1000)
                    {
                        Thread.Sleep(cycle * 1000 - (int)ts.TotalMilliseconds);
                    }
                }
            }
        }