3using System.Globalization;
5using System.Threading.Tasks;
16 private readonly XmlElement xml;
17 private readonly
string[] allowedValues;
18 private readonly
string name;
19 private readonly
string dataType;
20 private readonly
string defaultValue;
21 private readonly
string minimum;
22 private readonly
string maximum;
23 private readonly
string step;
24 private readonly
bool sendsEvents;
25 private readonly
bool hasAllowedValues =
false;
26 private readonly
bool hasAllowedValueRange =
false;
34 this.sendsEvents = (
Xml.HasAttribute(
"sendEvents") &&
Xml.GetAttribute(
"sendEvents") ==
"yes");
36 foreach (XmlNode N
in Xml.ChildNodes)
41 this.name = N.InnerText;
45 this.dataType = N.InnerText;
49 this.defaultValue = N.InnerText;
52 case "allowedValueList":
53 this.hasAllowedValues =
true;
54 foreach (XmlNode N2
in N.ChildNodes)
56 if (N2.LocalName ==
"allowedValue")
61 case "allowedValueRange":
62 this.hasAllowedValueRange =
true;
63 foreach (XmlNode N2
in N.ChildNodes)
68 this.minimum = N2.InnerText;
72 this.maximum = N2.InnerText;
76 this.step = N2.InnerText;
90 public XmlElement
Xml => this.xml;
95 public string Name => this.name;
140 public string Step => this.step;
149 switch (this.dataType)
163 return Value.ToString();
169 if (!(Value is
double d))
171 if (Value is
float f)
173 else if (Value is decimal dec)
176 d = Convert.ToDouble(Value);
179 return d.ToString(CultureInfo.InvariantCulture);
183 if (Value is
double d2)
185 else if (Value is
float f2)
187 else if (Value is decimal dec2)
190 d = Convert.ToDouble(Value);
192 return d.ToString(
"F4", CultureInfo.InvariantCulture);
195 if (!(Value is DateTime DT))
196 DT = Convert.ToDateTime(Value);
198 return DT.ToString(
"yyyyMMdd");
201 if (!(Value is DateTime DT2))
202 DT2 = Convert.ToDateTime(Value);
204 return DT2.ToString(
"yyyyMMddTHHmmss");
207 if (Value is DateTimeOffset DTO)
209 string s = DTO.ToString(
"yyyyMMddTHHmmss");
210 TimeSpan Zone = DTO.Offset;
212 if (Zone < TimeSpan.Zero)
221 return s + Zone.Hours.ToString(
"D2") +
":" + Zone.Minutes.ToString(
"D2");
225 if (!(Value is DateTime DT3))
226 DT3 = Convert.ToDateTime(Value);
228 return DT3.ToString(
"yyyyMMddTHHmmss");
232 if (Value is TimeSpan TS)
233 return TS.Hours.ToString(
"D2") +
":" + TS.Minutes.ToString(
"D2") +
":" + TS.Seconds.ToString(
"D2");
234 else if (Value is DateTime DT4)
235 return DT4.ToString(
"HH:mm:ss");
236 else if (TimeSpan.TryParse(Value.ToString(), out TS))
237 return TS.Hours.ToString(
"D2") +
":" + TS.Minutes.ToString(
"D2") +
":" + TS.Seconds.ToString(
"D2");
240 DT = Convert.ToDateTime(Value);
241 return DT.ToString(
"HH:mm:ss");
245 if (Value is TimeSpan TS2)
246 return TS2.Hours.ToString(
"D2") +
":" + TS2.Minutes.ToString(
"D2") +
":" + TS2.Seconds.ToString(
"D2");
247 else if (Value is DateTime DT5)
248 return DT5.ToString(
"HH:mm:ss");
249 else if (Value is DateTimeOffset DTO2)
251 string s = DTO2.ToString(
"HH:mm:ss");
252 TimeSpan Zone = DTO2.Offset;
254 if (Zone < TimeSpan.Zero)
263 return s + Zone.Hours.ToString(
"D2") +
":" + Zone.Minutes.ToString(
"D2");
265 else if (TimeSpan.TryParse(Value.ToString(), out TS))
266 return TS.Hours.ToString(
"D2") +
":" + TS.Minutes.ToString(
"D2") +
":" + TS.Seconds.ToString(
"D2");
269 DT = Convert.ToDateTime(Value);
270 return DT.ToString(
"HH:mm:ss");
274 if (!(Value is
bool b))
275 b = Convert.ToBoolean(Value);
277 return b ?
"1" :
"0";
280 if (!(Value is
byte[] Bin))
283 return Convert.ToBase64String(Bin);
286 if (!(Value is
byte[] Bin2))
289 StringBuilder sb =
new StringBuilder();
291 foreach (
byte b2
in Bin2)
292 sb.Append(b2.ToString(
"X2"));
294 return sb.ToString();
305 switch (this.dataType)
308 return byte.Parse(Value);
311 return ushort.Parse(Value);
314 return uint.Parse(Value);
317 return sbyte.Parse(Value);
320 return short.Parse(Value);
323 return int.Parse(Value);
326 return long.Parse(Value);
335 return new Uri(Value);
338 return new Guid(Value);
341 return float.Parse(Value.Replace(
".",
System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator));
347 return double.Parse(Value.Replace(
".",
System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator));
350 return DateTime.Parse(Value);
353 return DateTime.Parse(Value.Replace(
"T",
" "));
356 return DateTimeOffset.Parse(Value.Replace(
"T",
" "));
360 return TimeSpan.Parse(Value.Replace(
"T",
" "));
363 Value = Value.ToLower();
364 return (Value ==
"1" || Value ==
"true" || Value ==
"yes");
367 return Convert.FromBase64String(Value);
370 int i, c = Value.Length;
372 throw new Exception(
"Invalid bin.hex value");
374 byte[] Bin =
new byte[c / 2];
379 for (i = j = 0; i < c;)
383 if (ch >=
'0' && ch <=
'9')
384 b = (byte)(ch -
'0');
385 else if (ch >=
'A' && ch <=
'F')
386 b = (byte)(ch -
'A' + 10);
387 else if (ch >=
'a' && ch <=
'f')
388 b = (byte)(ch -
'a' + 10);
390 throw new Exception(
"Invalid bin.hex value");
395 if (ch >=
'0' && ch <=
'9')
396 b |= (byte)(ch -
'0');
397 else if (ch >=
'A' && ch <=
'F')
398 b |= (byte)(ch -
'A' + 10);
399 else if (ch >=
'a' && ch <=
'f')
400 b |= (byte)(ch -
'a' + 10);
402 throw new Exception(
"Invalid bin.hex value");
421 if (Value is
byte[] Bin)
Contains information about a response to a content request.
byte[] Encoded
Encoded object.
void AssertOk()
Asserts response is OK.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > 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.