Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AnalogInput.cs
1using Microsoft.Maker.RemoteWiring;
2using System;
3using System.Collections.Generic;
4using System.Threading.Tasks;
5using Waher.Content;
8using Waher.Script;
13
15{
20 {
24 Input
25 }
26
31 {
32 private AnalogInputPinMode mode = AnalogInputPinMode.Input;
33 private string expression = "Raw";
34 private string fieldName = "Value";
35 private string unit = string.Empty;
36 private Expression exp = null;
37 private byte nrDecimals = 0;
38 private bool initialized = false;
39
43 public AnalogInput()
44 : base()
45 {
46 }
47
51 [Page(5, "Arduino")]
52 [Header(9, "Mode:", 20)]
53 [ToolTip(10, "Select drive mode of pin.")]
54 [DefaultValue(AnalogInputPinMode.Input)]
55 [Option(AnalogInputPinMode.Input, 11, "Input")]
57 {
58 get => this.mode;
59 set
60 {
61 this.mode = value;
62 this.Initialize();
63 }
64 }
65
69 [Page(5, "Arduino")]
70 [Header(24, "Field Name:", 30)]
71 [ToolTip(25, "Name of calculated field.")]
72 [DefaultValue("Value")]
73 public string FieldName
74 {
75 get => this.fieldName;
76 set => this.fieldName = value;
77 }
78
82 [Page(5, "Arduino")]
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")]
85 [DefaultValue("Raw")]
86 public string Expression
87 {
88 get => this.expression;
89 set
90 {
91 if (string.IsNullOrEmpty(value))
92 this.exp = null;
93 else
94 this.exp = new Expression(value);
95
96 this.expression = value;
97 }
98 }
99
103 [Page(5, "Arduino")]
104 [Header(27, "Unit:", 50)]
105 [ToolTip(28, "Unit to use, if one is not provided by the expression above.")]
106 [DefaultValueStringEmpty]
107 public string Unit
108 {
109 get => this.unit;
110 set => this.unit = value;
111 }
112
116 [Page(5, "Arduino")]
117 [Header(22, "Number of decimals:", 60)]
118 [ToolTip(23, "Number of decimals to use when presenting calculated value.")]
119 [DefaultValue(0)]
120 public byte NrDecimals
121 {
122 get => this.nrDecimals;
123 set => this.nrDecimals = value;
124 }
125
129 public override Task<string> GetTypeNameAsync(Language Language)
130 {
131 return Language.GetStringAsync(typeof(Module), 16, "Analog Input");
132 }
133
137 public override void Initialize()
138 {
139 RemoteDevice Device = this.GetDevice().Result; // TODO: Avoid blocking call.
140
141 if (!(Device is null))
142 {
143 switch (this.Mode)
144 {
145 case AnalogInputPinMode.Input:
146 Device.pinMode(this.PinNrStr, PinMode.ANALOG);
147 break;
148 }
149
150 this.initialized = true;
151 }
152 else
153 this.initialized = false;
154 }
155
159 public async Task StartReadout(ISensorReadout Request)
160 {
161 try
162 {
163 RemoteDevice Device = await this.GetDevice()
164 ?? throw new Exception("Device not ready.");
165
166 List<Field> Fields = new List<Field>();
167 DateTime Now = DateTime.Now;
168
169 if (!this.initialized)
170 this.Initialize();
171
172 if (Request.IsIncluded(FieldType.Momentary))
173 {
174 ushort Raw = Device.analogRead(this.PinNrStr);
175 await this.CalcMomentary(Fields, Now, Raw);
176 }
177
178 if (Request.IsIncluded(FieldType.Status))
179 {
180 Fields.Add(new EnumField(this, Now, "Drive Mode", Device.getPinMode(this.PinNr), FieldType.Status, FieldQoS.AutomaticReadout,
181 typeof(Module).Namespace, 15));
182 }
183
184 if (Request.IsIncluded(FieldType.Identity))
185 {
186 Fields.Add(new Int32Field(this, Now, "Pin Number", this.PinNr, FieldType.Identity, FieldQoS.AutomaticReadout,
187 typeof(Module).Namespace, 14));
188
189 this.AddIdentityReadout(Fields, Now);
190 }
191
192 await Request.ReportFields(true, Fields);
193 }
194 catch (Exception ex)
195 {
196 await Request.ReportErrors(true, new ThingError(this, ex.Message));
197 }
198 }
199
200 private async Task CalcMomentary(List<Field> Fields, DateTime Now, ushort Raw)
201 {
202 Fields.Add(new Int32Field(this, Now, "Raw", Raw, FieldType.Momentary, FieldQoS.AutomaticReadout, typeof(Module).Namespace, 26));
203
204 if (this.exp is null && !string.IsNullOrEmpty(this.expression))
205 this.exp = new Expression(this.expression);
206
207 if (!(this.exp is null))
208 {
209 Variables v = new Variables()
210 {
211 { "Raw", (double)Raw }
212 };
213
214 object Value = await this.exp.EvaluateAsync(v);
215 Field F;
216
217 if (Value is double dbl)
218 F = new QuantityField(this, Now, this.fieldName, dbl, this.nrDecimals, this.unit, FieldType.Momentary, FieldQoS.AutomaticReadout);
219 else if (Value is PhysicalQuantity qty)
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)
222 F = new BooleanField(this, Now, this.fieldName, b, FieldType.Momentary, FieldQoS.AutomaticReadout);
223 else if (Value is DateTime DT)
224 F = new DateTimeField(this, Now, this.fieldName, DT, FieldType.Momentary, FieldQoS.AutomaticReadout);
225 else if (Value is Duration D)
226 F = new DurationField(this, Now, this.fieldName, D, FieldType.Momentary, FieldQoS.AutomaticReadout);
227 else if (Value is Enum E)
228 F = new EnumField(this, Now, this.fieldName, E, FieldType.Momentary, FieldQoS.AutomaticReadout);
229 else if (Value is int i32)
230 {
231 if (string.IsNullOrEmpty(this.unit))
232 F = new Int32Field(this, Now, this.fieldName, i32, FieldType.Momentary, FieldQoS.AutomaticReadout);
233 else
234 F = new QuantityField(this, Now, this.fieldName, i32, 0, this.unit, FieldType.Momentary, FieldQoS.AutomaticReadout);
235 }
236 else if (Value is long i64)
237 {
238 if (string.IsNullOrEmpty(this.unit))
239 F = new Int64Field(this, Now, this.fieldName, i64, FieldType.Momentary, FieldQoS.AutomaticReadout);
240 else
241 F = new QuantityField(this, Now, this.fieldName, i64, 0, this.unit, FieldType.Momentary, FieldQoS.AutomaticReadout);
242 }
243 else if (Value is string s)
244 F = new StringField(this, Now, this.fieldName, s, FieldType.Momentary, FieldQoS.AutomaticReadout);
245 else if (Value is TimeSpan TS)
246 F = new TimeField(this, Now, this.fieldName, TS, FieldType.Momentary, FieldQoS.AutomaticReadout);
247 else
248 F = new StringField(this, Now, this.fieldName, Value.ToString(), FieldType.Momentary, FieldQoS.AutomaticReadout);
249
250 if (this.fieldName == "Value")
251 {
252 F.Module = typeof(Module).Namespace;
253 F.StringIdSteps = new LocalizationStep[] { new LocalizationStep(13) };
254 }
255
256 Fields.Add(F);
257 }
258 }
259
263 public async Task Pin_ValueChanged(ushort Value)
264 {
265 List<Field> Fields = new List<Field>();
266
267 await this.CalcMomentary(Fields, DateTime.Now, Value);
268
269 foreach (Field F in Fields)
270 this.NewMomentaryValues(F);
271 }
272
276 public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
277 {
278 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
279
280 Result.AddLast(new StringParameter("Mode", await Language.GetStringAsync(typeof(Module), 19, "Mode"), this.mode.ToString()));
281
282 return Result;
283 }
284 }
285}
Contains information about a language.
Definition: Language.cs:17
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 ...
Definition: Language.cs:209
Contains information about a namespace in a language.
Definition: Namespace.cs:17
Class managing a script expression.
Definition: Expression.cs:39
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4275
Collection of variables.
Definition: Variables.cs:25
override Task< string > GetTypeNameAsync(Language Language)
TODO
Definition: AnalogInput.cs:129
override async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
TODO
Definition: AnalogInput.cs:276
async Task StartReadout(ISensorReadout Request)
TODO
Definition: AnalogInput.cs:159
async Task Pin_ValueChanged(ushort Value)
TODO
Definition: AnalogInput.cs:263
override void Initialize()
TODO
Definition: AnalogInput.cs:137
AnalogInputPinMode Mode
TODO
Definition: AnalogInput.cs:57
override string PinNrStr
TODO
Definition: AnalogPin.cs:25
byte PinNr
TODO
Definition: Pin.cs:34
async Task< RemoteDevice > GetDevice()
TODO
Definition: Pin.cs:55
virtual void AddIdentityReadout(List< Field > Fields, DateTime Now)
Adds defined identity fields to a sensor data readout.
void NewMomentaryValues(params Field[] Values)
Reports newly measured values.
Tokens available in request.
Definition: RequestOrigin.cs:9
Represents a boolean value that can be either true or false.
Definition: BooleanField.cs:11
Represents a date and optional time value.
Represents a duration value. Duration values adhere to the type specified by xsd:duration.
Represents a enum value.
Definition: EnumField.cs:11
Base class for all sensor data fields.
Definition: Field.cs:20
Represents a 32-bit integer value.
Definition: Int32Field.cs:10
Represents a 64-bit integer value.
Definition: Int64Field.cs:10
Represents a localization step, as defined in XEP-323: http://xmpp.org/extensions/xep-0323....
Represents a physical quantity value.
Represents a string value.
Definition: StringField.cs:10
Represents a time value. Time values adhere to the type specified by xsd:time.
Definition: TimeField.cs:14
Contains information about an error on a thing
Definition: ThingError.cs:10
Interface for sensor nodes.
Definition: ISensor.cs:9
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
Definition: FieldQoS.cs:10
FieldType
Field Type flags
Definition: FieldType.cs:10
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:13