private void ReadDetailed_Executed(object sender, ExecutedRoutedEventArgs e) { TreeNode Node = this.SelectedNode; if (Node == null || !Node.CanReadSensorData) { return; } SensorDataClientRequest Request = Node.StartSensorDataFullReadout(); if (Request == null) { return; } TabItem TabItem = new TabItem(); this.Tabs.Items.Add(TabItem); SensorDataView View = new SensorDataView(Request, Node, false); TabItem.Header = Node.Header; TabItem.Content = View; this.Tabs.SelectedItem = TabItem; }
public void SensorData_Test_01_ReadAll() { this.ConnectClients(); try { ManualResetEvent Done = new ManualResetEvent(false); ManualResetEvent Error = new ManualResetEvent(false); IEnumerable <Field> Fields = null; SensorDataClientRequest Request = this.sensorClient.RequestReadout(this.client2.FullJID, FieldType.All); Request.OnStateChanged += (sender, NewState) => Console.Out.WriteLine(NewState.ToString()); Request.OnErrorsReceived += (sender, Errors) => Error.Set(); Request.OnFieldsReceived += (sender, NewFields) => { Fields = NewFields; Done.Set(); }; Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 20000), "Readout not performed correctly"); foreach (Field Field in Fields) { Console.Out.WriteLine(Field.ToString()); } } finally { this.DisposeClients(); } }
private void Done() { lock (this.nodes) { this.NodesTotalLabel.Content = this.nodes.Count; this.NodesOkLabel.Content = this.nodes.Count - this.failed.Count; } if (this.subscription && !(this.request is SensorDataSubscriptionRequest)) { List <FieldSubscriptionRule> Rules = new List <FieldSubscriptionRule>(); Field Field; foreach (FieldItem FieldItem in this.SensorDataListView.Items) { Field = FieldItem.Field; if (Field is BooleanField B) { Rules.Add(new FieldSubscriptionRule(Field.Name, B.Value ? 1 : 0, 1)); } else if (Field is QuantityField Q) { Rules.Add(new FieldSubscriptionRule(Field.Name, Q.Value, 1)); } else if (Field is Int32Field I32) { Rules.Add(new FieldSubscriptionRule(Field.Name, I32.Value, 1)); } else if (Field is Int64Field I64) { Rules.Add(new FieldSubscriptionRule(Field.Name, I64.Value, 1)); } else if (Field is StringField || Field is TimeField || Field is DateField || Field is DateTimeField || Field is DurationField) { Rules.Add(new FieldSubscriptionRule(Field.Name, string.Empty, 1)); } } this.request.OnStateChanged -= this.Request_OnStateChanged; this.request.OnFieldsReceived -= this.Request_OnFieldsReceived; this.request.OnErrorsReceived -= this.Request_OnErrorsReceived; this.request = null; this.request = this.node.SubscribeSensorDataMomentaryReadout(Rules.ToArray()); this.request.OnStateChanged += this.Request_OnStateChanged; this.request.OnFieldsReceived += this.Request_OnFieldsReceived; this.request.OnErrorsReceived += this.Request_OnErrorsReceived; } }
public SensorDataView(SensorDataClientRequest Request, TreeNode Node, bool Subscription) { this.request = Request; this.node = Node; this.subscription = Subscription; InitializeComponent(); if (this.request != null) { this.request.OnStateChanged += new SensorDataReadoutStateChangedEventHandler(Request_OnStateChanged); this.request.OnFieldsReceived += new SensorDataReadoutFieldsReportedEventHandler(Request_OnFieldsReceived); this.request.OnErrorsReceived += new SensorDataReadoutErrorsReportedEventHandler(Request_OnErrorsReceived); } }
public SensorDataView(SensorDataClientRequest Request, TreeNode Node, bool Subscription) { this.request = Request; this.node = Node; this.subscription = Subscription; InitializeComponent(); if (this.request != null) { this.Request_OnErrorsReceived(this, Request.Errors); this.request.OnErrorsReceived += this.Request_OnErrorsReceived; this.Request_OnFieldsReceived(this, Request.ReadFields); this.request.OnFieldsReceived += this.Request_OnFieldsReceived; this.Request_OnStateChanged(this, Request.State); this.request.OnStateChanged += this.Request_OnStateChanged; } }
/// <summary> /// Executes a node. /// </summary> /// <param name="Variables">Set of variables for the activity.</param> /// <returns>Next node of execution, if different from the default, otherwise null (for default).</returns> public override async Task <LinkedListNode <IActivityNode> > Execute(Variables Variables) { string To = this.to.GetValue(Variables); if (!(this.GetActorObject(this.actor, Variables) is SensorClient SensorClient)) { throw new Exception("Actor not an XMPP Sensor Client."); } if (XmppClient.BareJidRegEx.IsMatch(To)) { RosterItem Item = SensorClient.Client[To]; if (Item is null) { throw new Exception("No connection in roster with Bare JID: " + To); } if (!Item.HasLastPresence || !Item.LastPresence.IsOnline) { throw new Exception("Contact not online: " + To); } To = Item.LastPresenceFullJid; } TaskCompletionSource <bool> T = new TaskCompletionSource <bool>(); Dictionary <string, Field> FieldsAsObject = this.responseType == SensorDataResponseType.Object ? new Dictionary <string, Field>() : null; List <Field> FieldsAsArray = this.responseType == SensorDataResponseType.Array ? new List <Field>() : null; List <ThingError> Errors = new List <ThingError>(); SensorDataClientRequest Request = SensorClient.RequestReadout(To, this.nodeReferences, this.fields, this.fieldTypes); Request.OnErrorsReceived += (Sender, NewErrors) => { lock (Errors) { Errors.AddRange(NewErrors); } return(Task.CompletedTask); }; Request.OnFieldsReceived += (Sender, NewFields) => { if (this.responseType == SensorDataResponseType.Object) { lock (FieldsAsObject) { foreach (Field F in NewFields) { FieldsAsObject[F.Name] = F; } } } else { lock (FieldsAsArray) { FieldsAsArray.AddRange(NewFields); } } return(Task.CompletedTask); }; Request.OnStateChanged += (Sender, NewState) => { switch (NewState) { case SensorDataReadoutState.Done: T.TrySetResult(true); break; case SensorDataReadoutState.Failure: case SensorDataReadoutState.Cancelled: T.TrySetResult(false); break; } return(Task.CompletedTask); }; if (!await T.Task) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Sensor Data readout failed. Errors reported: "); sb.AppendLine(); foreach (ThingError Error in Errors) { sb.AppendLine(Error.ErrorMessage); // TODO: Node reference, if available. } throw new Exception(sb.ToString()); } Variables[this.responseVariable] = (object)FieldsAsObject ?? FieldsAsArray.ToArray(); return(null); }