Skip to content

Commit

Permalink
fix: Cleanup imports/exports (#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec authored Dec 17, 2024
1 parent 068f03a commit 6fde32f
Show file tree
Hide file tree
Showing 61 changed files with 357 additions and 451 deletions.
36 changes: 20 additions & 16 deletions src/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ interface AdapterEventMap {
deviceLeave: [payload: AdapterEvents.DeviceLeavePayload];
}

abstract class Adapter extends events.EventEmitter<AdapterEventMap> {
type AdapterConstructor = new (
networkOptions: TsType.NetworkOptions,
serialPortOptions: TsType.SerialPortOptions,
backupPath: string,
adapterOptions: TsType.AdapterOptions,
) => Adapter;

export abstract class Adapter extends events.EventEmitter<AdapterEventMap> {
public hasZdoMessageOverhead: boolean;
public manufacturerID: Zcl.ManufacturerCode;
protected networkOptions: TsType.NetworkOptions;
Expand Down Expand Up @@ -50,28 +57,25 @@ abstract class Adapter extends events.EventEmitter<AdapterEventMap> {
backupPath: string,
adapterOptions: TsType.AdapterOptions,
): Promise<Adapter> {
const {ZStackAdapter} = await import('./z-stack/adapter');
const {DeconzAdapter} = await import('./deconz/adapter');
const {ZiGateAdapter} = await import('./zigate/adapter');
const {EZSPAdapter} = await import('./ezsp/adapter');
const {EmberAdapter} = await import('./ember/adapter');
const {ZBOSSAdapter} = await import('./zboss/adapter');
const adapterLookup = {
zstack: ZStackAdapter,
deconz: DeconzAdapter,
zigate: ZiGateAdapter,
ezsp: EZSPAdapter,
ember: EmberAdapter,
zboss: ZBOSSAdapter,
deconz: ['./deconz/adapter/deconzAdapter', 'DeconzAdapter'],
ember: ['./ember/adapter/emberAdapter', 'EmberAdapter'],
ezsp: ['./ezsp/adapter/ezspAdapter', 'EZSPAdapter'],
zstack: ['./z-stack/adapter/zStackAdapter', 'ZStackAdapter'],
zboss: ['./zboss/adapter/zbossAdapter', 'ZBOSSAdapter'],
zigate: ['./zigate/adapter/zigateAdapter', 'ZiGateAdapter'],
};

const [adapter, path] = await discoverAdapter(serialPortOptions.adapter, serialPortOptions.path);
const detectedAdapter = adapterLookup[adapter];

if (adapterLookup[adapter]) {
if (detectedAdapter) {
serialPortOptions.adapter = adapter;
serialPortOptions.path = path;

return new adapterLookup[adapter](networkOptions, serialPortOptions, backupPath, adapterOptions);
const adapterModule = await import(detectedAdapter[0]);
const AdapterCtor = adapterModule[detectedAdapter[1]] as AdapterConstructor;

return new AdapterCtor(networkOptions, serialPortOptions, backupPath, adapterOptions);
} else {
throw new Error(`Adapter '${adapter}' does not exists, possible options: ${Object.keys(adapterLookup).join(', ')}`);
}
Expand Down
18 changes: 8 additions & 10 deletions src/adapter/deconz/adapter/deconzAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Device from '../../../controller/model/device';
import * as Models from '../../../models';
import {Wait, Waitress} from '../../../utils';
import {wait, Waitress} from '../../../utils';
import {logger} from '../../../utils/logger';
import * as ZSpec from '../../../zspec';
import {BroadcastAddress} from '../../../zspec/enums';
Expand All @@ -28,7 +28,7 @@ interface WaitressMatcher {
direction: number;
}

class DeconzAdapter extends Adapter {
export class DeconzAdapter extends Adapter {
private driver: Driver;
private openRequestsQueue: WaitForDataRequest[];
private transactionID: number;
Expand Down Expand Up @@ -156,7 +156,7 @@ class DeconzAdapter extends Adapter {

try {
await this.driver.writeParameterRequest(PARAM.PARAM.Network.CHANNEL_MASK, setChannelMask);
await Wait(500);
await wait(500);
changed = true;
} catch (error) {
logger.debug('Could not set channel: ' + error, NS);
Expand All @@ -172,7 +172,7 @@ class DeconzAdapter extends Adapter {

try {
await this.driver.writeParameterRequest(PARAM.PARAM.Network.PAN_ID, this.networkOptions.panID);
await Wait(500);
await wait(500);
changed = true;
} catch (error) {
logger.debug('Could not set panid: ' + error, NS);
Expand All @@ -192,7 +192,7 @@ class DeconzAdapter extends Adapter {

try {
await this.driver.writeParameterRequest(PARAM.PARAM.Network.APS_EXT_PAN_ID, this.networkOptions.extendedPanID!);
await Wait(500);
await wait(500);
changed = true;
} catch (error) {
logger.debug('Could not set extended panid: ' + error, NS);
Expand All @@ -208,7 +208,7 @@ class DeconzAdapter extends Adapter {

try {
await this.driver.writeParameterRequest(PARAM.PARAM.Network.NETWORK_KEY, this.networkOptions.networkKey!);
await Wait(500);
await wait(500);
changed = true;
} catch (error) {
logger.debug('Could not set network key: ' + error, NS);
Expand All @@ -217,9 +217,9 @@ class DeconzAdapter extends Adapter {

if (changed) {
await this.driver.changeNetworkStateRequest(PARAM.PARAM.Network.NET_OFFLINE);
await Wait(2000);
await wait(2000);
await this.driver.changeNetworkStateRequest(PARAM.PARAM.Network.NET_CONNECTED);
await Wait(2000);
await wait(2000);
}

// write endpoints
Expand Down Expand Up @@ -754,5 +754,3 @@ class DeconzAdapter extends Adapter {
);
}
}

export default DeconzAdapter;
3 changes: 0 additions & 3 deletions src/adapter/deconz/adapter/index.ts

This file was deleted.

16 changes: 8 additions & 8 deletions src/adapter/ember/adapter/emberAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import equals from 'fast-deep-equal/es6';

import {Adapter, TsType} from '../..';
import {Backup, UnifiedBackupStorage} from '../../../models';
import {BackupUtils, Queue, Wait} from '../../../utils';
import {BackupUtils, Queue, wait} from '../../../utils';
import {logger} from '../../../utils/logger';
import * as ZSpec from '../../../zspec';
import {EUI64, ExtendedPanId, NodeId, PanId} from '../../../zspec/tstypes';
Expand Down Expand Up @@ -921,7 +921,7 @@ export class EmberAdapter extends Adapter {
'[INIT TC] Leave network',
);

await Wait(200); // settle down
await wait(200); // settle down

action = NetworkInitAction.LEFT;
}
Expand Down Expand Up @@ -1271,7 +1271,7 @@ export class EmberAdapter extends Adapter {

// XXX: this will block other requests for a while, but should ensure the key propagates without interference?
// could also stop dispatching entirely and do this outside the queue if necessary/better
await Wait(BROADCAST_NETWORK_KEY_SWITCH_WAIT_TIME);
await wait(BROADCAST_NETWORK_KEY_SWITCH_WAIT_TIME);

status = await this.ezsp.ezspBroadcastNetworkKeySwitch();

Expand Down Expand Up @@ -2009,9 +2009,9 @@ export class EmberAdapter extends Adapter {
`~x~> [ZCL to=${ieeeAddr}:${networkAddress} apsFrame=${JSON.stringify(apsFrame)}] Failed to send request with status=${SLStatus[status]}.`,
);
} else if (status === SLStatus.ZIGBEE_MAX_MESSAGE_LIMIT_REACHED || status === SLStatus.BUSY) {
await Wait(QUEUE_BUSY_DEFER_MSEC);
await wait(QUEUE_BUSY_DEFER_MSEC);
} else if (status === SLStatus.NETWORK_DOWN) {
await Wait(QUEUE_NETWORK_DOWN_DEFER_MSEC);
await wait(QUEUE_NETWORK_DOWN_DEFER_MSEC);
} else {
throw new Error(
`~x~> [ZCL to=${ieeeAddr}:${networkAddress} apsFrame=${JSON.stringify(apsFrame)}] Failed to send request with status=${SLStatus[status]}.`,
Expand Down Expand Up @@ -2075,7 +2075,7 @@ export class EmberAdapter extends Adapter {
}

// NOTE: since ezspMessageSentHandler could take a while here, we don't block, it'll just be logged if the delivery failed
await Wait(QUEUE_BUSY_DEFER_MSEC);
await wait(QUEUE_BUSY_DEFER_MSEC);
});
}

Expand Down Expand Up @@ -2117,7 +2117,7 @@ export class EmberAdapter extends Adapter {
}

// NOTE: since ezspMessageSentHandler could take a while here, we don't block, it'll just be logged if the delivery failed
await Wait(QUEUE_BUSY_DEFER_MSEC);
await wait(QUEUE_BUSY_DEFER_MSEC);
});
}

Expand Down Expand Up @@ -2246,7 +2246,7 @@ export class EmberAdapter extends Adapter {
}

// let adapter settle down
await Wait(QUEUE_NETWORK_DOWN_DEFER_MSEC);
await wait(QUEUE_NETWORK_DOWN_DEFER_MSEC);

this.interpanLock = false;
});
Expand Down
3 changes: 0 additions & 3 deletions src/adapter/ember/adapter/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/adapter/ember/uart/ash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {Socket} from 'net';
import {EventEmitter} from 'stream';

import {Wait} from '../../../utils';
import {wait} from '../../../utils';
import {logger} from '../../../utils/logger';
import {SerialPort} from '../../serialPort';
import SocketPortUtils from '../../socketPortUtils';
Expand Down Expand Up @@ -640,7 +640,7 @@ export class UartAsh extends EventEmitter<UartAshEventMap> {
}

logger.debug(`Waiting for RSTACK... ${i}/${CONFIG_TIME_RST}`, NS);
await Wait(CONFIG_TIME_RST_CHECK);
await wait(CONFIG_TIME_RST_CHECK);
}

return EzspStatus.HOST_FATAL_ERROR;
Expand Down
8 changes: 3 additions & 5 deletions src/adapter/events.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Header as ZclHeader} from '../zspec/zcl';

type DeviceJoinedPayload = {
export type DeviceJoinedPayload = {
networkAddress: number;
ieeeAddr: string;
};

type DeviceLeavePayload = {networkAddress?: number; ieeeAddr: string} | {networkAddress: number; ieeeAddr?: string};
export type DeviceLeavePayload = {networkAddress?: number; ieeeAddr: string} | {networkAddress: number; ieeeAddr?: string};

interface ZclPayload {
export interface ZclPayload {
clusterID: number;
address: number | string;
header: ZclHeader | undefined;
Expand All @@ -19,5 +19,3 @@ interface ZclPayload {
wasBroadcast: boolean;
destinationEndpoint: number;
}

export {DeviceJoinedPayload, ZclPayload, DeviceLeavePayload};
12 changes: 5 additions & 7 deletions src/adapter/ezsp/adapter/ezspAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import assert from 'assert';

import * as Models from '../../../models';
import {Queue, Wait, Waitress} from '../../../utils';
import {Queue, wait, Waitress} from '../../../utils';
import {logger} from '../../../utils/logger';
import * as ZSpec from '../../../zspec';
import * as Zcl from '../../../zspec/zcl';
Expand All @@ -25,7 +25,7 @@ interface WaitressMatcher {
commandIdentifier: number;
}

class EZSPAdapter extends Adapter {
export class EZSPAdapter extends Adapter {
private driver: Driver;
private waitress: Waitress<ZclPayload, WaitressMatcher>;
private interpanLock: boolean;
Expand Down Expand Up @@ -427,7 +427,7 @@ class EZSPAdapter extends Adapter {
* (contrary to network address requests) we will give the
* command some time to 'settle' in the network.
*/
await Wait(200);
await wait(200);
});
}

Expand All @@ -454,7 +454,7 @@ class EZSPAdapter extends Adapter {
* (contrary to network address requests) we will give the
* command some time to 'settle' in the network.
*/
await Wait(200);
await wait(200);
});
}

Expand All @@ -480,7 +480,7 @@ class EZSPAdapter extends Adapter {
const channel = (await this.getNetworkParameters()).channel;
await this.driver.setChannel(channel);
// Give adapter some time to restore, otherwise stuff crashes
await Wait(3000);
await wait(3000);
this.interpanLock = false;
});
}
Expand Down Expand Up @@ -605,5 +605,3 @@ class EZSPAdapter extends Adapter {
);
}
}

export default EZSPAdapter;
3 changes: 0 additions & 3 deletions src/adapter/ezsp/adapter/index.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/adapter/ezsp/driver/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {EventEmitter} from 'events';

import equals from 'fast-deep-equal/es6';

import {Wait, Waitress} from '../../../utils';
import {wait, Waitress} from '../../../utils';
import {logger} from '../../../utils/logger';
import * as ZSpec from '../../../zspec';
import {Clusters} from '../../../zspec/zcl/definition/cluster';
Expand Down Expand Up @@ -142,7 +142,7 @@ export class Driver extends EventEmitter {
logger.debug(`Stop error ${err}`, NS);
}
try {
await Wait(1000);
await wait(1000);
logger.debug(`Startup again.`, NS);
await this.startup();
} catch (err) {
Expand Down Expand Up @@ -293,7 +293,7 @@ export class Driver extends EventEmitter {
const netResult = await this.getKey(EmberKeyType.CURRENT_NETWORK_KEY);
logger.debug(`CURRENT_NETWORK_KEY: ${JSON.stringify(netResult)}`, NS);

await Wait(1000);
await wait(1000);
await this.ezsp.execCommand('setManufacturerCode', {code: DEFAULT_MFG_ID});

this.multicast = new Multicast(this);
Expand Down Expand Up @@ -607,7 +607,7 @@ export class Driver extends EventEmitter {
private async resetMfgId(mfgId: number): Promise<void> {
await this.ezsp.execCommand('setManufacturerCode', {code: mfgId});
// 60 sec for waiting
await Wait(60000);
await wait(60000);
await this.ezsp.execCommand('setManufacturerCode', {code: DEFAULT_MFG_ID});
}

Expand Down Expand Up @@ -683,7 +683,7 @@ export class Driver extends EventEmitter {
// need to repeat after pause
logger.error(`Request send status ${sendResult.status}. Attempt to repeat the request`, NS);

await Wait(delay);
await wait(delay);
} else {
result = sendResult.status == EmberStatus.SUCCESS;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/ezsp/driver/ezsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {EventEmitter} from 'events';

import {Queue, Wait, Waitress} from '../../../utils';
import {Queue, wait, Waitress} from '../../../utils';
import {logger} from '../../../utils/logger';
import {SerialPortOptions} from '../../tstype';
import {
Expand Down Expand Up @@ -350,7 +350,7 @@ export class Ezsp extends EventEmitter {
logger.error(`Connection attempt ${i} error: ${error}`, NS);

if (i < MAX_SERIAL_CONNECT_ATTEMPTS) {
await Wait(SERIAL_CONNECT_NEW_ATTEMPT_MIN_DELAY * i);
await wait(SERIAL_CONNECT_NEW_ATTEMPT_MIN_DELAY * i);
logger.debug(`Next attempt ${i + 1}`, NS);
}

Expand Down
5 changes: 2 additions & 3 deletions src/adapter/ezsp/driver/uart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import {EventEmitter} from 'events';
import net from 'net';

import {Queue, Wait, Waitress} from '../../../utils';
import {Queue, wait, Waitress} from '../../../utils';
import {logger} from '../../../utils/logger';
import wait from '../../../utils/wait';
import {SerialPort} from '../../serialPort';
import SocketPortUtils from '../../socketPortUtils';
import {SerialPortOptions} from '../../tstype';
Expand Down Expand Up @@ -416,7 +415,7 @@ export class SerialDriver extends EventEmitter {
logger.error(`Can't send DATA frame (${seq},${ackSeq},0): ${data.toString('hex')}`, NS);

try {
await Wait(500);
await wait(500);
const waiter = this.waitFor(nextSeq);
logger.debug(`->> DATA (${seq},${ackSeq},1): ${data.toString('hex')}`, NS);
this.writer.sendData(randData, seq, 1, ackSeq);
Expand Down
8 changes: 3 additions & 5 deletions src/adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Adapter from './adapter';
import * as Events from './events';
import * as TsType from './tstype';

export {TsType, Adapter, Events};
export {Adapter} from './adapter';
export * as Events from './events';
export * as TsType from './tstype';
3 changes: 0 additions & 3 deletions src/adapter/z-stack/adapter/index.ts

This file was deleted.

Loading

0 comments on commit 6fde32f

Please sign in to comment.