-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlibrary_device.js.html
377 lines (353 loc) · 14.5 KB
/
library_device.js.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: library/device.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: library/device.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* This file is part of Domotz Agent.
*
* @license
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* @requires module:sandbox/library/ssh
* @requires module:sandbox/library/snmp
* @requires module:sandbox/library/telnet
* @requires module:request
* @requires module:util
* @copyright Copyright (C) Domotz Inc
*/
/**
* The Domotz Device object.
* Exposes device related libraries for ssh, telnet, http remote call executions.
* Contains device information such as its Custom Driver Management credentials and IP address
* @example D.device
* @namespace device
* @memberof D
*/
var request = require('request');
var ssh = require('./ssh');
var telnet = require('./telnet');
const util = require('util');
/**
* The Http Library Options
* @typedef {object} HttpOptions
* @property {string} url - The url suffix of the request target
* @property {object} [headers] - The Request HTTP Headers defined as key value pairs. Eg {'Accept': 'text/html'}
* @property {string} [body] - The Request HTTP Body
* @property {integer} [port=80] - The port to use for the request. If protocol is set to https the default is 443
* @property {string|object} [auth] - The authentication to use. Eg 'basic' / {'bearer': 'the token'}
* @property {string} [username] - The device username. If not set the custom driver management purpose one is used
* @property {string} [password] - The device password. If not set the custom driver management purpose one is used
* @property {boolean} [jar=false] - if true, remember cookies for future use
* @property {http|https} [protocol=http] - The protocol to use in the request
*/
/**
* The Http library callback
* @typedef {callback} HttpCallback
* @property {string} [error] - The HTTP response error
* @property {object} response - The HTTP response object
* @property {string} [body] - The HTTP response body
*/
function createDevice(device, agentDriverSettings, myConsole) {
function buildTelnetOptions(options) {
options.host = device.ip;
if (options.command !== undefined) {
options.cmd = options.command + "\r";
delete options.command;
} else {
throw Error("D.sendTelnetCommand requires a 'command' in its options parameter");
}
myConsole.debug(util.inspect(options));
return options;
}
function buildSSHOptions(options) {
options.host = device.ip;
if (!options.username && device.credentials) {
options.username = device.credentials.username;
options.password = new Buffer(device.credentials.password, 'base64').toString();
}
myConsole.debug(util.inspect(options));
return options;
}
function buildRequest(options) {
if (typeof options !== 'object') {
options = { url: 'http://' + device.ip + options };
} else {
var protocol = "http";
var port = '';
var password = '';
if (options.port) {
port = ':' + options.port;
delete options.port;
}
if( options.protocol === 'https') {
protocol = options.protocol;
port = port || ":443";
delete options.protocol;
}
if (device.credentials) {
password = new Buffer(device.credentials.password, 'base64').toString();
options.url = options.url.replace('${username}', device.credentials.username);
options.url = options.url.replace('${password}', password);
}
if (options.auth === 'basic' && device.credentials) {
myConsole.debug(device.credentials);
options.auth = {
'user': device.credentials.username,
'pass': password,
'sendImmediately': true
};
} else {
options.auth = undefined;
}
if (protocol !== 'http' && protocol !== 'https') {
throw Error("Invalid protocol: " + protocol);
}
options.url = protocol + '://' + device.ip + port + options.url;
}
myConsole.debug(util.inspect(options));
return options;
}
return {
/**
* Returns stored username of a device.
* @example
* // returns 'the device username'
* D.device.username()
* @memberof D.device
* @readonly
* @function
* @return {string}
*/
username: function () {
return device.credentials.username;
},
/**
* Returns the stored password of a device.
* @example
* // returns 'the device password'
* D.device.password()
* @memberof D.device
* @readonly
* @function
* @return {string}
*/
password: function () {
return new Buffer(device.credentials.password, 'base64').toString();
},
/**
* Returns the IP address of the device object.
* @example
* // returns '192.168.1.1' for a device with this local IP address
* D.device.ip()
* @memberof D.device
* @readonly
* @function
* @return {string}
*/
ip: function () {
return device.ip;
},
/**
* Starts an SNMP session with the device.
* @private
* @example
* // returns an snmpSession object to use for snmp related queries
* D.device.createSNMPSession()
* @memberof D.device
* @readonly
* @function
* @return {snmpSession}
*/
createSNMPSession: function () {
return require('./snmp').createSessionForDevice(device);
},
/**
* Sends a command to the device via Telnet.
* @example
* D.device.sendTelnetCommand(options, callback)
* [See Telnet Driver Examples]{@link https://github.com/domotz/custom-driver/tree/master/examples/telnet}
* @memberof D.device
* @readonly
* @param {TelnetOptions} options - The Telnet Command execution options
* @param {TelnetCallback} callback - The Telnet Command execution callback function
* @function
*/
sendTelnetCommand: function (options, callback) {
myConsole.info("Performing Telnet Command: %s", options.command);
options = buildTelnetOptions(options);
telnet.sendTelnetCommand(myConsole, options, callback);
},
/**
* Sends a command to the device via SSH.
* @example D.device.sendSSHCommand(options, callback)
* @example
* [See SSH Driver Examples]{@link https://github.com/domotz/custom-driver/tree/SSB-1216/examples/ssh}
* @memberof D.device
* @readonly
* @param {SshOptions} options - The SSH Command execution options
* @param {SshCallback} callback - The SSH Command execution callback function
* @function
*/
sendSSHCommand: function (options, callback) {
myConsole.info("Performing SSH Command: %s", options.command);
options = buildSSHOptions(options);
return ssh.sendSSHCommand(myConsole, options, callback);
},
/**
* Sends a sequence of commands to the device via SSH.
* @private
* @example D.device.sendSSHCommands(options, callback)
* @memberof D.device
* @readonly
* @param {SshShellSequenceOptions} options - The SSH Commands execution options
* @param {SshShellSequenceCallback} callback - The SSH Commands execution callback function
* @function
*/
sendSSHCommands: function (options, callback) {
myConsole.info("Performing SSH Commands: %s", options.commands);
options = buildSSHOptions(options);
ssh.sendSSHShellSequence(myConsole, options, callback);
},
/**
* The Device HTTP library.
* Allows drivers to execute HTTP(s) requests towards the device.
* @example D.device.http
* [See HTTP Driver Examples]{@link https://github.com/domotz/custom-driver/tree/master/examples/http}
* @memberof D.device
* @namespace http
*/
http: {
/**
* Executes an HTTP GET request towards the device.
* @example D.device.http.get(options, callback)
* @memberof D.device.http
* @readonly
* @param {HttpOptions} options - The HTTP request execution options
* @param {HttpCallback} callback - The HTTP request execution callback
* @function
*/
get: function (options, callback) {
myConsole.info("Performing GET towards:" + options.url);
options = buildRequest(options);
request.get(options, callback);
},
/**
* Executes an HTTP POST request towards the device.
* @example D.device.http.post(options, callback)
* @memberof D.device.http
* @readonly
* @param {HttpOptions} options - The HTTP request execution options
* @param {HttpCallback} callback - The HTTP request execution callback
* @function
*/
post: function (options, callback) {
myConsole.info("Performing POST towards:" + options.url);
options = buildRequest(options);
request.post(options, callback);
},
/**
* Executes an HTTP PUT request towards the device.
* @example D.device.http.put(options, callback)
* @memberof D.device.http
* @readonly
* @param {HttpOptions} options - The HTTP request execution options
* @param {HttpCallback} callback - The HTTP request execution callback
* @function
*/
put: function (options, callback) {
myConsole.info("Performing PUT towards:" + options.url);
options = buildRequest(options);
request.put(options, callback);
},
/**
* Executes an HTTP DELETE request towards the device.
* @example D.device.http.delete(options, callback)
* @memberof D.device.http
* @readonly
* @param {HttpOptions} options - The HTTP request execution options
* @param {HttpCallback} callback - The HTTP request execution callback
* @function
*/
delete: function (options, callback) {
myConsole.info("Performing DELETE towards:" + options.url);
options = buildRequest(options);
request.delete(options, callback);
}
},
/**
* Creates a custom driver variable to be sent in the D.success callback.
* @example
* // returns {"uid": "1a", "unit": "C", "value": 60, "label": "CPU Temperature"}
* D.device.createVariable('1a', 'CPU Temperature', 60, 'C')
* @memberof D.device
* @function
* @readonly
* @param {string} uid - The identifier of the variable. Must be Unique. Max 50 characters
* @param {string} name - The Name/Label of the variable
* @param {string} value - The Value of the variable
* @param {string} unit - The Unit of measurement of the variable (eg %). Max 10 characters
* @return {Variable}
*/
createVariable: function (uid, name, value, unit) {
if (uid === null || uid === undefined || uid.length < 1 || uid.length > agentDriverSettings.max_var_id_len) {
throw Error("Invalid variable uid: " + uid);
}
if (unit) {
unit = unit.substr(0, agentDriverSettings.max_var_unit_len);
} else {
unit = null;
}
if (value == undefined) {
value = null;
} else {
value = String(value);
}
return {
"uid": uid,
"unit": unit,
"value": value,
"label": name
};
}
};
}
module.exports.device = createDevice;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Externals</h3><ul><li><a href="D.external__.html">_</a></li></ul><h3>Namespaces</h3><ul><li><a href="console.html">console</a></li><li><a href="D.html">D</a></li><li><a href="D.device.html">device</a></li><li><a href="D.device.http.html">http</a></li><li><a href="D.math.html">math</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Tue Dec 14 2021 14:10:54 GMT+0200 (EET)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>