Skip to content

Commit 1d0d02c

Browse files
Keith BannisterKeith Bannister
Keith Bannister
authored and
Keith Bannister
committedSep 4, 2022
Add UI to select user position to plot
1 parent f80009a commit 1d0d02c

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed
 

‎dashboard.js

+37-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let statusText = document.getElementById("status");
2020
let locationText = document.getElementById("location")
2121
let npositions = 0;
2222
var connectOptions = {};
23-
let positionUserName = "keith"; // If you get a position from mqtt topic position/$user then update the UI with that position
23+
let positionUserName = null; // If you get a position from mqtt topic position/$user then update the UI with that position
2424
let userLastPositions = {}; // dictionary of most recent posObj keyed by user name
2525
// make this null if you want to use your local position measurementes
2626
// TODO: Make this configurable in the UI
@@ -61,6 +61,8 @@ function mqtt_login() {
6161
// Make a unique-ish, random client ID
6262
const randint = Math.floor(Math.random()*1024*1024);
6363
// Client ID must be unique otherwise it disconnects the other client.
64+
// Ah wait, but "It is only used to identify the client to the broker when the connection is established in order to determine if stored messages and persistent subscriptions should be honoured.
65+
// Hmm, not sure if we want unique ones or not.
6466
const clientId = `${username}-${randint}`;
6567

6668
// Create a client instance
@@ -112,12 +114,31 @@ function updateTelemetry(data) {
112114
function updateUserPosition(sendingUser, posobj) {
113115
// called a position received from a sendingUser
114116
// may or may not be a new user
115-
116-
userLastPositions[sendingUser] = posobj;
117-
console.log("Got position from ", sendingUser, posobj, Object.keys(userLastPositions));
118117

119-
// TODO: Update UI so our user can choose which person to display positionUserNames - could even use distances
118+
const locationUserList = document.getElementById('locationUser');
119+
var $el = $('#locationUser');
120+
121+
// Add new user to list
122+
if (!(sendingUser in userLastPositions)) {
123+
$el.append($("<option></option>")
124+
.attr("value", sendingUser).text(sendingUser));
125+
}
126+
127+
// update list of sending locations
128+
userLastPositions[sendingUser] = posobj;
129+
}
120130

131+
function locationUserChanged() {
132+
var newUserName = document.getElementById("locationUser").value;
133+
if (newUserName == '') {
134+
newUserName = null;
135+
}
136+
positionUserName = newUserName; // Update position user global variable
137+
const userLocation = userLastPositions[newUserName];
138+
console.log('Location user changed to', newUserName, userLocation);
139+
clearData(locationChart); // clear chart
140+
breadCrumbLine.setLatLngs([]); // clear breadcrumbs on map
141+
updatePosition(userLocation); // Add new latest value
121142
}
122143

123144
function onMessageArrived(message) {
@@ -146,8 +167,6 @@ function onMessageArrived(message) {
146167
}
147168
}
148169

149-
150-
151170
mqtt_message_count += 1;
152171

153172
}
@@ -365,9 +384,11 @@ function locationSuccess(position) {
365384

366385
const posobj = positionToObject(position);
367386

387+
// Always save the current location to the null user
388+
userLastPositions[null] = posobj;
389+
368390
if (positionUserName === null) { // update UI if we're not using positions from MQTT
369391
updatePosition(posobj); // update UI
370-
371392
}
372393

373394
// send data to mqtt
@@ -414,6 +435,14 @@ function removeData(chart) {
414435
chart.update();
415436
}
416437

438+
function clearData(chart) {
439+
chart.data.labels.length = 0;
440+
chart.data.datasets.forEach((dataset) => {
441+
dataset.data.length = 0;
442+
});
443+
chart.update();
444+
}
445+
417446
// On Apline ready
418447
document.addEventListener('alpine:init', () => {
419448
dashboard_init();

‎index.html

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ <h3 class="text-xs mb-2 uppercase"> Speed</h3>
190190

191191
<div x-show="tab === 'settings'" class="h-full">
192192
<h3>Settings</h3>
193+
<h4>Plot location of this person</h4>
194+
<select id="locationUser" onchange="locationUserChanged()">
195+
<option value="">[My Location]</option>
196+
</select>
193197
<p>
194198
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed cursus
195199
facilisis tristique. Lorem ipsum dolor sit amet, consectetur adipiscing

0 commit comments

Comments
 (0)
Please sign in to comment.