Skip to content

Commit

Permalink
cleanup & format
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaw committed Jul 28, 2023
1 parent f4bac3a commit 9a58223
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pnpm build
Optionally the doccumentation can be built with:

```bash
pnpm generate:docs
pnpm docs
```

### Compatibility
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "@meshtastic/meshtasticjs",
"version": "2.1.20-0",
"version": "2.1.20-1",
"description": "Browser library for interfacing with meshtastic devices",
"license": "GPL-3.0-only",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"generate:docs": "typedoc src/index.ts"
"check": "rome check .",
"docs": "typedoc src/index.ts"
},
"keywords": [
"meshtastic bluetooth serial webserial webbluetooth ble http library meshtasticjs"
Expand Down
6 changes: 6 additions & 0 deletions rome.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
},
"organizeImports": {
"enabled": true
},
"files": {
"ignore": [
"node_modules",
"dist"
]
}
}
2 changes: 1 addition & 1 deletion src/adapters/iBleConnection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Types } from "../index.js";
import {
fromNumUUID,
fromRadioUUID,
serviceUUID,
toRadioUUID,
} from "../constants.js";
import { IMeshDevice } from "../iMeshDevice.js";
import { Types } from "../index.js";
import { typedArrayToBuffer } from "../utils/general.js";

/** Allows to connect to a Meshtastic device via bluetooth */
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/iHttpConnection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Types } from "../index.js";
import { IMeshDevice } from "../iMeshDevice.js";
import { Types } from "../index.js";
import { typedArrayToBuffer } from "../utils/general.js";

/** Allows to connect to a Meshtastic device over HTTP(S) */
Expand Down
28 changes: 15 additions & 13 deletions src/adapters/iSerialConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SubEvent } from "sub-events";

import { Types } from "../index.js";
import { IMeshDevice } from "../iMeshDevice.js";
import { Types } from "../index.js";
import { transformHandler } from "../utils/transformHandler.js";

/** Allows to connect to a Meshtastic device over WebSerial */
Expand All @@ -11,7 +11,7 @@ export class ISerialConnection extends IMeshDevice {

/** Serial port used to communicate with device. */
private port: SerialPort | undefined;
private readerHack: ReadableStreamDefaultReader<Uint8Array> | undefined;
private readerHack: ReadableStreamDefaultReader<Uint8Array> | undefined;
/** Transform stream for parsing raw serial data */
private transformer?: TransformStream<Uint8Array, Uint8Array>;

Expand Down Expand Up @@ -55,9 +55,9 @@ export class ISerialConnection extends IMeshDevice {
): Promise<void> {
this.onReleaseEvent.subscribe(async () => {
this.preventLock = true;
await reader.cancel();
await this.pipePromise?.catch(() => {});
reader.releaseLock();
await reader.cancel();
await this.pipePromise?.catch(() => {});
reader.releaseLock();
await this.port?.close();
});

Expand All @@ -72,9 +72,8 @@ export class ISerialConnection extends IMeshDevice {
.catch(() => {
this.log.debug(
Types.Emitter[Types.Emitter.readFromRadio],
`Releasing reader`,
"Releasing reader",
);
("Releasing reader");
});
}
}
Expand Down Expand Up @@ -134,8 +133,11 @@ export class ISerialConnection extends IMeshDevice {
concurrentLogOutput,
);

this.pipePromise = this.port.readable.pipeTo(this.transformer.writable);
const reader = this.readerHack = this.transformer.readable.getReader();
this.pipePromise = this.port.readable.pipeTo(
this.transformer.writable,
);
const reader = (this.readerHack =
this.transformer.readable.getReader());
void this.readFromRadio(reader);

this.updateDeviceStatus(Types.DeviceStatusEnum.DEVICE_CONNECTED);
Expand Down Expand Up @@ -166,10 +168,10 @@ export class ISerialConnection extends IMeshDevice {
// HACK: Inline onReleaseEvent
// -- This should be used as an event, like intened
this.preventLock = true;
await this.readerHack?.cancel();
await this.pipePromise?.catch(() => {});
this.readerHack?.releaseLock();
if(this.port?.readable) await this.port?.close();
await this.readerHack?.cancel();
await this.pipePromise?.catch(() => {});
this.readerHack?.releaseLock();
if (this.port?.readable) await this.port?.close();
// -------
this.updateDeviceStatus(Types.DeviceStatusEnum.DEVICE_DISCONNECTED);
this.complete();
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IBLEConnection } from "./adapters/iBleConnection.js";
import { IHTTPConnection } from "./adapters/iHttpConnection.js";
import { Types } from "./index.js";
import { ISerialConnection } from "./adapters/iSerialConnection.js";
import { Types } from "./index.js";

/**
* Allows to create new connections to devices and manages them. Alternatively,
Expand Down
22 changes: 14 additions & 8 deletions src/iMeshDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export abstract class IMeshDevice {
public async setConfig(config: Protobuf.Config): Promise<number> {
this.log.debug(
Types.Emitter[Types.Emitter.setConfig],
`Setting config, Variant: ${config.payloadVariant.case ?? "Unknown"}`,
`⚙️ Setting config, Variant: ${config.payloadVariant.case ?? "Unknown"}`,
);

if (!this.pendingSettingsChanges) {
Expand Down Expand Up @@ -259,7 +259,7 @@ export abstract class IMeshDevice {
): Promise<number> {
this.log.debug(
Types.Emitter[Types.Emitter.setModuleConfig],
"Setting module config",
"⚙️ Setting module config",
);

const moduleConfigMessage = new Protobuf.AdminMessage({
Expand All @@ -280,7 +280,7 @@ export abstract class IMeshDevice {
* Sets devices owner data
*/
public async setOwner(owner: Protobuf.User): Promise<number> {
this.log.debug(Types.Emitter[Types.Emitter.setOwner], "Setting owner");
this.log.debug(Types.Emitter[Types.Emitter.setOwner], "👤 Setting owner");

const setOwnerMessage = new Protobuf.AdminMessage({
payloadVariant: {
Expand Down Expand Up @@ -359,7 +359,10 @@ export abstract class IMeshDevice {
public async getConfig(
configType: Protobuf.AdminMessage_ConfigType,
): Promise<number> {
this.log.debug(Types.Emitter[Types.Emitter.getConfig], "Requesting config");
this.log.debug(
Types.Emitter[Types.Emitter.getConfig],
"⚙️ Requesting config",
);

const getRadioRequestMessage = new Protobuf.AdminMessage({
payloadVariant: {
Expand All @@ -383,7 +386,7 @@ export abstract class IMeshDevice {
): Promise<number> {
this.log.debug(
Types.Emitter[Types.Emitter.getModuleConfig],
"Requesting module config",
"⚙️ Requesting module config",
);

const getRadioRequestMessage = new Protobuf.AdminMessage({
Expand All @@ -402,7 +405,10 @@ export abstract class IMeshDevice {

/** Gets devices Owner */
public async getOwner(): Promise<number> {
this.log.debug(Types.Emitter[Types.Emitter.getOwner], "Requesting owner");
this.log.debug(
Types.Emitter[Types.Emitter.getOwner],
"👤 Requesting owner",
);

const getOwnerRequestMessage = new Protobuf.AdminMessage({
payloadVariant: {
Expand All @@ -424,7 +430,7 @@ export abstract class IMeshDevice {
public async getMetadata(nodeNum: number): Promise<number> {
this.log.debug(
Types.Emitter[Types.Emitter.getMetadata],
`Requesting metadata from ${nodeNum}`,
`🏷️ Requesting metadata from ${nodeNum}`,
);

const getDeviceMetricsRequestMessage = new Protobuf.AdminMessage({
Expand Down Expand Up @@ -879,7 +885,7 @@ export abstract class IMeshDevice {
case "encrypted":
this.log.debug(
Types.Emitter[Types.Emitter.handleMeshPacket],
"Device received encrypted data packet, ignoring.",
"🔐 Device received encrypted data packet, ignoring.",
);
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Client } from "./client.js";
import { IMeshDevice } from "./iMeshDevice.js";
import { IBLEConnection } from "./adapters/iBleConnection.js";
import { IHTTPConnection } from "./adapters/iHttpConnection.js";
import { ISerialConnection } from "./adapters/iSerialConnection.js";
import { Client } from "./client.js";
import * as Constants from "./constants.js";
import { IMeshDevice } from "./iMeshDevice.js";
import * as Protobuf from "./protobufs.js";
import * as Types from "./types.js";
import * as Constants from "./constants.js";

export {
Client,
Expand Down
102 changes: 51 additions & 51 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import type {
} from "./index.js";

export enum DeviceStatusEnum {
DEVICE_RESTARTING,
DEVICE_DISCONNECTED,
DEVICE_CONNECTING,
DEVICE_RECONNECTING,
DEVICE_CONNECTED,
DEVICE_CONFIGURING,
DEVICE_CONFIGURED,
DEVICE_RESTARTING = 1,
DEVICE_DISCONNECTED = 2,
DEVICE_CONNECTING = 3,
DEVICE_RECONNECTING = 4,
DEVICE_CONNECTED = 5,
DEVICE_CONFIGURING = 6,
DEVICE_CONFIGURED = 7,
}

export type ConnectionParameters =
Expand Down Expand Up @@ -66,45 +66,45 @@ export interface PacketMetadata<T> {
}

export enum EmitterScope {
iMeshDevice,
iSerialConnection,
iNodeSerialConnection,
iBleConnection,
iHttpConnection,
iMeshDevice = 1,
iSerialConnection = 2,
iNodeSerialConnection = 3,
iBleConnection = 4,
iHttpConnection = 5,
}

export enum Emitter {
constructor,
sendText,
sendWaypoint,
sendPacket,
sendRaw,
setConfig,
setModuleConfig,
confirmSetConfig,
setOwner,
setChannel,
confirmSetChannel,
clearChannel,
getChannel,
getAllChannels,
getConfig,
getModuleConfig,
getOwner,
configure,
handleFromRadio,
handleMeshPacket,
connect,
ping,
readFromRadio,
writeToRadio,
setDebugMode,
getMetadata,
resetPeers,
shutdown,
reboot,
rebootOTA,
factoryReset,
constructor = 0,
sendText = 1,
sendWaypoint = 2,
sendPacket = 3,
sendRaw = 4,
setConfig = 5,
setModuleConfig = 6,
confirmSetConfig = 7,
setOwner = 8,
setChannel = 9,
confirmSetChannel = 10,
clearChannel = 11,
getChannel = 12,
getAllChannels = 13,
getConfig = 14,
getModuleConfig = 15,
getOwner = 16,
configure = 17,
handleFromRadio = 18,
handleMeshPacket = 19,
connect = 20,
ping = 21,
readFromRadio = 22,
writeToRadio = 23,
setDebugMode = 24,
getMetadata = 25,
resetPeers = 26,
shutdown = 27,
reboot = 28,
rebootOTA = 29,
factoryReset = 30,
}

export interface LogEvent {
Expand All @@ -116,14 +116,14 @@ export interface LogEvent {
}

export enum ChannelNumber {
PRIMARY,
CHANNEL1,
CHANNEL2,
CHANNEL3,
CHANNEL4,
CHANNEL5,
CHANNEL6,
ADMIN,
PRIMARY = 1,
CHANNEL1 = 2,
CHANNEL2 = 3,
CHANNEL3 = 4,
CHANNEL4 = 5,
CHANNEL5 = 6,
CHANNEL6 = 7,
ADMIN = 8,
}

export type ConnectionType =
Expand Down
2 changes: 1 addition & 1 deletion src/utils/eventSystem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SubEvent } from "sub-events";
import { Protobuf, Types } from "../index.js";
import { PacketMetadata } from "../types.js";
import { SubEvent } from "sub-events";

export class EventSystem {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/queue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SubEvent } from "sub-events";
import { Protobuf } from "../index.js";
import { PacketError } from "../types.js";
import { SubEvent } from "sub-events";

export interface IQueueItem {
id: number;
Expand Down

0 comments on commit 9a58223

Please sign in to comment.