-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathConnectedDevice.cs
More file actions
112 lines (96 loc) · 3.21 KB
/
ConnectedDevice.cs
File metadata and controls
112 lines (96 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Networking.XMPP;
using Waher.Runtime.Language;
using Waher.Things.Attributes;
using Waher.Things.Metering.NodeTypes;
using Waher.Things.Xmpp.Commands;
namespace Waher.Things.Xmpp
{
/// <summary>
/// Node representing a device that is connected to XMPP.
/// </summary>
public class ConnectedDevice : XmppDevice
{
/// <summary>
/// Node representing a device that is connected to XMPP.
/// </summary>
public ConnectedDevice()
: base()
{
}
/// <summary>
/// XMPP Address
/// </summary>
[Page(2, "XMPP", 100)]
[Header(3, "JID:")]
[ToolTip(4, "XMPP Address.")]
public string JID { get; set; }
/// <summary>
/// Gets the type name of the node.
/// </summary>
/// <param name="Language">Language to use.</param>
/// <returns>Localized type node.</returns>
public override Task<string> GetTypeNameAsync(Language Language)
{
return Language.GetStringAsync(typeof(ConcentratorDevice), 56, "XMPP Device");
}
/// <summary>
/// If the node accepts a presumptive parent, i.e. can be added to that parent (if that parent accepts the node as a child).
/// </summary>
/// <param name="Parent">Presumptive parent node.</param>
/// <returns>If the parent is acceptable.</returns>
public override Task<bool> AcceptsParentAsync(INode Parent)
{
return Task.FromResult(
Parent is Root ||
Parent is NodeCollection);
}
/// <summary>
/// If the node accepts a presumptive child, i.e. can receive as a child (if that child accepts the node as a parent).
/// </summary>
/// <param name="Child">Presumptive child node.</param>
/// <returns>If the child is acceptable.</returns>
public override Task<bool> AcceptsChildAsync(INode Child)
{
return Task.FromResult(false);
}
/// <summary>
/// Gets the Full JID of the connected device.
/// </summary>
/// <param name="Client">Concentrator client.</param>
/// <param name="HasContact">If a contact with the Bare JID was found.</param>
/// <param name="HasSubscription">If a subscription was found, but no presence.</param>
/// <returns>Full JID, if found, null or empty otherwise.</returns>
public string GetRemoteFullJid(XmppClient Client, out bool HasContact, out bool HasSubscription)
{
HasContact = false;
HasSubscription = false;
RosterItem Contact = Client[this.JID];
if (Contact is null)
return null;
HasContact = true;
if (!(Contact.State == SubscriptionState.Both || Contact.State == SubscriptionState.To))
return null;
HasSubscription = true;
return Contact.LastPresenceFullJid;
}
/// <summary>
/// Available command objects. If no commands are available, null is returned.
/// </summary>
public override Task<IEnumerable<ICommand>> Commands => this.GetCommands();
private async Task<IEnumerable<ICommand>> GetCommands()
{
List<ICommand> Result = new List<ICommand>();
Result.AddRange(await base.Commands);
XmppClient Client = await this.GetClient();
if (!(Client is null))
{
this.GetRemoteFullJid(Client, out bool HasContact, out bool HasSubscription);
if (!HasContact || !HasSubscription)
Result.Add(new SubscribeToPresence(this));
}
return Result.ToArray();
}
}
}