-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracker.html
147 lines (127 loc) · 5.25 KB
/
tracker.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
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=0.5">
<html>
<head>
<style type="text/css">
.prname {
color: black;
font-weight: normal;
}
.prvalue {
color: green;
font-weight: bold;
}
.prnewvalue {
color: red;
font-weight: bold;
}
</style>
</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>
<hr>
<div id="printarea">
</div>
<hr>
</div>
<center>
<div id="map" style="width:90%;height:70vh;border:0;"></div>
<textarea readonly name="mylog" style="width:90%;height:15vh;" value=""></textarea>
<div id="dbg" align="right" style="width:90%">
</div>
</form>
<script>
var count = 1;
var location_topic = "/7355608";
var myid = btoa(Math.random()).substring(0, 12);
var map = undefined;
var marker = undefined;
var oldobj = 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 + "/tx/#");
write('Subscribed to ' + form1.stopic.value + "/tx/#");
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 + "/tx/telemetry") {
write(count++ + ' New message:\t' + message.payloadString);
try {
var rcvobj = JSON.parse(message.payloadString);
if ( (typeof rcvobj.lat == 'number') && (typeof rcvobj.lng == 'number') && ( (rcvobj.lat!=0) && (rcvobj.lng!=0) ) ) {
var center = new google.maps.LatLng(rcvobj.lat, rcvobj.lng);
marker.setMap(null);
marker = new google.maps.Marker({ position: center, map: map });
map.setCenter(center);
map.setZoom(17);
}
var propValue;
var dd = new Date();
document.getElementById("printarea").innerHTML = "<u><b>" + dd.toLocaleTimeString() + "</u></b>";
for (var propName in rcvobj) {
propValue = rcvobj[propName];
document.getElementById("printarea").innerHTML +="<span class=\"prname\"> | "+ propName + ":</span>";
if ((typeof oldobj != "undefined") && (oldobj[propName] != propValue))
{
document.getElementById("printarea").innerHTML +="<span class=\"prnewvalue\">" + propValue + "</span>";
}
else
{
document.getElementById("printarea").innerHTML +="<span class=\"prvalue\">" + propValue + "</span>";
}
}
oldobj = rcvobj;
} catch (e) {
write("Error: " + e.message);
}
}
}
function write(text) {
form1.mylog.value += text + '\n';
if (form1.mylog.value.split('\n').length > 10)
{
form1.mylog.value = form1.mylog.value.split('\n').slice(1).join('\n');
}
form1.mylog.scrollTop = form1.mylog.scrollHeight;
document.getElementById("dbg").innerHTML = form1.mylog.value.split('\n').length + ' - ' + form1.mylog.value.length;
}
</script>
</body>
</html>