2016-03-30 14:49:37 +02:00

72 lines
1.9 KiB
JavaScript

/* Magic Mirror
* Node Helper Superclass
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
var Class = require('../../../js/class.js');
var MMSocket = require('../../../js/socketclient.js');
NodeHelper = Class.extend({
init: function() {
console.log('Initializing new module helper ...');
},
start: function() {
console.log('Staring module helper: ' + this.name);
},
/* socketNotificationReceived(notification, payload)
* This method is called when a socket notification arrives.
*
* argument notification string - The identifier of the noitication.
* argument payload mixed - The payload of the notification.
*/
socketNotificationReceived: function(notification, payload) {
Log.log(this.name + ' received a socket notification: ' + notification + ' - Payload: ' + payload);
},
/* setName(data)
* Set the module name.
*
* argument name string - Module name.
*/
setName: function(name) {
this.name = name;
this.socket();
},
/* socket()
* Returns a socket object. If it doesn't exsist, it's created.
* It also registers the notification callback.
*/
socket: function() {
if (typeof this._socket === 'undefined') {
this._socket = this._socket = new MMSocket(this.name);
}
var self = this;
this._socket.setNotificationCallback(function(notification, payload) {
self.socketNotificationReceived(notification, payload);
});
return this._socket;
},
/* sendSocketNotification(notification, payload)
* Send a socket notification to the node helper.
*
* argument notification string - The identifier of the noitication.
* argument payload mixed - The payload of the notification.
*/
sendSocketNotification: function(notification, payload) {
this.socket().sendNotification(notification, payload);
}
});
NodeHelper.create = function(moduleDefinition) {
return NodeHelper.extend(moduleDefinition);
};
module.exports = NodeHelper;