/// <summary> /// Sends a search request. /// </summary> /// <param name="Callback">Callback method called when response of search request is returned.</param> /// <param name="State">State object to pass on to the callback method.</param> public void SendSearchRequest(SearchResultEventHandler Callback, object State) { StringBuilder Xml = new StringBuilder(); Xml.Append("<query xmlns='"); Xml.Append(XmppClient.NamespaceSearch); Xml.Append("'>"); if (!this.supportsForms) { if (!string.IsNullOrEmpty(this.first)) { Xml.Append("<first>"); Xml.Append(XML.Encode(this.first)); Xml.Append("</first>"); } if (!string.IsNullOrEmpty(this.last)) { Xml.Append("<last>"); Xml.Append(XML.Encode(this.last)); Xml.Append("</last>"); } if (!string.IsNullOrEmpty(this.nick)) { Xml.Append("<nick>"); Xml.Append(XML.Encode(this.nick)); Xml.Append("</nick>"); } if (!string.IsNullOrEmpty(this.email)) { Xml.Append("<email>"); Xml.Append(XML.Encode(this.email)); Xml.Append("</email>"); } Xml.Append("</query>"); this.client.SendIqSet(this.From, Xml.ToString(), this.OldSearchResult, new object[] { Callback, State }); } else { this.searchForm.SerializeSubmit(Xml); Xml.Append("</query>"); this.client.SendIqSet(this.From, Xml.ToString(), this.FormSearchResult, new object[] { Callback, State }); } }
/// <summary> /// Gets devices owned by the caller. /// </summary> /// <param name="Offset">Device list offset.</param> /// <param name="MaxCount">Maximum number of things to return.</param> /// <param name="Callback">Method to call when result has been received.</param> /// <param name="State">State object to pass on to the callback method.</param> public void GetDevices(int Offset, int MaxCount, SearchResultEventHandler Callback, object State) { StringBuilder Request = new StringBuilder(); Request.Append("<getDevices xmlns='"); Request.Append(NamespaceProvisioningOwner); Request.Append("' offset='"); Request.Append(Offset.ToString()); Request.Append("' maxCount='"); Request.Append(MaxCount.ToString()); Request.Append("'/>"); this.client.SendIqGet(this.provisioningServerAddress, Request.ToString(), (sender, e) => { ThingRegistryClient.ParseResultSet(Offset, MaxCount, this, e, Callback, State); }, null); }
private void CallResponseMethod(SearchResultEventHandler Callback, object State, List <Dictionary <string, string> > Records, IqResultEventArgs e) { SearchResultEventArgs e2 = new SearchResultEventArgs(Records.ToArray(), e); e2.State = State; if (Callback != null) { try { Callback(this.client, e2); } catch (Exception ex) { this.client.Exception(ex); } } }
private async Task CallResponseMethod(SearchResultEventHandler Callback, object State, List <Dictionary <string, string> > Records, Field[] Headers, IqResultEventArgs e) { SearchResultEventArgs e2 = new SearchResultEventArgs(Records.ToArray(), Headers, e) { State = State }; if (!(Callback is null)) { try { await Callback(this.client, e2); } catch (Exception ex) { this.client.Exception(ex); } } }
private void OldSearchResult(object Sender, IqResultEventArgs e) { object[] P = (object[])e.State; SearchResultEventHandler Callback = (SearchResultEventHandler)P[0]; object State = P[1]; List <Dictionary <string, string> > Records = new List <Dictionary <string, string> >(); if (e.Ok) { foreach (XmlNode N in e.Response.ChildNodes) { if (N.LocalName == "query") { foreach (XmlNode N2 in N.ChildNodes) { if (N2.LocalName == "item") { Dictionary <string, string> Record = new Dictionary <string, string>(); Record["jid"] = XML.Attribute((XmlElement)N2, "jid"); foreach (XmlNode N3 in N2.ChildNodes) { XmlElement E = N3 as XmlElement; if (E != null) { Record[E.LocalName] = E.InnerText; } } Records.Add(Record); } } } } } this.CallResponseMethod(Callback, State, Records, e); }
private void FormSearchResult(object Sender, IqResultEventArgs e) { object[] P = (object[])e.State; SearchResultEventHandler Callback = (SearchResultEventHandler)P[0]; object State = P[1]; List <Dictionary <string, string> > Records = new List <Dictionary <string, string> >(); if (e.Ok) { foreach (XmlNode N in e.Response.ChildNodes) { if (N.LocalName == "query") { foreach (XmlNode N2 in N.ChildNodes) { if (N2.LocalName == "x") { DataForm Form = new DataForm(this.client, (XmlElement)N2, null, null, e.From, e.To); Dictionary <string, string> Record = new Dictionary <string, string>(); foreach (Field[] FormRecord in Form.Records) { foreach (Field FormField in FormRecord) { Record[FormField.Var] = FormField.ValueString; } } Records.Add(Record); } } } } } this.CallResponseMethod(Callback, State, Records, e); }
private Task FormSearchResult(object Sender, IqResultEventArgs e) { object[] P = (object[])e.State; SearchResultEventHandler Callback = (SearchResultEventHandler)P[0]; object State = P[1]; List <Dictionary <string, string> > Records = new List <Dictionary <string, string> >(); List <Field> Headers = new List <Field>(); if (e.Ok) { foreach (XmlNode N in e.Response.ChildNodes) { if (N.LocalName == "query") { foreach (XmlNode N2 in N.ChildNodes) { if (N2.LocalName == "x") { DataForm Form = new DataForm(this.client, (XmlElement)N2, null, null, e.From, e.To); Dictionary <string, bool> HeadersSorted = new Dictionary <string, bool>(); string Header; if (Form.Header != null) { foreach (Field F in Form.Header) { Header = F.Var; if (!HeadersSorted.ContainsKey(Header)) { HeadersSorted[Header] = true; Headers.Add(F); } } } foreach (Field[] FormRecord in Form.Records) { Dictionary <string, string> Record = new Dictionary <string, string>(); foreach (Field FormField in FormRecord) { Header = FormField.Var; Record[Header] = FormField.ValueString; if (!HeadersSorted.ContainsKey(Header)) { HeadersSorted[Header] = true; Headers.Add(new TextSingleField(null, Header, string.IsNullOrEmpty(FormField.Label) ? Header : FormField.Label, false, null, null, string.Empty, new StringDataType(), new BasicValidation(), string.Empty, false, false, false)); } } Records.Add(Record); } } } } } } return(this.CallResponseMethod(Callback, State, Records, Headers.ToArray(), e)); }
private Task OldSearchResult(object Sender, IqResultEventArgs e) { object[] P = (object[])e.State; SearchResultEventHandler Callback = (SearchResultEventHandler)P[0]; object State = P[1]; List <Dictionary <string, string> > Records = new List <Dictionary <string, string> >(); List <Field> Headers = new List <Field>(); if (e.Ok) { foreach (XmlNode N in e.Response.ChildNodes) { if (N.LocalName == "query") { Dictionary <string, bool> HeadersSorted = new Dictionary <string, bool>(); string Header; foreach (XmlNode N2 in N.ChildNodes) { if (N2.LocalName == "item") { Dictionary <string, string> Record = new Dictionary <string, string>() { { "jid", XML.Attribute((XmlElement)N2, "jid") } }; foreach (XmlNode N3 in N2.ChildNodes) { if (N3 is XmlElement E) { Header = E.LocalName; Record[Header] = E.InnerText; if (!HeadersSorted.ContainsKey(Header)) { HeadersSorted[Header] = true; switch (Header) { case "first": Headers.Add(new TextSingleField(null, Header, "First Name", false, null, null, string.Empty, new StringDataType(), new BasicValidation(), string.Empty, false, false, false)); break; case "last": Headers.Add(new TextSingleField(null, Header, "Last Name", false, null, null, string.Empty, new StringDataType(), new BasicValidation(), string.Empty, false, false, false)); break; case "nick": Headers.Add(new TextSingleField(null, Header, "Nick Name", false, null, null, string.Empty, new StringDataType(), new BasicValidation(), string.Empty, false, false, false)); break; case "email": Headers.Add(new TextSingleField(null, Header, "e-Mail", false, null, null, string.Empty, new StringDataType(), new BasicValidation(), string.Empty, false, false, false)); break; default: Headers.Add(new TextSingleField(null, Header, Header, false, null, null, string.Empty, new StringDataType(), new BasicValidation(), string.Empty, false, false, false)); break; } } } } Records.Add(Record); } } } } } return(this.CallResponseMethod(Callback, State, Records, Headers.ToArray(), e)); }
internal static void ParseResultSet(int Offset, int MaxCount, object Sender, IqResultEventArgs e, SearchResultEventHandler Callback, object State) { List <SearchResultThing> Things = new List <SearchResultThing>(); List <MetaDataTag> MetaData = new List <MetaDataTag>(); ThingReference Node; XmlElement E = e.FirstElement; XmlElement E2, E3; string Jid; string OwnerJid; string NodeId; string SourceId; string Partition; string Name; bool More = false; if (e.Ok && E != null && E.LocalName == "found" && E.NamespaceURI == NamespaceDiscovery) { More = XML.Attribute(E, "more", false); foreach (XmlNode N in E.ChildNodes) { E2 = N as XmlElement; if (E2.LocalName == "thing" && E2.NamespaceURI == NamespaceDiscovery) { Jid = XML.Attribute(E2, "jid"); OwnerJid = XML.Attribute(E2, "owner"); NodeId = XML.Attribute(E2, "id"); SourceId = XML.Attribute(E2, "src"); Partition = XML.Attribute(E2, "pt"); if (string.IsNullOrEmpty(NodeId) && string.IsNullOrEmpty(SourceId) && string.IsNullOrEmpty(Partition)) { Node = ThingReference.Empty; } else { Node = new ThingReference(NodeId, SourceId, Partition); } MetaData.Clear(); foreach (XmlNode N2 in E2.ChildNodes) { E3 = N2 as XmlElement; if (E3 == null) { continue; } Name = XML.Attribute(E3, "name"); switch (E3.LocalName) { case "str": MetaData.Add(new MetaDataStringTag(Name, XML.Attribute(E3, "value"))); break; case "num": MetaData.Add(new MetaDataNumericTag(Name, XML.Attribute(E3, "value", 0.0))); break; } } Things.Add(new SearchResultThing(Jid, OwnerJid, Node, MetaData.ToArray())); } } } if (Callback != null) { SearchResultEventArgs e2 = new SearchResultEventArgs(e, State, Offset, MaxCount, More, Things.ToArray()); try { Callback(Sender, e2); } catch (Exception ex) { Log.Critical(ex); } } }
/// <summary> /// Searches for publically available things in the thing registry. /// </summary> /// <param name="Offset">Search offset.</param> /// <param name="MaxCount">Maximum number of things to return.</param> /// <param name="SearchOperators">Search operators to use in search.</param> /// <param name="Callback">Method to call when result has been received.</param> /// <param name="State">State object to pass on to the callback method.</param> public void Search(int Offset, int MaxCount, SearchOperator[] SearchOperators, SearchResultEventHandler Callback, object State) { StringBuilder Request = new StringBuilder(); Request.Append("<search xmlns='"); Request.Append(NamespaceDiscovery); Request.Append("' offset='"); Request.Append(Offset.ToString()); Request.Append("' maxCount='"); Request.Append(MaxCount.ToString()); Request.Append("'>"); foreach (SearchOperator Operator in SearchOperators) { Operator.Serialize(Request); } Request.Append("</search>"); this.client.SendIqGet(this.thingRegistryAddress, Request.ToString(), (sender, e) => { ParseResultSet(Offset, MaxCount, this, e, Callback, State); }, null); }
/// <summary> /// Searches for publically available things in the thing registry. /// </summary> /// <param name="Offset">Search offset.</param> /// <param name="MaxCount">Maximum number of things to return.</param> /// <param name="SearchOperators">Search operators to use in search.</param> /// <param name="Callback">Method to call when result has been received.</param> /// <param name="State">State object to pass on to the callback method.</param> public void Search(int Offset, int MaxCount, SearchOperator[] SearchOperators, SearchResultEventHandler Callback, object State) { StringBuilder Request = new StringBuilder(); Request.Append("<search xmlns='"); Request.Append(NamespaceDiscovery); Request.Append("' offset='"); Request.Append(Offset.ToString()); Request.Append("' maxCount='"); Request.Append(MaxCount.ToString()); Request.Append("'>"); foreach (SearchOperator Operator in SearchOperators) { Operator.Serialize(Request); } Request.Append("</search>"); this.client.SendIqGet(this.thingRegistryAddress, Request.ToString(), (sender, e) => { List <SearchResultThing> Things = new List <SearchResultThing>(); List <MetaDataTag> MetaData = new List <MetaDataTag>(); ThingReference Node; XmlElement E = e.FirstElement; XmlElement E2, E3; string Jid; string OwnerJid; string NodeId; string SourceId; string Partition; string Name; bool More = false; if (e.Ok && E != null && E.LocalName == "found" && E.NamespaceURI == NamespaceDiscovery) { More = XML.Attribute(E, "more", false); foreach (XmlNode N in E.ChildNodes) { E2 = N as XmlElement; if (E2.LocalName == "thing" && E2.NamespaceURI == NamespaceDiscovery) { Jid = XML.Attribute(E2, "jid"); OwnerJid = XML.Attribute(E2, "owner"); NodeId = XML.Attribute(E2, "id"); SourceId = XML.Attribute(E2, "src"); Partition = XML.Attribute(E2, "pt"); if (string.IsNullOrEmpty(NodeId) && string.IsNullOrEmpty(SourceId) && string.IsNullOrEmpty(Partition)) { Node = ThingReference.Empty; } else { Node = new ThingReference(NodeId, SourceId, Partition); } MetaData.Clear(); foreach (XmlNode N2 in E2.ChildNodes) { E3 = N2 as XmlElement; if (E3 == null) { continue; } Name = XML.Attribute(E3, "name"); switch (E3.LocalName) { case "str": MetaData.Add(new MetaDataStringTag(Name, XML.Attribute(E3, "value"))); break; case "num": MetaData.Add(new MetaDataNumericTag(Name, XML.Attribute(E3, "value", 0.0))); break; } } Things.Add(new SearchResultThing(Jid, OwnerJid, Node, MetaData.ToArray())); } } } if (Callback != null) { SearchResultEventArgs e2 = new SearchResultEventArgs(e, State, Offset, MaxCount, More, Things.ToArray()); try { Callback(this, e2); } catch (Exception ex) { Log.Critical(ex); } } }, null); }