2using System.Collections.Generic;
4using System.Threading.Tasks;
15 private readonly XmlElement xml;
16 private readonly
string[] allowedValues;
17 private readonly
string name;
18 private readonly
string dataType;
19 private readonly
string defaultValue;
20 private readonly
string minimum;
21 private readonly
string maximum;
22 private readonly
string step;
23 private readonly
bool sendsEvents;
24 private readonly
bool hasAllowedValues =
false;
25 private readonly
bool hasAllowedValueRange =
false;
33 this.sendsEvents = (
Xml.HasAttribute(
"sendEvents") &&
Xml.GetAttribute(
"sendEvents") ==
"yes");
35 foreach (XmlNode N
in Xml.ChildNodes)
40 this.name = N.InnerText;
44 this.dataType = N.InnerText;
48 this.defaultValue = N.InnerText;
51 case "allowedValueList":
52 this.hasAllowedValues =
true;
53 foreach (XmlNode N2
in N.ChildNodes)
55 if (N2.LocalName ==
"allowedValue")
60 case "allowedValueRange":
61 this.hasAllowedValueRange =
true;
62 foreach (XmlNode N2
in N.ChildNodes)
67 this.minimum = N2.InnerText;
71 this.maximum = N2.InnerText;
75 this.step = N2.InnerText;
89 public XmlElement
Xml => this.xml;
94 public string Name => this.name;
139 public string Step => this.step;
148 switch (this.dataType)
162 return Value.ToString();
168 if (!(Value is
double d))
170 if (Value is
float f)
172 else if (Value is decimal dec)
175 d = Convert.ToDouble(Value);
178 return d.ToString().Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator,
".");
182 if (Value is
double d2)
184 else if (Value is
float f2)
186 else if (Value is decimal dec2)
189 d = Convert.ToDouble(Value);
191 return d.ToString(
"F4").Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator,
".");
194 if (!(Value is DateTime DT))
195 DT = Convert.ToDateTime(Value);
197 return DT.ToString(
"yyyyMMdd");
200 if (!(Value is DateTime DT2))
201 DT2 = Convert.ToDateTime(Value);
203 return DT2.ToString(
"yyyyMMddTHHmmss");
206 if (Value is DateTimeOffset DTO)
208 string s = DTO.ToString(
"yyyyMMddTHHmmss");
209 TimeSpan Zone = DTO.Offset;
211 if (Zone < TimeSpan.Zero)
220 return s + Zone.Hours.ToString(
"D2") +
":" + Zone.Minutes.ToString(
"D2");
224 if (!(Value is DateTime DT3))
225 DT3 = Convert.ToDateTime(Value);
227 return DT3.ToString(
"yyyyMMddTHHmmss");
231 if (Value is TimeSpan TS)
232 return TS.Hours.ToString(
"D2") +
":" + TS.Minutes.ToString(
"D2") +
":" + TS.Seconds.ToString(
"D2");
233 else if (Value is DateTime DT4)
234 return DT4.ToString(
"HH:mm:ss");
235 else if (TimeSpan.TryParse(Value.ToString(), out TS))
236 return TS.Hours.ToString(
"D2") +
":" + TS.Minutes.ToString(
"D2") +
":" + TS.Seconds.ToString(
"D2");
239 DT = Convert.ToDateTime(Value);
240 return DT.ToString(
"HH:mm:ss");
244 if (Value is TimeSpan TS2)
245 return TS2.Hours.ToString(
"D2") +
":" + TS2.Minutes.ToString(
"D2") +
":" + TS2.Seconds.ToString(
"D2");
246 else if (Value is DateTime DT5)
247 return DT5.ToString(
"HH:mm:ss");
248 else if (Value is DateTimeOffset DTO2)
250 string s = DTO2.ToString(
"HH:mm:ss");
251 TimeSpan Zone = DTO2.Offset;
253 if (Zone < TimeSpan.Zero)
262 return s + Zone.Hours.ToString(
"D2") +
":" + Zone.Minutes.ToString(
"D2");
264 else if (TimeSpan.TryParse(Value.ToString(), out TS))
265 return TS.Hours.ToString(
"D2") +
":" + TS.Minutes.ToString(
"D2") +
":" + TS.Seconds.ToString(
"D2");
268 DT = Convert.ToDateTime(Value);
269 return DT.ToString(
"HH:mm:ss");
273 if (!(Value is
bool b))
274 b = Convert.ToBoolean(Value);
276 return b ?
"1" :
"0";
279 if (!(Value is
byte[] Bin))
282 return Convert.ToBase64String(Bin);
285 if (!(Value is
byte[] Bin2))
288 StringBuilder sb =
new StringBuilder();
290 foreach (
byte b2
in Bin2)
291 sb.Append(b2.ToString(
"X2"));
293 return sb.ToString();
304 switch (this.dataType)
307 return byte.Parse(Value);
310 return ushort.Parse(Value);
313 return uint.Parse(Value);
316 return sbyte.Parse(Value);
319 return short.Parse(Value);
322 return int.Parse(Value);
325 return long.Parse(Value);
334 return new Uri(Value);
337 return new Guid(Value);
340 return float.Parse(Value.Replace(
".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator));
346 return double.Parse(Value.Replace(
".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator));
349 return DateTime.Parse(Value);
352 return DateTime.Parse(Value.Replace(
"T",
" "));
355 return DateTimeOffset.Parse(Value.Replace(
"T",
" "));
359 return TimeSpan.Parse(Value.Replace(
"T",
" "));
362 Value = Value.ToLower();
363 return (Value ==
"1" || Value ==
"true" || Value ==
"yes");
366 return Convert.FromBase64String(Value);
369 int i, c = Value.Length;
371 throw new Exception(
"Invalid bin.hex value");
373 byte[] Bin =
new byte[c / 2];
378 for (i = j = 0; i < c;)
382 if (ch >=
'0' && ch <=
'9')
383 b = (byte)(ch -
'0');
384 else if (ch >=
'A' && ch <=
'F')
385 b = (byte)(ch -
'A' + 10);
386 else if (ch >=
'a' && ch <=
'f')
387 b = (byte)(ch -
'a' + 10);
389 throw new Exception(
"Invalid bin.hex value");
394 if (ch >=
'0' && ch <=
'9')
395 b |= (byte)(ch -
'0');
396 else if (ch >=
'A' && ch <=
'F')
397 b |= (byte)(ch -
'A' + 10);
398 else if (ch >=
'a' && ch <=
'f')
399 b |= (byte)(ch -
'a' + 10);
401 throw new Exception(
"Invalid bin.hex value");
420 if (Value is
byte[] Bin)
Static class managing encoding and decoding of internet content.
static Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Contains information about a state variable.
string Name
State Variable Name
string DefaultValue
Default Value
static async Task< byte[]> SerializeToBinary(object Value)
Serializes an object to an array of bytes.
string Minimum
Smallest value allowed. Provided if HasAllowedValueRange is true.
bool HasAllowedValueRange
If Minimum, Maximum and Step defines a range of allowed values.
object XmlStringToValue(string Value)
Converts a value from its XML string representation to an object value with the correct type.
string Maximum
Largest value allowed. Provided if HasAllowedValueRange is true.
XmlElement Xml
Underlying XML definition.
bool HasAllowedValues
If AllowedValues contains a list of allowed values.
async Task< string > ValueToXmlString(object Value)
Converts a value to its XML string representation.
bool SendsEvents
If state variable sends events.
string Step
Step value. Provided if HasAllowedValueRange is true.
string[] AllowedValues
List of allowed values. Provided if HasAllowedValues is true.