Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DataSourceGraph.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using System.Web;
13
15{
20 {
25 {
26 }
27
33 public Grade Supports(Uri GraphUri)
34 {
35 if (!Gateway.IsDomain(GraphUri.Host, true) ||
36 !string.IsNullOrEmpty(GraphUri.Query) ||
37 !string.IsNullOrEmpty(GraphUri.Fragment) ||
39 {
40 return Grade.NotAtAll;
41 }
42
43 string s = GraphUri.AbsolutePath;
44 string[] Parts = s.Split('/');
45 int c = Parts.Length;
46
47 if (c < 1 || !string.IsNullOrEmpty(Parts[0]))
48 return Grade.NotAtAll;
49
50 if (string.IsNullOrEmpty(Parts[c - 1]))
51 c--;
52
53 if (c == 1)
54 return Grade.Ok;
55
56 if (!Gateway.ConcentratorServer.TryGetDataSource(HttpUtility.UrlDecode(Parts[1]), out _))
57 return Grade.NotAtAll;
58
59 return Grade.Excellent;
60 }
61
70 public async Task<ISemanticCube> LoadGraph(Uri GraphUri, ScriptNode Node, bool NullIfNotFound,
71 RequestOrigin Caller)
72 {
73 if (!Gateway.IsDomain(GraphUri.Host, true) ||
74 !string.IsNullOrEmpty(GraphUri.Query) ||
75 !string.IsNullOrEmpty(GraphUri.Fragment) ||
77 {
78 if (NullIfNotFound)
79 return null;
80 else
81 throw new NotFoundException("Graph not found.");
82 }
83
84 IDynamicGraph DynamicGraph = await FindDynamicGraph(GraphUri.AbsolutePath, Caller);
85 if (DynamicGraph is null)
86 {
87 if (NullIfNotFound)
88 return null;
89 else
90 throw new NotFoundException("Graph not found.");
91 }
92
93 // TODO: Check access rights
94
96 Language Language = await Translator.GetDefaultLanguageAsync(); // TODO: Check Accept-Language HTTP header.
97
98 await DynamicGraph.GenerateGraph(Result, Language, Caller);
99
100 return Result;
101 }
102
109 public static async Task<IDynamicGraph> FindDynamicGraph(string Path, RequestOrigin Caller)
110 {
111 string[] Parts = Path.Split('/');
112 int c = Parts.Length;
113
114 if (c < 1 || !string.IsNullOrEmpty(Parts[0]))
115 return null;
116
117 if (string.IsNullOrEmpty(Parts[c - 1]))
118 c--;
119
120 if (c == 1)
121 return new MachineGraph();
122
123 string SourceID = HttpUtility.UrlDecode(Parts[1]);
124 if (!Gateway.ConcentratorServer.TryGetDataSource(SourceID, out IDataSource Source))
125 return null;
126
127 if (!await Source.CanViewAsync(Caller))
128 return null;
129
130 if (c == 2) // DOMAIN/Source
131 return new SourceGraph(Source);
132
133 switch (c)
134 {
135 case 3: // /DOMAIN/Source/NodeID
136 string NodeID = HttpUtility.UrlDecode(Parts[2]);
137 INode NodeObj = await Source.GetNodeAsync(new ThingReference(NodeID, SourceID));
138 if (NodeObj is null)
139 return null;
140
141 if (!await NodeObj.CanViewAsync(Caller))
142 return null;
143
144 return new NodeGraph(NodeObj);
145
146 case 4: // /DOMAIN/Source/Partition/NodeID
147 string Partition = HttpUtility.UrlDecode(Parts[2]);
148 NodeID = HttpUtility.UrlDecode(Parts[3]);
149 NodeObj = await Source.GetNodeAsync(new ThingReference(NodeID, SourceID, Partition));
150 if (NodeObj is null)
151 return null;
152
153 if (!await NodeObj.CanViewAsync(Caller))
154 return null;
155
156 return new NodeGraph(NodeObj);
157
158 case 5: // /DOMAIN/Source/NodeID/Category/Action
159 NodeID = HttpUtility.UrlDecode(Parts[2]);
160 NodeObj = await Source.GetNodeAsync(new ThingReference(NodeID, SourceID));
161 if (NodeObj is null)
162 return null;
163
164 if (!await NodeObj.CanViewAsync(Caller))
165 return null;
166
167 return HttpUtility.UrlDecode(Parts[3]) switch
168 {
169 "read" => new NodeReadActionGraph(NodeObj, HttpUtility.UrlDecode(Parts[4])),
170 // TODO: cmd, edit, control
171 _ => null,
172 };
173 case 6: // /DOMAIN/Source/Partition/NodeID/Category/Action
174 Partition = HttpUtility.UrlDecode(Parts[2]);
175 NodeID = HttpUtility.UrlDecode(Parts[3]);
176 NodeObj = await Source.GetNodeAsync(new ThingReference(NodeID, SourceID, Partition));
177 if (NodeObj is null)
178 return null;
179
180 if (!await NodeObj.CanViewAsync(Caller))
181 return null;
182
183 return HttpUtility.UrlDecode(Parts[4]) switch
184 {
185 "read" => new NodeReadActionGraph(NodeObj, HttpUtility.UrlDecode(Parts[5])),
186 // TODO: cmd, edit, control
187 _ => null,
188 };
189 default:
190 return null;
191 }
192 }
193
199 public static Uri GetSourceUri(string SourceID)
200 {
201 return new Uri(Gateway.GetUrl("/" + HttpUtility.UrlEncode(SourceID)));
202 }
203
209 public static Uri GetNodeUri(INode Node)
210 {
211 StringBuilder sb = new StringBuilder();
212 GetNodeUri(Node, sb);
213 return new Uri(Gateway.GetUrl(sb.ToString()));
214 }
215
221 public static void GetNodeUri(INode Node, StringBuilder sb)
222 {
223 sb.Append('/');
224 sb.Append(HttpUtility.UrlEncode(Node.SourceId));
225
226 if (!string.IsNullOrEmpty(Node.Partition))
227 {
228 sb.Append('/');
229 sb.Append(HttpUtility.UrlEncode(Node.Partition));
230 }
231
232 sb.Append('/');
233 sb.Append(HttpUtility.UrlEncode(Node.NodeId));
234 }
235
242 public static Uri GetCommandUri(INode Node, ICommand Command)
243 {
244 StringBuilder sb = new StringBuilder();
245 GetCommandUri(Node, Command, sb);
246 return new Uri(Gateway.GetUrl(sb.ToString()));
247 }
248
255 public static void GetCommandUri(INode Node, ICommand Command, StringBuilder sb)
256 {
257 GetNodeUri(Node, sb);
258 sb.Append("/cmd/");
259 sb.Append(HttpUtility.UrlEncode(Command.CommandID));
260 }
261
268 public static Uri GetOperationUri(INode Node, string Category, string Action)
269 {
270 StringBuilder sb = new StringBuilder();
271 GetOperationUri(Node, Category, Action, sb);
272 return new Uri(Gateway.GetUrl(sb.ToString()));
273 }
274
282 public static void GetOperationUri(INode Node, string Category, string Action, StringBuilder sb)
283 {
284 GetNodeUri(Node, sb);
285 sb.Append('/');
286 sb.Append(Category);
287 sb.Append('/');
288 sb.Append(Action);
289 }
290
291 }
292}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static bool IsDomain(string DomainOrHost, bool IncludeAlternativeDomains)
If a domain or host name represents the gateway.
Definition: Gateway.cs:5174
static string GetUrl(string LocalResource)
Gets a URL for a resource.
Definition: Gateway.cs:5079
static ConcentratorServer ConcentratorServer
XMPP Concentrator Server.
Definition: Gateway.cs:4053
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Contains information about a language.
Definition: Language.cs:17
Basic access point for runtime language localization.
Definition: Translator.cs:16
static async Task< Language > GetDefaultLanguageAsync()
Gets the default language.
Definition: Translator.cs:223
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Tokens available in request.
Definition: RequestOrigin.cs:9
Makes harmonized data sources and nodes available as semantic information.
async Task< ISemanticCube > LoadGraph(Uri GraphUri, ScriptNode Node, bool NullIfNotFound, RequestOrigin Caller)
Loads the graph
DataSourceGraph()
Makes harmonized data sources and nodes available as semantic information.
static Uri GetCommandUri(INode Node, ICommand Command)
Gets the Graph URI for a node command.
static Uri GetNodeUri(INode Node)
Gets the Graph URI for a node.
static void GetCommandUri(INode Node, ICommand Command, StringBuilder sb)
Gets the Graph URI for a node command.
static Uri GetSourceUri(string SourceID)
Gets the Graph URI for a data source.
static void GetNodeUri(INode Node, StringBuilder sb)
Gets the Graph URI for a node.
Grade Supports(Uri GraphUri)
How well the source handles a given Graph URI.
static void GetOperationUri(INode Node, string Category, string Action, StringBuilder sb)
Gets the Graph URI for a node operation.
static async Task< IDynamicGraph > FindDynamicGraph(string Path, RequestOrigin Caller)
Finds a dynamic graph, based on its relative path.
static Uri GetOperationUri(INode Node, string Category, string Action)
Gets the Graph URI for a node operation.
Dynamic graph of a node in a data source on the Neuron.
Definition: NodeGraph.cs:18
Dynamic graph of a data source on the Neuron.
Definition: SourceGraph.cs:16
Contains a reference to a thing
Interface for commands.
Definition: ICommand.cs:32
Interface for datasources that are published through the concentrator interface.
Definition: IDataSource.cs:14
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Task< bool > CanViewAsync(RequestOrigin Caller)
If the node is visible to the caller.
string Partition
Optional partition in which the Node ID is unique.
string SourceId
Optional ID of source containing node.
Interface for dynamic graphs.
Task GenerateGraph(InMemorySemanticCube Result, Language Language, RequestOrigin Caller)
Generates the semantic graph.
Definition: ImplTypes.g.cs:58
Grade
Grade enumeration
Definition: Grade.cs:7