/// <summary> /// 移除采集点信息 /// </summary> /// <param name="configName">配置信息名称</param> private void RemoveFromAllDataDict(string configName) { if (AllConfigDict.ContainsKey(configName)) { pb_config_rtds configObject = (pb_config_rtds)AllConfigDict[configName]; // 校验采集点在其他配置中是否使用 foreach (var tagObject in configObject.TagInfors) { if (TagAndConfigDict.ContainsKey(tagObject.Name)) { if (TagAndConfigDict[tagObject.Name].Count == 1) { if (AllDataDict.ContainsKey(tagObject.Name)) { AllDataDict.Remove(tagObject.Name); } TagAndConfigDict.Remove(tagObject.Name); } else { TagAndConfigDict[tagObject.Name].Remove(configName); } } } } }
/// <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); } } }
/// <summary> /// 设置服务状态 /// </summary> /// <param name="value"></param> private void SetRtdsStatus(string value) { // 实时更新状态 if (AllDataDict.ContainsKey("s_rtds_status_1")) { ((pb_data_sensor)AllDataDict["s_rtds_status_1"]).Value = ByteString.CopyFromUtf8(value); ((pb_data_sensor)AllDataDict["s_rtds_status_1"]).Time = GetCurrentTimeUnix(); } }
/// <summary> /// 获取当前运行状态 /// </summary> /// <param name="runTag"></param> /// <param name="runTagValue"></param> /// <returns></returns> private static string GetRunStatus(string runTag, string runTagValue) { if (AllDataDict.ContainsKey(runTag)) { runTagValue = ((pb_data_sensor)AllDataDict[runTag]).Value.ToStringUtf8(); } else { runTagValue = "notexist"; } return(runTagValue); }
/// <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); } } } }