forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProvisionedMeteringNode.cs
More file actions
209 lines (182 loc) · 6.22 KB
/
ProvisionedMeteringNode.cs
File metadata and controls
209 lines (182 loc) · 6.22 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Networking.XMPP.DataForms;
using Waher.Networking.XMPP.DataForms.FieldTypes;
using Waher.Networking.XMPP.DataForms.Layout;
using Waher.Persistence;
using Waher.Persistence.Attributes;
using Waher.Persistence.Filters;
using Waher.Runtime.Inventory;
using Waher.Runtime.Language;
using Waher.Runtime.Settings;
using Waher.Things.Attributes;
using Waher.Things.DisplayableParameters;
namespace Waher.Things.Metering
{
/// <summary>
/// Base class for all provisioned metering nodes.
/// </summary>
public abstract class ProvisionedMeteringNode : MetaMeteringNode, IPropertyFormAnnotation
{
private string owner = string.Empty;
private bool provisioned = false;
private bool isPublic = false;
/// <summary>
/// Base class for all provisioned metering nodes.
/// </summary>
public ProvisionedMeteringNode()
: base()
{
}
/// <summary>
/// If the node is provisioned is not. Property is editable.
/// </summary>
[Page(18, "Provisioning", 50)]
[Header(19, "Provision node.")]
[ToolTip(20, "If checked, the node will be registered in the Thing Registry (if available), and access rights will be controlled by the corresponding owner (if a Provisioning Server is available).")]
[DefaultValue(false)]
public bool Provisioned
{
get { return this.provisioned; }
set
{
if (this.provisioned && !value && !string.IsNullOrEmpty(this.owner))
throw new Exception("Device is owned by " + this.owner + ". Device must be disowned first.");
this.provisioned = value;
}
}
/// <summary>
/// If the node is provisioned is not. Property is editable.
/// </summary>
[Page(18, "Provisioning", 50)]
[Header(21, "Owner:")]
[ToolTip(22, "Communication address of owner.")]
[DefaultValueStringEmpty]
[ReadOnly]
public string OwnerAddress
{
get { return this.owner; }
set
{
if (this.provisioned && !string.IsNullOrEmpty(this.owner) && this.owner != value)
throw new Exception("Device is owned by " + this.owner + ". Device must be disowned first.");
this.owner = value;
}
}
/// <summary>
/// If the node is public in the regitry or not.
/// </summary>
[Page(18, "Provisioning", 50)]
[Header(23, "Public node.")]
[ToolTip(24, "If the node is registered as a public node in the Thing Registry.")]
[DefaultValue(false)]
[ReadOnly]
public bool Public
{
get { return this.isPublic; }
set
{
if (this.provisioned && !value && this.isPublic && !string.IsNullOrEmpty(this.owner))
throw new Exception("Device is owned by " + this.owner + ". Device must be disowned first.");
this.isPublic = value;
}
}
/// <summary>
/// If node can be provisioned.
/// </summary>
public override bool IsProvisioned => this.provisioned;
/// <summary>
/// Who the owner of the node is. The empty string means the node has no owner.
/// </summary>
public override string Owner => this.owner;
/// <summary>
/// If the node is public.
/// </summary>
public override bool IsPublic => this.isPublic;
/// <summary>
/// Gets displayable parameters.
/// </summary>
/// <param name="Language">Language to use.</param>
/// <param name="Caller">Information about caller.</param>
/// <returns>Set of displayable parameters.</returns>
public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
{
LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
Result.AddLast(new BooleanParameter("Provisioned", await Language.GetStringAsync(typeof(MeteringTopology), 25, "Provisioned"), this.provisioned));
if (this.provisioned)
{
Result.AddLast(new StringParameter("Owner", await Language.GetStringAsync(typeof(MeteringTopology), 26, "Owner"), this.owner));
Result.AddLast(new BooleanParameter("Public", await Language.GetStringAsync(typeof(MeteringTopology), 27, "Public"), this.isPublic));
}
return Result;
}
/// <summary>
/// Called when node has been claimed by an owner.
/// </summary>
/// <param name="Owner">Owner</param>
/// <param name="IsPublic">If node is public.</param>
public override Task Claimed(string Owner, bool IsPublic)
{
this.owner = Owner;
this.isPublic = IsPublic;
if (this.ObjectId != Guid.Empty)
return this.NodeUpdated();
else
return Task.CompletedTask;
}
/// <summary>
/// Called when node has been disowned by its owner.
/// </summary>
public override Task Disowned()
{
this.owner = string.Empty;
this.isPublic = false;
if (this.ObjectId != Guid.Empty)
return this.NodeUpdated();
else
return Task.CompletedTask;
}
/// <summary>
/// Called when node has been removed from the registry.
/// </summary>
public override Task Removed()
{
this.isPublic = false;
if (this.ObjectId != Guid.Empty)
return this.NodeUpdated();
else
return Task.CompletedTask;
}
/// <summary>
/// Annotates the property form.
/// </summary>
/// <param name="Form">Form being built.</param>
public virtual async Task AnnotatePropertyForm(FormState Form)
{
if (this.provisioned && string.IsNullOrEmpty(this.owner))
{
string Uri = await RuntimeSettings.GetAsync("IoTDisco.KEY." + this.NodeId + "." + this.SourceId + "." + this.Partition, string.Empty);
if (!string.IsNullOrEmpty(Uri))
{
Language Language = await Translator.GetLanguageAsync(Form.LanguageCode);
Namespace Namespace = await Language.GetNamespaceAsync(typeof(MeteringTopology).Namespace);
Field Field;
Field = new TextMultiField(Form.Form, "IoTDiscoUri", await Namespace.GetStringAsync(94, "URI to claim node:"), false,
new string[] { Uri }, null, await Namespace.GetStringAsync(95, "The owner can use this URI to claim ownership of the node."),
null, null, null, false, true, false)
{
Priority = HeaderAttribute.DefaultPriority,
Ordinal = Form.FieldOrdinal++
};
Form.Fields.Add(Field);
if (Form.PageByLabel.TryGetValue(await Namespace.GetStringAsync(18, "Provisioning"), out Page Page))
Page.Add(new FieldReference(Form.Form, Field.Var));
}
}
}
}
}