Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScanSource.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Events;
8
10{
15 {
16 private readonly ConcentratorSourceNode sourceNode;
17
24 : base(Concentrator, "1")
25 {
26 this.sourceNode = SourceNode;
27 }
28
32 public override string CommandID => nameof(ScanSource);
33
37 public override CommandType Type => CommandType.Simple;
38
43 public override Task<string> GetNameAsync(Language Language)
44 {
45 return Language.GetStringAsync(typeof(ConcentratorDevice), 53, "Scan Source");
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 await this.ScanChildSources(Client, FullJid);
65 await this.ScanRootNodes(Client, FullJid);
66 }
67 catch (Exception ex)
68 {
69 Log.Exception(ex);
70 }
71 }
72
73 private async Task ScanChildSources(ConcentratorClient Client, string FullJid)
74 {
75 DataSourceReference[] Sources = await Client.GetChildDataSourcesAsync(
76 FullJid, this.sourceNode.RemoteSourceID);
77
78 Dictionary<string, ConcentratorSourceNode> BySourceId = new Dictionary<string, ConcentratorSourceNode>();
79
80 foreach (INode Child in await this.sourceNode.ChildNodes)
81 {
82 if (Child is ConcentratorSourceNode SourceNode)
83 BySourceId[SourceNode.RemoteSourceID] = SourceNode;
84 }
85
86 LinkedList<ScanSource> NewScans = null;
87
88 foreach (DataSourceReference Source in Sources)
89 {
90 if (BySourceId.ContainsKey(Source.SourceID))
91 continue;
92
93 ConcentratorSourceNode SourceNode = new ConcentratorSourceNode()
94 {
95 NodeId = await MeteringNode.GetUniqueNodeId(Source.SourceID),
96 RemoteSourceID = Source.SourceID
97 };
98
99 await this.sourceNode.AddAsync(SourceNode);
100
101 BySourceId[Source.SourceID] = SourceNode;
102
103 if (NewScans is null)
104 NewScans = new LinkedList<ScanSource>();
105
106 NewScans.AddLast(new ScanSource(this.Concentrator, SourceNode));
107 }
108
109 if (!(NewScans is null))
110 {
111 foreach (ScanSource ScanSource in NewScans)
112 await ScanSource.ExecuteCommandAsync();
113 }
114 }
115
116 private async Task ScanRootNodes(ConcentratorClient Client, string FullJid)
117 {
118 NodeInformation[] Nodes = await Client.GetRootNodesAsync(
119 FullJid, this.sourceNode.RemoteSourceID, false, false,
120 string.Empty, string.Empty, string.Empty, string.Empty);
121
122 Dictionary<string, ConcentratorNode> ByNodeId = new Dictionary<string, ConcentratorNode>();
123
124 foreach (INode Child in await this.sourceNode.ChildNodes)
125 {
126 if (Child is ConcentratorNode XmppNode)
127 ByNodeId[XmppNode.RemoteNodeID] = XmppNode;
128 }
129
130 LinkedList<ScanNode> NewScans = null;
131
132 foreach (NodeInformation Node in Nodes)
133 {
134 if (ByNodeId.ContainsKey(Node.NodeId))
135 continue;
136
137 ConcentratorNode NewNode;
138
139 if (Node.IsReadable)
140 {
141 NewNode = new SensorNode()
142 {
143 NodeId = await MeteringNode.GetUniqueNodeId(Node.NodeId),
144 RemoteNodeID = Node.NodeId
145 };
146 }
147 else
148 {
149 NewNode = new ConcentratorNode()
150 {
151 NodeId = await MeteringNode.GetUniqueNodeId(Node.NodeId),
152 RemoteNodeID = Node.NodeId
153 };
154 }
155
156 await this.sourceNode.AddAsync(NewNode);
157
158 ByNodeId[Node.NodeId] = NewNode;
159
160 if (this.sourceNode.RemoteSourceID == MeteringTopology.SourceID)
161 {
162 if (NewScans is null)
163 NewScans = new LinkedList<ScanNode>();
164
165 NewScans.AddLast(new ScanNode(this.Concentrator, NewNode));
166 }
167 }
168
169 if (!(NewScans is null))
170 {
171 foreach (ScanNode ScanNode in NewScans)
172 await ScanNode.ExecuteCommandAsync();
173 }
174 }
175
180 public override ICommand Copy()
181 {
182 return new ScanSource(this.Concentrator, this.sourceNode);
183 }
184 }
185}
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.
async Task< NodeInformation[]> GetRootNodesAsync(string To, string SourceID, bool Parameters, bool Messages, string Language, string ServiceToken, string DeviceToken, string UserToken)
Gets information about all root nodes in a data source.
async Task< DataSourceReference[]> GetChildDataSourcesAsync(string To, string SourceID)
Gets all child data sources for a data source on the server.
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.
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
const string SourceID
Source ID for the metering topology data source.
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 source node on a concentrator node for its child sources and root nodes.
Definition: ScanSource.cs:15
override Task ExecuteCommandAsync()
Executes the command.
Definition: ScanSource.cs:51
override Task< string > GetNameAsync(Language Language)
Gets the name of data source.
Definition: ScanSource.cs:43
override ICommand Copy()
Creates a copy of the command object.
Definition: ScanSource.cs:180
ScanSource(ConcentratorDevice Concentrator, ConcentratorSourceNode SourceNode)
Scans a source node on a concentrator node for its child sources and root nodes.
Definition: ScanSource.cs:23
override string CommandID
ID of command.
Definition: ScanSource.cs:32
override CommandType Type
Type of command.
Definition: ScanSource.cs:37
Node representing 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
CommandType
Command type.
Definition: ICommand.cs:11