forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLwm2mServerObject.cs
More file actions
113 lines (100 loc) · 2.8 KB
/
Lwm2mServerObject.cs
File metadata and controls
113 lines (100 loc) · 2.8 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Networking.CoAP;
using Waher.Persistence;
using Waher.Persistence.Filters;
namespace Waher.Networking.LWM2M
{
/// <summary>
/// LWM2M Server object.
/// </summary>
public class Lwm2mServerObject : Lwm2mObject, ICoapPutMethod, ICoapPostMethod
{
/// <summary>
/// LWM2M Server object.
/// </summary>
public Lwm2mServerObject()
: base(1)
{
}
/// <summary>
/// Loads any Bootstrap information.
/// </summary>
public override async Task LoadBootstrapInfo()
{
this.ClearInstances();
foreach (Lwm2mServerObjectInstance Instance in await Database.Find<Lwm2mServerObjectInstance>(
new FilterFieldEqualTo("Id", this.Id), "InstanceId"))
{
try
{
this.Add(Instance);
}
catch (Exception)
{
await Database.Delete(Instance);
}
}
await base.LoadBootstrapInfo();
}
/// <summary>
/// Deletes any Bootstrap information.
/// </summary>
public override async Task DeleteBootstrapInfo()
{
await base.DeleteBootstrapInfo();
this.ClearInstances();
}
/// <summary>
/// If the resource handles subpaths.
/// </summary>
public override bool HandlesSubPaths => true;
/// <summary>
/// If the PUT method is allowed.
/// </summary>
public bool AllowsPUT => true;
/// <summary>
/// Executes the PUT 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 void PUT(CoapMessage Request, CoapResponse Response)
{
if (this.Client.State == Lwm2mState.Bootstrap &&
this.Client.IsFromBootstrapServer(Request))
{
if (!string.IsNullOrEmpty(Request.SubPath) &&
ushort.TryParse(Request.SubPath.Substring(1), out ushort InstanceId))
{
Lwm2mServerObjectInstance Instance = new Lwm2mServerObjectInstance(InstanceId);
this.Add(Instance);
this.Client.Endpoint.Register(Instance);
Instance.AfterRegister(this.Client);
Request.Path += Request.SubPath;
Request.SubPath = string.Empty;
Instance.PUT(Request, Response);
}
else
Response.RST(CoapCode.BadRequest);
}
else
Response.RST(CoapCode.Unauthorized);
}
/// <summary>
/// If the POST method is allowed.
/// </summary>
public bool AllowsPOST => this.AllowsPUT;
/// <summary>
/// Executes the POST 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 void POST(CoapMessage Request, CoapResponse Response)
{
this.PUT(Request, Response);
}
}
}