Skip to content

Commit

Permalink
streamline connection code & bump deps
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaw committed Aug 13, 2023
1 parent 0f79c46 commit 427e98b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshtastic/meshtasticjs",
"version": "2.2.0-2",
"version": "2.2.1-0",
"description": "Browser library for interfacing with meshtastic devices",
"license": "GPL-3.0-only",
"scripts": {
Expand Down Expand Up @@ -37,15 +37,15 @@
"url": "https://github.com/meshtastic/js.git"
},
"dependencies": {
"@buf/meshtastic_protobufs.bufbuild_es": "1.3.0-20230807174505-db3cb0c7cf53.1",
"@buf/meshtastic_protobufs.bufbuild_es": "1.3.0-20230812103233-732b987c2c85.1",
"@bufbuild/protobuf": "^1.3.0",
"crc": "^4.3.2",
"sub-events": "^1.9.0",
"tslib": "^2.6.1",
"tslog": "^4.9.1"
},
"devDependencies": {
"@types/node": "^20.4.9",
"@types/node": "^20.4.10",
"@types/w3c-web-serial": "^1.0.3",
"@types/web-bluetooth": "^0.0.17",
"rome": "^12.1.3",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions src/adapters/iBleConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,42 @@ import { typedArrayToBuffer } from "../utils/general.js";
/** Allows to connect to a Meshtastic device via bluetooth */
export class IBLEConnection extends IMeshDevice {
/** Defines the connection type as ble */
connType: Types.ConnectionTypeName;
public connType: Types.ConnectionTypeName;

public portId: string;

/** Currently connected BLE device */
device: BluetoothDevice | undefined;
public device: BluetoothDevice | undefined;

GATTServer: BluetoothRemoteGATTServer | undefined;
private GATTServer: BluetoothRemoteGATTServer | undefined;

/** Short Description */
service: BluetoothRemoteGATTService | undefined;
private service: BluetoothRemoteGATTService | undefined;

/** Short Description */
toRadioCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;
private toRadioCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;

/** Short Description */
fromRadioCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;
private fromRadioCharacteristic:
| BluetoothRemoteGATTCharacteristic
| undefined;

/** Short Description */
fromNumCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;

/** States if the device was force disconnected by a user */
userInitiatedDisconnect: boolean;
private fromNumCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;

constructor(configId?: number) {
super(configId);

this.log = this.log.getSubLogger({ name: "iHttpConnection" });

this.connType = "ble";
this.portId = "";
this.device = undefined;
this.service = undefined;
this.GATTServer = undefined;
this.toRadioCharacteristic = undefined;
this.fromRadioCharacteristic = undefined;
this.fromNumCharacteristic = undefined;
this.userInitiatedDisconnect = false;
// this.pendingRead = false;

this.log.debug(
Expand Down Expand Up @@ -96,6 +97,8 @@ export class IBLEConnection extends IMeshDevice {
/** Set device if specified, else request. */
this.device = device ?? (await this.getDevice(deviceFilter));

this.portId = this.device.id;

/** Setup event listners */
this.device.addEventListener("gattserverdisconnected", () => {
this.log.info(
Expand Down Expand Up @@ -184,7 +187,6 @@ export class IBLEConnection extends IMeshDevice {

/** Disconnects from the Meshtastic device */
public disconnect(): void {
this.userInitiatedDisconnect = true;
this.device?.gatt?.disconnect();
this.updateDeviceStatus(Types.DeviceStatusEnum.DEVICE_DISCONNECTED);
this.complete();
Expand Down
25 changes: 14 additions & 11 deletions src/adapters/iHttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ import { typedArrayToBuffer } from "../utils/general.js";
/** Allows to connect to a Meshtastic device over HTTP(S) */
export class IHTTPConnection extends IMeshDevice {
/** Defines the connection type as http */
connType: Types.ConnectionTypeName;
public connType: Types.ConnectionTypeName;

/** URL of the device that is to be connected to. */
url: string;
protected portId: string;

/** Enables receiving messages all at once, versus one per request */
receiveBatchRequests: boolean;
private receiveBatchRequests: boolean;

readLoop: ReturnType<typeof setInterval> | null;
private readLoop: ReturnType<typeof setInterval> | null;

peningRequest: boolean;
private peningRequest: boolean;

abortController: AbortController;
private abortController: AbortController;

constructor(configId?: number) {
super(configId);

this.log = this.log.getSubLogger({ name: "iHttpConnection" });

this.connType = "http";
this.url = "http://meshtastic.local";
this.portId = "";
this.receiveBatchRequests = false;
this.readLoop = null;
this.peningRequest = false;
Expand All @@ -50,7 +50,7 @@ export class IHTTPConnection extends IMeshDevice {

this.receiveBatchRequests = receiveBatchRequests;

this.url = `${tls ? "https://" : "http://"}${address}`;
this.portId = `${tls ? "https://" : "http://"}${address}`;

if (
this.deviceStatus === Types.DeviceStatusEnum.DEVICE_CONNECTING &&
Expand Down Expand Up @@ -106,7 +106,10 @@ export class IHTTPConnection extends IMeshDevice {

let pingSuccessful = false;

await fetch(`${this.url}/hotspot-detect.html`, { signal, mode: "no-cors" })
await fetch(`${this.portId}/hotspot-detect.html`, {
signal,
mode: "no-cors",
})
.then(() => {
pingSuccessful = true;
this.updateDeviceStatus(Types.DeviceStatusEnum.DEVICE_CONNECTED);
Expand All @@ -130,7 +133,7 @@ export class IHTTPConnection extends IMeshDevice {
while (readBuffer.byteLength > 0) {
this.peningRequest = true;
await fetch(
`${this.url}/api/v1/fromradio?all=${
`${this.portId}/api/v1/fromradio?all=${
this.receiveBatchRequests ? "true" : "false"
}`,
{
Expand Down Expand Up @@ -169,7 +172,7 @@ export class IHTTPConnection extends IMeshDevice {
protected async writeToRadio(data: Uint8Array): Promise<void> {
const { signal } = this.abortController;

await fetch(`${this.url}/api/v1/toradio`, {
await fetch(`${this.portId}/api/v1/toradio`, {
signal,
method: "PUT",
headers: {
Expand Down
11 changes: 9 additions & 2 deletions src/adapters/iSerialConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import { transformHandler } from "../utils/transformHandler.js";
/** Allows to connect to a Meshtastic device over WebSerial */
export class ISerialConnection extends IMeshDevice {
/** Defines the connection type as serial */
connType: Types.ConnectionTypeName;
public connType: Types.ConnectionTypeName;

protected portId: string;

/** Serial port used to communicate with device. */
private port: SerialPort | undefined;
public port: SerialPort | undefined;

private readerHack: ReadableStreamDefaultReader<Uint8Array> | undefined;

/** Transform stream for parsing raw serial data */
private transformer?: TransformStream<Uint8Array, Uint8Array>;

Expand All @@ -36,6 +40,7 @@ export class ISerialConnection extends IMeshDevice {
this.log = this.log.getSubLogger({ name: "iSerialConnection" });

this.connType = "serial";
this.portId = "";
this.port = undefined;
this.transformer = undefined;
this.onReleaseEvent = new SubEvent<boolean>();
Expand Down Expand Up @@ -108,6 +113,8 @@ export class ISerialConnection extends IMeshDevice {
/** Set device if specified, else request. */
this.port = port ?? (await this.getPort());

// this.portId = this.port. TODO: add once WebSerial adds unique/port identifiers

/** Setup event listners */
this.port.addEventListener("disconnect", () => {
this.log.info(
Expand Down
2 changes: 2 additions & 0 deletions src/iMeshDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export abstract class IMeshDevice {
/** Abstract property that states the connection type */
protected abstract connType: Types.ConnectionTypeName;

protected abstract portId: string;

/** Logs to the console and the logging event emitter */
protected log: Logger<unknown>;

Expand Down

0 comments on commit 427e98b

Please sign in to comment.