1.使用svg實現組態畫面和動態數據展現javascript
經過js的定時器調用webservice方法獲取數據後更新節點數據html
/// <summary>啓動定時刷新</summary> function Start() { InitSvgElement(); this.timer = setInterval("GetTagValues()", 1000); } /// <summary>啓動定時刷新</summary> function Stop() { clearInterval(this.timer); }
初始化svg的節點變量java
/// <summary>初始化SVG元素</summary> function InitSvgElement() { this.svgDoc = document.getElementById('svgDoc').getSVGDocument(); this.svgRoot = this.svgDoc.documentElement; this.svg_jzwd = this.svgDoc.getElementById("tspan-jzwd"); this.svg_jzyl = this.svgDoc.getElementById("tspan-jzyl"); this.svg_czyl = this.svgDoc.getElementById("tspan-czyl"); this.svg_czwd = this.svgDoc.getElementById("tspan-czwd"); this.svg_yl = this.svgDoc.getElementById("tspan-yl"); this.svg_wd = this.svgDoc.getElementById("tspan-wd"); this.svg_yll = this.svgDoc.getElementById("rect-ylg"); }
經過ajax訪問webservicenode
/// <summary>獲取測點數據</summary> function GetTagValues() { try { $.ajax({ type: "POST", url: "../PsWebService.asmx/GetTagValues", data: { ip: ip, port: port, tagNames: tags }, dataType: 'xml', error: function (request) { alert("調用webService異常"); }, success: function (msg) { var tagValueJSON = $(msg).find("string").text(); UpdateTagLables(tagValueJSON); } }); } catch (e) { alert(e.Message); } }
更新節點變量的值git
/// <summary>更新標籤數據</summary> function UpdateTagLables(valueJSON) { var tagValueArray = eval('(' + valueJSON + ')'); if (tagValueArray.length != 6) { // alert("數據長度不足,請按照順序設置測點!" + tagValueArray.length); return; } else { this.SetElementValue(this.svg_jzwd, tagValueArray[0].Value, " ℃"); this.SetElementValue(this.svg_jzyl, tagValueArray[1].Value, " MPa"); this.SetElementValue(this.svg_czwd, tagValueArray[2].Value, " ℃"); this.SetElementValue(this.svg_czyl, tagValueArray[3].Value, " MPa"); this.SetElementValue(this.svg_wd, tagValueArray[4].Value, " ℃"); this.SetElementValue(this.svg_yl, tagValueArray[5].Value, " MPa"); var yl = (tagValueArray[5].Value / 1000) * 68; //當前值除以量程(1000)獲得百分比,而後乘以壓力計的最大寬度得出實際應該顯示的高度 yl = CurrencyFormatted(yl); this.svg_yll.setAttributeNS(null, "width", yl); } } /// <summary>設置元素值,數據進行格式化保留兩位小數</summary> function SetElementValue(element, value, unit) { var newValue = CurrencyFormatted(value); if ($.browser.msie&&$.browser.version=="8.0") { //IE 下的寫法 element.getFirstChild().setNodeValue(newValue + unit); } if ($.browser.msie && $.browser.version == "9.0") { //IE 下的寫法 element.firstChild.textContent = newValue + unit; } if ($.browser.msie && $.browser.version == "10.0") { //IE 下的寫法 element.firstChild.textContent = newValue + unit; } if ($.browser.chrome) { //Chrome下的寫法 element.firstChild.textContent = newValue + unit; } else { element.firstChild.textContent = newValue + unit; } }
2.webservice方法調用pSpace的sdk類庫獲取數據web
[WebMethod] public string GetTagValues(string ip, string port, string tagNames) { try { //\TestTag\mnl_1 List<PsPointValue> list = new List<PsPointValue>(); PsApi api = new PsApi(); api.Init(ip, port, "admin", "admin888"); if (api.Conn()) { PsPointValue ps = new PsPointValue(); string[] tagNameArray = tagNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); list = api.GetDataSnapshot(tagNameArray); api.DisConn(); } return JsonConvert.SerializeObject(list); } catch (Exception ex) { return ex.Message; } }
/// <summary> /// pSpaceCTL.NET.dll 的接口服務封裝 /// </summary> public class PsApi : IDisposable { #region 屬性 /// <summary> /// IP地址 /// </summary> public string IP { get; set; } /// <summary> /// 是否空閒 /// </summary> public bool IsFree { get; set; } /// <summary> /// 數據庫鏈接信息 /// </summary> public DbConnector Dbconn { get; private set; } /// <summary> /// 測點樹 /// </summary> public TagTree TagTree { get; private set; } /// <summary> /// 根節點 /// </summary> public TagNode RootNode { get; private set; } /// <summary> /// 測點管理類 /// </summary> public TagManager TagManager { get; private set; } /// <summary> /// 實時數據變動訂閱器類 /// </summary> public RealSubscriber RealDataSubScriber { get; private set; } /// <summary> /// 最後的異常 /// </summary> public PsApiException LastError { get; private set; } #endregion #region 事件 public event Action<PsApiException> OnPsApiException; #endregion #region Init() /// <summary> /// 初始化PsServer鏈接屬性 /// 注:從app.config文件讀取PsServerIP、PsServerPort、PsServerUser、PsServerUserPwd 屬性 /// </summary> public void Init() { string ip = ConfigurationManager.AppSettings["PsServerIP"]; string port = ConfigurationManager.AppSettings["PsServerPort"]; string user = ConfigurationManager.AppSettings["PsServerUser"]; string pwd = ConfigurationManager.AppSettings["PsServerUserPwd"]; this.Init(ip, port, user, pwd); } /// <summary> /// 初始化PsServer鏈接屬性 /// </summary> /// <param name="ip">PsServer 地址</param> /// <param name="port">PsServer 端口</param> /// <param name="user">用戶名</param> /// <param name="userPwd">密碼</param> public void Init(string ip, string port, string user, string userPwd) { if (ip.Trim().Equals("")) throw new ArgumentException("參數不能爲空", "ip", null); if (port.Trim().Equals("")) throw new ArgumentException("參數不能爲空", "port", null); if (user.Trim().Equals("")) throw new ArgumentException("參數不能爲空", "user", null); if (userPwd.Trim().Equals("")) throw new ArgumentException("參數不能爲空", "userPwd", null); this.IP = ip; this.Dbconn = new DbConnector(); this.Dbconn.UserName = user; this.Dbconn.Password = userPwd; this.Dbconn.ServerName = ip + ":" + port; } #endregion #region Conn() and DisConn() /// <summary> /// 鏈接psServer /// </summary> /// <returns>True:成功,False:失敗</returns> public bool Conn() { try { Common.StartAPI(); if (this.Dbconn.IsConnected()) this.Dbconn.Disconnect(); this.Dbconn.CanReconnect = false; //不自動重連 DbError dbError = this.Dbconn.Connect(); if (dbError.HasErrors) { this.LastError = new PsApiException(dbError.ErrorMessage + "(" + this.Dbconn.ServerName + ")", null); return false; } else { this.TagTree = TagTree.CreateInstance(this.Dbconn); this.RootNode = this.TagTree.GetTreeRoot(); this.RealDataSubScriber = new RealSubscriber(); return true; } } catch (Exception ex) { this.LastError = new PsApiException("鏈接psServer失敗", ex); return false; } } /// <summary> /// 是否已經鏈接 /// </summary> /// <returns></returns> public bool IsConnected() { return this.Dbconn.IsConnected(); } /// <summary> /// 斷開鏈接 /// </summary> /// <returns> /// True:成功,False:失敗 /// </returns> public bool DisConn() { if (this.Dbconn.IsConnected()) { try { this.Dbconn.Disconnect(); Common.StopAPI(); return true; } catch (Exception) { return false; } } return true; } #endregion #region GetAllTags() /// <summary> /// 獲取全部節點 /// </summary> public List<PsPointInfo> GetAllTags() { if (this.RootNode == null) throw new Exception("根節點未實例化"); TagVector tagList = this.RootNode.GetSubTags(true);//從根節點獲取全部子節點 List<PsPointInfo> pointList = new List<PsPointInfo>(); for (int i = 0; i < tagList.Count; i++) { TagNode node = tagList[i] as TagNode; if (node == null) continue; PsPointInfo info = new PsPointInfo() { TagId = node.TagId, ParentTagId = (uint)node.Properties["ParentId"], Unit = node.Properties.ContainsKey("EngineeringUnit") ? node.Properties["EngineeringUnit"].ToString() : "", Name = node.TagName, LongName = node.TagLongName, Desc = node.Properties["Description"].ToString(), IsNode = (bool)node.Properties["IsNode"] }; pointList.Add(info); } return pointList; } /// <summary> /// 獲取全部測點 /// </summary> /// <param name="parentTagLongName">上級節點."0"或""表示根節點</param> /// <param name="allLevel"></param> /// <returns></returns> public TagVector GetSubTags(string parentTagLongName, bool allLevel) { if (this.RootNode == null) throw new Exception("根節點未實例化"); TagVector tagList = null; if (parentTagLongName == "0" || parentTagLongName == "") tagList = this.RootNode.GetSubTags(allLevel);//從根節點獲取全部子節點 else { TagNode parentTag = this.RootNode.SelectSingleNode(parentTagLongName + "@LongName") as TagNode; if (parentTag != null) tagList = parentTag.GetSubTags(allLevel);//從根節點獲取全部子節點 } return tagList; } /// <summary> /// 根據測點標識獲取測點 /// </summary> /// <param name="tagIdsLongList">測點標識列表, 測點要大於uint.MinValu,小於 uint.MaxValue</param> /// <returns></returns> public TagVector GetTags(List<long> tagIdsLongList) { List<uint> tagIdsIntList = new List<uint>(); foreach (long tagIds in tagIdsLongList) { if (tagIds >= uint.MinValue && tagIds <= uint.MaxValue) { tagIdsIntList.Add((uint)tagIds); } } List<PropField> list = new List<PropField>(); TagManager tmg = new TagManager(this.TagTree);//測點管理器 return tmg.GetTagListById(tagIdsIntList, list); } /// <summary> /// 獲取節點列表 /// </summary> /// <param name="tagLongName"></param> /// <returns></returns> public List<ITag> GetTags(List<string> tagLongName) { List<ITag> list = new List<ITag>(); foreach (string tagName in tagLongName) { ITag tag = this.RootNode.SelectSingleNode(tagName + "@LongName"); if (tag != null) list.Add(tag); } return list; } /// <summary> /// 根據測點標識獲取測點 /// </summary> /// <param name="tagId"></param> /// <returns></returns> public ITag GetTag(uint tagId) { TagManager tmg = new TagManager(this.TagTree);//測點管理器 List<PropField> list = new List<PropField>(); PropField aa = new PropField(); // AnalogTagElement tag=this.TagTree.CreateTag<AnalogTagElement>(""); AnalogTagElement tag = tmg.GetTagById(tagId, list) as AnalogTagElement; AnalogTagElement tag2 = tmg.GetTagById(tagId, tag.TagType.PropFields) as AnalogTagElement; return tag; } /// <summary> /// 判斷節點是否存在 /// </summary> /// <param name="tagLongName">節點長名</param> /// <returns></returns> public bool ExistTagNode(string tagLongName) { return this.RootNode.SelectSingleNode(tagLongName + "@LongName") is TagNode; } /// <summary> /// 判斷節點是否存在 /// </summary> /// <param name="tagLongName">節點長名</param> /// <returns></returns> public bool FindNode(string tagLongName) { ITag tag = this.RootNode.SelectSingleNode(tagLongName + "@LongName"); //AnalogTagElement aa = tag as AnalogTagElement; DigitalTagElement aa = tag as DigitalTagElement; StringBuilder sb = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for (int i = 0; i < aa.TagType.PropFields.Count; i++) { pSpaceCTLNET.PropField pro = aa.TagType.PropFields[i]; sb.AppendFormat("PropId:{0} PropDesc:{1} PropName:{2} ReadOnly:{3} DataType:{4}", pro.PropId, pro.PropDesc, pro.PropName, pro.ReadOnly, pro.DataType); sb.AppendLine(); } for (int i = 0; i < aa.TagType.PropFields.Count; i++) { pSpaceCTLNET.PropField pro = aa.TagType.PropFields[i]; // sb2.AppendFormat("dic.Add(\"{0}\", this.{0}); //{1}",pro.PropName,pro.DataType); sb2.AppendLine(" /// <summary>"); sb2.AppendFormat(" /// {0}", pro.PropDesc); sb2.AppendLine(); sb2.AppendLine(" /// </summary>"); sb2.AppendFormat(" public {0} {1};", pro.DataType, pro.PropName); sb2.AppendLine(); sb2.AppendLine(); } string str2 = sb2.ToString(); string str = sb.ToString(); return true; } #endregion #region Ps6服務器數據點操做 #region 添加根節點 /// <summary> /// 建立或查詢節點 /// 若是 /// </summary> /// <param name="parentTagID">父節點長名,爲空時建立根節點</param> /// <param name="tagName">長名</param> /// <param name="desc">長名描述</param> /// <returns>0:建立失敗 大於0:建立成功,爲TagId</returns> public TagNode CreateOrFindTagNode(uint parentTagID, string tagName, string desc) { try { TagNode tagNode = null; if (parentTagID == 0) { tagNode = this.RootNode.SelectSingleNode(tagName + "@LongName") as TagNode; if (tagNode != null) return tagNode; tagNode = this.TagTree.CreateTag<TagNode>(tagName); tagNode.Properties["Description"] = desc; if (this.RootNode.AppendChild(tagNode) == 0) return tagNode; else return null; } else { TagManager tmg = new TagManager(this.TagTree);//測點管理器 List<PropField> list = new List<PropField>(); TagNode parentTag = tmg.GetTagById(parentTagID, list) as TagNode; if (parentTag == null) throw new ArgumentException("父節點不存在", parentTagID.ToString()); tagNode = this.TagTree.CreateTag<TagNode>(tagName); tagNode.Properties["Description"] = desc; if (tmg.AddTag(parentTag, tagNode) == 0) return tagNode; else return null; } } catch (Exception ex) { throw new Exception("CreateOrFindTagNode Exception", ex); } } /// <summary> /// 建立數據點 /// </summary> /// <param name="parentTagName"></param> /// <param name="tagType"></param> /// <param name="tagName"></param> /// <param name="attributes"></param> /// <returns></returns> public ITag CreateTag(TagNode parentTag, string tagType, string tagName, Dictionary<string, object> attributes) { try { string longTagName = parentTag == null ? tagName : parentTag.TagLongName + "\\" + tagName; if (parentTag == null) parentTag = this.RootNode; longTagName = longTagName.Replace("/", ""); ITag newTag = null; ITag oldTag = null; if (tagType == "psAnalog") { oldTag = this.RootNode.SelectSingleNode(longTagName + "@LongName") as AnalogTagElement; if (oldTag == null) newTag = this.TagTree.CreateTag<AnalogTagElement>(tagName); } else if (tagType == "psDigital") { oldTag = this.RootNode.SelectSingleNode(longTagName + "@LongName") as DigitalTagElement; if (oldTag == null) newTag = this.TagTree.CreateTag<DigitalTagElement>(tagName); } else if (tagType == "psStringType") { oldTag = this.RootNode.SelectSingleNode(longTagName + "@LongName") as StringTagElement; if (oldTag == null) newTag = this.TagTree.CreateTag<StringTagElement>(tagName); } if (oldTag == null) { foreach (string key in attributes.Keys) { newTag[key] = attributes[key]; } TagManager tmg = new TagManager(this.TagTree); if (tmg.AddTag(parentTag, newTag) == 0) { newTag = tmg.GetTagById(newTag.TagId, newTag.TagType.PropFields); //添加成功後接着查詢 } } else { TagManager tmg = new TagManager(this.TagTree); newTag = tmg.GetTagById(oldTag.TagId, oldTag.TagType.PropFields); //添加成功後接着查詢 } return newTag; } catch (Exception ex) { throw new Exception("CreateTag Error:" + ex.Message); } } /// <summary> /// 設置測點屬性 /// </summary> /// <param name="parentTag"></param> /// <param name="tagType"></param> /// <param name="tagID"></param> /// <param name="tagName"></param> /// <param name="attributes"></param> /// <returns></returns> public ITag SetTagProperties(string tagType, uint tagID, string tagName, Dictionary<string, object> attributes) { try { TagManager tmg = this.TagTree.GetMgr(); ITag newTag = null; List<PropField> list = new List<PropField>(); if (tmg.IsTagExist(tagID)) { ITag tag = tmg.GetTagById(tagID, list); tag.TagName = tagName; if (tagType == "psAnalog") newTag = this.TagTree.CreateTag<AnalogTagElement>(tagName); else if (tagType == "psDigital") newTag = this.TagTree.CreateTag<DigitalTagElement>(tagName); else if (tagType == "psStringType") newTag = this.TagTree.CreateTag<StringTagElement>(tagName); foreach (string key in attributes.Keys) { tag[key] = attributes[key]; } tmg.SetTagProperties(tag, newTag.TagType.PropFields); newTag = tmg.GetTagById(tagID, newTag.TagType.PropFields); } return newTag; } catch (Exception ex) { throw new Exception("設置測點屬性失敗:" + ex.Message); } } /// <summary> /// 刪除測點 /// </summary> /// <param name="tagID">測點標識</param> /// <returns>成功時返回0,失敗時返回-1</returns> public int RemoveTag(uint tagID) { try { TagManager tmg = this.TagTree.GetMgr(); List<PropField> list = new List<PropField>(); if (tmg.IsTagExist(tagID)) { ITag tag = tmg.GetTagById(tagID, list); return tmg.RemoveTag(tag); } return -1; } catch (Exception ex) { throw new Exception("CreateTag Error:" + ex.Message); } } #endregion #region 添加模擬量測點 #endregion #region 添加開關量測點 /// <summary> /// 向服務器添加一個開關量測點 /// </summary> /// <param name="psTagPointInfo">測點參數類</param> /// <returns></returns> public bool AddDigitalTag(PsTagPointInfo psTagPointInfo) { DigitalTagElement digitalTag = new DigitalTagElement(); TagNode test_Tag = this.RootNode.SelectSingleNode(psTagPointInfo.ParentTagName + "@LongName") as TagNode; if (test_Tag == null) throw new Exception("根節點不存在"); try { // 建立一個開關量測點 digitalTag = this.TagTree.CreateTag<DigitalTagElement>(psTagPointInfo.TagName); digitalTag["His_IsSave"] = psTagPointInfo.His_IsSave;//是否保存歷史 digitalTag["EnableAlarm"] = psTagPointInfo.EnableAlarm;//是否報警 } catch (DbException ex) { throw new Exception("建立模擬量測點失敗!" + ex.Message); } TagManager tmg = new TagManager(this.TagTree);//測點管理器 int result = tmg.AddTag(test_Tag, digitalTag);//向服務器添加一個測點 if (result == 0) return true; else if (result == -1) throw new Exception("測點已存在!"); return true; } #endregion #region 根據指定測點,刪除節點及其子節點 /// <summary> /// 根據指定測點,刪除節點及其子節點 /// </summary> /// <param name="tagName"></param> /// <returns></returns> public bool DeleteTagPoint(string tagName) { TagNode test_Tag = this.RootNode.SelectSingleNode(tagName + "@LongName") as TagNode; if (test_Tag == null) throw new Exception("根節點不存在"); int result = test_Tag.RemoveTag(test_Tag); //從服務器刪除指定測點或節點,當前刪除根節點及其子節點 if (result == 0) return true; else throw new Exception("刪除測點失敗!"); } #endregion #region 修改測點屬性 /// <summary> /// 修改測點的屬性 /// </summary> /// <param name="psTagPointInfo">測點信息</param> /// <returns></returns> public bool updateTagProperties(PsTagPointInfo psTagPointInfo) { AnalogTagElement analogTag = new AnalogTagElement(); TagNode test_Tag = this.RootNode.SelectSingleNode(psTagPointInfo.ParentTagName + "@LongName") as TagNode; if (test_Tag == null) throw new Exception("根節點不存在"); test_Tag.Properties["PV_Quality"] = QUALITY_MASK.GOOD;//質量戳 192 =Good test_Tag.Properties["His_IsSave"] = psTagPointInfo.His_IsSave;//是否保存歷史 test_Tag.Properties["His_CompressMode"] = psTagPointInfo.His_CompressMode; // 旋轉門壓縮 test_Tag.Properties["His_IsCompressRatePercentage"] = psTagPointInfo.His_IsCompressRatePercentage; // 是否使用百分比 test_Tag.Properties["His_CompressRate"] = psTagPointInfo.His_CompressRate; // 壓縮率爲10% test_Tag.Properties["EnableAlarm"] = psTagPointInfo.EnableAlarm;//是否報警 test_Tag.Properties["RangeMinimum"] = psTagPointInfo.RangeMinimum; // 量程下限 test_Tag.Properties["RangeMaximum"] = psTagPointInfo.RangeMaximum; // 量程上限 test_Tag.Properties["Desc"] = psTagPointInfo.Desc; //描述 //更新測點全部屬性 int t = test_Tag.Update(); if (t == 0) return true; else return false; } #endregion #region 根據點長名獲取實時值 /// <summary> /// 根據點長名獲取實時值 /// </summary> /// <param name="longTagName">點長名</param> /// <returns></returns> public PsPointValue GetDataSnapshot(string longTagName) { ITag tag = this.RootNode.SelectSingleNode(longTagName + "@LongName") as ITag; if (tag == null) return null; RealHisData data = DataIO.Snapshot(this.Dbconn, tag as ITagElement); return new PsPointValue() { Time = data.TimeStamp, Value = (short)(char)data.Value }; } /// <summary> /// 根據點長名獲取實時值 /// </summary> /// <param name="longTagName">點長名</param> /// <returns></returns> public List<PsPointValue> GetDataSnapshot(string[] tagLongNameArray) { List<PsPointValue> psPointValueList = new List<PsPointValue>(); TagVector vector = new TagVector(); for (int i = 0; i < tagLongNameArray.Length; i++) { ITag tag = this.RootNode.SelectSingleNode(tagLongNameArray[i] + "@LongName") as ITag; if (tag == null) throw new ArgumentException(string.Format("測點{0}不存在", tagLongNameArray[i])); vector.Add(tag); } RealDataSet ds = new RealDataSet(); BatchResults data = DataIO.Snapshot(this.Dbconn, vector, ds); for (int i = 0; i < ds.Count; i++) { psPointValueList.Add(new PsPointValue() { TagName = tagLongNameArray[i], Time = ds[i].TimeStamp, Value = ds[i].Value.ToString() }); } return psPointValueList; } #endregion #region 根據點長名獲取歷史值 /// <summary> /// 讀歷史值 返回String類型值 /// </summary> public string ReadRawData(string longTagName, DateTime StartTime, DateTime EndTime) { DateTime[] times = new DateTime[] { StartTime, EndTime }; string result = ""; ITag tag = this.RootNode.SelectSingleNode(longTagName + "@LongName") as ITag; if (tag != null) { //添加模擬點 TagVector tagVector = new TagVector(); //模擬測點集合 tagVector.Add(tag); HisDataSet hisDataSet = new HisDataSet(); //批量操做的結果集類 BatchResults batchResults = DataIO.ReadRaw(this.Dbconn, tagVector, StartTime, EndTime, hisDataSet); //插入歷史數據 int totalCount = 0; foreach (TagHisValues val in hisDataSet) { for (int i = 0; i < val.Data.Count; i++) { string timeStamp = val.Data[i].TimeStamp.ToString(); object value = val.Data[i].Value != null ? val.Data[i].Value : "0"; result += "TimeStamp:" + timeStamp + ",Value:" + value + ";"; } //計數 totalCount += val.Data.Count; } result = string.Format("結果:{0}, 共{1}條記錄,用時:{2}秒,歷史值:{3}", !batchResults.HasErrors, totalCount, (EndTime - StartTime).TotalMilliseconds.ToString(), result); } return result; } /// <summary> /// 獲取歷史值 返回Dictionary /// </summary> /// <param name="longTagName"></param> /// <param name="StartTime"></param> /// <param name="EndTime"></param> /// <returns></returns> public Dictionary<DateTime, string> ReadRawData1(string longTagName, DateTime StartTime, DateTime EndTime) { Dictionary<DateTime, string> dic = new Dictionary<DateTime, string>(); DateTime[] times = new DateTime[] { StartTime, EndTime }; string result = ""; ITag tag = this.RootNode.SelectSingleNode(longTagName + "@LongName") as ITag; if (tag != null) { //添加模擬點 TagVector tagVector = new TagVector(); //模擬測點集合 tagVector.Add(tag); HisDataSet hisDataSet = new HisDataSet(); //批量操做的結果集類 BatchResults batchResults = DataIO.ReadRaw(this.Dbconn, tagVector, StartTime, EndTime, hisDataSet); //插入歷史數據 int totalCount = 0; foreach (TagHisValues val in hisDataSet) { for (int i = 0; i < val.Data.Count; i++) { DateTime timeStamp = val.Data[i].TimeStamp; object value = val.Data[i].Value != null ? val.Data[i].Value : "0"; string value1 = Convert.ToString(value); result += "TimeStamp:" + timeStamp + ",Value:" + value + ";"; dic.Add(timeStamp, value.ToString()); } //計數 totalCount += val.Data.Count; } result = string.Format("結果:{0}, 共{1}條記錄,用時:{2}秒,歷史值:{3}", !batchResults.HasErrors, totalCount, (EndTime - StartTime).TotalMilliseconds.ToString(), result); } return dic; } #endregion #endregion #region 報警事件管理 #region 獲取實時報警數據 /// <summary> /// 獲取實時報警數據 /// </summary> /// <param name="tagName"></param> /// <returns></returns> public AlarmDataSet GetRealAlarmData(string tagId, string alarmTopic, string alarmLevel, bool isNoAck) { //實例化報警類 DbAlarmIO dbAlarmIo = new DbAlarmIO(); //實例化報警過濾器 DbAlarmFilter alarmfilters = new DbAlarmFilter(); AlarmDataSet alarmDataSet = new AlarmDataSet(); uint uTagId = 0; if (uint.TryParse(tagId, out uTagId)) alarmfilters.TagId = uTagId; uint uAlarmTopic = 0; if (uint.TryParse(alarmTopic, out uAlarmTopic)) alarmfilters.AlarmTopic = (DbAlarmTopicEnum)uAlarmTopic; byte bAlarmLevel = 0; if (byte.TryParse(alarmLevel, out bAlarmLevel)) { alarmfilters.AlarmLowLevel = bAlarmLevel; alarmfilters.AlarmHighLevel = bAlarmLevel; } if (isNoAck) alarmfilters.bAlarmHaveAcked = false; //填充報警集合 dbAlarmIo.Alarm_Real_Query(this.Dbconn, alarmfilters, alarmDataSet); return alarmDataSet; } /// <summary> /// 獲取實時報警數據 /// </summary> /// <param name="tagName"></param> /// <returns></returns> public AlarmDataSet GetRealAlarmData() { //實例化報警類 DbAlarmIO dbAlarmIo = new DbAlarmIO(); //實例化報警過濾器 DbAlarmFilter alarmfilters = new DbAlarmFilter(); AlarmDataSet alarmDataSet = new AlarmDataSet(); //填充報警集合 dbAlarmIo.Alarm_Real_Query(this.Dbconn, alarmfilters, alarmDataSet); return alarmDataSet; } #endregion #region 獲取歷史報警數據 /// <summary> /// 獲取歷史報警數據 /// </summary> /// <param name="longTagName">點長名</param> /// <param name="startTime">開始時間</param> /// <param name="endTime">結束時間</param> /// <returns></returns> public AlarmDataSet GetHisAlarmData(DateTime startTime, DateTime endTime, string tagId, string alarmTopic, string alarmLevel, bool isNoAck) { //實例化報警類 DbAlarmIO dbAlarmIo = new DbAlarmIO(); //實例化報警過濾器 DbAlarmFilter alarmfilters = new DbAlarmFilter(); AlarmDataSet alarmDataSet = new AlarmDataSet(); uint uTagId = 0; if (uint.TryParse(tagId, out uTagId)) alarmfilters.TagId = uTagId; uint uAlarmTopic = 0; if (uint.TryParse(alarmTopic, out uAlarmTopic)) alarmfilters.AlarmTopic = (DbAlarmTopicEnum)uAlarmTopic; byte bAlarmLevel = 0; if (byte.TryParse(alarmLevel, out bAlarmLevel)) { alarmfilters.AlarmLowLevel = bAlarmLevel; alarmfilters.AlarmHighLevel = bAlarmLevel; } if (isNoAck) alarmfilters.bAlarmHaveAcked = false; //填充報警集合 dbAlarmIo.Alarm_His_Query(this.Dbconn, alarmfilters, startTime, endTime, alarmDataSet); return alarmDataSet; } #endregion #region 報警實時訂閱 /// <summary> /// 實時報警訂閱 /// </summary> public string RealSubScriber() { //初始化事件結束的通知 AutoResetEvent realSubscriber_evt = new AutoResetEvent(false); //實時數據訂閱器類 RealSubscriber realSubscriber = new RealSubscriber(); //將方法加入線程 ThreadPool.QueueUserWorkItem(new WaitCallback(TestRealView), realSubscriber_evt); return "123"; } /// <summary> /// 實時報警訂閱 /// </summary> /// <param name="state"></param> public void TestRealView(object state) { } /// <summary> /// 實時報警訂閱 /// </summary> /// <param name="state"></param> public void TestRealView2(object state, string l) { //實時報警變動訂閱器類 RealSubscriber realSubScriber = new RealSubscriber(); //測點樹 TagTree tagTree = TagTree.CreateInstance(this.Dbconn); //得到根節點 TagNode root = tagTree.GetTreeRoot(); //點表 TagVector vector = new TagVector(); //向點表添加測點 vector.Add(root); //實時數據集 RealDataSet realData = new RealDataSet(); //根據點表建立訂閱器 realSubScriber.CreateAndRead(this.Dbconn, vector, realData); //建立檢測測點是否變化的事件 realSubScriber.RealDataChanged += new RealDataChangedEventHandler(realSubscriber_RealDataChanged); //測點有變化時通知 realSubScriber.Enabled = true; //通知等待線程 AutoResetEvent evt = (AutoResetEvent)state; //等待接收信號 evt.WaitOne(); //關閉訂閱器 realSubScriber.Close(); } #endregion #endregion #region 實時數據訂閱 /// <summary> /// 啓動實時數據的訂閱 /// </summary> /// <param name="tagLongNameList"></param> public void StartRealDataSubScriber(List<string> tagLongNameList) { //點表 TagVector vector = new TagVector(); List<ITag> list = this.GetTags(tagLongNameList); list.ForEach(q => vector.Add(q)); //實時數據集 RealDataSet realData = new RealDataSet(); //根據點表建立訂閱器 this.RealDataSubScriber.CreateAndRead(this.Dbconn, vector, realData); //建立檢測測點是否變化的事件 this.RealDataSubScriber.RealDataChanged += new RealDataChangedEventHandler(realSubscriber_RealDataChanged); } /// <summary> /// 中止實時數據的訂閱 /// </summary> public void EndRealDataSubScriber() { this.RealDataSubScriber.RealDataChanged -= realSubscriber_RealDataChanged; this.RealDataSubScriber.Close(); } /// <summary> /// 訂閱報警事件 /// </summary> /// <param name="sender"></param> /// <param name="dataset"></param> public void realSubscriber_RealDataChanged(object sender, RealDataSet dataset) { StringBuilder sb = new StringBuilder(); foreach (TagValue val in dataset) { sb.Append(val.TagId); sb.Append(":("); sb.Append(val.Value.ToString()); sb.Append(","); sb.Append(val.TimeStamp.ToString()); sb.Append(","); sb.Append(val.QualityStamp.ToString()); sb.Append(")|"); } //String.Format("檢測到 {0}條實時值變化。{1}", dataset.Count, sb.ToString()); } #endregion #region IDisposable 成員 public void Dispose() { throw new NotImplementedException(); } #endregion }
若是是winform程序使用iocomp控件庫實現組態界面比較方便。web程序中也能夠使用html 5 去作,但電力、工業上使用svg的方案比較多。ajax