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 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Magic Mirror
  2. * Node Helper: Calendar
  3. *
  4. * By Michael Teeuw https://michaelteeuw.nl
  5. * MIT Licensed.
  6. */
  7. const NodeHelper = require("node_helper");
  8. const CalendarFetcher = require("./calendarfetcher.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 method.
  17. socketNotificationReceived: function (notification, payload) {
  18. if (notification === "ADD_CALENDAR") {
  19. this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents, payload.selfSignedCert, payload.id);
  20. }
  21. },
  22. /**
  23. * Creates a fetcher for a new url if it doesn't exist yet.
  24. * Otherwise it reuses the existing one.
  25. *
  26. * @param {string} url The url of the calendar
  27. * @param {number} fetchInterval How often does the calendar needs to be fetched in ms
  28. * @param {string[]} excludedEvents An array of words / phrases from event titles that will be excluded from being shown.
  29. * @param {number} maximumEntries The maximum number of events fetched.
  30. * @param {number} maximumNumberOfDays The maximum number of days an event should be in the future.
  31. * @param {object} auth The object containing options for authentication against the calendar.
  32. * @param {boolean} broadcastPastEvents If true events from the past maximumNumberOfDays will be included in event broadcasts
  33. * @param {boolean} selfSignedCert If true, the server certificate is not verified against the list of supplied CAs.
  34. * @param {string} identifier ID of the module
  35. */
  36. createFetcher: function (url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert, identifier) {
  37. try {
  38. new URL(url);
  39. } catch (error) {
  40. Log.error("Calendar Error. Malformed calendar url: ", url, error);
  41. this.sendSocketNotification("CALENDAR_ERROR", { error_type: "MODULE_ERROR_MALFORMED_URL" });
  42. return;
  43. }
  44. let fetcher;
  45. if (typeof this.fetchers[identifier + url] === "undefined") {
  46. Log.log("Create new calendarfetcher for url: " + url + " - Interval: " + fetchInterval);
  47. fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert);
  48. fetcher.onReceive((fetcher) => {
  49. this.broadcastEvents(fetcher, identifier);
  50. });
  51. fetcher.onError((fetcher, error) => {
  52. Log.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error);
  53. let error_type = NodeHelper.checkFetchError(error);
  54. this.sendSocketNotification("CALENDAR_ERROR", {
  55. id: identifier,
  56. error_type
  57. });
  58. });
  59. this.fetchers[identifier + url] = fetcher;
  60. } else {
  61. Log.log("Use existing calendarfetcher for url: " + url);
  62. fetcher = this.fetchers[identifier + url];
  63. fetcher.broadcastEvents();
  64. }
  65. fetcher.startFetch();
  66. },
  67. /**
  68. *
  69. * @param {object} fetcher the fetcher associated with the calendar
  70. * @param {string} identifier the identifier of the calendar
  71. */
  72. broadcastEvents: function (fetcher, identifier) {
  73. this.sendSocketNotification("CALENDAR_EVENTS", {
  74. id: identifier,
  75. url: fetcher.url(),
  76. events: fetcher.events()
  77. });
  78. }
  79. });