-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path13a-environment.js
108 lines (92 loc) · 3.34 KB
/
13a-environment.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
//
// Copyright (c) 2020 Cisco Systems
// Licensed under the MIT License
//
/**
* Implements Environment Variables for Macros:
* A* environment.js : this macro manages volatile ENV variables (stores/updates and returns the current values for an environment variable)
* B. getenv.js : this is not a macro but a code snippet that you insert into existing macros to retrieve ENV variables (local to your device)
*
* Quick start:
* - customize the default list of ENV variables for your device
* - deploy the 'environment.js' macro to your device
* - insert the 'getenv()' code snipped into other macros on the same device
* - access ENV variables from macros once 'env-ready' event is emmitted
*
* Note: if a variable is not found in ENV, '' is returned (empty)
*
* Example of a Macro using the getenv() function:
*
* // Fired when the environnment Macro is ready to provide ENV variables
* xapi.on('env-ready', async (ready) => {
* if (!ready) {
* console.log('Environment macro is not responding! aborting...');
* return;
* }
*
* let variable = 'DEVICE_SECRET'
* let secret = await getenv(variable);
* console.log(`echo \$${variable} = ${secret}`);
* });
*
*/
const xapi = require('xapi');
// List of variables for the environment
const ENV = {
'PING': 'PONG',
// ADD environment variables below
'DEVICE_SECRET' : 1234
};
xapi.event.on('Message Send Text', (msg) => {
console.debug(`new "Message Send" Event with text: ${msg}`);
// WORKAROUND: needed if message was sent from the cloud via POST https://api.ciscospark.com/v1/xapi/command/message.send
msg = msg.replace(/\'/g, '"');
let parsed;
try {
parsed = JSON.parse(msg);
}
catch (err) {
console.err(`cannot JSON parse the text in 'Message Send' Event: ${msg}, ignoring...`);
return
}
let data = parsed;
if (!data.env) {
console.debug(`"Message Send" Event is not about Env: ${msg}, ignoring`);
return;
}
console.debug(`"Message Sent" event concerns env variable: "${data.env}"`);
// GET ENV
if (data.operation && (data.operation == 'get')) {
if (!data.env) {
console.warn(`no ENV variable specified in: "${msg}", ignoring...`);
return;
}
console.debug(`requested value for env variable: "${data.env}"`);
let response = {
'env': data.env,
'operation': 'get_response',
'value': '' // default to '' if variable is not found
}
let value = ENV[data.env];
if (value) {
response.value = value;
}
// Publish value for ENV variable
xapi.command('Message Send', { Text: JSON.stringify(response) });
return;
}
// RESPONSE provided => ignore
if (data.operation && (data.operation == 'get_response')) {
console.debug(`ignoring get_response type of "Message Sent" event: ${msg}`);
return;
}
// SET ENV
// Note that the storage is volatile (dynamically added variables will be lost every time the macro or runtime is restared)
if (data.operation && (data.operation == 'set')) {
console.log(`setting value: "${data.value}" for env variable: "${data.env}"`);
ENV[data.env] = data.value;
return;
}
console.warn(`operation for "Message Sent" event is not supported:"${msg}, ignoring...`);
return;
})