-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.html
249 lines (243 loc) · 9.14 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MQTT device telemetry example</title>
<script src="https://unpkg.com/[email protected]/dist/mqtt.min.js"></script>
<script>
var devicesList = {};
var telemetry = {};
var mapready = false;
var login = null;
var client;
function debounce(func, wait) {
var timeout;
return function() {
var that = this, args = arguments;
var callAfter = function() {
timeout = null;
func.apply(that, args);
};
clearTimeout(timeout);
timeout = setTimeout(callAfter, wait);
};
}
// draw table
function obj2table(obj) {
var html = '<table><tr><th>Key</th><th>Value</th></tr>';
var objkeys = Object.keys(obj).sort()
for (var key of objkeys) {
var item = obj[key];
var value = (typeof(item) === 'object') ? obj2table(item) : item.toString();
html += '<tr><td>' + key + '</td><td>' + value + '</tr>';
}
html += '</table>';
return html;
}
// debounced telemetry redraw function
var telemetrydebounce = debounce(function () {
// fill telemetry table
document.getElementById('telemetryblock').innerHTML = obj2table(telemetry);
// show position on map
if (mapready && telemetry['position.latitude'] && telemetry['position.longitude']) {
document.getElementById('map').contentWindow.postMessage('MapView|cmd:{"clear": ["markers"], "appendpoints": [[' + telemetry['position.latitude'] + ',' + telemetry['position.longitude'] + ']],"addmarkers": [[' + telemetry['position.latitude'] + ',' + telemetry['position.longitude'] + ']]}', '*')
}
}, 200);
// mqtt processing function
function main () {
if (client && client.end) { client.end()}
client = mqtt.connect('wss://' + login.region['mqtt-ws'], {
username: login.token,
clientId: 'MQTTdevicetelemetryexample' + Math.random().toString(16).substr(2, 8),
protocolVersion: 5,
clean: true,
wsOptions: {
objectMode: false,
perMessageDeflate: true
},
resubscribe: false,
keepalive: 240
});
client.on('error', function (err) {
console.log(err);
client.end();
});
// on connect - clear lists and subscribe
client.on('connect', function () {
devicesList = {};
telemetry = {};
console.log('client connected');
// subscribe to device list
client.subscribe('flespi/state/gw/devices/+', { qos: 0 });
// message processing
client.on('message', function (topic, message, packet) {
console.log('message:= ' + message.toString() + '\ntopic:= ' + topic);
topic = topic.split('/');
if (topic[3] === 'devices' && topic.length === 7) {
// fill telemetry object
telemetry[topic[topic.length - 1]] = JSON.parse(message);
telemetrydebounce();
} else if (topic[3] === 'devices' && topic.length === 5) {
// fill device list object
id = parseInt(topic[topic.length - 1]);
if (message) {
message = JSON.parse(message);
devicesList[message.id] = {value: message.id, label: (message.name || '') + ' #' + message.id};
} else {
delete devicesList[id];
}
var list = '<option value="0">Click me to select device</option>';
for (var id in devicesList) {
list += '<option value="' + devicesList[id].value + '">' + devicesList[id].label + '</option>';
}
document.getElementById('deviceselector').innerHTML = list;
} else {
console.log ('not proccessed message');
}
});
})
}
var selectedid = 0;
function selectdevice () {
// clear map
document.getElementById('map').contentWindow.postMessage('MapView|cmd:{"clear":"all"}', '*');
// unsubscribe old telemetry topic
client.unsubscribe('flespi/state/gw/devices/' + selectedid + '/telemetry/+');
selectedid = document.getElementById('deviceselector').value;
// subscribe new telemetry topic
client.subscribe('flespi/state/gw/devices/' + selectedid + '/telemetry/+', { qos: 0 });
//clear telemetry and draw
telemetry = {};
telemetrydebounce();
}
// listen browser postmessage
window.addEventListener('message', function (event) {
// listen login event
if (typeof event.data === 'string' && ~event.data.indexOf('FlespiLogin')) {
console.log(JSON.parse(event.data.replace('FlespiLogin|token:', '')))
document.getElementById("login").style="display:none;"
document.getElementById("deviceselector").style="display:block;"
login = JSON.parse(event.data.replace('FlespiLogin|token:', ''))
main();
}
// listen map ready state
if (typeof event.data === 'string' && event.data.indexOf('MapView|state') === 0) {
var data = JSON.parse(event.data.replace('MapView|state:', ''))
if (data.ready) {
// set mapready flag and send map configuration
mapready = true
document.getElementById('map').contentWindow.postMessage('MapView|cmd:{"zoomcontrol":true, "fullscreencontrol": true}', '*')
}
}
})
</script>
<style>
body, html{
margin:0;
padding:0;
border:0;
overflow: hidden;
font-family: Geneva, Arial, Helvetica, sans-serif;
height: 100%;
}
.header {
position: absolute;
top:0;
left:0;
right:0;
height: 50px;
background:#333;
color: fff;
}
#login {
height:50px;
border:0;
width:100%;
}
#deviceselector {
display: none;
width:100%;
height: 100%;
margin:0;
border: 0;
background: #333;
color: #fff;
font-size: 20px
}
#deviceselector option{
height:30px;
}
#telemetryblock {
margin:0;
width: 30%;
position: absolute;
top: 50px;
left: 0;
bottom: 0;
border:0;
box-sizing: border-box;
overflow: auto;
background: #555;
color: #fff;
}
#telemetryblock table {
border-collapse: collapse;
width: 100%;
}
#telemetryblock th, #telemetryblock td {
text-align: left;
padding: 8px;
}
#telemetryblock tr:nth-child(even) {
background-color: rgba(0,0,0,.1);
}
#mapblock {
margin:0;
width: 70%;
position: absolute;
top: 50px;
right: 0;
bottom: 0;
border:0;
box-sizing: border-box;
}
#map {
width: 100%;
height: 100%;
border: 0;
display: block;
}
::-webkit-scrollbar {
width: 10px;
height: 10px;
}
::-webkit-scrollbar-track {
background-color: rgba(255,255,255,.3);
}
::-webkit-scrollbar-track:hover {
background-color: rgba(255,255,255,.6);
}
::-webkit-scrollbar-thumb {
background-color: rgba(0,0,0,.3);
border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
background-color: rgba(0,0,0, .5);
}
::-webkit-scrollbar-corner {
background-color: rgba(255,255,255,.6);
}
</style>
</head>
<body>
<div class="header">
<select id="deviceselector" onchange="selectdevice()" value="0"></select>
<iframe id="login" src="https://flespi.io/frame/index.html#333;ccc"></iframe>
</div>
<div id="telemetryblock"></div>
<div id="mapblock">
<iframe id="map" src="https://flespi.io/mapview/index.html" frameborder="0" allowfullscreen="allowfullscreen"></iframe>
</div>
</body>
</html>