From bfc6bc362c8501e55cadf660aba9e8ed1bb052d4 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Wed, 20 Nov 2024 22:00:51 +1300 Subject: [PATCH] send current timestamp to connected node when config has completed --- src/js/Connection.js | 20 ++++++++++++++++++++ src/js/NodeAPI.js | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/js/Connection.js b/src/js/Connection.js index c1064e4..7410fff 100644 --- a/src/js/Connection.js +++ b/src/js/Connection.js @@ -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 { @@ -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") { @@ -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 diff --git a/src/js/NodeAPI.js b/src/js/NodeAPI.js index a0ef500..171d18c 100644 --- a/src/js/NodeAPI.js +++ b/src/js/NodeAPI.js @@ -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