MagicMirror/js/node_helper.js

140 lines
3.4 KiB
JavaScript
Raw Normal View History

/* Magic Mirror
* Node Helper Superclass
*
2020-04-28 23:05:28 +02:00
* By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.
*/
2020-05-02 10:39:09 +02:00
const Class = require("./class.js");
2021-02-18 19:14:53 +01:00
const Log = require("logger");
2020-05-02 10:39:09 +02:00
const express = require("express");
2021-01-05 18:44:36 +01:00
const NodeHelper = Class.extend({
init() {
2020-05-11 07:25:42 +02:00
Log.log("Initializing new module helper ...");
},
2021-01-05 18:44:36 +01:00
loaded(callback) {
Log.log(`Module helper loaded: ${this.name}`);
callback();
},
2021-01-05 18:44:36 +01:00
start() {
Log.log(`Starting module helper: ${this.name}`);
},
/* stop()
* Called when the MagicMirror server receives a `SIGINT`
* Close any open connections, stop any sub-processes and
* gracefully exit the module.
*
*/
2021-01-05 18:44:36 +01:00
stop() {
Log.log(`Stopping module helper: ${this.name}`);
},
/* socketNotificationReceived(notification, payload)
* This method is called when a socket notification arrives.
*
* argument notification string - The identifier of the notification.
* argument payload mixed - The payload of the notification.
*/
2021-01-05 18:44:36 +01:00
socketNotificationReceived(notification, payload) {
Log.log(`${this.name} received a socket notification: ${notification} - Payload: ${payload}`);
},
/* setName(name)
* Set the module name.
*
* argument name string - Module name.
*/
2021-01-05 18:44:36 +01:00
setName(name) {
this.name = name;
},
/* setPath(path)
* Set the module path.
*
* argument path string - Module path.
*/
2021-01-05 18:44:36 +01:00
setPath(path) {
this.path = path;
},
/* sendSocketNotification(notification, payload)
* Send a socket notification to the node helper.
*
* argument notification string - The identifier of the notification.
* argument payload mixed - The payload of the notification.
*/
2021-01-05 18:44:36 +01:00
sendSocketNotification(notification, payload) {
this.io.of(this.name).emit(notification, payload);
},
/* setExpressApp(app)
* Sets the express app object for this module.
* This allows you to host files from the created webserver.
*
* argument app Express app - The Express app object.
*/
2021-01-05 18:44:36 +01:00
setExpressApp(app) {
this.expressApp = app;
2021-01-05 18:44:36 +01:00
app.use(`/${this.name}`, express.static(`${this.path}/public`));
},
/* setSocketIO(io)
* Sets the socket io object for this module.
* Binds message receiver.
*
* argument io Socket.io - The Socket io object.
*/
2021-01-05 18:44:36 +01:00
setSocketIO(io) {
this.io = io;
2021-01-05 18:44:36 +01:00
Log.log(`Connecting socket for: ${this.name}`);
io.of(this.name).on("connection", (socket) => {
// add a catch all event.
2021-01-05 18:44:36 +01:00
const onevent = socket.onevent;
socket.onevent = function (packet) {
2021-01-05 18:44:36 +01:00
const args = packet.data || [];
onevent.call(this, packet); // original call
packet.data = ["*"].concat(args);
onevent.call(this, packet); // additional call to catch-all
};
// register catch all.
2021-01-05 18:44:36 +01:00
socket.on("*", (notification, payload) => {
if (notification !== "*") {
2021-01-05 18:44:36 +01:00
this.socketNotificationReceived(notification, payload);
}
});
});
}
});
2021-04-24 09:14:08 +02:00
NodeHelper.checkFetchStatus = function (response) {
// response.status >= 200 && response.status < 300
if (response.ok) {
return response;
} else {
throw Error(response.statusText);
}
};
NodeHelper.checkFetchError = function (error) {
let error_type = "MODULE_ERROR_UNSPECIFIED";
if (error.code === "EAI_AGAIN") {
error_type = "MODULE_ERROR_NO_CONNECTION";
2021-05-02 14:43:12 +02:00
} else if (error.message === "Unauthorized") {
error_type = "MODULE_ERROR_UNAUTHORIZED";
}
return error_type;
};
NodeHelper.create = function (moduleDefinition) {
return NodeHelper.extend(moduleDefinition);
};
2021-01-05 18:44:36 +01:00
module.exports = NodeHelper;