Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProvisionedMeteringNode.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Text;
5using System.Threading.Tasks;
7using Waher.Events;
17
19{
24 {
25 private string owner = string.Empty;
26 private bool provisioned = false;
27 private bool isPublic = false;
28
33 : base()
34 {
35 }
36
40 [Page(18, "Provisioning", 50)]
41 [Header(19, "Provision node.")]
42 [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).")]
43 [DefaultValue(false)]
44 public bool Provisioned
45 {
46 get => this.provisioned;
47 set
48 {
49 if (this.provisioned && !value && !string.IsNullOrEmpty(this.owner))
50 throw new Exception("Device is owned by " + this.owner + ". Device must be disowned first.");
51
52 this.provisioned = value;
53 }
54 }
55
59 [Page(18, "Provisioning", 50)]
60 [Header(21, "Owner:")]
61 [ToolTip(22, "Communication address of owner.")]
62 [DefaultValueStringEmpty]
63 [ReadOnly]
64 public string OwnerAddress
65 {
66 get => this.owner;
67 set
68 {
69 if (this.provisioned && !string.IsNullOrEmpty(this.owner) && this.owner != value)
70 throw new Exception("Device is owned by " + this.owner + ". Device must be disowned first.");
71
72 this.owner = value;
73 }
74 }
75
79 [Page(18, "Provisioning", 50)]
80 [Header(23, "Public node.")]
81 [ToolTip(24, "If the node is registered as a public node in the Thing Registry.")]
82 [DefaultValue(false)]
83 [ReadOnly]
84 public bool Public
85 {
86 get => this.isPublic;
87 set
88 {
89 if (this.provisioned && !value && this.isPublic && !string.IsNullOrEmpty(this.owner))
90 throw new Exception("Device is owned by " + this.owner + ". Device must be disowned first.");
91
92 this.isPublic = value;
93 }
94 }
95
99 public override bool IsProvisioned => this.provisioned;
100
104 public override string Owner => this.owner;
105
109 public override bool IsPublic => this.isPublic;
110
117 public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
118 {
119 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
120
121 Result.AddLast(new BooleanParameter("Provisioned", await Language.GetStringAsync(typeof(MeteringTopology), 25, "Provisioned"), this.provisioned));
122
123 if (this.provisioned)
124 {
125 Result.AddLast(new StringParameter("Owner", await Language.GetStringAsync(typeof(MeteringTopology), 26, "Owner"), this.owner));
126 Result.AddLast(new BooleanParameter("Public", await Language.GetStringAsync(typeof(MeteringTopology), 27, "Public"), this.isPublic));
127 }
128
129 return Result;
130 }
131
137 public override Task Claimed(string Owner, bool IsPublic)
138 {
139 this.owner = Owner;
140 this.isPublic = IsPublic;
141
142 if (this.ObjectId != Guid.Empty)
143 return this.NodeUpdated();
144 else
145 return Task.CompletedTask;
146 }
147
151 public override Task Disowned()
152 {
153 this.owner = string.Empty;
154 this.isPublic = false;
155
156 if (this.ObjectId != Guid.Empty)
157 return this.NodeUpdated();
158 else
159 return Task.CompletedTask;
160 }
161
165 public override Task Removed()
166 {
167 this.isPublic = false;
168
169 if (this.ObjectId != Guid.Empty)
170 return this.NodeUpdated();
171 else
172 return Task.CompletedTask;
173 }
174
179 public Task<string> GetDiscoUri()
180 {
181 return this.GetDiscoUri(false);
182 }
183
189 public async Task<string> GetDiscoUri(bool OnlyClaim)
190 {
191 string s = await RuntimeSettings.GetAsync("IoTDisco.KEY." + this.NodeId + "." + this.SourceId + "." + this.Partition, string.Empty);
192 if (!string.IsNullOrEmpty(s))
193 return s;
194
195 if (OnlyClaim)
196 return string.Empty;
197
198 if (!Types.TryGetModuleParameter("XMPP", out object Obj))
199 return string.Empty;
200
201 Type T = Obj.GetType();
202 PropertyInfo PI = T.GetRuntimeProperty("BareJID");
203 string BareJid = PI?.GetValue(Obj) as string;
204
205 if (string.IsNullOrEmpty(BareJid))
206 return string.Empty;
207
208 StringBuilder sb = new StringBuilder();
209
210 sb.Append("iotdisco:JID=");
211 sb.Append(Uri.EscapeDataString(BareJid));
212 sb.Append(";NID=");
213 sb.Append(Uri.EscapeDataString(this.NodeId));
214 sb.Append(";SID=");
215 sb.Append(Uri.EscapeDataString(this.SourceId));
216
217 if (!string.IsNullOrEmpty(this.Partition))
218 {
219 sb.Append(";PT=");
220 sb.Append(Uri.EscapeDataString(this.Partition));
221 }
222
223 return sb.ToString();
224 }
225
230 public virtual async Task AnnotatePropertyForm(FormState Form)
231 {
232 if (this.provisioned && string.IsNullOrEmpty(this.owner))
233 {
234 string Uri = await this.GetDiscoUri(true);
235 if (!string.IsNullOrEmpty(Uri))
236 {
239 Field Field;
240
241 Field = new TextMultiField(Form.Form, "IoTDiscoUri", await Namespace.GetStringAsync(94, "URI to claim node:"), false,
242 new string[] { Uri }, null, await Namespace.GetStringAsync(95, "The owner can use this URI to claim ownership of the node."),
243 null, null, null, false, true, false)
244 {
246 Ordinal = Form.FieldOrdinal++
247 };
248
249 Form.Fields.Add(Field);
250
251 if (Form.PageByLabel.TryGetValue(await Namespace.GetStringAsync(18, "Provisioning"), out Page Page))
252 Page.Add(new FieldReference(Form.Form, Field.Var));
253
255
256 await QrCodeUrlRequested.Raise(this, e);
257
258 if (!string.IsNullOrEmpty(e.Url))
259 {
260 MediaField MediaField = new MediaField("QrCode", new Media(e.Url, ImageCodec.ContentTypePng, 400, 400))
261 {
263 Ordinal = Form.FieldOrdinal++
264 };
265
266 Form.Fields.Add(MediaField);
267
269 }
270 }
271 }
272 }
273
277 public static event EventHandlerAsync<GetQrCodeUrlEventArgs> QrCodeUrlRequested;
278 }
279}
Image encoder/decoder.
Definition: ImageCodec.cs:14
const string ContentTypePng
image/png
Definition: ImageCodec.cs:30
Base class for form fields
Definition: Field.cs:16
string Var
Variable name
Definition: Field.cs:81
Current state of a property form being built.
Dictionary< string, Page > PageByLabel
Pages by page label.
Class managing a page in a data form layout.
Definition: Page.cs:11
void Add(LayoutElement Element)
Adds a layout element.
Definition: Section.cs:135
Class containing information about media content in a data form.
Definition: Media.cs:18
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:583
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
async Task< Namespace > GetNamespaceAsync(string Name)
Gets the namespace object, given its name, if available.
Definition: Language.cs:99
Contains information about a namespace in a language.
Definition: Namespace.cs:17
Task< LanguageString > GetStringAsync(int Id)
Gets the string object, given its ID, if available.
Definition: Namespace.cs:65
Basic access point for runtime language localization.
Definition: Translator.cs:16
static async Task< Language > GetLanguageAsync(string Code)
Gets the languge object, given its language code, if available.
Definition: Translator.cs:42
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
Defines a localizable header string for the property.
const int DefaultPriority
Default priority of parameters (100).
Event arguments for events that request an URL to a QR code.
Base class for metering nodes with interoperable meta-information.
virtual async Task NodeUpdated()
Persists changes to the node, and generates a node updated event.
Guid ObjectId
Object ID in persistence layer.
Definition: MeteringNode.cs:90
string SourceId
Optional ID of source containing node.
string Partition
Optional partition in which the Node ID is unique.
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
Base class for all provisioned metering nodes.
bool Public
If the node is public in the regitry or not.
bool Provisioned
If the node is provisioned is not. Property is editable.
ProvisionedMeteringNode()
Base class for all provisioned metering nodes.
override Task Disowned()
Called when node has been disowned by its owner.
virtual async Task AnnotatePropertyForm(FormState Form)
Annotates the property form.
override Task Removed()
Called when node has been removed from the registry.
override Task Claimed(string Owner, bool IsPublic)
Called when node has been claimed by an owner.
override async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
string OwnerAddress
If the node is provisioned is not. Property is editable.
static EventHandlerAsync< GetQrCodeUrlEventArgs > QrCodeUrlRequested
Event raised when a QR code URL is requested.
override string Owner
Who the owner of the node is. The empty string means the node has no owner.
Task< string > GetDiscoUri()
Gets a discovery URI for the thing.
override bool IsProvisioned
If node can be provisioned.
override bool IsPublic
If the node is public.
async Task< string > GetDiscoUri(bool OnlyClaim)
Gets a discovery URI for the thing.
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for objects that want to annotate their property forms.