Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

node_helper.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Magic Mirror
  2. * Node Helper: Newsfeed
  3. *
  4. * By Michael Teeuw https://michaelteeuw.nl
  5. * MIT Licensed.
  6. */
  7. const NodeHelper = require("node_helper");
  8. const NewsfeedFetcher = require("./newsfeedfetcher.js");
  9. const Log = require("logger");
  10. module.exports = NodeHelper.create({
  11. // Override start method.
  12. start: function () {
  13. Log.log("Starting node helper for: " + this.name);
  14. this.fetchers = [];
  15. },
  16. // Override socketNotificationReceived received.
  17. socketNotificationReceived: function (notification, payload) {
  18. if (notification === "ADD_FEED") {
  19. this.createFetcher(payload.feed, payload.config);
  20. }
  21. },
  22. /**
  23. * Creates a fetcher for a new feed if it doesn't exist yet.
  24. * Otherwise it reuses the existing one.
  25. *
  26. * @param {object} feed The feed object
  27. * @param {object} config The configuration object
  28. */
  29. createFetcher: function (feed, config) {
  30. const url = feed.url || "";
  31. const encoding = feed.encoding || "UTF-8";
  32. const reloadInterval = feed.reloadInterval || config.reloadInterval || 5 * 60 * 1000;
  33. try {
  34. new URL(url);
  35. } catch (error) {
  36. Log.error("Newsfeed Error. Malformed newsfeed url: ", url, error);
  37. this.sendSocketNotification("NEWSFEED_ERROR", { error_type: "MODULE_ERROR_MALFORMED_URL" });
  38. return;
  39. }
  40. let fetcher;
  41. if (typeof this.fetchers[url] === "undefined") {
  42. Log.log("Create new newsfetcher for url: " + url + " - Interval: " + reloadInterval);
  43. fetcher = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings);
  44. fetcher.onReceive(() => {
  45. this.broadcastFeeds();
  46. });
  47. fetcher.onError((fetcher, error) => {
  48. Log.error("Newsfeed Error. Could not fetch newsfeed: ", url, error);
  49. let error_type = NodeHelper.checkFetchError(error);
  50. this.sendSocketNotification("NEWSFEED_ERROR", {
  51. error_type
  52. });
  53. });
  54. this.fetchers[url] = fetcher;
  55. } else {
  56. Log.log("Use existing newsfetcher for url: " + url);
  57. fetcher = this.fetchers[url];
  58. fetcher.setReloadInterval(reloadInterval);
  59. fetcher.broadcastItems();
  60. }
  61. fetcher.startFetch();
  62. },
  63. /**
  64. * Creates an object with all feed items of the different registered feeds,
  65. * and broadcasts these using sendSocketNotification.
  66. */
  67. broadcastFeeds: function () {
  68. const feeds = {};
  69. for (let f in this.fetchers) {
  70. feeds[f] = this.fetchers[f].items();
  71. }
  72. this.sendSocketNotification("NEWS_ITEMS", feeds);
  73. }
  74. });