Skip to content

Commit e14075b

Browse files
committed
add button to refresh node position
1 parent 2c8ece2 commit e14075b

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

src/components/pages/NodePage.vue

+34-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@
7171

7272
<!-- position -->
7373
<div>
74-
<div class="flex bg-gray-200 p-2 font-semibold">Position</div>
74+
<div class="flex bg-gray-200 p-2 font-semibold items-center">
75+
<div>Position</div>
76+
<div class="ml-auto">
77+
<RefreshButton @click="requestPosition" :is-refreshing="isRequestingPosition"/>
78+
</div>
79+
</div>
7580
<ul role="list" class="flex-1 divide-y divide-gray-200">
7681

7782
<!-- position -->
@@ -87,7 +92,7 @@
8792
<li class="flex p-3">
8893
<div class="text-sm font-medium text-gray-900">Altitude</div>
8994
<div class="ml-auto text-sm text-gray-700">
90-
<span v-if="node.position && node.position.altitude != null">{{ node.position.altitude }}</span>
95+
<span v-if="node.position && node.position.altitude">{{ node.position.altitude }}</span>
9196
<span v-else>???</span>
9297
</div>
9398

@@ -189,6 +194,7 @@ export default {
189194
data() {
190195
return {
191196
isRequestingDeviceMetrics: false,
197+
isRequestingPosition: false,
192198
};
193199
},
194200
mounted() {
@@ -252,6 +258,32 @@ export default {
252258
this.isRequestingDeviceMetrics = false;
253259
254260
},
261+
async requestPosition() {
262+
263+
// do nothing if already requesting position
264+
if(this.isRequestingPosition){
265+
return;
266+
}
267+
268+
// show loading
269+
this.isRequestingPosition = true;
270+
271+
try {
272+
273+
// fetch position from node
274+
const position = await NodeAPI.requestPosition(this.node.num);
275+
276+
// update this nodes position
277+
this.node.position = position;
278+
279+
} catch(e) {
280+
DialogUtils.showErrorAlert(e);
281+
}
282+
283+
// no longer requesting position
284+
this.isRequestingPosition = false;
285+
286+
},
255287
},
256288
computed: {
257289
node() {

src/js/Connection.js

+18
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,24 @@ class Connection {
404404

405405
});
406406

407+
// listen for positions
408+
connection.events.onPositionPacket.subscribe(async (positionPacket) => {
409+
410+
await databaseToBeReady;
411+
console.log("onPositionPacket", positionPacket);
412+
413+
// find node this position is from, otherwise do nothing
414+
const from = positionPacket.from;
415+
const node = GlobalState.nodesById[from];
416+
if(!node){
417+
return;
418+
}
419+
420+
// update position for node
421+
node.position = positionPacket.data;
422+
423+
});
424+
407425
}
408426

409427
static async onPacketAck(requestId, ackedByNodeId, hopsAway) {

src/js/NodeAPI.js

+22
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,28 @@ class NodeAPI {
400400

401401
}
402402

403+
/**
404+
* Sends our Position to, and requests the Position from the provided nodeId
405+
* @param nodeId the node id to exchange Position with
406+
* @returns {Promise<*>}
407+
*/
408+
static async requestPosition(nodeId) {
409+
410+
// create empty position message
411+
// todo send our actual position
412+
const position = Protobuf.Mesh.Position.fromJson({});
413+
414+
// send packet and wait for response
415+
const portNum = Protobuf.Portnums.PortNum.POSITION_APP;
416+
const byteData = position.toBinary();
417+
const channel = NodeUtils.getNodeChannel(nodeId);
418+
const responseMeshPacket = await this.sendPacketAndWaitForResponse(nodeId, portNum, byteData, channel, true);
419+
420+
// return position from response
421+
return Protobuf.Mesh.Position.fromBinary(responseMeshPacket.payloadVariant.value.payload);
422+
423+
}
424+
403425
/**
404426
* Sends an AdminMessage to the provided node id, and waits for a response, or timeouts out after the provided delay
405427
* @param nodeId the node id to send the admin message to

0 commit comments

Comments
 (0)