-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistDA.cs
217 lines (184 loc) · 7.15 KB
/
HistDA.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Historian = Proficy.Historian.ClientAccess.API; // v7.1
namespace CopyTagsRawDataFromGEProficyToOsisoftPI
{
class HistDA
{
internal class TagData
{
public string Value { get; set; }
public string Quality { get; set; }
public DateTime TimeStamp { get; set; }
}
private const string iHistSrv = "WHBC-DB-IH";
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 DataTable QueryRawTagValues(DateTime queryStart, string tagName, out string dataType)
{
dataType = string.Empty;
Connect();
if (!IsConnected)
Connect();
Historian.TagQueryParams queryTags = new Historian.TagQueryParams { PageSize = 500 };
Historian.ItemErrors itemErrors = new Historian.ItemErrors();
Historian.DataSet dataSet = new Historian.DataSet();
List<Historian.Tag> histTagData = new List<Historian.Tag>();
List<Historian.Tag> tempTagData;
queryTags.Categories = Historian.Tag.Categories.Engineering;
//Historian.DataQueryParams queryValues =
// new Historian.RawByTimeQuery(queryStart, tagName)
// { Fields = Historian.DataFields.Time | Historian.DataFields.Value | Historian.DataFields.Quality, PageSize = 500 };
Historian.DataQueryParams queryValues =
new Historian.RawByNumberQuery(queryStart, 500000, tagName)
{ Fields = Historian.DataFields.Time | Historian.DataFields.Value | Historian.DataFields.Quality, PageSize = 500 };
try
{
while (sc.ITags.Query(ref queryTags, out tempTagData))
histTagData.AddRange(tempTagData);
histTagData.AddRange(tempTagData);
switch (histTagData[0].DataType)
{
case Historian.Tag.NativeDataType.VariableString:
dataType = "string";
break;
case Historian.Tag.NativeDataType.FixedString:
dataType = "string";
break;
case Historian.Tag.NativeDataType.Bool:
dataType = "bool";
break;
case Historian.Tag.NativeDataType.Integer:
dataType = "int";
break;
case Historian.Tag.NativeDataType.DoubleInteger:
dataType = "int";
break;
case Historian.Tag.NativeDataType.QuadInteger:
dataType = "int";
break;
case Historian.Tag.NativeDataType.DoubleFloat:
dataType = "float";
break;
case Historian.Tag.NativeDataType.Float:
dataType = "float";
break;
default:
dataType = "na";
break;
}
dataSet.Clear();
Historian.DataSet finalSet = new Historian.DataSet();
while (sc.IData.Query(ref queryValues, out dataSet, out _))
finalSet.AddRange(dataSet);
finalSet.AddRange(dataSet);
var tagData = ProcessHistValues(finalSet, tagName, dataType);
return Extensions.ConvertToDataTable(tagData);
}
catch(Exception e)
{
Console.WriteLine("Error occurred during query: " + e.Message);
return null;
}
finally
{
Disconnect();
}
}
public List<TagData> ProcessHistValues(Historian.DataSet finalSet, string tagName, string dataType)
{
var tagData = new List<TagData>();
switch (dataType)
{
case "float":
for (int i = 0; i < finalSet[tagName].Count(); i++)
{
var data = new TagData
{
Value = finalSet[tagName].GetValue(i) == null ? string.Empty : Convert.ToDecimal(finalSet[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 = finalSet[tagName].GetQuality(i).ToString().ToUpper(),
TimeStamp = finalSet[tagName].GetTime(i)
};
tagData.Add(data);
}
break;
default:
for (int i = 0; i < finalSet[tagName].Count(); i++)
{
var data = new TagData
{
Value = finalSet[tagName].GetValue(i) == null ? string.Empty : Convert.ToString(finalSet[tagName].GetValue(i)),
Quality = finalSet[tagName].GetQuality(i).ToString(),
TimeStamp = finalSet[tagName].GetTime(i)
};
tagData.Add(data);
}
break;
}
return tagData;
}
public void Connect()
{
if (sc == null)
{
sc = new Historian.ServerConnection(new Historian.ConnectionProperties
{
ServerHostName = IHistSrv,
OpenTimeout = new TimeSpan(0, 0, 10),
ReceiveTimeout = new TimeSpan(0, 5, 0),
ServerCertificateValidationMode = Historian.CertificateValidationMode.None
});
}
if (!IsConnected)
{
try
{
sc.Connect();
}
catch (Exception ex)
{
Console.WriteLine("Error at connecting to Historian server: " + ex.Message);
}
}
}
public void Disconnect()
{
if (IsConnected)
{
try
{
sc.Disconnect();
}
catch (Exception ex)
{
Console.WriteLine("Error at disconnecting from Historian server: " + ex.Message);
}
}
Dispose();
}
private void Dispose()
{
if (sc != null)
{
((IDisposable)sc).Dispose();
}
}
}
}