start implementing client based on meshtxt

This commit is contained in:
liamcottle
2025-02-13 01:02:27 +13:00
parent fd680c2aeb
commit 6d4e7e4431
24 changed files with 3279 additions and 3 deletions

64
src/js/Connection.js Normal file
View File

@@ -0,0 +1,64 @@
import GlobalState from "./GlobalState.js";
import {BleConnection, Constants, SerialConnection} from "@liamcottle/meshcore.js";
class Connection {
static async connectViaBluetooth() {
await this.connect(await BleConnection.open());
}
static async connectViaSerial() {
await this.connect(await SerialConnection.open());
}
static async connect(connection) {
GlobalState.connection = connection;
GlobalState.connection.on("connected", () => this.onConnected());
GlobalState.connection.on("disconnected", () => this.onDisconnected());
}
static async disconnect() {
// disconnect
GlobalState.connection?.close();
// update ui
GlobalState.connection = null;
}
static async onConnected() {
// clear previous connection state
GlobalState.contacts = [];
GlobalState.connection.on(Constants.PushCodes.Advert, async () => {
console.log("Advert");
await this.loadContacts();
});
GlobalState.connection.on(Constants.PushCodes.PathUpdated, async (event) => {
console.log("PathUpdated", event);
await this.loadContacts();
});
await this.loadSelfInfo();
await this.loadContacts();
}
static async onDisconnected() {
await this.disconnect();
}
static async loadSelfInfo() {
GlobalState.selfInfo = await GlobalState.connection.getSelfInfo();
}
static async loadContacts() {
GlobalState.contacts = await GlobalState.connection.getContacts();
}
}
export default Connection;