Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IpHost.cs
1using System;
3using System.Net.NetworkInformation;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
18
20{
25 {
26 private string host = string.Empty;
27 private bool pingHost = true;
28
34 public override Task<bool> AcceptsParentAsync(INode Parent)
35 {
36 return Task.FromResult(
37 Parent is Root ||
39 Parent is IpHost);
40 }
41
47 public override Task<bool> AcceptsChildAsync(INode Child)
48 {
49 return Task.FromResult(
50 Child is IpHost ||
51 Child is PortMonitor);
52 }
53
59 public override Task<string> GetTypeNameAsync(Language Language)
60 {
61 return Language.GetStringAsync(typeof(IpHost), 6, "IP Host");
62 }
63
67 [Page(1, "IP")]
68 [Header(2, "Host Name:", 50)]
69 [ToolTip(3, "Host Name or IP Address of device.")]
70 [DefaultValueStringEmpty]
71 [Required]
72 public string Host
73 {
74 get => this.host;
75 set => this.host = value;
76 }
77
81 [Page(1, "IP")]
82 [Header(38, "Ping host.", 50)]
83 [ToolTip(39, "If host name should be pinged during readout.")]
84 [DefaultValue(true)]
85 public bool PingHost
86 {
87 get => this.pingHost;
88 set => this.pingHost = value;
89 }
90
94 public override bool IsReadable => !this.Disabled;
95
100 public async virtual Task StartReadout(ISensorReadout Request)
101 {
102 try
103 {
104 string Module = typeof(IpHost).Namespace;
106
107 foreach (INode Node in await this.ChildNodes)
108 {
109 if (Node is PortMonitor Monitor && Node.IsReadable)
110 {
111 DateTime Now = DateTime.UtcNow;
112 string Protocol = Monitor.Protocol;
113
114 try
115 {
116 if (string.IsNullOrEmpty(Protocol))
117 Protocol = Monitor.Port.ToString();
118
119 using BinaryTcpClient Client = await Monitor.ConnectTcp(false);
120
121 Fields.Add(new QuantityField(this, Now, Protocol,
122 (DateTime.UtcNow - Now).TotalMilliseconds, 0, "ms",
123 FieldType.Momentary, FieldQoS.AutomaticReadout));
124 }
125 catch (Exception ex)
126 {
127 await Request.ReportErrors(false, new ThingError(this, Now,
128 "Unable to connect using " + Protocol + ": " + ex.Message));
129 }
130 }
131 }
132
133 if (this.pingHost)
134 {
135 using Ping Icmp = new Ping();
136
137 PingReply Response = await Icmp.SendPingAsync(this.host, 2000, data, options);
138 DateTime Now = DateTime.UtcNow;
139
140 if (Response.Status == IPStatus.Success)
141 {
142 if (Request.IsIncluded(FieldType.Identity))
143 {
144 Fields.Add(new StringField(this, Now, "IP Address", Response.Address.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 8));
145 this.AddIdentityReadout(Fields, Now);
146 }
147 }
148 else
149 {
150 await Request.ReportErrors(false, new ThingError(this, Now,
151 GetErrorMessage(Response)));
152 }
153 }
154
155 await Request.ReportFields(true, Fields.ToArray());
156 }
157 catch (PingException ex)
158 {
159 if (!(ex.InnerException is null))
160 await Request.ReportErrors(true, new ThingError(this, ex.InnerException.Message));
161 else
162 await Request.ReportErrors(true, new ThingError(this, ex.Message));
163 }
164 catch (Exception ex)
165 {
166 await Request.ReportErrors(true, new ThingError(this, ex.Message));
167 }
168 }
169
170 private static string GetErrorMessage(PingReply Response)
171 {
172 return Response.Status switch
173 {
174 IPStatus.Success => null,
175 IPStatus.BadDestination => "Bad destination",
176 IPStatus.BadHeader => "Bad header",
177 IPStatus.BadOption => "Bad option",
178 IPStatus.BadRoute => "Bad route",
179 IPStatus.DestinationHostUnreachable => "Destination host unreachable",
180 IPStatus.DestinationNetworkUnreachable => "Destination network unreachable",
181 IPStatus.DestinationPortUnreachable => "Destination port unreachable",
182 IPStatus.DestinationProhibited => "Destination prohibited",
183 //case IPStatus.DestinationProtocolUnreachable: return "Destination protocol unreachable";
184 IPStatus.DestinationScopeMismatch => "Destination scope mismatch",
185 IPStatus.DestinationUnreachable => "Destination unreachable",
186 IPStatus.HardwareError => "Hardware error",
187 IPStatus.IcmpError => "ICMP error",
188 IPStatus.NoResources => "No resources",
189 IPStatus.PacketTooBig => "Packet too big",
190 IPStatus.ParameterProblem => "Parameter problem",
191 IPStatus.SourceQuench => "Source quench",
192 IPStatus.TimedOut => "Timed out",
193 IPStatus.TimeExceeded => "Time exceeded",
194 IPStatus.TtlExpired => "TTL expired",
195 IPStatus.TtlReassemblyTimeExceeded => "TTL reassembly time exceeded",
196 IPStatus.UnrecognizedNextHeader => "Unrecognized next header",
197 _ => "Unknown error",
198 };
199 }
200
201 private static readonly byte[] data = Encoding.ASCII.GetBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
202 private static readonly PingOptions options = new PingOptions(64, true);
203
210 public async override Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
211 {
212 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
213
214 Result.AddLast(new StringParameter("Host", await Language.GetStringAsync(typeof(IpHost), 9, "Host"), this.host));
215
216 return Result;
217 }
218 }
219}
Implements a binary TCP Client, by encapsulating a TcpClient. It also makes the use of TcpClient safe...
Access to the current request.
Definition: Request.cs:12
Access to the current response.
Definition: Response.cs:12
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
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
Node representing an IP Host machine.
Definition: IpHost.cs:25
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: IpHost.cs:59
async override Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: IpHost.cs:210
override bool IsReadable
If the node can be read.
Definition: IpHost.cs:94
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: IpHost.cs:47
virtual async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: IpHost.cs:100
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: IpHost.cs:34
string Host
Host name or IP address.
Definition: IpHost.cs:73
bool PingHost
If host should be pinged during readout.
Definition: IpHost.cs:86
Node representing a port on an IP Host machine.
Definition: PortMonitor.cs:25
virtual void AddIdentityReadout(ChunkedList< Field > Fields, DateTime Now)
Adds defined identity fields to a sensor data readout.
Class for the root node of the Metering topology.
Definition: Root.cs:10
Base class for all provisioned metering nodes.
Tokens available in request.
Definition: RequestOrigin.cs:9
Represents a physical quantity value.
Represents a string value.
Definition: StringField.cs:10
Contains information about an error on a thing
Definition: ThingError.cs:10
Interface for objects that contain a reference to a host.
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
bool IsReadable
If the node can be read.
Definition: INode.cs:92
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
Interface for sensor nodes.
Definition: ISensor.cs:9
Interface for classes managing sensor data readouts.
Definition: ImplTypes.g.cs:58
FieldQoS
Field Quality of Service flags
Definition: FieldQoS.cs:10
FieldType
Field Type flags
Definition: FieldType.cs:10