-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqttpoke.js
215 lines (177 loc) · 5.19 KB
/
mqttpoke.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
/* mqttpoke (c) 2024 Ryan Griggs. All rights reserved.
Released under MIT license.
https://github.com/ryangriggs/mqttpoke
*/
$(document).ready(function() {
$("#uri").val(Cookies.get('uri'));
$("#topic").val(Cookies.get('topic'));
$("#username").val(Cookies.get('username'));
$("#password").val(Cookies.get('password'));
// Highlight all inputs on click
$("input[type=text]").on('click', function() {
this.select();
});
// Hotkeys on value boxes:
$(".item input.value").on('keydown', function(event) {
// Enter key: publish
if (event.keyCode == 13)
{
$(this).closest('.item').find('.btnItemPublish').click();
}
});
$('#btnConnect').on('click', function() {
connect();
});
$(".btnItemPublish").on('click', function() {
const item = $(this).closest('.item');
const topic = item.find('.topic').text();
const type = item.find('.type .value').val();
let value = null;
if (type == 'bool') { value = item.find('.message .bool .value').is(':checked'); }
else { value = item.find(`.message .${type} .value`).val(); }
// Convert value to appropriate datatype.
let typedValue = null; let temp; let buf;
switch (type)
{
case 'bool':
typedValue = new Uint8Array([ value ? 1 : 0 ]);
break;
case 'int16s':
typedValue = new ArrayBuffer(2);
new DataView(typedValue).setInt16(0, value);
break;
case 'int32s':
typedValue = new ArrayBuffer(4);
new DataView(typedValue).setInt32(0, value);
break;
case 'float32':
typedValue = new ArrayBuffer(4);
new DataView(typedValue).setFloat32(0, value);
break;
case 'text':
typedValue = new TextEncoder().encode(value);
break;
}
client.publish(topic, new Uint8Array(typedValue), { retain: true });
log(`Published ${topic} = ${value}`);
});
$(".btnItemRemove").on('click', function() {
let el = $(this).closest('.item');
let topic = el.find('.topic').text();
client.publish(topic, new Uint8Array(), { retain: true }); // Remove from server by publishing 0-length data.
el.remove();
});
$("#btnSubscribe").on('click', function() {
const topic = prompt("Enter topic name");
if (!topic) { return; }
addItem(topic, "");
client.subscribe(topic);
})
});
function formatData(select)
{
let el = $(select).closest('.item');
let type = $(select).val();
let topic = el.find('.topic').text();
let message = el.prop('message');
el.find('.message .display').hide();
el.find(`.message .${type}`).show();
Cookies.set(`Type: ${topic}`, type, { expires: 365 });
setValue(el, message);
}
function messageHandler(topic, message)
{
console.log("Message:", topic, message);
// Find topic in list
let result = false;
$(".data .item").not('.template').each(function() {
let el = $(this);
if (el.find('.topic').text() == topic) { result = el; return;}
});
if (!result)
{
result = addItem(topic, message);
}
setValue(result, message);
formatData(result.find('.type .value')[0]);
}
function addItem(topic, message)
{
result = $(".data .item.template").clone(true);
result.removeClass('template');
$('.data .item').last().after(result);
result.find('.topic').text(topic);
result.find('.message .text .value').text(message);
let type = Cookies.get(`Type: ${topic}`);
if (type) { result.find('.type .value').val(type); } // Set saved type
return result;
}
function setValue(element, message)
{
const type = element.find('.type .value').val();
let v; let temp;
switch (type)
{
case 'text':
v = message.toString();
break;
case 'bool':
v = message[0] == 1;
element.find('.message .bool .value').prop('checked', v);
break;
case 'int16s':
v = message.readInt16BE(0);
break;
case 'int32s':
v = message.readInt32BE(0);
break;
case 'float32':
v = message.readFloatBE(0);
break;
}
element.prop('message', message);
element.find(`.message .${type} .value`).val(v); // Sets the text value for all types (invisible for boolean type)
// let count = parseInt(element.find('.counter').text());
// count++;
// element.find('.counter').text(count);
element.find('.updated').text(new moment().format('HH:mm:ss.S YYYY-MM-DD'));
}
let client;
function connect()
{
const uri = $("#uri").val();
const username = $("#username").val();
const password = $("#password").val();
const topic = $("#topic").val();
// Disconnect any existing connection
if (client)
{
log("Closing existing MQTT connection...");
client.end();
}
Cookies.set('uri', uri, { expires: 365 });
Cookies.set('username', username, { expires: 365 });
Cookies.set('password', password, { expires: 365 });
Cookies.set('topic', topic, { expires: 365 });
log("Connecting to MQTT server...");
client = mqtt.connect(uri, { username, password });
client.on('connect', () => {
log("Connected to server.");
client.subscribe(topic);
});
client.on('error', (err) => {
log("Connection error:" + err, "error");
});
client.on('message', (topic, message) => {
messageHandler(topic, message)
});
}
function log(message, severity = "info")
{
const l = $("#log .entry.template").clone(true);
l.removeClass('template');
$("#log .entry").first().before(l);
l.find('.message').text(message);
l.addClass(severity);
l.find('.date').text(moment().format('M/D/YYYY h:mm:ss a'));
}