-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindexv2.html
100 lines (84 loc) · 3.39 KB
/
indexv2.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
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<html>
<head>
</head>
<body>
<script src="mqttws31.js" type="text/javascript"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDKRSkP_59vD7V3aWp_DAK7EDJ6ittSwM8&callback=initMap"></script>
<form name="form1" action="javascript:void(0);">
<div style="margin-left: 5%;margin-right: 5%;">
<h4>My MQTT Map</h4>
<button onclick="connect()">Connect!</button>
<button onclick="client.disconnect()">Disconnect</button>
<br><input type="text" name="stopic" style="width: 100%">
<br>
</div>
<center>
<div id="map" style="width:90%;height:70vh;border:0;"></div>
<textarea readonly name="mylog" style="width:90%;height:15vh;" value=""></textarea>
</form>
<script>
var root_topic = '/guto';
var count = 1;
var location_topic = root_topic + '/rx/loc';
var myid = btoa(Math.random()).substring(0, 12);
var map = undefined;
var marker = undefined;
client = new Paho.MQTT.Client("test.mosquitto.org", 8081, myid);
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
form1.stopic.value = location_topic;
form1.mylog.value = "";
document.onload = connect();
function initMap() {
var zero = { lat: -22.817076, lng: -47.069760 };
map = new google.maps.Map(document.getElementById('map'), { zoom: 16, center: zero });
marker = new google.maps.Marker({ position: zero, map: map });
}
function connect() {
client.connect({ onSuccess: onConnect, useSSL: true });
write('Connecting... ID: ' + myid);
}
function subscribe() {
client.subscribe(form1.stopic.value);
write('Subscribed to ' + form1.stopic.value);
form1.stopic.readOnly = true;
}
function onConnect() {
write('Connected to test.mosquitto.org!');
subscribe();
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
write('Connection Lost ' + responseObject.errorMessage);
}
else {
write('Disconnected');
}
form1.stopic.readOnly = false;
}
function onMessageArrived(message) {
var dname = message.destinationName;
if (dname == form1.stopic.value) {
write(count++ + ' New message:\t' + message.payloadString);
try {
var loc = JSON.parse(message.payloadString);
if ((typeof loc.lat == 'number') && (typeof loc.lng == 'number')) {
var center = new google.maps.LatLng(loc.lat, loc.lng);
marker.setMap(null);
marker = new google.maps.Marker({ position: center, map: map });
map.setCenter(center);
map.setZoom(17);
}
} catch (e) {
}
}
}
function write(text) {
form1.mylog.value += text + '\n';
form1.mylog.scrollTop = form1.mylog.scrollHeight
}
</script>
</body>
</html>