Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScanNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Events;
8
10{
15 {
16 private readonly ConcentratorNode node;
17
24 : base(Concentrator, "1")
25 {
26 this.node = Node;
27 }
28
32 public override string CommandID => nameof(ScanNode);
33
37 public override CommandType Type => CommandType.Simple;
38
43 public override Task<string> GetNameAsync(Language Language)
44 {
45 return Language.GetStringAsync(typeof(ConcentratorDevice), 55, "Scan Node");
46 }
47
51 public override Task ExecuteCommandAsync()
52 {
53 this.StartSearch();
54 return Task.CompletedTask;
55 }
56
57 private async void StartSearch()
58 {
59 try
60 {
61 ConcentratorClient Client = await this.GetConcentratorClient();
62 string FullJid = this.GetRemoteFullJid(Client.Client);
63
64 string RemoteSourceID = string.Empty;
65 string RemotePartition = string.Empty;
66 INode Loop = await this.node.GetParent();
67
68 while (!(Loop is null))
69 {
70 if (Loop is ConcentratorPartitionNode PartitionNode)
71 RemotePartition = PartitionNode.RemotePartitionID;
72 else if (Loop is ConcentratorSourceNode SourceNode)
73 {
74 RemoteSourceID = SourceNode.RemoteSourceID;
75 break;
76 }
77
78 if (Loop is MeteringNode MeteringNode)
79 Loop = await MeteringNode.GetParent();
80 else
81 Loop = Loop.Parent;
82 }
83
84 NodeInformation[] Nodes = await Client.GetChildNodesAsync(FullJid,
85 this.node.RemoteNodeID, RemoteSourceID, RemotePartition,
86 false, false, string.Empty, string.Empty, string.Empty, string.Empty);
87
88 Dictionary<string, ConcentratorNode> ByNodeId = new Dictionary<string, ConcentratorNode>();
89
90 foreach (INode Child in await this.node.ChildNodes)
91 {
92 if (Child is ConcentratorNode Node)
93 ByNodeId[Node.RemoteNodeID] = Node;
94 }
95
96 LinkedList<ScanNode> NewScans = null;
97
98 foreach (NodeInformation Node in Nodes)
99 {
100 if (ByNodeId.ContainsKey(Node.NodeId))
101 continue;
102
103 ConcentratorNode NewNode;
104
105 if (Node.IsReadable)
106 {
107 NewNode = new SensorNode()
108 {
109 NodeId = await MeteringNode.GetUniqueNodeId(Node.NodeId),
110 RemoteNodeID = Node.NodeId
111 };
112 }
113 else
114 {
115 NewNode = new ConcentratorNode()
116 {
117 NodeId = await MeteringNode.GetUniqueNodeId(Node.NodeId),
118 RemoteNodeID = Node.NodeId
119 };
120 }
121
122 await this.node.AddAsync(NewNode);
123
124 ByNodeId[Node.NodeId] = NewNode;
125
126 if (NewScans is null)
127 NewScans = new LinkedList<ScanNode>();
128
129 NewScans.AddLast(new ScanNode(this.Concentrator, NewNode));
130 }
131
132 if (!(NewScans is null))
133 {
134 foreach (ScanNode ScanNode in NewScans)
135 await ScanNode.ExecuteCommandAsync();
136 }
137 }
138 catch (Exception ex)
139 {
140 Log.Exception(ex);
141 }
142 }
143
148 public override ICommand Copy()
149 {
150 return new ScanNode(this.Concentrator, this.node);
151 }
152 }
153}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Implements an XMPP concentrator client interface.
Task< NodeInformation[]> GetChildNodesAsync(string To, IThingReference Node, bool Parameters, bool Messages, string Language, string ServiceToken, string DeviceToken, string UserToken)
Gets information about all child nodes of a node in a data source.
XmppClient Client
XMPP Client.
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
Base class for all metering nodes.
Definition: MeteringNode.cs:28
static async Task< string > GetUniqueNodeId(string NodeId)
Gets a Node ID, based on NodeId that is not already available in the database.
async Task< INode > GetParent()
Gets the parent of the node.
Abstract base class for concentrator commands.
ConcentratorDevice Concentrator
Reference to the concentrator node.
async Task< ConcentratorClient > GetConcentratorClient()
Gets the concentrator client, if it exists.
string GetRemoteFullJid(XmppClient Client)
Gets the Full JID of the connected device.
Scans a node on a concentrator node for its child nodes.
Definition: ScanNode.cs:15
override ICommand Copy()
Creates a copy of the command object.
Definition: ScanNode.cs:148
ScanNode(ConcentratorDevice Concentrator, ConcentratorNode Node)
Scans a node on a concentrator node for its child nodes.
Definition: ScanNode.cs:23
override Task< string > GetNameAsync(Language Language)
Gets the name of data source.
Definition: ScanNode.cs:43
override string CommandID
ID of command.
Definition: ScanNode.cs:32
override Task ExecuteCommandAsync()
Executes the command.
Definition: ScanNode.cs:51
override CommandType Type
Type of command.
Definition: ScanNode.cs:37
Node representing an XMPP concentrator.
Base class for nodes in a remote concentrator.
Node representing a partition in a data source in an XMPP concentrator.
Node representing a data source in an XMPP concentrator.
Interface for commands.
Definition: ICommand.cs:32
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
Definition: INode.cs:140
INode Parent
Parent Node, or null if a root node.
Definition: INode.cs:116
CommandType
Command type.
Definition: ICommand.cs:11