MagicMirror/js/node_helper.js

127 lines
3.3 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");
2020-05-11 07:25:42 +02:00
const Log = require("./logger.js");
2020-05-02 10:39:09 +02:00
const express = require("express");
2020-05-02 10:39:09 +02:00
var NodeHelper = Class.extend({
2020-05-25 18:57:15 +02:00
init: function () {
2020-05-11 07:25:42 +02:00
Log.log("Initializing new module helper ...");
},
2020-05-25 18:57:15 +02:00
loaded: function (callback) {
2020-05-11 07:25:42 +02:00
Log.log("Module helper loaded: " + this.name);
callback();
},
2020-05-25 18:57:15 +02:00
start: function () {
2020-05-11 07:25:42 +02:00
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.
*
*/
2020-05-25 18:57:15 +02:00
stop: function () {
2020-05-11 07:25:42 +02:00
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.
*/
2020-05-25 18:57:15 +02:00
socketNotificationReceived: function (notification, payload) {
2020-05-11 07:25:42 +02:00
Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
},
/* setName(name)
* Set the module name.
*
* argument name string - Module name.
*/
setName: function (name) {
this.name = name;
},
/* setPath(path)
* Set the module path.
*
* argument path string - Module path.
*/
setPath: function (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.
*/
sendSocketNotification: function (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.
*/
setExpressApp: function (app) {
this.expressApp = app;
var publicPath = this.path + "/public";
app.use("/" + this.name, express.static(publicPath));
},
/* setSocketIO(io)
* Sets the socket io object for this module.
* Binds message receiver.
*
* argument io Socket.io - The Socket io object.
*/
setSocketIO: function (io) {
var self = this;
self.io = io;
2020-05-11 07:25:42 +02:00
Log.log("Connecting socket for: " + this.name);
var namespace = this.name;
io.of(namespace).on("connection", function (socket) {
// add a catch all event.
var onevent = socket.onevent;
socket.onevent = function (packet) {
var 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.
socket.on("*", function (notification, payload) {
if (notification !== "*") {
2020-05-11 07:25:42 +02:00
//Log.log('received message in namespace: ' + namespace);
self.socketNotificationReceived(notification, payload);
}
});
});
}
});
NodeHelper.create = function (moduleDefinition) {
return NodeHelper.extend(moduleDefinition);
};
2020-05-02 10:39:09 +02:00
/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = NodeHelper;
}