1using Microsoft.Maker.RemoteWiring;
3using System.Collections.Generic;
4using System.Threading.Tasks;
33 private string expression =
"Raw";
34 private string fieldName =
"Value";
35 private string unit =
string.Empty;
37 private byte nrDecimals = 0;
38 private bool initialized =
false;
52 [Header(9,
"Mode:", 20)]
53 [ToolTip(10,
"Select drive mode of pin.")]
70 [Header(24,
"Field Name:", 30)]
71 [ToolTip(25,
"Name of calculated field.")]
72 [DefaultValue(
"Value")]
75 get => this.fieldName;
76 set => this.fieldName = value;
83 [Header(20,
"Value Expression:", 40)]
84 [ToolTip(21,
"Enter expression to scale the raw value into a physical quantity. The raw value will be available in the variable 'Raw'. Any script construct can be used, including adding units. For more information, see: https://waher.se/Script.md")]
88 get => this.expression;
91 if (
string.IsNullOrEmpty(value))
96 this.expression = value;
104 [Header(27,
"Unit:", 50)]
105 [ToolTip(28,
"Unit to use, if one is not provided by the expression above.")]
106 [DefaultValueStringEmpty]
110 set => this.unit = value;
117 [Header(22,
"Number of decimals:", 60)]
118 [ToolTip(23,
"Number of decimals to use when presenting calculated value.")]
122 get => this.nrDecimals;
123 set => this.nrDecimals = value;
139 RemoteDevice Device = this.
GetDevice().Result;
141 if (!(Device is
null))
146 Device.pinMode(this.
PinNrStr, PinMode.ANALOG);
150 this.initialized =
true;
153 this.initialized =
false;
163 RemoteDevice Device = await this.
GetDevice()
164 ??
throw new Exception(
"Device not ready.");
166 List<Field> Fields =
new List<Field>();
167 DateTime Now = DateTime.Now;
169 if (!this.initialized)
174 ushort Raw = Device.analogRead(this.
PinNrStr);
175 await this.CalcMomentary(Fields, Now, Raw);
200 private async Task CalcMomentary(List<Field> Fields, DateTime Now, ushort Raw)
204 if (this.exp is
null && !
string.IsNullOrEmpty(this.expression))
207 if (!(this.exp is
null))
211 {
"Raw", (double)Raw }
217 if (Value is
double dbl)
220 F =
new QuantityField(
this, Now, this.fieldName, qty.Magnitude,
this.nrDecimals, qty.Unit.ToString(),
FieldType.Momentary,
FieldQoS.AutomaticReadout);
221 else if (Value is
bool b)
223 else if (Value is DateTime DT)
227 else if (Value is Enum E)
229 else if (Value is
int i32)
231 if (
string.IsNullOrEmpty(this.unit))
236 else if (Value is
long i64)
238 if (
string.IsNullOrEmpty(this.unit))
243 else if (Value is
string s)
245 else if (Value is TimeSpan TS)
250 if (this.fieldName ==
"Value")
252 F.Module = typeof(Module).Namespace;
265 List<Field> Fields =
new List<Field>();
267 await this.CalcMomentary(Fields, DateTime.Now, Value);
269 foreach (
Field F
in Fields)
278 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(
Language, Caller) as LinkedList<Parameter>;
Contains information about a language.
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Contains information about a namespace in a language.
Class managing a script expression.
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
override string PinNrStr
TODO
async Task< RemoteDevice > GetDevice()
TODO
void NewMomentaryValues(params Field[] Values)
Reports newly measured values.
Tokens available in request.
Represents a boolean value that can be either true or false.
Represents a date and optional time value.
Represents a duration value. Duration values adhere to the type specified by xsd:duration.
Base class for all sensor data fields.
Represents a 32-bit integer value.
Represents a 64-bit integer value.
Represents a localization step, as defined in XEP-323: http://xmpp.org/extensions/xep-0323....
Represents a physical quantity value.
Represents a string value.
Represents a time value. Time values adhere to the type specified by xsd:time.
Contains information about an error on a thing
Interface for sensor nodes.
Interface for classes managing sensor data readouts.
bool IsIncluded(string FieldName)
Checks if a field with the given parameters is included in the readout.
Task ReportErrors(bool Done, params ThingError[] Errors)
Report error states to the client.
Task ReportFields(bool Done, params Field[] Fields)
Report read fields to the client.
FieldQoS
Field Quality of Service flags
FieldType
Field Type flags
Represents a duration value, as defined by the xsd:duration data type: http://www....