-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.js
407 lines (332 loc) · 13.5 KB
/
UI.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import MqttClientHandler from "./MqttClientHandler";
class UI {
/**
* Constains all necessary methods for dyanmic creation of UI items (tabs, buttons, text-area, etc)
* Uses bootstrap (hence ajax and jquery)
*/
constructor() {
}
}
UI.prototype.add_input_form = function (parent_id_) {
const _id = "input_form";
const _input_form = '<input id="' + _id + '" type="file" class="form-control"></input>'
$("#" + parent_id_).append(_input_form);
console.debug("added '" + _id + "' to '" + parent_id_ + "'");
return _id;
}
UI.prototype.add_input_form_cb = function (cb_) {
const _id = "input_form";
$("#" + _id).on("change",
function (e) {
console.debug(_id + ": on_change");
cb_();
}
);
console.debug("add_input_form_cb: added callback");
}
/**
* Creates an empty bootstrap-list of tabs that will be filled with ui-items (vertically)
*/
UI.prototype.add_tablist_group = function (parent_id_) {
const _id = "list_tab";
const _list = '<div id="' + _id + '" class="list-group" role="tablist"></div>'
$("#" + parent_id_).append(_list);
console.debug("added '" + _id + "' to '" + parent_id_ + "'");
return _id;
}
/**
* 1. Creates `tab_broker` with 2 children: `column 1` and `column 2`
* 2. Attaches `button_broker` to `col 1` and `text_broker` to `col 2`
* 3. `text_broker` will reflect broker status (loaded, wrong format) or broker info when connected (host, port, etc)
* @param {String} parent_id_
*/
UI.prototype.add_broker_tab = function (parent_id_) {
const _sufix = "_broker";
const _id_tab = this.add_tab(parent_id_, _sufix);
const _id_row = this.add_row(_id_tab, _sufix);
const _id_col1 = this.add_col(_id_row, _sufix, 1);
const _id_col2 = this.add_col(_id_row, _sufix, 2);
// add button to col1
const _id_button = this.add_button(_id_col1, _sufix, "Connect / Disconnect");
// add badge to button. Badge value will be updated `on_connected`, `on_reconnect`
const _id_badge = this.add_badge(_id_button, _sufix);
$("#" + _id_badge).text("Status");
$("#" + _id_badge).attr("class", "badge badge-light bg-secondary");
// add text to col2
// it will reflect broker status (loaded, wrong format) or broker info when connected (host, port, etc)
const _id_text = this.add_textarea(_id_col2, _sufix, "");
}
UI.prototype.add_broker_button_cb = function (cb_) {
const _id = "button_broker";
$("#" + _id).on("click",
function (e) {
console.debug(_id + ": onClick");
cb_();
}
);
console.debug("add_broker_button_cb: added callback");
}
UI.prototype.reset_broker_tab_status = function () {
const _button = $("#" + "button_broker");
_button.prop("disabled", false);
const _badge = $("#" + "badge_broker");
_badge.text("Status");
_badge.attr("class", "badge badge-light bg-secondary");
}
UI.prototype.disable_broker_button = function (disable_) {
$("#" + "button_broker").prop("disabled", disable_);
}
// compound elements (tabs)
/**
* Triggers all methods needed to create the list of tabs (buttons and text) for each topic
*
* Assummes `this.cfg` is already filled and format is correct
*
* 1. Removes previous tabs (if existing)
* 2. Adds new tabs and filles the new items
* 3. Attaches buttons callbacks
* 4. Disables all buttons initilly
*/
UI.prototype.setup_ui = function (data_, mqtt_h_) {
this.remove_items_from_tablist();
this.add_items_to_tablist(data_);
this.disable_all_buttons_of_topics(data_);
this.add_buttons_cb(data_, mqtt_h_);
}
/**
* Removes all tabs except `tab_broker`
*/
UI.prototype.remove_items_from_tablist = function () {
console.debug("removing previous items from tablist (if existing)")
const _parent_id = "list_tab";
const _children = $("#" + _parent_id).children();
for (let i = 0; i < _children.length; i++) {
let _c = _children[i]
if ("tab_broker" != _c.id) {
_c.remove();
}
}
}
/**
* It adds bootstrap UI elements as children of `parent_id`
*
* For each item into `ui_setup.json`, it will create a tab:
* 1. Every tab will contain `button`, `text-box` and `color`
* 2. A subscriber button (ex: `Room temperature`) when clicked will subscribe or unsubscribe from topic
* - When subscribed color will change to `green`
* - When received message will be reflected into text-box
* - When unsubscribed color will change to `red`
* - text-box will cleared
* 3. A publisher button (ex: `Increase AC temp`) when clicked will publish to topic
* - When publish succeeded color will blink to `green` for 100 ms
* - When publish failed color will blink to `red` for 100 ms
*
* @param {Dictionary} data_ json containing UI configuration
*/
UI.prototype.add_items_to_tablist = function (data_) {
console.debug("adding new items into tablist")
const _parent_id = "list_tab";
const _items = data_.items;
for (let i = 0; i < _items.length; i++) {
let _item = _items[i];
let _id_tab = this.add_tab(_parent_id, i);
let _id_row = this.add_row(_id_tab, i);
let _id_col1 = this.add_col(_id_row, i, 1);
let _id_col2 = this.add_col(_id_row, i, 2);
let _id_button = this.add_button(_id_col1, i, _item.name);
let _id_badge = this.add_badge(_id_button, i);
let _id_text = this.add_textarea(_id_col2, i, _item.topic);
}
console.debug("added ui-items: " + _items.length + " to '" + _parent_id + "'");
}
UI.prototype.disable_all_buttons_of_topics = function (data_) {
for (let i = 0; i < data_.items.length; i++) {
let _id = "button" + i;
$("#" + _id).prop("disabled", true);
}
}
UI.prototype.enable_all_buttons_of_topics = function (data_) {
for (let i = 0; i < data_.items.length; i++) {
let _id = "button" + i;
$("#" + _id).prop("disabled", false);
}
}
/**
* Attaches `onClick` callbacks dynamically. These callbacks trigger mqtt publish / subscribe
*
* It assumes all items form `ui_setup.json` have `name` and `topic` fields
*
* NOTE: when adding callbacks: if item has "message" will be created a mqtt-publisher, otherwise it will create a mqtt-subscriber
* NOTE: one click for subscribing and one click for un-subscribing
*
* @param {Dictionary} data_ json containing UI configuration
* @param {MqttClientHandler} mqtt_h_ mqtt client handler instance to trigger publish / subscribe / unsubscribe
*/
UI.prototype.add_buttons_cb = function (data_, mqtt_h_) {
const _items = data_.items;
for (let i = 0; i < _items.length; i++) {
let _item = _items[i];
let _id_button = 'button' + i;
let _id_badge = "badge" + i;
let _id_text = "text" + i;
// it is a publish topic
if (undefined !== _item.message) {
$("#" + _id_button).on("click",
function (e) {
console.debug(_id_button + " onClick");
if (!mqtt_h_.is_connected()) {
console.error("Not connected to broker");
return;
}
// prevent attending burst of clicks
if (undefined === _item.processing) {
_item.processing = true;
const _on_published = function (e) {
$("#" + _id_badge).attr("class", "badge badge-light bg-warning");
$("#" + _id_text).val(_item.message);
$("#" + _id_button).prop("disabled", true);
setTimeout(() => {
$("#" + _id_badge).attr("class", "badge badge-light bg-secondary");
}, 100);
setTimeout(() => {
$("#" + _id_text).val("");
$("#" + _id_button).prop("disabled", false);
_item.processing = undefined;
}, 300);
}.bind(this);
mqtt_h_.publish(_item, _on_published)
}
}.bind(this)
);
}
// it is a subscribe topic
else {
$("#" + _id_button).on("click",
function (e) {
console.debug(_id_button + " onClick");
if (!mqtt_h_.is_connected()) {
console.error("Not connected to broker");
return;
}
// prevent attending burst of clicks
if (undefined === _item.processing) {
_item.processing = true;
if (undefined === _item.subscribed) {
const _on_subscribed = function (e) {
$("#" + _id_badge).attr("class", "badge badge-light bg-success");
// set "sub" flag
_item.subscribed = true;
_item.processing = undefined;
}.bind(this);
mqtt_h_.subscribe(_item, _on_subscribed);
}
else {
const _on_unsubscribed = function (e) {
$("#" + _id_badge).attr("class", "badge badge-light bg-danger");
// un-set "sub" flag
_item.subscribed = undefined;
_item.processing = undefined;
}.bind(this);
mqtt_h_.unsubscribe(_item, _on_unsubscribed)
}
}
}.bind(this)
);
}
}
console.debug("added ui-items callbacks: " + _items.length);
}
// update elements
UI.prototype.set_broker_info_text = function (data_) {
const _broker = data_.mqtt_broker;
const _url = "mqtt://" + _broker.host + ":" + _broker.port;
const _options = _broker.options;
const _options_str = "clientId " + _options.clientId + "\n"
+ "keepalive " + _options.keepalive + "\n"
+ "reconnect " + _options.reconnectPeriod + "\n"
+ "connectTimeout " + _options.connectTimeout;
const _content = _url + "\n" + _options_str;
this.update_broker_text(_content)
}
UI.prototype.unset_broker_info_text = function () {
this.update_broker_text("");
}
UI.prototype.update_broker_text = function (txt_) {
$("#" + "text_broker").val(txt_);
}
UI.prototype.set_broker_badge_connected = function () {
const _id = "badge_broker";
$("#" + _id).text("Connected");
$("#" + _id).attr("class", "badge badge-light bg-success");
}
UI.prototype.set_broker_badge_disconnected = function () {
const _id = "badge_broker";
$("#" + _id).text("Disconnected");
$("#" + _id).attr("class", "badge badge-light bg-danger");
}
UI.prototype.set_broker_badge_reconnecting = function () {
const _id = "badge_broker";
$("#" + _id).text("Reconnecting");
$("#" + _id).attr("class", "badge badge-light bg-warning");
}
UI.prototype.set_broker_badge_idle = function () {
const _id = "badge_broker";
$("#" + _id).text("Status");
$("#" + _id).attr("class", "badge badge-light bg-secondary");
}
UI.prototype.update_item_text = function (item_, txt_) {
const _id = "text" + item_.index;
$("#" + _id).val(txt_);
}
// single elements
UI.prototype.add_tab = function (parent_id_, sufix_) {
const _id = "tab" + sufix_;
const _tab = '<a id="' + _id + '" class="list-group-item" role="tab"></a>';
$("#" + parent_id_).append(_tab);
console.debug("added '" + _id + "' to '" + parent_id_ + "'");
return _id;
}
UI.prototype.add_row = function (parent_id_, sufix_) {
const _id = "row" + sufix_;
const _row = '<div id="' + _id + '" class="row justify-content-md-center"></div>';
$("#" + parent_id_).append(_row);
console.debug("added '" + _id + "' to '" + parent_id_ + "'");
return _id;
}
UI.prototype.add_col = function (parent_id_, sufix_, col_num_) {
if (undefined === col_num_) {
console.error("add_col: col_num undefined");
return;
}
const _id = "col" + sufix_ + "_" + col_num_;
const _col = '<div id="' + _id + '" class="col-md-auto"></div>';
$("#" + parent_id_).append(_col);
console.debug("added '" + _id + "' to '" + parent_id_ + "'");
return _id;
}
UI.prototype.add_button = function (parent_id_, sufix_, name_) {
const _id = "button" + sufix_;
const _button = '<button id="' + _id + '" class="btn btn-primary" style="width: 300px">'
+ name_
+ '</button>';
$("#" + parent_id_).append(_button);
console.debug("added '" + _id + "' to '" + parent_id_ + "'");
return _id;
}
UI.prototype.add_badge = function (parent_id_, sufix_) {
const _id = "badge" + sufix_;
const _badge = ' <span id="' + _id + '" class="badge badge-light bg-secondary"> </span> ';
$("#" + parent_id_).append(_badge);
console.debug("added '" + _id + "' to '" + parent_id_ + "'");
return _id;
}
UI.prototype.add_textarea = function (parent_id_, sufix_, topic_) {
const _id = "text" + sufix_;
const _text = '<textarea id="' + _id + '" class="form-control text-dark bg-light" type="text" style="width: 300px" placeholder="'
+ topic_
+ '" readonly></textarea>'
$("#" + parent_id_).append(_text);
console.debug("added '" + _id + "' to '" + parent_id_ + "'");
return _id;
}
export default UI;