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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Magic Mirror
  2. * Node Helper Superclass
  3. *
  4. * By Michael Teeuw https://michaelteeuw.nl
  5. * MIT Licensed.
  6. */
  7. const Class = require("./class.js");
  8. const Log = require("logger");
  9. const express = require("express");
  10. const NodeHelper = Class.extend({
  11. init() {
  12. Log.log("Initializing new module helper ...");
  13. },
  14. loaded(callback) {
  15. Log.log(`Module helper loaded: ${this.name}`);
  16. callback();
  17. },
  18. start() {
  19. Log.log(`Starting module helper: ${this.name}`);
  20. },
  21. /* stop()
  22. * Called when the MagicMirror server receives a `SIGINT`
  23. * Close any open connections, stop any sub-processes and
  24. * gracefully exit the module.
  25. *
  26. */
  27. stop() {
  28. Log.log(`Stopping module helper: ${this.name}`);
  29. },
  30. /* socketNotificationReceived(notification, payload)
  31. * This method is called when a socket notification arrives.
  32. *
  33. * argument notification string - The identifier of the notification.
  34. * argument payload mixed - The payload of the notification.
  35. */
  36. socketNotificationReceived(notification, payload) {
  37. Log.log(`${this.name} received a socket notification: ${notification} - Payload: ${payload}`);
  38. },
  39. /* setName(name)
  40. * Set the module name.
  41. *
  42. * argument name string - Module name.
  43. */
  44. setName(name) {
  45. this.name = name;
  46. },
  47. /* setPath(path)
  48. * Set the module path.
  49. *
  50. * argument path string - Module path.
  51. */
  52. setPath(path) {
  53. this.path = path;
  54. },
  55. /* sendSocketNotification(notification, payload)
  56. * Send a socket notification to the node helper.
  57. *
  58. * argument notification string - The identifier of the notification.
  59. * argument payload mixed - The payload of the notification.
  60. */
  61. sendSocketNotification(notification, payload) {
  62. this.io.of(this.name).emit(notification, payload);
  63. },
  64. /* setExpressApp(app)
  65. * Sets the express app object for this module.
  66. * This allows you to host files from the created webserver.
  67. *
  68. * argument app Express app - The Express app object.
  69. */
  70. setExpressApp(app) {
  71. this.expressApp = app;
  72. app.use(`/${this.name}`, express.static(`${this.path}/public`));
  73. },
  74. /* setSocketIO(io)
  75. * Sets the socket io object for this module.
  76. * Binds message receiver.
  77. *
  78. * argument io Socket.io - The Socket io object.
  79. */
  80. setSocketIO(io) {
  81. this.io = io;
  82. Log.log(`Connecting socket for: ${this.name}`);
  83. io.of(this.name).on("connection", (socket) => {
  84. // add a catch all event.
  85. const onevent = socket.onevent;
  86. socket.onevent = function (packet) {
  87. const args = packet.data || [];
  88. onevent.call(this, packet); // original call
  89. packet.data = ["*"].concat(args);
  90. onevent.call(this, packet); // additional call to catch-all
  91. };
  92. // register catch all.
  93. socket.on("*", (notification, payload) => {
  94. if (notification !== "*") {
  95. this.socketNotificationReceived(notification, payload);
  96. }
  97. });
  98. });
  99. }
  100. });
  101. NodeHelper.checkFetchStatus = function (response) {
  102. // response.status >= 200 && response.status < 300
  103. if (response.ok) {
  104. return response;
  105. } else {
  106. throw Error(response.statusText);
  107. }
  108. };
  109. /**
  110. * Look at the specified error and return an appropriate error type, that
  111. * can be translated to a detailed error message
  112. *
  113. * @param {Error} error the error from fetching something
  114. * @returns {string} the string of the detailed error message in the translations
  115. */
  116. NodeHelper.checkFetchError = function (error) {
  117. let error_type = "MODULE_ERROR_UNSPECIFIED";
  118. if (error.code === "EAI_AGAIN") {
  119. error_type = "MODULE_ERROR_NO_CONNECTION";
  120. } else if (error.message === "Unauthorized") {
  121. error_type = "MODULE_ERROR_UNAUTHORIZED";
  122. }
  123. return error_type;
  124. };
  125. NodeHelper.create = function (moduleDefinition) {
  126. return NodeHelper.extend(moduleDefinition);
  127. };
  128. module.exports = NodeHelper;