This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
forked from paulcull/ninja-diskspace
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (96 loc) · 3.38 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var Device = require('./lib/device')
, util = require('util')
, stream = require('stream')
, configHandlers = require('./lib/config-handlers');
// Give our driver a stream interface
util.inherits(dsDriver,stream);
// should be an option, but for now
// /dev/mmcblk0p2 3736512 1026724 2522448 29% /
// variable used in grep to filter down -
// for example for my mac the filter is s2 for the main drive
// in theory you could mount other network drives and monitor those too
// e.g. my macbook is 0s2
// e.g. my NB is 0p2
var default_disk = '0p2';
var default_poll_interval = 60;
// Enable/disable driver
var enabled = true;
// Our greeting to the user.
var HELLO_WORLD_ANNOUNCEMENT = {
"contents": [
{ "type": "heading", "text": "Ninja Diskspace Monitor Loaded" },
{ "type": "paragraph", "text": "The Ninja Diskspace Monitor 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 dsDriver(opts,app) {
var self = this;
this.opts = opts;
app.on('client::up',function(){
if (enabled)
{ // 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',HELLO_WORLD_ANNOUNCEMENT);
opts.hasSentAnnouncement = true;
opts.poll_interval = default_poll_interval;
opts.disk_string = default_disk;
self.save();
}
/*if (!opts.disk_string) {
opts.platform = process.platform
if (process.platform == 'darwin')
{opts.disk_string = '0s2'}
else if (process.platform =='win32')
{opts.disk_string = 'c:'}
else
{opts.disk_string = '0p2'}
self.save();
}
*/
// Register a device
self.emit('register', new Device(app, opts));
}
});
};
/**
* 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.
*/
dsDriver.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,this.opts.disk_string,this.opts.poll_interval,cb);
}
else if (typeof configHandlers[rpc.method] === "function") {
return configHandlers[rpc.method].call(this,this.opts,rpc.params,cb);
}
else {
return cb(true);
}
};
// Export it
module.exports = dsDriver;