Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RssFeedNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
13using Waher.Things.Ip;
18
20{
25 {
26 private string url = string.Empty;
27
33 public override Task<bool> AcceptsParentAsync(INode Parent)
34 {
35 return Task.FromResult(Parent is Root || Parent is IpHost || Parent is VirtualNode);
36 }
37
43 public override Task<bool> AcceptsChildAsync(INode Child)
44 {
45 return Task.FromResult(false);
46 }
47
53 public override Task<string> GetTypeNameAsync(Language Language)
54 {
55 return Language.GetStringAsync(typeof(RssFeedNode), 1, "RSS Feed");
56 }
57
61 [Page(2, "RSS")]
62 [Header(3, "URL:", 50)]
63 [ToolTip(4, "Link to RSS Feed.")]
64 [DefaultValueStringEmpty]
65 [Required]
66 public string Url
67 {
68 get => this.url;
69 set => this.url = value;
70 }
71
78 public async override Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
79 {
80 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
81
82 Result.AddLast(new StringParameter("URL", await Language.GetStringAsync(typeof(RssFeedNode), 5, "URL"), this.url));
83
84 return Result;
85 }
86
90 public override bool IsReadable => true;
91
96 public async virtual Task StartReadout(ISensorReadout Request)
97 {
98 try
99 {
100 if (!Uri.TryCreate(this.url, UriKind.Absolute, out Uri Link))
101 throw new Exception("Invalid URL.");
102
103 object Obj = await InternetContent.GetAsync(Link, Gateway.Certificate, 10000,
104 new KeyValuePair<string, string>("Accept", RssCodec.ContentType));
105
106 if (!(Obj is RssDocument Doc))
107 {
108 if (Obj is XmlDocument Xml)
109 Doc = new RssDocument(Xml, Link);
110 else
111 throw new Exception("URL did not return an RSS document.");
112 }
113
114 DateTime Now = DateTime.UtcNow;
115 List<Field> Fields = new List<Field>();
116
117 foreach (RssChannel Channel in Doc.Channels)
118 {
119 DateTime Publication = Now;
120
121 if (Fields.Count > 0)
122 {
123 await Request.ReportFields(false, Fields.ToArray());
124 Fields.Clear();
125 }
126
127 if (!string.IsNullOrEmpty(Channel.Title))
128 Fields.Add(new StringField(this, Now, "Channel, Title", Channel.Title, FieldType.Identity, FieldQoS.AutomaticReadout));
129
130 if (!(Channel.Link is null))
131 Fields.Add(new StringField(this, Now, "Channel, Link", Channel.Link.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
132
133 if (!string.IsNullOrEmpty(Channel.Description))
134 Fields.Add(new StringField(this, Now, "Channel, Description", Channel.Description, FieldType.Identity, FieldQoS.AutomaticReadout));
135
136 if (!string.IsNullOrEmpty(Channel.Language))
137 Fields.Add(new StringField(this, Now, "Channel, Language", Channel.Language, FieldType.Identity, FieldQoS.AutomaticReadout));
138
139 if (!string.IsNullOrEmpty(Channel.Copyright))
140 Fields.Add(new StringField(this, Now, "Channel, Copyright", Channel.Copyright, FieldType.Identity, FieldQoS.AutomaticReadout));
141
142 if (!string.IsNullOrEmpty(Channel.ManagingEditor))
143 Fields.Add(new StringField(this, Now, "Channel, Managing Editor", Channel.ManagingEditor, FieldType.Identity, FieldQoS.AutomaticReadout));
144
145 if (!string.IsNullOrEmpty(Channel.WebMaster))
146 Fields.Add(new StringField(this, Now, "Channel, Web Master", Channel.WebMaster, FieldType.Identity, FieldQoS.AutomaticReadout));
147
148 if (Channel.PublicationDate.HasValue)
149 {
150 Publication = Channel.PublicationDate.Value.UtcDateTime;
151 Fields.Add(new DateTimeField(this, Now, "Channel, Publication", Publication, FieldType.Status, FieldQoS.AutomaticReadout));
152 }
153
154 if (!string.IsNullOrEmpty(Channel.Generator))
155 Fields.Add(new StringField(this, Now, "Channel, Generator", Channel.Generator, FieldType.Identity, FieldQoS.AutomaticReadout));
156
157 if (!(Channel.Documentation is null))
158 Fields.Add(new StringField(this, Now, "Channel, Documentation", Channel.Documentation.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
159
160 Fields.Add(new TimeField(this, Now, "Channel, Time To Live", Channel.TimeToLive, FieldType.Status, FieldQoS.AutomaticReadout));
161
162 if (!(Channel.Categories is null))
163 {
164 int i = 0;
165
166 foreach (RssCategory Category in Channel.Categories)
167 {
168 string s = "Channel, Category " + (++i).ToString();
169
170 Fields.Add(new StringField(this, Now, s + ", Name", Category.Name, FieldType.Identity, FieldQoS.AutomaticReadout));
171
172 if (!(Category.Domain is null))
173 Fields.Add(new StringField(this, Now, s + ", Domain", Category.Domain.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
174 }
175 }
176
177 if (!(Channel.Image is null))
178 {
179 if (!(Channel.Image.Url is null))
180 Fields.Add(new StringField(this, Now, "Channel, Image, Url", Channel.Image.Url.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
181
182 if (!string.IsNullOrEmpty(Channel.Image.Title))
183 Fields.Add(new StringField(this, Now, "Channel, Image, Title", Channel.Image.Title, FieldType.Identity, FieldQoS.AutomaticReadout));
184
185 if (!(Channel.Image.Link is null))
186 Fields.Add(new StringField(this, Now, "Channel, Image, Link", Channel.Image.Link.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
187
188 if (Channel.Image.Width > 0)
189 Fields.Add(new StringField(this, Now, "Channel, Image, Width", Channel.Image.Width.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
190
191 if (Channel.Image.Height > 0)
192 Fields.Add(new StringField(this, Now, "Channel, Image, Height", Channel.Image.Height.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
193
194 if (!string.IsNullOrEmpty(Channel.Image.Description))
195 Fields.Add(new StringField(this, Now, "Channel, Image, Description", Channel.Image.Description, FieldType.Identity, FieldQoS.AutomaticReadout));
196 }
197
198 if (!(Channel.Items is null))
199 {
200 foreach (RssItem Item in Channel.Items)
201 {
202 Publication = Now;
203
204 if (Item.PublicationDate.HasValue)
205 Publication = Item.PublicationDate.Value.UtcDateTime;
206
207 if (!string.IsNullOrEmpty(Item.Title))
208 Fields.Add(new StringField(this, Publication, "Title", Item.Title, FieldType.Historical, FieldQoS.AutomaticReadout));
209
210 if (!(Item.Link is null))
211 Fields.Add(new StringField(this, Publication, "Link", Item.Link.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
212
213 if (!string.IsNullOrEmpty(Item.Description))
214 Fields.Add(new StringField(this, Publication, "Description", Item.Description, FieldType.Historical, FieldQoS.AutomaticReadout));
215
216 if (!string.IsNullOrEmpty(Item.Author))
217 Fields.Add(new StringField(this, Publication, "Author", Item.Author, FieldType.Historical, FieldQoS.AutomaticReadout));
218
219 if (!(Item.Comments is null))
220 Fields.Add(new StringField(this, Publication, "Comments", Item.Comments.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
221
222 if (!(Item.Guid is null))
223 {
224 Fields.Add(new StringField(this, Publication, "Guid", Item.Guid.Id, FieldType.Historical, FieldQoS.AutomaticReadout));
225 Fields.Add(new BooleanField(this, Publication, "Guid, IsPermaLink", Item.Guid.IsPermaLink, FieldType.Historical, FieldQoS.AutomaticReadout));
226 }
227
228 if (!(Item.Source is null))
229 {
230 if (!(Item.Source.Url is null))
231 Fields.Add(new StringField(this, Publication, "Source, URL", Item.Source.Url.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
232
233 if (!string.IsNullOrEmpty(Item.Source.Title))
234 Fields.Add(new StringField(this, Publication, "Source, Title", Item.Source.Title, FieldType.Historical, FieldQoS.AutomaticReadout));
235 }
236
237 if (!(Item.Enclosure is null))
238 {
239 if (!(Item.Enclosure.Url is null))
240 Fields.Add(new StringField(this, Publication, "Enclosure, URL", Item.Enclosure.Url.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
241
242 if (!string.IsNullOrEmpty(Item.Enclosure.ContentType))
243 Fields.Add(new StringField(this, Publication, "Enclosure, Content-Type", Item.Enclosure.ContentType, FieldType.Historical, FieldQoS.AutomaticReadout));
244
245 if (Item.Enclosure.Length > 0)
246 Fields.Add(new Int64Field(this, Publication, "Enclosure, Length", Item.Enclosure.Length, FieldType.Historical, FieldQoS.AutomaticReadout));
247 }
248
249 if (!(Item.Categories is null))
250 {
251 int i = 0;
252
253 foreach (RssCategory Category in Item.Categories)
254 {
255 string s = "Category " + (++i).ToString();
256
257 Fields.Add(new StringField(this, Publication, s + ", Name", Category.Name, FieldType.Historical, FieldQoS.AutomaticReadout));
258
259 if (!(Category.Domain is null))
260 Fields.Add(new StringField(this, Now, s + ", Domain", Category.Domain.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
261 }
262 }
263 }
264 }
265 }
266
267 if (!(Doc.Warnings is null) && Doc.Warnings.Length > 0)
268 {
269 List<ThingError> Errors = new List<ThingError>();
270
271 foreach (RssWarning Warning in Doc.Warnings)
272 Errors.Add(new ThingError(this, Warning.Message));
273
274 await Request.ReportErrors(false, Errors.ToArray());
275 }
276
277 await Request.ReportFields(true, Fields.ToArray());
278 }
279 catch (Exception ex)
280 {
281 await Request.ReportErrors(true, new ThingError(this, ex.Message));
282 }
283 }
284
285 }
286}
Static class managing encoding and decoding of internet content.
static Task< object > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
Identifies a categorization taxonomy.
Definition: RssCategory.cs:12
string Name
Name of category
Definition: RssCategory.cs:54
Contains information about an RSS Channel.
Definition: RssChannel.cs:12
RssImage Image
Channel image.
Definition: RssChannel.cs:245
string Copyright
Copyright notice for content in the channel.
Definition: RssChannel.cs:187
string Title
The name of the channel. It's how people refer to your service. If you have an HTML website that cont...
Definition: RssChannel.cs:164
TimeSpan TimeToLive
ttl stands for time to live. It's a number of minutes that indicates how long a channel can be cached...
Definition: RssChannel.cs:240
Uri Link
The URL to the HTML website corresponding to the channel.
Definition: RssChannel.cs:169
string ManagingEditor
Email address for person responsible for editorial content.
Definition: RssChannel.cs:192
string Language
The language the channel is written in. This allows aggregators to group all Italian language sites,...
Definition: RssChannel.cs:182
Uri Documentation
A URL that points to the documentation for the format used in the RSS file. It's probably a pointer t...
Definition: RssChannel.cs:229
string WebMaster
Email address for person responsible for technical issues relating to channel.
Definition: RssChannel.cs:197
RssCategory[] Categories
Specify one or more categories that the channel belongs to. Follows the same rules as the <item>-leve...
Definition: RssChannel.cs:217
string Description
Phrase or sentence describing the channel.
Definition: RssChannel.cs:174
string Generator
A string indicating the program used to generate the channel.
Definition: RssChannel.cs:222
DateTimeOffset? PublicationDate
The publication date for the content in the channel. For example, the New York Times publishes on a d...
Definition: RssChannel.cs:206
Encodes and Decodes RSS documents.
Definition: RssCodec.cs:19
const string ContentType
application/rss+xml
Definition: RssCodec.cs:26
Contains information from an RSS feed.
Definition: RssDocument.cs:13
string ContentType
Content-Type of content
Definition: RssEnclosure.cs:60
Uri Url
URL to the content.
Definition: RssEnclosure.cs:50
long Length
Length of content.
Definition: RssEnclosure.cs:55
bool IsPermaLink
If the guid element has an attribute named isPermaLink with a value of true, the reader may assume th...
Definition: RssGuid.cs:30
string Id
ID of Guid
Definition: RssGuid.cs:35
int Width
Width of image in pixels.
Definition: RssImage.cs:107
string Title
Describes the image, it's used in the ALT attribute of the HTML <img> tag when the channel is rendere...
Definition: RssImage.cs:95
int Height
Height of image in pixels.
Definition: RssImage.cs:112
Uri Url
URL of a GIF, JPEG or PNG image that represents the channel.
Definition: RssImage.cs:89
string Description
Contains text that is included in the TITLE attribute of the link formed around the image in the HTML...
Definition: RssImage.cs:118
Uri Link
The URL of the site, when the channel is rendered, the image is a link to the site....
Definition: RssImage.cs:102
A channel may contain any number of <item>s. An item may represent a "story" – much like a story in a...
Definition: RssItem.cs:17
Uri Comments
URL of a page for comments relating to the item.
Definition: RssItem.cs:162
Uri Link
The URL of the item.
Definition: RssItem.cs:142
string Title
The title of the item.
Definition: RssItem.cs:137
string Description
The item synopsis.
Definition: RssItem.cs:147
RssCategory[] Categories
Includes the item in one or more categories.
Definition: RssItem.cs:157
RssGuid Guid
A string that uniquely identifies the item.
Definition: RssItem.cs:172
DateTimeOffset? PublicationDate
Indicates when the item was published.
Definition: RssItem.cs:177
string Author
Email address of the author of the item.
Definition: RssItem.cs:152
RssEnclosure Enclosure
Describes a media object that is attached to the item.
Definition: RssItem.cs:167
RssSource Source
The RSS channel that the item came from.
Definition: RssItem.cs:182
string Title
Title of the source.
Definition: RssSource.cs:54
Uri Url
URL to the source.
Definition: RssSource.cs:49
Contains a warning message.
Definition: RssWarning.cs:9
string Message
Warning message.
Definition: RssWarning.cs:38
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static X509Certificate2 Certificate
Domain certificate.
Definition: Gateway.cs:2349
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
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
Node representing an RSS Feed
Definition: RssFeedNode.cs:25
virtual async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: RssFeedNode.cs:96
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: RssFeedNode.cs:33
string Url
Host name or IP address.
Definition: RssFeedNode.cs:67
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: RssFeedNode.cs:43
override bool IsReadable
If the node can be read.
Definition: RssFeedNode.cs:90
async override Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: RssFeedNode.cs:78
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: RssFeedNode.cs:53
Represents a boolean value that can be either true or false.
Definition: BooleanField.cs:11
Represents a date and optional time value.
Represents a 64-bit integer value.
Definition: Int64Field.cs:10
Represents a string value.
Definition: StringField.cs:10
Represents a time value. Time values adhere to the type specified by xsd:time.
Definition: TimeField.cs:14
Contains information about an error on a thing
Definition: ThingError.cs:10
Virtual node, that can be used as a placeholder for services.
Definition: VirtualNode.cs:28
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.
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