Skip to content

Commit

Permalink
feat: Enforce TS strict type checking and other improvements (#1146)
Browse files Browse the repository at this point in the history
* Enforce TS `strict` type checking

* Impl. for adapter/ember. Fix a few leftovers.

* Prettier fix.

* Partial impl. for adapter/deconz

* Fix more eslint for adapter/deconz

* Impl. for adapter/zigate

* Prettier fix.

* Partial impl. for adapter/ezsp

* Fix waitFor definition & command response detection.

* Partial impl. for adapter/zstack

* Fix eslint

* Updates

* Some updates

* Updates

* Updates

* Updates

* Updates

* Updates

* Add EventEmitter typing.

* Fix test & prettier.

* Fix lint.

* Fix `device` always defined when emitting.

* Revert deconz request enqueue.

* Update deconz

* Cleanup.

* Don't allow errors to logger

* Revert some Error logging from `stack` to `message.

---------

Co-authored-by: Koen Kanters <[email protected]>
Co-authored-by: Koen Kanters <--global>
  • Loading branch information
Nerivec and Koenkk authored Aug 16, 2024
1 parent 3a5b075 commit 98e3384
Show file tree
Hide file tree
Showing 111 changed files with 3,437 additions and 3,494 deletions.
41 changes: 30 additions & 11 deletions src/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import * as Models from '../models';
import {logger} from '../utils/logger';
import {BroadcastAddress} from '../zspec/enums';
import * as Zcl from '../zspec/zcl';
import {ZclPayload} from './events';
import * as AdapterEvents from './events';
import * as TsType from './tstype';

const NS = 'zh:adapter';

abstract class Adapter extends events.EventEmitter {
interface AdapterEventMap {
deviceJoined: [payload: AdapterEvents.DeviceJoinedPayload];
zclPayload: [payload: AdapterEvents.ZclPayload];
disconnected: [];
deviceAnnounce: [payload: AdapterEvents.DeviceAnnouncePayload];
deviceLeave: [payload: AdapterEvents.DeviceLeavePayload];
networkAddress: [payload: AdapterEvents.NetworkAddressPayload];
}

abstract class Adapter extends events.EventEmitter<AdapterEventMap> {
public readonly greenPowerGroup = 0x0b84;
protected networkOptions: TsType.NetworkOptions;
protected adapterOptions: TsType.AdapterOptions;
Expand Down Expand Up @@ -49,8 +58,9 @@ abstract class Adapter extends events.EventEmitter {

let adapters: AdapterImplementation[];
const adapterLookup = {zstack: ZStackAdapter, deconz: DeconzAdapter, zigate: ZiGateAdapter, ezsp: EZSPAdapter, ember: EmberAdapter};

if (serialPortOptions.adapter && serialPortOptions.adapter !== 'auto') {
if (adapterLookup.hasOwnProperty(serialPortOptions.adapter)) {
if (adapterLookup[serialPortOptions.adapter]) {
adapters = [adapterLookup[serialPortOptions.adapter]];
} else {
throw new Error(`Adapter '${serialPortOptions.adapter}' does not exists, possible options: ${Object.keys(adapterLookup).join(', ')}`);
Expand Down Expand Up @@ -79,13 +89,16 @@ abstract class Adapter extends events.EventEmitter {
}
} else if (serialPortOptions.path.startsWith('mdns://')) {
const mdnsDevice = serialPortOptions.path.substring(7);

if (mdnsDevice.length == 0) {
throw new Error(`No mdns device specified. You must specify the coordinator mdns service type after mdns://, e.g. mdns://my-adapter`);
}

const bj = new Bonjour();
const mdnsTimeout = 2000; // timeout for mdns scan

logger.info(`Starting mdns discovery for coordinator: ${mdnsDevice}`, NS);

await new Promise((resolve, reject) => {
bj.findOne({type: mdnsDevice}, mdnsTimeout, function (service: Service) {
if (service) {
Expand All @@ -96,16 +109,22 @@ abstract class Adapter extends events.EventEmitter {
service.txt.radio_type == 'znp' ? 'zstack' : service.txt.radio_type
) as TsType.SerialPortOptions['adapter'];
const mdnsBaud = parseInt(service.txt.baud_rate);

logger.info(`Coordinator Ip: ${mdnsIp}`, NS);
logger.info(`Coordinator Port: ${mdnsPort}`, NS);
logger.info(`Coordinator Radio: ${mdnsAdapter}`, NS);
logger.info(`Coordinator Baud: ${mdnsBaud}\n`, NS);
bj.destroy();

serialPortOptions.path = `tcp://${mdnsIp}:${mdnsPort}`;
serialPortOptions.adapter = mdnsAdapter;
serialPortOptions.baudRate = mdnsBaud;

if (adapterLookup.hasOwnProperty(serialPortOptions.adapter) && serialPortOptions.adapter !== 'auto') {
if (
serialPortOptions.adapter &&
serialPortOptions.adapter !== 'auto' &&
adapterLookup[serialPortOptions.adapter] !== undefined
) {
adapter = adapterLookup[serialPortOptions.adapter];
resolve(new adapter(networkOptions, serialPortOptions, backupPath, adapterOptions));
} else {
Expand Down Expand Up @@ -170,21 +189,21 @@ abstract class Adapter extends events.EventEmitter {
public abstract addInstallCode(ieeeAddress: string, key: Buffer): Promise<void>;

public abstract waitFor(
networkAddress: number,
networkAddress: number | undefined,
endpoint: number,
frameType: Zcl.FrameType,
direction: Zcl.Direction,
transactionSequenceNumber: number,
transactionSequenceNumber: number | undefined,
clusterID: number,
commandIdentifier: number,
timeout: number,
): {promise: Promise<ZclPayload>; cancel: () => void};
): {promise: Promise<AdapterEvents.ZclPayload>; cancel: () => void};

/**
* ZDO
*/

public abstract permitJoin(seconds: number, networkAddress: number): Promise<void>;
public abstract permitJoin(seconds: number, networkAddress?: number): Promise<void>;

public abstract lqi(networkAddress: number): Promise<TsType.LQI>;

Expand Down Expand Up @@ -213,7 +232,7 @@ abstract class Adapter extends events.EventEmitter {
clusterID: number,
destinationAddressOrGroup: string | number,
type: 'endpoint' | 'group',
destinationEndpoint: number,
destinationEndpoint?: number,
): Promise<void>;

public abstract removeDevice(networkAddress: number, ieeeAddr: string): Promise<void>;
Expand All @@ -231,7 +250,7 @@ abstract class Adapter extends events.EventEmitter {
disableResponse: boolean,
disableRecovery: boolean,
sourceEndpoint?: number,
): Promise<ZclPayload>;
): Promise<AdapterEvents.ZclPayload | void>;

public abstract sendZclFrameToGroup(groupID: number, zclFrame: Zcl.Frame, sourceEndpoint?: number): Promise<void>;

Expand All @@ -245,7 +264,7 @@ abstract class Adapter extends events.EventEmitter {

public abstract sendZclFrameInterPANToIeeeAddr(zclFrame: Zcl.Frame, ieeeAddress: string): Promise<void>;

public abstract sendZclFrameInterPANBroadcast(zclFrame: Zcl.Frame, timeout: number): Promise<ZclPayload>;
public abstract sendZclFrameInterPANBroadcast(zclFrame: Zcl.Frame, timeout: number): Promise<AdapterEvents.ZclPayload>;

public abstract restoreChannelInterPAN(): Promise<void>;
}
Expand Down
Loading

0 comments on commit 98e3384

Please sign in to comment.