-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistorianDA.cs
222 lines (180 loc) · 7.55 KB
/
HistorianDA.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.Linq;
//using MESBadTagChecker.Code.Models;
using Historian = Proficy.Historian.ClientAccess.API;
namespace MESBadTagChecker.Code
{
//from "Models"
public class TagDataModel
{
public string TagName { get; set; }
public string Quality { get; set; }
public string TimeStamp { get; set; }
public string Desc { get; set; }
public string EGU { get; set; }
public string LoEGU { get; set; }
public string HiEGU { get; set; }
public string Value { get; set; }
}
//
public class HistorianDA
{
private const string IHistSrv = "yourhistorianservername";
Historian.ServerConnection sc;
private bool IsConnected
{
get { return sc.IsConnected(); }
}
public List<TagDataModel> GetBadTags(string[] plants)
{
var badTagDatas = new List<TagDataModel>();
try
{
Connect();
if (!IsConnected)
Connect();
foreach (string plant in plants) //for the tagname mask
{
Historian.TagQueryParams queryTags = new Historian.TagQueryParams { PageSize = 500 }; // PageSize is the batch size of the while loop below, not recommended to set higher than 500
Historian.ItemErrors itemErrors = new Historian.ItemErrors();
Historian.DataSet dataSet = new Historian.DataSet();
List<Historian.Tag> tagDatas = new List<Historian.Tag>();
List<Historian.Tag> tempTagDatas;
queryTags.Criteria.TagnameMask = $"BC.{plant}*"; //tagname mask
queryTags.Criteria.CollectionDisabled = false;
queryTags.Categories = Historian.Tag.Categories.Basic;
while (sc.ITags.Query(ref queryTags, out tempTagDatas))
tagDatas.AddRange(tempTagDatas);
tagDatas.AddRange(tempTagDatas);
for (int i = 0; i < tagDatas.Count; i++)
{
var badTagData = new TagDataModel
{
TagName = tagDatas[i].Name,
Desc = tagDatas[i].Description
};
badTagDatas.Add(badTagData);
}
queryTags = new Historian.TagQueryParams { PageSize = 500 };
queryTags.Criteria.TagnameMask = $"BC.{plant}*";
queryTags.Criteria.CollectionDisabled = false;
queryTags.Categories = Historian.Tag.Categories.Engineering; //for engineering units and limits
tagDatas.Clear();
tempTagDatas.Clear();
string[] tagNames = badTagDatas.AsEnumerable().Select(r => r.TagName).ToArray(); //get only tagnames
while (sc.ITags.Query(ref queryTags, out tempTagDatas))
tagDatas.AddRange(tempTagDatas);
tagDatas.AddRange(tempTagDatas);
for (int i = 0; i < tagDatas.Count; i++)
{
var obj = badTagDatas.FirstOrDefault(x => x.TagName == tagDatas[i].Name); //get object by tagname
if (obj != null)
{
obj.EGU = tagDatas[i].EngineeringUnits;
obj.LoEGU = tagDatas[i].LoEngineeringUnits.ToString();
obj.HiEGU = tagDatas[i].HiEngineeringUnits.ToString();
}
}
// query the latest values
Historian.DataQueryParams queryValueQuality = new Historian.CurrentValueQuery(tagNames) { Fields = Historian.DataFields.Value | Historian.DataFields.Quality | Historian.DataFields.Time };
sc.IData.Query(ref queryValueQuality, out dataSet, out itemErrors);
for (int i = 0; i < tagNames.Length; i++)
{
var obj = badTagDatas.FirstOrDefault(x => x.TagName == tagNames[i]);
if (obj != null)
{
obj.Value = dataSet[tagNames[i]].GetValue(0) != null ? dataSet[tagNames[i]].GetValue(0).ToString() : "-N/A-";
obj.Quality = dataSet[tagNames[i]].GetQuality(0).ToString();
obj.TimeStamp = dataSet[tagNames[i]].GetTime(0).ToString("yyyy.MM.dd. HH:mm:ss");
}
}
badTagDatas = badTagDatas.Where(x => x.Quality.Contains("Bad")).ToList(); // filter to get only BAD qulity tags
}
return badTagDatas;
}
catch (Exception ex)
{
Console.WriteLine("Tag query error: " + ex.Message);
throw;
}
finally
{
Disconnect();
}
}
// method to get the calculation for a tag (if exists)
public string GetCalculationScript(string tagName)
{
string calc = string.Empty;
try
{
Connect();
if (!IsConnected)
Connect();
Historian.TagQueryParams queryTags = new Historian.TagQueryParams { PageSize = 500 };
List<Historian.Tag> tagDatas = new List<Historian.Tag>();
List<Historian.Tag> tempTagDatas;
queryTags.Criteria.TagnameMask = tagName;
queryTags.Categories = Historian.Tag.Categories.All; // "All" to get the calculation field text
while (sc.ITags.Query(ref queryTags, out tempTagDatas))
tagDatas.AddRange(tempTagDatas);
tagDatas.AddRange(tempTagDatas);
for (int i = 0; i < tagDatas.Count; i++)
{
calc = tagDatas[i].Calculation.ToString();
}
return calc;
}
catch (Exception ex)
{
Console.WriteLine("Calculation query error: " + ex.Message);
throw;
}
finally
{
Disconnect();
}
}
private void Connect()
{
if (sc == null)
{
sc = new Historian.ServerConnection(new Historian.ConnectionProperties
{
ServerHostName = IHistSrv,
OpenTimeout = new TimeSpan(0, 0, 10),
ServerCertificateValidationMode = Historian.CertificateValidationMode.None
});
}
if (!sc.IsConnected())
try
{
sc.Connect();
}
catch (Exception ex)
{
Console.WriteLine("Error at connecting: " + ex.Message);
throw;
}
}
private void Disconnect()
{
Dispose();
if (sc.IsConnected())
try
{
sc.Disconnect();
}
catch (Exception ex)
{
Console.WriteLine("Error at disconnecting: " + ex.Message);
throw;
}
}
private void Dispose()
{
((IDisposable)sc).Dispose();
}
}
}