mirror of
https://github.com/aljazceru/meshcore-web.git
synced 2025-12-17 16:24:18 +01:00
start implementing client based on meshtxt
This commit is contained in:
64
src/js/Connection.js
Normal file
64
src/js/Connection.js
Normal 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;
|
||||
Reference in New Issue
Block a user