Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PortMonitor.cs
1using System;
3using System.Security.Cryptography.X509Certificates;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
13using Waher.Security;
18
19namespace Waher.Things.Ip
20{
25 {
26 private string protocol = string.Empty;
27 private int port = 0;
28 private bool tls = false;
29
35 public override Task<bool> AcceptsChildAsync(INode Child)
36 {
37 return Task.FromResult(false);
38 }
39
45 public override Task<bool> AcceptsParentAsync(INode Parent)
46 {
47 return Task.FromResult(Parent is IpHost);
48 }
49
55 public override Task<string> GetTypeNameAsync(Language Language)
56 {
57 return Language.GetStringAsync(typeof(PortMonitor), 40, "Port Monitor");
58 }
59
63 [Page(1, "IP")]
64 [Header(4, "Port Number:", 60)]
65 [ToolTip(5, "Port number to use when communicating with device.")]
66 [DefaultValue(0)]
67 [Range(0, 65535)]
68 [Required]
69 public int Port
70 {
71 get => this.port;
72 set => this.port = value;
73 }
74
78 [Page(1, "IP")]
79 [Header(11, "Encrypted (TLS)", 70)]
80 [ToolTip(12, "Check if Transport Layer Encryption (TLS) should be used.")]
81 public bool Tls
82 {
83 get => this.tls;
84 set => this.tls = value;
85 }
86
90 [Page(1, "IP")]
91 [Header(41, "Protocol:", 60)]
92 [ToolTip(42, "Name of protocol or service on port number.")]
93 [DefaultValueStringEmpty]
94 [Required]
95 public string Protocol
96 {
97 get => this.protocol;
98 set => this.protocol = value;
99 }
100
104 [Obsolete]
105 public string PortName
106 {
107 get => this.protocol;
108 set => this.protocol = value;
109 }
110
116 public async Task<string> GetHostName()
117 {
118 if (await this.GetParent() is IpHost Parent)
119 return Parent.Host;
120 else
121 throw new Exception("Parent node not an IP Host node.");
122 }
123
132 public async Task<BinaryTcpClient> ConnectTcp(bool DecoupledEvents, params ISniffer[] Sniffers)
133 {
134 X509Certificate Certificate = Types.TryGetModuleParameter<X509Certificate>("X509");
135 BinaryTcpClient Client = new BinaryTcpClient(DecoupledEvents, Sniffers);
136 string HostName = await this.GetHostName();
137 await Client.ConnectAsync(HostName, this.port, this.tls);
138
139 if (this.tls)
140 {
141 await Client.UpgradeToTlsAsClient(Certificate, Crypto.SecureTls);
142 Client.Continue();
143 }
144
145 return Client;
146 }
147
157 public async Task<TextTcpClient> ConnectTcp(Encoding Encoding, bool DecoupledEvents, params ISniffer[] Sniffers)
158 {
159 X509Certificate Certificate = Types.TryGetModuleParameter<X509Certificate>("X509");
160 TextTcpClient Client = new TextTcpClient(Encoding, DecoupledEvents, Sniffers);
161 string HostName = await this.GetHostName();
162 await Client.ConnectAsync(HostName, this.port, this.tls);
163
164 if (this.tls)
165 {
166 await Client.UpgradeToTlsAsClient(Certificate, Crypto.SecureTls);
167 Client.Continue();
168 }
169
170 return Client;
171 }
172
179 public async override Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
180 {
181 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
182
183 Result.AddLast(new Int32Parameter("Port", await Language.GetStringAsync(typeof(IpHost), 10, "Port"), this.port));
184
185 if (!string.IsNullOrEmpty(this.Protocol))
186 Result.AddLast(new StringParameter("Protocol", await Language.GetStringAsync(typeof(IpHost), 43, "Protocol"), this.protocol));
187
188 return Result;
189 }
190
195 public async virtual Task StartReadout(ISensorReadout Request)
196 {
197 try
198 {
199 DateTime Now = DateTime.UtcNow;
200 string Module = typeof(IpHost).Namespace;
201
202 using BinaryTcpClient Client = await this.ConnectTcp(false);
203
204 List<Field> Fields = new List<Field>()
205 {
206 new QuantityField(this, Now, "Connect", (DateTime.UtcNow-Now).TotalMilliseconds, 0, "ms", FieldType.Momentary, FieldQoS.AutomaticReadout, Module, 13)
207 };
208
209 if (Request.IsIncluded(FieldType.Identity) && this.Tls)
210 {
211 IpHostPort.AddCertificateFields(Client.RemoteCertificate,
212 Client.RemoteCertificateValid, this, Now, Fields);
213 }
214
215 await Request.ReportFields(true, Fields);
216 }
217 catch (Exception ex)
218 {
219 await Request.ReportErrors(true, new ThingError(this, ex.Message));
220 }
221 }
222
223 }
224}
Implements a binary TCP Client, by encapsulating a TcpClient. It also makes the use of TcpClient safe...
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
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
Extract the fields of a type or an object.
Definition: Fields.cs:17
Makes sure an expression is defined. Otherwise, an exception is thrown.
Definition: Required.cs:13
Helper methods for encrypting and decrypting streams of data.
Definition: Crypto.cs:14
const SslProtocols SecureTls
TLS 1.2 & 1.3
Definition: Crypto.cs:18
Node representing an IP Host machine.
Definition: IpHost.cs:25
Node representing a port on an IP Host machine.
Definition: IpHostPort.cs:24
static void AddCertificateFields(X509Certificate Certificate, bool IsValid, ThingReference Node, DateTime Timestamp, List< Field > Fields)
Adds information from a certificate as fields to a collection of fields.
Definition: IpHostPort.cs:180
Node representing a port on an IP Host machine.
Definition: PortMonitor.cs:25
async override Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: PortMonitor.cs:179
async Task< string > GetHostName()
Gets the host name or IP address of the parent IP Host node.
Definition: PortMonitor.cs:116
string PortName
Old property, replaced by Protocol.
Definition: PortMonitor.cs:106
override Task< bool > AcceptsParentAsync(INode Parent)
If the node accepts a presumptive parent, i.e. can be added to that parent (if that parent accepts th...
Definition: PortMonitor.cs:45
virtual async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: PortMonitor.cs:195
string Protocol
Port number.
Definition: PortMonitor.cs:96
async Task< BinaryTcpClient > ConnectTcp(bool DecoupledEvents, params ISniffer[] Sniffers)
Connect to the remote host and port using a binary protocol over TCP.
Definition: PortMonitor.cs:132
override Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a presumptive child, i.e. can receive as a child (if that child accepts the node ...
Definition: PortMonitor.cs:35
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: PortMonitor.cs:55
async Task< TextTcpClient > ConnectTcp(Encoding Encoding, bool DecoupledEvents, params ISniffer[] Sniffers)
Connect to the remote host and port using a text-based protocol over TCP.
Definition: PortMonitor.cs:157
bool Tls
If connection is encrypted using TLS or not.
Definition: PortMonitor.cs:82
async Task< INode > GetParent()
Gets the parent of the node.
Base class for all provisioned metering nodes.
Tokens available in request.
Definition: RequestOrigin.cs:9
Represents a physical quantity value.
Contains information about an error on a thing
Definition: ThingError.cs:10
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:10
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
INode Parent
Parent Node, or null if a root node.
Definition: INode.cs:116
Interface for sensor nodes.
Definition: ISensor.cs:9
Interface for classes managing sensor data readouts.
bool IsIncluded(string FieldName)
Checks if a field with the given parameters is included in the readout.
Task ReportErrors(bool Done, params ThingError[] Errors)
Report error states to the client.
Task ReportFields(bool Done, params Field[] Fields)
Report read fields to the client.
FieldQoS
Field Quality of Service flags
Definition: FieldQoS.cs:10
FieldType
Field Type flags
Definition: FieldType.cs:10