Skip to content

Commit

Permalink
add button to refresh node position
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcottle committed Dec 8, 2024
1 parent 2c8ece2 commit e14075b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/components/pages/NodePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@

<!-- position -->
<div>
<div class="flex bg-gray-200 p-2 font-semibold">Position</div>
<div class="flex bg-gray-200 p-2 font-semibold items-center">
<div>Position</div>
<div class="ml-auto">
<RefreshButton @click="requestPosition" :is-refreshing="isRequestingPosition"/>
</div>
</div>
<ul role="list" class="flex-1 divide-y divide-gray-200">

<!-- position -->
Expand All @@ -87,7 +92,7 @@
<li class="flex p-3">
<div class="text-sm font-medium text-gray-900">Altitude</div>
<div class="ml-auto text-sm text-gray-700">
<span v-if="node.position && node.position.altitude != null">{{ node.position.altitude }}</span>
<span v-if="node.position && node.position.altitude">{{ node.position.altitude }}</span>
<span v-else>???</span>
</div>

Expand Down Expand Up @@ -189,6 +194,7 @@ export default {
data() {
return {
isRequestingDeviceMetrics: false,
isRequestingPosition: false,
};
},
mounted() {
Expand Down Expand Up @@ -252,6 +258,32 @@ export default {
this.isRequestingDeviceMetrics = false;
},
async requestPosition() {
// do nothing if already requesting position
if(this.isRequestingPosition){
return;
}
// show loading
this.isRequestingPosition = true;
try {
// fetch position from node
const position = await NodeAPI.requestPosition(this.node.num);
// update this nodes position
this.node.position = position;
} catch(e) {
DialogUtils.showErrorAlert(e);
}
// no longer requesting position
this.isRequestingPosition = false;
},
},
computed: {
node() {
Expand Down
18 changes: 18 additions & 0 deletions src/js/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,24 @@ class Connection {

});

// listen for positions
connection.events.onPositionPacket.subscribe(async (positionPacket) => {

await databaseToBeReady;
console.log("onPositionPacket", positionPacket);

// find node this position is from, otherwise do nothing
const from = positionPacket.from;
const node = GlobalState.nodesById[from];
if(!node){
return;
}

// update position for node
node.position = positionPacket.data;

});

}

static async onPacketAck(requestId, ackedByNodeId, hopsAway) {
Expand Down
22 changes: 22 additions & 0 deletions src/js/NodeAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,28 @@ class NodeAPI {

}

/**
* Sends our Position to, and requests the Position from the provided nodeId
* @param nodeId the node id to exchange Position with
* @returns {Promise<*>}
*/
static async requestPosition(nodeId) {

// create empty position message
// todo send our actual position
const position = Protobuf.Mesh.Position.fromJson({});

// send packet and wait for response
const portNum = Protobuf.Portnums.PortNum.POSITION_APP;
const byteData = position.toBinary();
const channel = NodeUtils.getNodeChannel(nodeId);
const responseMeshPacket = await this.sendPacketAndWaitForResponse(nodeId, portNum, byteData, channel, true);

// return position from response
return Protobuf.Mesh.Position.fromBinary(responseMeshPacket.payloadVariant.value.payload);

}

/**
* Sends an AdminMessage to the provided node id, and waits for a response, or timeouts out after the provided delay
* @param nodeId the node id to send the admin message to
Expand Down

0 comments on commit e14075b

Please sign in to comment.