-
Notifications
You must be signed in to change notification settings - Fork 30
/
5-rolling-messages.js
91 lines (75 loc) · 2.42 KB
/
5-rolling-messages.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
//
// Copyright (c) 2018 Cisco Systems
// Licensed under the MIT License
//
/**
* Modify some of your device's configuration settings
* In this example, we'll change the Halfwake and Awake messages
*/
//
// Connect to the device
//
const jsxapi = require('jsxapi');
// Check args
if (!process.env.JSXAPI_DEVICE_URL || !process.env.JSXAPI_USERNAME) {
console.log("Please specify info to connect to your device as JSXAPI_DEVICE_URL, JSXAPI_USERNAME, JSXAPI_PASSWORD env variables");
console.log("Bash example: JSXAPI_DEVICE_URL='ssh://192.168.1.34' JSXAPI_USERNAME='integrator' JSXAPI_PASSWORD='integrator' node example.js");
process.exit(1);
}
// Empty passwords are supported
const password = process.env.JSXAPI_PASSWORD ? process.env.JSXAPI_PASSWORD : "";
// Connect to a device
const xapi = jsxapi.connect(process.env.JSXAPI_DEVICE_URL, {
username: process.env.JSXAPI_USERNAME,
password: password
});
xapi.on('error', (err) => {
console.error(`connexion failed: ${err}, exiting`);
process.exit(1);
});
//
// Custom logic
//
function update(value) {
// Update Awake message
xapi.config.set('UserInterface CustomMessage', value)
.then(() => {
console.log('updated Awake message')
})
.catch((err) => {
console.error(`could not update Awake message : ${err.message}`)
});
}
// Iterator that rolls values among an array for a max number of times
let current = {}
current.options = [];
current.iterations = 0;
current.iterate = function (original, max) {
// Reset options if array is empty
if (this.options.length === 0) {
this.options = original;
}
this.iterations++;
if (this.iterations > max) {
clearInterval(current.timer);
return true;
}
// Pop next option
var choice = this.options.pop();
update(choice);
return false;
}
xapi.on('ready', () => {
console.log("connexion successful");
const delay = 3; //seconds
const iterations = 3;
console.log(`Starting loop to roll Awake messages every ${delay} seconds, with ${iterations} iterations`);
current.timer = setInterval(function () {
const completed = current.iterate(["Read a tutorial", "Launch a Sandbox", "Attend an event", "Pick a DevNet activity among:"], iterations);
// Roll completed
if (completed) {
console.log("Exiting.");
xapi.close();
}
}, delay * 1000);
});