-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
328 lines (306 loc) · 10.1 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
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
const vorpal = require('vorpal')();
const mqtt = require('mqtt');
const clc = require('cli-color');
const Table = require('cli-table');
const figlet = require('figlet');
const fs = require('fs');
const colors = ['green', 'yellow', 'blue', 'magenta', 'cyan'];
const state = {
current: undefined,
connections: []
};
async function init() {
function dupConnection(options) {
return state.connections.some(({ url, clientId, username, status }) =>
url == options.url &&
clientId == options.clientId &&
username == options.username &&
status != 'backoff' &&
status != 'kill'
);
}
function addConnection(options) {
var index = state.connections.length;
var color = clc[colors[index % 6]];
var client = mqtt.connect(options.url, { reconnectPeriod: 5000, ...options });
var connection = { index, color, client, reconnect: 0, status: 'connecting', ...options };
client.on('connect', () => {
state.connections[index].status = 'connected';
});
client.on('error', (err) => {
state.connections[index].status = 'error';
state.connections[index].error = err.message;
});
client.on('close', () => {
if (state.connections[index].status == 'kill') {
return;
}
if (state.connections[index].reconnect >= 3) {
state.connections[index].status = 'backoff';
return client.end(true);
}
state.connections[index].status = 'reconnect';
state.connections[index].reconnect++;
});
client.on('message', (topic, message, packet) => {
if (connection.silent) {
return false;
}
vorpal.log(color(`${index}> delivered to ${options.username}/${options.clientId} ${topic} ${message.toString()}`));
});
state.connections.push(connection);
return connection;
}
function lsConnections() {
var table = new Table({
head: ['#', 'url', 'clientId', 'user', 'pass', 'status', 'error']
});
state.connections.forEach((con, i) => {
let status = clc.blue('connecting');
if (con.status == 'connected') {
status = clc.green(con.status);
} else if (con.status == 'reconnect') {
status = clc.yellow(`${con.status} (${con.reconnect})`);
} else if (con.status == 'error' || con.status == 'backoff') {
status = clc.red(con.status);
} else if (con.status == 'kill') {
return;
}
var row = [`${i}${state.current == i ? '*' : ''}`, con.url, con.clientId, con.username, con.password, status, con.error || ''];
if (!con.save) {
row = row.map(c => clc.inverse(c));
}
table.push(row);
})
vorpal.log(table.toString());
}
function checkConnection(index) {
return state.connections.length > index &&
state.connections[index].status == 'connected';
}
function saveConnection({ url, clientId, username, password }) {
var connections = JSON.parse(vorpal.localStorage.getItem('connections') || '[]');
connections.push({ url, clientId, username, password });
vorpal.localStorage.setItem('connections', JSON.stringify(connections));
}
function reloadConnections() {
try {
JSON.parse(vorpal.localStorage.getItem('connections') || '[]').forEach(opts => addConnection({ save: true, ...opts }));
JSON.parse(fs.readFileSync('./connections.json').toString()).filter(opts => !dupConnection(opts)).forEach(opts => addConnection({ save: true, ...opts }));
} catch (e) {
void (0);
}
lsConnections();
}
vorpal
.command('connect', 'Connect to mqtt.')
.option('-h, --url <url>', 'the host required for connecting to your broker.')
.option('-c, --clientId <clientId>', 'the clientId required by your broker, if any.')
.option('-u, --username <username>', 'the username required by your broker, if any.')
.option('-p, --password <password>', 'the password required by your broker, if any.')
.option('--clean <clean>', 'set to false to receive QoS 1 and 2 messages while offline.')
.option('--save', 'save connection.')
.types({
string: ['u', 'username', 'p', 'password']
})
.validate((args) => {
var { options } = args;
if (!options.url) {
return 'the host required for connecting to your broker';
} else if (!options.clientId) {
return 'the clientId required by your broker';
} else if (!options.username) {
return 'the username required by your broker';
} else if (!options.password) {
return 'the password required by your broker';
}
return true;
})
.action((args, callback) => {
if (!dupConnection(args.options)) {
var { index, save } = addConnection(args.options);
if (save) {
saveConnection(args.options);
}
state.current = index;
}
lsConnections();
callback();
});
vorpal
.command('ls', 'List of available connections.')
.action((args, callback) => {
lsConnections();
callback();
});
vorpal
.command('use [number]', 'Use a mqtt connections.')
.autocomplete({
data: () => state.connections.map((v, i) => `${i}`)
})
.validate((args) => {
if ('number' in args && checkConnection(args.number)) {
return true
}
return clc.red('please check connection status');
})
.action((args, callback) => {
state.current = args.number;
callback();
});
vorpal
.command('silent [number]', 'Silent a mqtt connections.')
.option('--off', 'disable silent.')
.autocomplete({
data: () => state.connections.map((v, i) => `${i}`)
})
.validate((args) => {
if ('number' in args && state.connections.length > args.number) {
return true
}
return clc.red('invalid connection number');
})
.action((args, callback) => {
var { number, options: { off } } = args;
if (off) {
delete state.connections[number].silent;
} else {
state.connections[number].silent = true;
}
callback();
});
vorpal
.command('pub', 'Publish message to the topic.')
.option('-t, --topic <topic>', 'the topic to publish.')
.option('-p, --payload <payload>', 'the message to publish.')
.option('-q, --qos <qos>', 'the QoS.')
.option('--js2json', 'convert payload to json before publishing.')
.types({
string: ['t', 'topic']
})
.validate((args) => {
var { options } = args;
if (!checkConnection(state.current)) {
return 'please check connection status';
} else if (!options.topic) {
return 'the topic to publish';
} else if (!options.payload) {
return 'the message to publish';
}
return true;
})
.action((args, callback) => {
var { options: { topic, payload, qos } } = args;
var client = state.connections[state.current].client;
if (args.options.js2json) {
try {
payload = JSON.stringify(eval('(' + payload + ')'));
} catch (e) {
vorpal.log(e);
}
}
client.publish(topic, Buffer.from(payload), { qos }, () => callback());
});
vorpal
.command('sub', 'Subscribe topic.')
.option('-t, --topic <topic>', 'the topic to subscribe.')
.option('-q, --qos <qos>', 'the QoS.')
.types({
string: ['t', 'topic']
})
.validate((args) => {
var { options } = args;
if (!checkConnection(state.current)) {
return 'please check connection status';
} else if (!options.topic) {
return 'the topic to subscribe';
}
return true;
})
.action((args, callback) => {
var { options: { topic, qos } } = args;
var client = state.connections[state.current].client;
client.subscribe(topic, { qos: qos || 1 }, () => callback());
});
vorpal
.command('unsub', 'Unsubscribe topic.')
.option('-t, --topic <topic>', 'the topic to unsubscribe.')
.types({
string: ['t', 'topic']
})
.validate((args) => {
var { options } = args;
if (!checkConnection(state.current)) {
return 'please check connection status';
} else if (!options.topic) {
return 'the topic to subscribe';
}
return true;
})
.action((args, callback) => {
var { options: { topic, qos } } = args;
var client = state.connections[state.current].client;
client.unsubscribe(topic, () => callback());
});
vorpal
.command('save', 'Save current connection.')
.validate((args) => {
if (checkConnection(state.current)) {
return true
}
return clc.red('please use a connection and check its status');
})
.action((args, callback) => {
var connection = state.connections[state.current];
saveConnection(connection);
state.connections[state.current].save = true;
callback();
});
vorpal
.command('kill [number]', 'Kill a mqtt connections.')
.autocomplete({
data: () => state.connections.map((v, i) => `${i}`)
})
.validate((args) => {
if ('number' in args && state.connections.length > args.number) {
return true
}
return clc.red('invalid connection number');
})
.action((args, callback) => {
var { number } = args;
state.connections[number].client.end(true);
state.connections[number].status = 'kill';
var connections = state.connections.filter(con => con.status != 'kill')
.map(({ url, clientId, username, password }) => ({ url, clientId, username, password }));
vorpal.localStorage.setItem('connections', JSON.stringify(connections));
if (number == state.current) {
state.current == undefined;
}
lsConnections();
callback();
});
vorpal
.command('restart', 'Restart.')
.action((args, callback) => {
state.connections.forEach(con => con.client.end(true));
state.connections.length = 0;
state.state = undefined;
reloadConnections();
callback();
});
figlet('Mqtt Cli', function (err, data) {
if (err) return
vorpal
.history('mqtt-cli')
.localStorage('mqtt-cli')
.log(data)
.delimiter('mqtt$')
.show();
reloadConnections();
});
}
init();
process.on('uncaughtException', function (error) {
vorpal.log(error);
});