-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathadapter.ts
187 lines (144 loc) · 6.93 KB
/
adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as TsType from './tstype';
import {ZclDataPayload} from './events';
import events from 'events';
import {ZclFrame, FrameType, Direction} from '../zcl';
import Debug from "debug";
import {LoggerStub} from "../controller/logger-stub";
import * as Models from "../models";
const debug = Debug("zigbee-herdsman:adapter");
abstract class Adapter extends events.EventEmitter {
public readonly greenPowerGroup = 0x0b84;
protected networkOptions: TsType.NetworkOptions;
protected adapterOptions: TsType.AdapterOptions;
protected serialPortOptions: TsType.SerialPortOptions;
protected backupPath: string;
protected logger?: LoggerStub;
protected constructor(
networkOptions: TsType.NetworkOptions, serialPortOptions: TsType.SerialPortOptions, backupPath: string,
adapterOptions: TsType.AdapterOptions, logger?: LoggerStub)
{
super();
this.networkOptions = networkOptions;
this.adapterOptions = adapterOptions;
this.serialPortOptions = serialPortOptions;
this.backupPath = backupPath;
this.logger = logger;
}
/**
* Utility
*/
public static async create(
networkOptions: TsType.NetworkOptions,
serialPortOptions: TsType.SerialPortOptions,
backupPath: string,
adapterOptions: TsType.AdapterOptions,
logger?: LoggerStub,
): 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');
type AdapterImplementation = (typeof ZStackAdapter | typeof DeconzAdapter | typeof ZiGateAdapter
| typeof EZSPAdapter);
let adapters: AdapterImplementation[];
const adapterLookup = {zstack: ZStackAdapter, deconz: DeconzAdapter, zigate: ZiGateAdapter,
ezsp: EZSPAdapter};
if (serialPortOptions.adapter && serialPortOptions.adapter !== 'auto') {
if (adapterLookup.hasOwnProperty(serialPortOptions.adapter)) {
adapters = [adapterLookup[serialPortOptions.adapter]];
} else {
throw new Error(
`Adapter '${serialPortOptions.adapter}' does not exists, possible ` +
`options: ${Object.keys(adapterLookup).join(', ')}`
);
}
} else {
adapters = Object.values(adapterLookup);
}
// Use ZStackAdapter by default
let adapter: AdapterImplementation = adapters[0];
if (!serialPortOptions.path) {
debug('No path provided, auto detecting path');
for (const candidate of adapters) {
const path = await candidate.autoDetectPath();
if (path) {
debug(`Auto detected path '${path}' from adapter '${candidate.name}'`);
serialPortOptions.path = path;
adapter = candidate;
break;
}
}
if (!serialPortOptions.path) {
throw new Error("No path provided and failed to auto detect path");
}
} else {
try {
// Determine adapter to use
for (const candidate of adapters) {
if (await candidate.isValidPath(serialPortOptions.path)) {
debug(`Path '${serialPortOptions.path}' is valid for '${candidate.name}'`);
adapter = candidate;
break;
}
}
} catch (error) {
debug(`Failed to validate path: '${error}'`);
}
}
return new adapter(networkOptions, serialPortOptions, backupPath, adapterOptions, logger);
}
public abstract start(): Promise<TsType.StartResult>;
public abstract stop(): Promise<void>;
public abstract getCoordinator(): Promise<TsType.Coordinator>;
public abstract getCoordinatorVersion(): Promise<TsType.CoordinatorVersion>;
public abstract reset(type: 'soft' | 'hard'): Promise<void>;
public abstract supportsLED(): Promise<boolean>;
public abstract setLED(enabled: boolean): Promise<void>;
public abstract supportsBackup(): Promise<boolean>;
public abstract backup(): Promise<Models.Backup>;
public abstract getNetworkParameters(): Promise<TsType.NetworkParameters>;
public abstract setTransmitPower(value: number): Promise<void>;
public abstract waitFor(
networkAddress: number, endpoint: number, frameType: FrameType, direction: Direction,
transactionSequenceNumber: number, clusterID: number, commandIdentifier: number, timeout: number,
): {promise: Promise<ZclDataPayload>; cancel: () => void};
/**
* ZDO
*/
public abstract permitJoin(seconds: number, networkAddress: number): Promise<void>;
public abstract lqi(networkAddress: number): Promise<TsType.LQI>;
public abstract routingTable(networkAddress: number): Promise<TsType.RoutingTable>;
public abstract nodeDescriptor(networkAddress: number): Promise<TsType.NodeDescriptor>;
public abstract activeEndpoints(networkAddress: number): Promise<TsType.ActiveEndpoints>;
public abstract simpleDescriptor(networkAddress: number, endpointID: number): Promise<TsType.SimpleDescriptor>;
public abstract bind(
destinationNetworkAddress: number, sourceIeeeAddress: string, sourceEndpoint: number,
clusterID: number, destinationAddressOrGroup: string | number, type: 'endpoint' | 'group',
destinationEndpoint?: number
): Promise<void>;
public abstract unbind(
destinationNetworkAddress: number, sourceIeeeAddress: string, sourceEndpoint: number,
clusterID: number, destinationAddressOrGroup: string | number, type: 'endpoint' | 'group',
destinationEndpoint: number
): Promise<void>;
public abstract removeDevice(networkAddress: number, ieeeAddr: string): Promise<void>;
/**
* ZCL
*/
public abstract sendZclFrameToEndpoint(
ieeeAddr: string, networkAddress: number, endpoint: number, zclFrame: ZclFrame, timeout: number,
disableResponse: boolean, disableRecovery: boolean, sourceEndpoint?: number,
): Promise<ZclDataPayload>;
public abstract sendZclFrameToGroup(groupID: number, zclFrame: ZclFrame, sourceEndpoint?: number): Promise<void>;
public abstract sendZclFrameToAll(endpoint: number, zclFrame: ZclFrame, sourceEndpoint: number): Promise<void>;
/**
* InterPAN
*/
public abstract setChannelInterPAN(channel: number): Promise<void>;
public abstract sendZclFrameInterPANToIeeeAddr(zclFrame: ZclFrame, ieeeAddress: string): Promise<void>;
public abstract sendZclFrameInterPANBroadcast(
zclFrame: ZclFrame, timeout: number
): Promise<ZclDataPayload>;
public abstract restoreChannelInterPAN(): Promise<void>;
}
export default Adapter;