-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathLwm2mDeviceObjectInstance.cs
More file actions
118 lines (105 loc) · 4.84 KB
/
Lwm2mDeviceObjectInstance.cs
File metadata and controls
118 lines (105 loc) · 4.84 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
using System;
using System.Text;
using System.Threading.Tasks;
using Waher.Networking.LWM2M.Events;
namespace Waher.Networking.LWM2M
{
/// <summary>
/// LWM2M Device object instance.
/// </summary>
public class Lwm2mDeviceObjectInstance : Lwm2mObjectInstance
{
private readonly Lwm2mResourceString manufacturer;
private readonly Lwm2mResourceString modelNr;
private readonly Lwm2mResourceString serialNr;
private readonly Lwm2mResourceString firmwareVersion;
private readonly Lwm2mResourceCommand reboot;
private readonly Lwm2mResourceInteger errorCodes; // TODO: Implement
private readonly Lwm2mResourceTime currentTime;
private readonly Lwm2mResourceString timeZone;
private readonly Lwm2mResourceString supportedBindings;
private readonly Lwm2mResourceString deviceType;
private readonly Lwm2mResourceString hardwareVersion;
private readonly Lwm2mResourceString softwareVersion;
/// <summary>
/// LWM2M Device object instance.
/// </summary>
public Lwm2mDeviceObjectInstance(string Manufacturer, string ModelNr, string SerialNr,
string FirmwareVersion, string DeviceType, string HardwareVersion,
string SoftwareVersion)
: base(3, 0)
{
DateTime Now = DateTime.Now;
TimeSpan TimeZone = Now - Now.ToUniversalTime();
StringBuilder sb = new StringBuilder();
if (TimeZone < TimeSpan.Zero)
{
sb.Append('-');
TimeZone = -TimeZone;
}
sb.Append(TimeZone.Hours.ToString("D2"));
sb.Append(':');
sb.Append(TimeZone.Minutes.ToString("D2"));
this.manufacturer = new Lwm2mResourceString("Manufacturer", 3, 0, 0, false, false, Manufacturer);
this.modelNr = new Lwm2mResourceString("Model Number", 3, 0, 1, false, false, ModelNr);
this.serialNr = new Lwm2mResourceString("Serial Number", 3, 0, 2, false, false, SerialNr);
this.firmwareVersion = new Lwm2mResourceString("Firmware Version", 3, 0, 3, false, false, FirmwareVersion);
this.reboot = new Lwm2mResourceCommand("Reboot", 3, 0, 4);
this.errorCodes = new Lwm2mResourceInteger("Error Code", 3, 0, 11, false, false, 0, true);
this.currentTime = new Lwm2mResourceTime("Current Time", 3, 0, 13, true, false, Now);
this.timeZone = new Lwm2mResourceString("Time Zone", 3, 0, 14, true, true, sb.ToString());
this.supportedBindings = new Lwm2mResourceString("Supported Bindings", 3, 0, 16, false, false, "U");
this.deviceType = new Lwm2mResourceString("Device Type", 3, 0, 17, false, false, DeviceType);
this.hardwareVersion = new Lwm2mResourceString("Hardware Version", 3, 0, 18, false, false, HardwareVersion);
this.softwareVersion = new Lwm2mResourceString("Software Version", 3, 0, 19, false, false, SoftwareVersion);
this.currentTime.OnAfterRegister += this.CurrentTime_OnAfterRegister;
this.currentTime.OnBeforeGet += this.CurrentTime_OnBeforeGet;
this.reboot.OnExecute += this.Reboot_OnExecute;
this.Add(this.manufacturer);
this.Add(this.modelNr);
this.Add(this.serialNr);
this.Add(this.firmwareVersion);
this.Add(this.reboot);
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 5)); // Factory Reset
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 6)); // Available Power Sources
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 7)); // Power Source Voltage
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 8)); // Power Source Current
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 9)); // Battery Level
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 10)); // Battery Level
this.Add(this.errorCodes);
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 12)); // Reset Error Code
this.Add(this.currentTime);
this.Add(this.timeZone);
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 15)); // Timezone
this.Add(this.supportedBindings);
this.Add(this.deviceType);
this.Add(this.hardwareVersion);
this.Add(this.softwareVersion);
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 20)); // Battery Status
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 21)); // Memory Total
this.Add(new Lwm2mResourceNotSupported(3, this.InstanceId, 22)); // ExtDevInfo
}
private Task Reboot_OnExecute(object Sender, EventArgs e)
{
return this.Object.Client.Reboot();
}
private Task CurrentTime_OnAfterRegister(object Sender, EventArgs e)
{
return this.currentTime.TriggerAll(new TimeSpan(0, 0, 1));
}
private Task CurrentTime_OnBeforeGet(object Sender, CoapRequestEventArgs e)
{
this.currentTime.TimeValue = DateTime.Now;
return Task.CompletedTask;
}
/// <summary>
/// Called after the resource has been registered on a CoAP Endpoint.
/// </summary>
/// <param name="Client">LWM2M Client</param>
public override async Task AfterRegister(Lwm2mClient Client)
{
await base.AfterRegister(Client);
await this.TriggerAll(new TimeSpan(0, 0, 1));
}
}
}