-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHistorianDA.cs
197 lines (160 loc) · 6.11 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Historian = Proficy.Historian.ClientAccess.API; // v7.1
namespace ExportHistorianTagDataToCSV
{
class HistorianDA
{
internal class TagData
{
public string TagName { get; set; }
public string Value { get; set; }
public string Quality { get; set; }
public string TimeStamp { get; set; }
}
public struct TagSpecifics
{
public string Tagname;
public string[] DependencyTagnames;
public delegate double Formula(double[] formulaMembers);
public Formula CalcFormula;
}
private const string iHistSrv = "yourHistServer";
public string IHistSrv
{
get
{
return iHistSrv;
}
//TODO: setter
}
//variable to hold the Historian server connection
Historian.ServerConnection sc;
public bool IsConnected
{
get { return sc != null && sc.IsConnected(); }
}
public HashSet<string> QueryFilteredTags(string tagFilter)
{
if (sc == null)
{
MessageBox.Show("First, establish the connection with the Historian server");
return null;
}
else
{
Connect();
if (!IsConnected)
Connect();
try
{
Historian.TagQueryParams queryTags = new Historian.TagQueryParams { PageSize = 100 }; // 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> filteredTags = new List<Historian.Tag>();
List<Historian.Tag> tempTags;
queryTags.Criteria.TagnameMask = tagFilter; //filtering tags
//queryTags.Criteria.DataType = Historian.Tag.NativeDataType.VariableString; //
//execute the query and populate the list datatype
while (sc.ITags.Query(ref queryTags, out tempTags))
filteredTags.AddRange(tempTags);
filteredTags.AddRange(tempTags);
//impossible to have two tags with the same name, but... using distinct anyway...
return new HashSet<string>(filteredTags.Select(e => e.Name).Distinct().ToList());
}
catch (Exception ex)
{
MessageBox.Show("Tag query error: " + ex.Message);
return null;
}
}
}
public DataTable QueryTagInterpolatedValues(DateTime queryStart, DateTime queryEnd, uint samples, string tagName)
{
//int totalSamples = 0;
Connect();
if (!IsConnected)
Connect();
Historian.DataQueryParams query =
new Historian.InterpolatedQuery(queryStart, queryEnd, samples, tagName) { Fields = Historian.DataFields.Time | Historian.DataFields.Value | Historian.DataFields.Quality };
Historian.DataSet set = new Historian.DataSet();
query.Criteria.SamplingMode = Historian.DataCriteria.SamplingModeType.Interpolated;
sc.IData.Query(
ref query,
out set,
out _);
//totalSamples += set.TotalSamples;
var tagData = new List<TagData>();
for (int i = 0; i < set[tagName].Count(); i++)
{
var data = new TagData
{
TagName = tagName,
Value = set[tagName].GetValue(i) == null ? string.Empty : Convert.ToDecimal(set[tagName].GetValue(i)).ToString("N", CultureInfo.GetCultureInfo("hu-HU")), //the decimal separator is dot in the US or UK, etc. (decimal point), comma in Hungary, Germany, etc.
Quality = set[tagName].GetQuality(i).ToString(),
TimeStamp = set[tagName].GetTime(i).ToString("yyyy.MM.dd. HH:mm:ss")
};
tagData.Add(data);
}
return Extensions.ConvertToDataTable(tagData);
}
// TODO:
public DataTable QueryTagInterpolatedValues(DateTime queryStart, DateTime queryEnd, int samples, int queryType, params TagSpecifics[] tags)
{
// TODO: InterpolatedQuery, CalculatedQuery, RawByTimeQuery
//int totalSamples = 0;
return null;
}
public 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 (!IsConnected)
{
try
{
sc.Connect();
}
catch (Exception ex)
{
MessageBox.Show("Error at connecting to Historian server: " + ex.Message);
}
}
}
public void Disconnect()
{
if (IsConnected)
{
try
{
sc.Disconnect();
}
catch (Exception ex)
{
MessageBox.Show("Error at disconnecting from Historian server: " + ex.Message);
}
}
Dispose();
}
private void Dispose()
{
if (sc != null)
{
((IDisposable)sc).Dispose();
}
}
}
}