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;
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(
36 Parent is Root ||
38 Parent is IpHost ||
40 }
41
47 public override Task<bool> AcceptsChildAsync(INode Child)
48 {
49 return Task.FromResult(false);
50 }
51
57 public override Task<string> GetTypeNameAsync(Language Language)
58 {
59 return Language.GetStringAsync(typeof(RssFeedNode), 1, "RSS Feed");
60 }
61
65 [Page(2, "RSS")]
66 [Header(3, "URL:", 50)]
67 [ToolTip(4, "Link to RSS Feed.")]
68 [DefaultValueStringEmpty]
69 [Required]
70 public string Url
71 {
72 get => this.url;
73 set => this.url = value;
74 }
75
82 public async override Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
83 {
84 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
85
86 Result.AddLast(new StringParameter("URL", await Language.GetStringAsync(typeof(RssFeedNode), 5, "URL"), this.url));
87
88 return Result;
89 }
90
94 public override bool IsReadable => !this.Disabled;
95
100 public async virtual Task StartReadout(ISensorReadout Request)
101 {
102 try
103 {
104 if (!Uri.TryCreate(this.url, UriKind.Absolute, out Uri Link))
105 {
106 await Request.ReportErrors(true, new ThingError(this, "Invalid URL."));
107 return;
108 }
109
111 new KeyValuePair<string, string>("Accept", RssCodec.ContentType));
112
113 if (Content.HasError)
114 {
115 await Request.ReportErrors(true, new ThingError(this, Content.Error.Message));
116 return;
117 }
118
119 if (!(Content.Decoded is RssDocument Doc))
120 {
121 if (Content.Decoded is XmlDocument Xml)
122 Doc = new RssDocument(Xml, Link);
123 else
124 {
125 await Request.ReportErrors(true, new ThingError(this, "URL did not return an RSS document."));
126 return;
127 }
128 }
129
130 DateTime Now = DateTime.UtcNow;
131 List<Field> Fields = new List<Field>();
132
133 foreach (RssChannel Channel in Doc.Channels)
134 {
135 DateTime Publication = Now;
136
137 if (Fields.Count > 0)
138 {
139 await Request.ReportFields(false, Fields.ToArray());
140 Fields.Clear();
141 }
142
143 if (!string.IsNullOrEmpty(Channel.Title))
144 Fields.Add(new StringField(this, Now, "Channel, Title", Channel.Title, FieldType.Identity, FieldQoS.AutomaticReadout));
145
146 if (!(Channel.Link is null))
147 Fields.Add(new StringField(this, Now, "Channel, Link", Channel.Link.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
148
149 if (!string.IsNullOrEmpty(Channel.Description))
150 Fields.Add(new StringField(this, Now, "Channel, Description", Channel.Description, FieldType.Identity, FieldQoS.AutomaticReadout));
151
152 if (!string.IsNullOrEmpty(Channel.Language))
153 Fields.Add(new StringField(this, Now, "Channel, Language", Channel.Language, FieldType.Identity, FieldQoS.AutomaticReadout));
154
155 if (!string.IsNullOrEmpty(Channel.Copyright))
156 Fields.Add(new StringField(this, Now, "Channel, Copyright", Channel.Copyright, FieldType.Identity, FieldQoS.AutomaticReadout));
157
158 if (!string.IsNullOrEmpty(Channel.ManagingEditor))
159 Fields.Add(new StringField(this, Now, "Channel, Managing Editor", Channel.ManagingEditor, FieldType.Identity, FieldQoS.AutomaticReadout));
160
161 if (!string.IsNullOrEmpty(Channel.WebMaster))
162 Fields.Add(new StringField(this, Now, "Channel, Web Master", Channel.WebMaster, FieldType.Identity, FieldQoS.AutomaticReadout));
163
164 if (Channel.PublicationDate.HasValue)
165 {
166 Publication = Channel.PublicationDate.Value.UtcDateTime;
167 Fields.Add(new DateTimeField(this, Now, "Channel, Publication", Publication, FieldType.Status, FieldQoS.AutomaticReadout));
168 }
169
170 if (!string.IsNullOrEmpty(Channel.Generator))
171 Fields.Add(new StringField(this, Now, "Channel, Generator", Channel.Generator, FieldType.Identity, FieldQoS.AutomaticReadout));
172
173 if (!(Channel.Documentation is null))
174 Fields.Add(new StringField(this, Now, "Channel, Documentation", Channel.Documentation.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
175
176 Fields.Add(new TimeField(this, Now, "Channel, Time To Live", Channel.TimeToLive, FieldType.Status, FieldQoS.AutomaticReadout));
177
178 if (!(Channel.Categories is null))
179 {
180 int i = 0;
181
182 foreach (RssCategory Category in Channel.Categories)
183 {
184 string s = "Channel, Category " + (++i).ToString();
185
186 Fields.Add(new StringField(this, Now, s + ", Name", Category.Name, FieldType.Identity, FieldQoS.AutomaticReadout));
187
188 if (!(Category.Domain is null))
189 Fields.Add(new StringField(this, Now, s + ", Domain", Category.Domain.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
190 }
191 }
192
193 if (!(Channel.Image is null))
194 {
195 if (!(Channel.Image.Url is null))
196 Fields.Add(new StringField(this, Now, "Channel, Image, Url", Channel.Image.Url.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
197
198 if (!string.IsNullOrEmpty(Channel.Image.Title))
199 Fields.Add(new StringField(this, Now, "Channel, Image, Title", Channel.Image.Title, FieldType.Identity, FieldQoS.AutomaticReadout));
200
201 if (!(Channel.Image.Link is null))
202 Fields.Add(new StringField(this, Now, "Channel, Image, Link", Channel.Image.Link.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
203
204 if (Channel.Image.Width > 0)
205 Fields.Add(new StringField(this, Now, "Channel, Image, Width", Channel.Image.Width.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
206
207 if (Channel.Image.Height > 0)
208 Fields.Add(new StringField(this, Now, "Channel, Image, Height", Channel.Image.Height.ToString(), FieldType.Identity, FieldQoS.AutomaticReadout));
209
210 if (!string.IsNullOrEmpty(Channel.Image.Description))
211 Fields.Add(new StringField(this, Now, "Channel, Image, Description", Channel.Image.Description, FieldType.Identity, FieldQoS.AutomaticReadout));
212 }
213
214 if (!(Channel.Items is null))
215 {
216 foreach (RssItem Item in Channel.Items)
217 {
218 Publication = Now;
219
220 if (Item.PublicationDate.HasValue)
221 Publication = Item.PublicationDate.Value.UtcDateTime;
222
223 if (!string.IsNullOrEmpty(Item.Title))
224 Fields.Add(new StringField(this, Publication, "Title", Item.Title, FieldType.Historical, FieldQoS.AutomaticReadout));
225
226 if (!(Item.Link is null))
227 Fields.Add(new StringField(this, Publication, "Link", Item.Link.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
228
229 if (!string.IsNullOrEmpty(Item.Description))
230 Fields.Add(new StringField(this, Publication, "Description", Item.Description, FieldType.Historical, FieldQoS.AutomaticReadout));
231
232 if (!string.IsNullOrEmpty(Item.Author))
233 Fields.Add(new StringField(this, Publication, "Author", Item.Author, FieldType.Historical, FieldQoS.AutomaticReadout));
234
235 if (!(Item.Comments is null))
236 Fields.Add(new StringField(this, Publication, "Comments", Item.Comments.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
237
238 if (!(Item.Guid is null))
239 {
240 Fields.Add(new StringField(this, Publication, "Guid", Item.Guid.Id, FieldType.Historical, FieldQoS.AutomaticReadout));
241 Fields.Add(new BooleanField(this, Publication, "Guid, IsPermaLink", Item.Guid.IsPermaLink, FieldType.Historical, FieldQoS.AutomaticReadout));
242 }
243
244 if (!(Item.Source is null))
245 {
246 if (!(Item.Source.Url is null))
247 Fields.Add(new StringField(this, Publication, "Source, URL", Item.Source.Url.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
248
249 if (!string.IsNullOrEmpty(Item.Source.Title))
250 Fields.Add(new StringField(this, Publication, "Source, Title", Item.Source.Title, FieldType.Historical, FieldQoS.AutomaticReadout));
251 }
252
253 if (!(Item.Enclosure is null))
254 {
255 if (!(Item.Enclosure.Url is null))
256 Fields.Add(new StringField(this, Publication, "Enclosure, URL", Item.Enclosure.Url.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
257
258 if (!string.IsNullOrEmpty(Item.Enclosure.ContentType))
259 Fields.Add(new StringField(this, Publication, "Enclosure, Content-Type", Item.Enclosure.ContentType, FieldType.Historical, FieldQoS.AutomaticReadout));
260
261 if (Item.Enclosure.Length > 0)
262 Fields.Add(new Int64Field(this, Publication, "Enclosure, Length", Item.Enclosure.Length, FieldType.Historical, FieldQoS.AutomaticReadout));
263 }
264
265 if (!(Item.Categories is null))
266 {
267 int i = 0;
268
269 foreach (RssCategory Category in Item.Categories)
270 {
271 string s = "Category " + (++i).ToString();
272
273 Fields.Add(new StringField(this, Publication, s + ", Name", Category.Name, FieldType.Historical, FieldQoS.AutomaticReadout));
274
275 if (!(Category.Domain is null))
276 Fields.Add(new StringField(this, Now, s + ", Domain", Category.Domain.ToString(), FieldType.Historical, FieldQoS.AutomaticReadout));
277 }
278 }
279 }
280 }
281 }
282
283 if (!(Doc.Warnings is null) && Doc.Warnings.Length > 0)
284 {
285 List<ThingError> Errors = new List<ThingError>();
286
287 foreach (RssWarning Warning in Doc.Warnings)
288 Errors.Add(new ThingError(this, Warning.Message));
289
290 await Request.ReportErrors(false, Errors.ToArray());
291 }
292
293 await Request.ReportFields(true, Fields.ToArray());
294 }
295 catch (Exception ex)
296 {
297 await Request.ReportErrors(true, new ThingError(this, ex.Message));
298 }
299 }
300
301 }
302}
Contains information about a response to a content request.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > 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:21
const string ContentType
application/rss+xml
Definition: RssCodec.cs:28
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:147
static X509Certificate2 Certificate
Domain certificate.
Definition: Gateway.cs:3082
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:25
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
Node representing an RSS Feed
Definition: RssFeedNode.cs:25
virtual async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: RssFeedNode.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: RssFeedNode.cs:33
string Url
Host name or IP address.
Definition: RssFeedNode.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: RssFeedNode.cs:47
override bool IsReadable
If the node can be read.
Definition: RssFeedNode.cs:94
async override Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: RssFeedNode.cs:82
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: RssFeedNode.cs:57
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