Skip to content

Commit

Permalink
send current timestamp to connected node when config has completed
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcottle committed Nov 20, 2024
1 parent 47030d9 commit bfc6bc3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/js/Connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import GlobalState from "./GlobalState.js";
import {BleConnection, Constants, HttpConnection, Protobuf, SerialConnection, Types,} from "@meshtastic/js";
import Database from "./Database.js";
import NodeAPI from "./NodeAPI.js";

class Connection {

Expand Down Expand Up @@ -191,6 +192,8 @@ class Connection {

await databaseToBeReady;

console.log("onFromRadio", data);

// handle packets
// we are doing this to get error info for a request id as it's not provided in the onRoutingPacket event
if(data.payloadVariant.case.toString() === "packet") {
Expand All @@ -216,6 +219,23 @@ class Connection {
}
}

// handle config complete
if(data.payloadVariant.case.toString() === "configCompleteId"){

console.log("config complete");

// send current timestamp to meshtastic device
// this allows it to send us an semi accurate rx timestamp for packets when we connect later on
// if we don't set the time, the node may not know a time at all, in which case rxTime will be zero
// or, the node might have a timestamp, but its drifted out of sync
// so we will just send the node the current time each time we connect to it
try {
const timestampInSeconds = Math.floor(Date.now() / 1000);
await NodeAPI.setTime(timestampInSeconds);
} catch(e) {}

}

});

// listen for node info
Expand Down
25 changes: 25 additions & 0 deletions src/js/NodeAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ class NodeAPI {

}

/**
* Sets the provided timestamp as the current time on the meshtastic device.
* @param timestamp the timestamp in seconds to set as the current time
* @returns {Promise<*>}
*/
static async setTime(timestamp) {

// create admin message packet
var adminMessage = Protobuf.Admin.AdminMessage.fromJson({
setTimeOnly: timestamp,
});

// create packet data
const byteData = adminMessage.toBinary().buffer;
const portNum = Protobuf.Portnums.PortNum.ADMIN_APP;
const destination = GlobalState.myNodeId;
const channel = 0;
const wantAck = true;
const wantResponse = false;

// send packet
return await GlobalState.connection.sendPacket(byteData, portNum, destination, channel, wantAck, wantResponse);

}

/**
* Removes the node from global state, and also tells the meshtastic device to remove the node.
* @param nodeId the node id to remove
Expand Down

0 comments on commit bfc6bc3

Please sign in to comment.