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;
2using System.Collections.Generic;
3using System.Net.NetworkInformation;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
14
15namespace Waher.Things.Ip
16{
21 {
22 private string host = string.Empty;
23
29 public override Task<bool> AcceptsParentAsync(INode Parent)
30 {
31 return Task.FromResult(Parent is Root || Parent is IpHost);
32 }
33
39 public override Task<bool> AcceptsChildAsync(INode Child)
40 {
41 return Task.FromResult(Child is IpHost);
42 }
43
49 public override Task<string> GetTypeNameAsync(Language Language)
50 {
51 return Language.GetStringAsync(typeof(IpHost), 6, "IP Host");
52 }
53
57 [Page(1, "IP")]
58 [Header(2, "Host Name:", 50)]
59 [ToolTip(3, "Host Name or IP Address of device.")]
60 [DefaultValueStringEmpty]
61 [Required]
62 public string Host
63 {
64 get => this.host;
65 set => this.host = value;
66 }
67
71 public override bool IsReadable => true;
72
77 public async virtual Task StartReadout(ISensorReadout Request)
78 {
79 try
80 {
81 using (Ping Icmp = new Ping())
82 {
83 PingReply Response = await Icmp.SendPingAsync(this.host, 2000, data, options);
84 DateTime Now = DateTime.Now;
85 string Module = typeof(IpHost).Namespace;
86
87 if (Response.Status == IPStatus.Success)
88 {
89 List<Field> Fields = new List<Field>()
90 {
91 new QuantityField(this, Now, "Ping", Response.RoundtripTime, 0, "ms", FieldType.Momentary, FieldQoS.AutomaticReadout, Module, 7)
92 };
93
94 if (Request.IsIncluded(FieldType.Identity))
95 {
96 Fields.Add(new StringField(this, Now, "IP Address", Response.Address.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 8));
97 this.AddIdentityReadout(Fields, Now);
98 }
99
100 await Request.ReportFields(true, Fields);
101 }
102 else
103 {
104 await Request.ReportErrors(true, new ThingError(this, Now,
105 GetErrorMessage(Response)));
106 }
107 }
108 }
109 catch (PingException ex)
110 {
111 if (!(ex.InnerException is null))
112 await Request.ReportErrors(true, new ThingError(this, ex.InnerException.Message));
113 else
114 await Request.ReportErrors(true, new ThingError(this, ex.Message));
115 }
116 catch (Exception ex)
117 {
118 await Request.ReportErrors(true, new ThingError(this, ex.Message));
119 }
120 }
121
122 private static string GetErrorMessage(PingReply Response)
123 {
124 switch (Response.Status)
125 {
126 case IPStatus.Success: return null;
127 case IPStatus.BadDestination: return "Bad destination";
128 case IPStatus.BadHeader: return "Bad header";
129 case IPStatus.BadOption: return "Bad option";
130 case IPStatus.BadRoute: return "Bad route";
131 case IPStatus.DestinationHostUnreachable: return "Destination host unreachable";
132 case IPStatus.DestinationNetworkUnreachable: return "Destination network unreachable";
133 case IPStatus.DestinationPortUnreachable: return "Destination port unreachable";
134 case IPStatus.DestinationProhibited: return "Destination prohibited";
135 //case IPStatus.DestinationProtocolUnreachable: return "Destination protocol unreachable";
136 case IPStatus.DestinationScopeMismatch: return "Destination scope mismatch";
137 case IPStatus.DestinationUnreachable: return "Destination unreachable";
138 case IPStatus.HardwareError: return "Hardware error";
139 case IPStatus.IcmpError: return "ICMP error";
140 case IPStatus.NoResources: return "No resources";
141 case IPStatus.PacketTooBig: return "Packet too big";
142 case IPStatus.ParameterProblem: return "Parameter problem";
143 case IPStatus.SourceQuench: return "Source quench";
144 case IPStatus.TimedOut: return "Timed out";
145 case IPStatus.TimeExceeded: return "Time exceeded";
146 case IPStatus.TtlExpired: return "TTL expired";
147 case IPStatus.TtlReassemblyTimeExceeded: return "TTL reassembly time exceeded";
148 case IPStatus.UnrecognizedNextHeader: return "Unrecognized next header";
149 case IPStatus.Unknown:
150 default:
151 return "Unknown error";
152 }
153 }
154
155 private static readonly byte[] data = Encoding.ASCII.GetBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
156 private static readonly PingOptions options = new PingOptions(64, true);
157
164 public async override Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
165 {
166 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
167
168 Result.AddLast(new StringParameter("Host", await Language.GetStringAsync(typeof(IpHost), 9, "Host"), this.host));
169
170 return Result;
171 }
172 }
173}
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
Node representing an IP Host machine.
Definition: IpHost.cs:21
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: IpHost.cs:49
async override Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: IpHost.cs:164
override bool IsReadable
If the node can be read.
Definition: IpHost.cs:71
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:39
virtual async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: IpHost.cs:77
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:29
string Host
Host name or IP address.
Definition: IpHost.cs:63
virtual void AddIdentityReadout(List< 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:11
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
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