-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathLwm2mObject.cs
281 lines (246 loc) · 6.72 KB
/
Lwm2mObject.cs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Networking.CoAP;
using Waher.Networking.CoAP.ContentFormats;
using Waher.Networking.CoAP.Options;
using Waher.Persistence.Attributes;
namespace Waher.Networking.LWM2M
{
/// <summary>
/// Base class for all LWM2M objects.
/// </summary>
[CollectionName("Lwm2mObjects")]
[TypeName(TypeNameSerialization.FullName)]
[Index("Id")]
public abstract class Lwm2mObject : CoapResource, ICoapGetMethod
{
private readonly SortedDictionary<int, Lwm2mObjectInstance> instances = new SortedDictionary<int, Lwm2mObjectInstance>();
private Lwm2mClient client = null;
private string objectId = null;
private ushort id;
/// <summary>
/// Base class for all LWM2M objects.
/// </summary>
/// <param name="Id">ID of object.</param>
/// <param name="Instances">Object instances.</param>
public Lwm2mObject(ushort Id, params Lwm2mObjectInstance[] Instances)
: base("/" + Id.ToString())
{
this.id = Id;
foreach (Lwm2mObjectInstance Instance in Instances)
this.Add(Instance);
}
/// <summary>
/// Object ID in database.
/// </summary>
[ObjectId]
public string ObjectId
{
get => this.objectId;
set => this.objectId = value;
}
/// <summary>
/// Adds an object instance.
/// </summary>
/// <param name="Instance">Object instance.</param>
public void Add(Lwm2mObjectInstance Instance)
{
lock (this.instances)
{
if (Instance.InstanceId < 0)
throw new ArgumentException("Invalid object instance ID.", nameof(Instance));
if (this.instances.ContainsKey(Instance.InstanceId))
{
throw new ArgumentException("An object instance with ID " + Instance.InstanceId +
" is already registered.", nameof(Instance));
}
this.instances[Instance.InstanceId] = Instance;
Instance.Object = this;
}
}
/// <summary>
/// Removes all instances.
/// </summary>
protected void ClearInstances()
{
Lwm2mObjectInstance[] Instances;
lock (this.instances)
{
Instances = new Lwm2mObjectInstance[this.instances.Count];
this.instances.Values.CopyTo(Instances, 0);
this.instances.Clear();
}
foreach (Lwm2mObjectInstance Instance in Instances)
{
this.client?.CoapEndpoint.Unregister(Instance);
foreach (Lwm2mResource Resource in Instance.Resources)
this.client?.CoapEndpoint.Unregister(Resource);
}
}
/// <summary>
/// LWM2M Client.
/// </summary>
public Lwm2mClient Client
{
get => this.client;
internal set => this.client = value;
}
/// <summary>
/// ID of object.
/// </summary>
public ushort Id
{
get => this.id;
set
{
if (this.id != value)
{
this.id = value;
this.Path = "/" + this.id.ToString();
}
}
}
/// <summary>
/// Registered instances.
/// </summary>
public Lwm2mObjectInstance[] Instances
{
get
{
Lwm2mObjectInstance[] Result;
lock (this.instances)
{
Result = new Lwm2mObjectInstance[this.instances.Count];
this.instances.Values.CopyTo(Result, 0);
}
return Result;
}
}
/// <summary>
/// If the object has instances registered on it.
/// </summary>
public bool HasInstances
{
get
{
lock (this.instances)
{
return this.instances.Count > 0;
}
}
}
/// <summary>
/// Loads any Bootstrap information.
/// </summary>
public virtual async Task LoadBootstrapInfo()
{
foreach (Lwm2mObjectInstance Instance in this.Instances)
await Instance.LoadBootstrapInfo();
}
/// <summary>
/// Deletes any Bootstrap information.
/// </summary>
public virtual async Task DeleteBootstrapInfo()
{
foreach (Lwm2mObjectInstance Instance in this.Instances)
await Instance.DeleteBootstrapInfo();
}
/// <summary>
/// Applies any Bootstrap information.
/// </summary>
public virtual async Task ApplyBootstrapInfo()
{
foreach (Lwm2mObjectInstance Instance in this.Instances)
await Instance.ApplyBootstrapInfo();
}
/// <summary>
/// Encodes available object links.
/// </summary>
/// <param name="IncludeSecurity">If security objects are to be included.</param>
/// <param name="Output">Link output.</param>
public void EncodeObjectLinks(bool IncludeSecurity, StringBuilder Output)
{
if (this.id > 0 || IncludeSecurity)
{
if (this.HasInstances)
{
foreach (Lwm2mObjectInstance Instance in this.Instances)
Instance.EncodeObjectLinks(IncludeSecurity, Output);
}
else
{
Output.Append(',');
Output.Append("</");
Output.Append(this.id.ToString());
Output.Append('>');
this.EncodeLinkParameters(Output);
}
}
}
/// <summary>
/// Encodes any link parameters to the object link.
/// </summary>
/// <param name="Output">Link output.</param>
public virtual void EncodeLinkParameters(StringBuilder Output)
{
}
/// <summary>
/// If the GET method is allowed.
/// </summary>
public bool AllowsGET => true;
/// <summary>
/// Executes the GET method on the resource.
/// </summary>
/// <param name="Request">CoAP Request</param>
/// <param name="Response">CoAP Response</param>
/// <exception cref="CoapException">If an error occurred when processing the method.</exception>
public Task GET(CoapMessage Request, CoapResponse Response)
{
bool FromBootstrapServer = this.client.IsFromBootstrapServer(Request);
if (this.id == 0 && !FromBootstrapServer)
{
Response.RST(CoapCode.Unauthorized);
return Task.CompletedTask;
}
if (!string.IsNullOrEmpty(Request.SubPath))
{
Response.RST(CoapCode.NotFound);
return Task.CompletedTask;
}
if (!Request.IsAcceptable(CoreLinkFormat.ContentFormatCode))
{
Response.RST(CoapCode.NotAcceptable);
return Task.CompletedTask;
}
StringBuilder Output = new StringBuilder();
this.EncodeObjectLinks(this.client.State == Lwm2mState.Bootstrap && FromBootstrapServer, Output);
Response.Respond(CoapCode.Content, Encoding.UTF8.GetBytes(Output.ToString()), 64,
new CoapOptionContentFormat(CoreLinkFormat.ContentFormatCode));
return Task.CompletedTask;
}
/// <inheritdoc/>
public override string ToString()
{
return this.Path + ": " + this.GetType().FullName;
}
/// <summary>
/// Event raised after the resource has been registered
/// </summary>
public event EventHandlerAsync OnAfterRegister = null;
/// <summary>
/// Called after the resource has been registered on a CoAP Endpoint.
/// </summary>
public virtual async Task AfterRegister()
{
foreach (Lwm2mObjectInstance Instance in this.Instances)
{
this.client.CoapEndpoint.Register(Instance);
await Instance.AfterRegister(this.client);
}
await this.OnAfterRegister.Raise(this, EventArgs.Empty);
}
}
}