forked from pablorodiz/ninjablocks-wake-on-lan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (69 loc) · 2.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var Device = require('./lib/device')
, util = require('util')
, stream = require('stream')
, configHandlers = require('./lib/config-handlers');
// Give our driver a stream interface
util.inherits(myDriver,stream);
// Our greeting to the user.
var ANNOUNCEMENT = {
"contents": [
{ "type": "heading", "text": "Wake on LAN driver loaded" },
{ "type": "paragraph", "text": "Wake on LAN driver has been loaded. You should not see this message again." }
]
};
/**
* Called when our client starts up
* @constructor
*
* @param {Object} opts Saved/default driver configuration
* @param {Object} app The app event emitter
* @param {String} app.id The client serial number
*
* @property {Function} save When called will save the contents of `opts`
* @property {Function} config Will be called when config data is received from the Ninja Platform
*
* @fires register - Emit this when you wish to register a device (see Device)
* @fires config - Emit this when you wish to send config data back to the Ninja Platform
*/
function myDriver(opts,app) {
var self = this;
app.on('client::up',function(){
// The client is now connected to the Ninja Platform
// Check if we have sent an announcement before.
// If not, send one and save the fact that we have.
if (!opts.hasSentAnnouncement) {
self.emit('announcement',ANNOUNCEMENT);
opts.hasSentAnnouncement = true;
self.save();
}
// Register a device
self.emit('register', new Device());
});
};
/**
* Called when a user prompts a configuration.
* If `rpc` is null, the user is asking for a menu of actions
* This menu should have rpc_methods attached to them
*
* @param {Object} rpc RPC Object
* @param {String} rpc.method The method from the last payload
* @param {Object} rpc.params Any input data the user provided
* @param {Function} cb Used to match up requests.
*/
myDriver.prototype.config = function(rpc,cb) {
var self = this;
// If rpc is null, we should send the user a menu of what he/she
// can do.
// Otherwise, we will try action the rpc method
if (!rpc) {
return configHandlers.menu.call(this,cb);
}
else if (typeof configHandlers[rpc.method] === "function") {
return configHandlers[rpc.method].call(this,rpc.params,cb);
}
else {
return cb(true);
}
};
// Export it
module.exports = myDriver;