forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriptionOptions.cs
More file actions
181 lines (155 loc) · 4.79 KB
/
SubscriptionOptions.cs
File metadata and controls
181 lines (155 loc) · 4.79 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using System.Text;
using Waher.Content;
using Waher.Content.Xml;
using Waher.Networking.XMPP.DataForms;
using Waher.Networking.XMPP.DataForms.FieldTypes;
namespace Waher.Networking.XMPP.PubSub
{
/// <summary>
/// Contains options for a node subscription
/// </summary>
public class SubscriptionOptions
{
private SubscriptionType? type = null;
private SubscriptonDepth? depth = null;
private bool? deliver = null;
private bool? digest = null;
private bool? includeBody = null;
private int? digestFrequencyMilliseconds = null;
private DateTime? expires = null;
/// <summary>
/// Contains options for a node subscription
/// </summary>
public SubscriptionOptions()
{
}
/// <summary>
/// Contains options for a node subscription
/// </summary>
public SubscriptionOptions(DataForm Form)
{
foreach (Field F in Form.Fields)
{
switch (F.Var)
{
case "pubsub#deliver":
if (CommonTypes.TryParse(F.ValueString, out bool b))
this.deliver = b;
break;
case "pubsub#digest":
if (CommonTypes.TryParse(F.ValueString, out b))
this.digest = b;
break;
case "pubsub#include_body":
if (CommonTypes.TryParse(F.ValueString, out b))
this.includeBody = b;
break;
case "pubsub#digest_frequency":
if (int.TryParse(F.ValueString, out int i))
this.digestFrequencyMilliseconds = i;
break;
case "pubsub#expire":
if (DateTime.TryParse(F.ValueString, out DateTime TP))
this.expires = TP;
break;
case "pubsub#subscription_type":
if (Enum.TryParse(F.ValueString, out SubscriptionType SubscriptionType))
this.type = SubscriptionType;
break;
case "pubsub#subscription_depth":
if (F.ValueString == "1")
this.depth = SubscriptonDepth.one;
else if (Enum.TryParse(F.ValueString, out SubscriptonDepth SubscriptonDepth))
this.depth = SubscriptonDepth;
break;
}
}
}
/// <summary>
/// Type of subscription
/// </summary>
public SubscriptionType? Type
{
get { return this.type; }
set { this.type = value; }
}
/// <summary>
/// Subscription depth
/// </summary>
public SubscriptonDepth? Depth
{
get { return this.depth; }
set { this.depth = value; }
}
/// <summary>
/// Whether an entity wants to receive or disable notifications
/// </summary>
public bool? Deliver
{
get { return this.deliver; }
set { this.deliver = value; }
}
/// <summary>
/// Whether an entity wants to receive digests (aggregations) of notifications or all notifications individually
/// </summary>
public bool? Digest
{
get { return this.digest; }
set { this.digest = value; }
}
/// <summary>
/// Whether an entity wants to receive an XMPP message body in addition to the payload format
/// </summary>
public bool? IncludeBody
{
get { return this.includeBody; }
set { this.includeBody = value; }
}
/// <summary>
/// The minimum number of milliseconds between sending any two notification digests
/// </summary>
public int? DigestFrequencyMilliseconds
{
get { return this.digestFrequencyMilliseconds; }
set { this.digestFrequencyMilliseconds = value; }
}
/// <summary>
/// The DateTime at which a leased subscription will end or has ended
/// </summary>
public DateTime? Expires
{
get { return this.expires; }
set { this.expires = value; }
}
/// <summary>
/// Creates a data form for subsmission.
/// </summary>
/// <param name="Client">XMPP Publish/Subscribe Client</param>
/// <returns>Data form.</returns>
public DataForm ToForm(PubSubClient Client)
{
List<Field> Fields = new List<Field>()
{
new HiddenField("FORM_TYPE", "http://jabber.org/protocol/pubsub#subscribe_options")
};
if (this.deliver.HasValue)
Fields.Add(new BooleanField("pubsub#deliver", this.deliver.Value));
if (this.digest.HasValue)
Fields.Add(new BooleanField("pubsub#digest", this.digest.Value));
if (this.includeBody.HasValue)
Fields.Add(new BooleanField("pubsub#include_body", this.includeBody.Value));
if (this.digestFrequencyMilliseconds.HasValue)
Fields.Add(new TextSingleField("pubsub#digest_frequency", this.digestFrequencyMilliseconds.Value.ToString()));
if (this.expires.HasValue)
Fields.Add(new TextSingleField("pubsub#expire", XML.Encode(this.expires.Value)));
if (this.type.HasValue)
Fields.Add(new TextSingleField("pubsub#subscription_type", this.type.Value.ToString()));
if (this.depth.HasValue)
Fields.Add(new TextSingleField("pubsub#subscription_depth", this.depth.Value == SubscriptonDepth.one ? "1" : this.depth.Value.ToString()));
return new DataForm(Client.Client, FormType.Form, Client.Client.FullJID,
Client.ComponentAddress, Fields.ToArray());
}
}
}