@switchbot/homebridge-switchbot 2.4.1-beta.6 → 2.5.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config.schema.json +6 -2
- package/dist/device/blindtilt.d.ts +118 -0
- package/dist/device/blindtilt.d.ts.map +1 -0
- package/dist/device/blindtilt.js +1196 -0
- package/dist/device/blindtilt.js.map +1 -0
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +2 -0
- package/dist/device/curtain.js.map +1 -1
- package/dist/platform.d.ts +1 -0
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +96 -19
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +4 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/package.json +7 -7
- package/src/device/blindtilt.ts +1262 -0
- package/src/device/curtain.ts +2 -0
- package/src/platform.ts +97 -19
- package/src/settings.ts +8 -0
|
@@ -0,0 +1,1262 @@
|
|
|
1
|
+
import https from 'https';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { Context } from 'vm';
|
|
4
|
+
import { MqttClient } from 'mqtt';
|
|
5
|
+
import { IncomingMessage } from 'http';
|
|
6
|
+
import { interval, Subject } from 'rxjs';
|
|
7
|
+
import { connectAsync } from 'async-mqtt';
|
|
8
|
+
import superStringify from 'super-stringify';
|
|
9
|
+
import { SwitchBotPlatform } from '../platform';
|
|
10
|
+
import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
|
|
11
|
+
import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
|
|
12
|
+
import { device, devicesConfig, serviceData, switchbot, deviceStatus, ad, HostDomain, DevicePath } from '../settings';
|
|
13
|
+
import { sleep } from '../utils';
|
|
14
|
+
|
|
15
|
+
export class BlindTilt {
|
|
16
|
+
// Services
|
|
17
|
+
windowCoveringService: Service;
|
|
18
|
+
batteryService?: Service;
|
|
19
|
+
|
|
20
|
+
// Characteristic Values
|
|
21
|
+
CurrentPosition!: CharacteristicValue;
|
|
22
|
+
PositionState!: CharacteristicValue;
|
|
23
|
+
TargetPosition!: CharacteristicValue;
|
|
24
|
+
BatteryLevel?: CharacteristicValue;
|
|
25
|
+
StatusLowBattery?: CharacteristicValue;
|
|
26
|
+
CurrentHorizontalTiltAngle!: CharacteristicValue;
|
|
27
|
+
TargetHorizontalTiltAngle!: CharacteristicValue;
|
|
28
|
+
|
|
29
|
+
// OpenAPI Others
|
|
30
|
+
deviceStatus!: any; //deviceStatusResponse;
|
|
31
|
+
slidePosition: deviceStatus['slidePosition'];
|
|
32
|
+
moving: deviceStatus['moving'];
|
|
33
|
+
brightness: deviceStatus['brightness'];
|
|
34
|
+
setPositionMode?: string | number;
|
|
35
|
+
Mode!: string;
|
|
36
|
+
|
|
37
|
+
// BLE Others
|
|
38
|
+
connected?: boolean;
|
|
39
|
+
switchbot!: switchbot;
|
|
40
|
+
serviceData!: serviceData;
|
|
41
|
+
spaceBetweenLevels!: number;
|
|
42
|
+
address!: ad['address'];
|
|
43
|
+
calibration: serviceData['calibration'];
|
|
44
|
+
battery: serviceData['battery'];
|
|
45
|
+
position: serviceData['position'];
|
|
46
|
+
inMotion: serviceData['inMotion'];
|
|
47
|
+
|
|
48
|
+
// Target
|
|
49
|
+
setNewTarget!: boolean;
|
|
50
|
+
setNewTargetTimer!: NodeJS.Timeout;
|
|
51
|
+
|
|
52
|
+
//MQTT stuff
|
|
53
|
+
mqttClient: MqttClient | null = null;
|
|
54
|
+
|
|
55
|
+
// Config
|
|
56
|
+
set_minStep!: number;
|
|
57
|
+
updateRate!: number;
|
|
58
|
+
set_minLux!: number;
|
|
59
|
+
set_maxLux!: number;
|
|
60
|
+
scanDuration!: number;
|
|
61
|
+
deviceLogging!: string;
|
|
62
|
+
deviceRefreshRate!: number;
|
|
63
|
+
setCloseMode!: string;
|
|
64
|
+
setOpenMode!: string;
|
|
65
|
+
|
|
66
|
+
// Updates
|
|
67
|
+
blindTiltUpdateInProgress!: boolean;
|
|
68
|
+
doBlindTiltUpdate!: Subject<void>;
|
|
69
|
+
|
|
70
|
+
// Connection
|
|
71
|
+
private readonly BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI');
|
|
72
|
+
private readonly OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI');
|
|
73
|
+
|
|
74
|
+
constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
|
|
75
|
+
// default placeholders
|
|
76
|
+
this.logs(device);
|
|
77
|
+
this.refreshRate(device);
|
|
78
|
+
this.scan(device);
|
|
79
|
+
this.config(device);
|
|
80
|
+
this.setupMqtt(device);
|
|
81
|
+
this.context();
|
|
82
|
+
|
|
83
|
+
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
84
|
+
this.doBlindTiltUpdate = new Subject();
|
|
85
|
+
this.blindTiltUpdateInProgress = false;
|
|
86
|
+
this.setNewTarget = false;
|
|
87
|
+
|
|
88
|
+
// Retrieve initial values and updateHomekit
|
|
89
|
+
this.refreshStatus();
|
|
90
|
+
|
|
91
|
+
// set accessory information
|
|
92
|
+
accessory
|
|
93
|
+
.getService(this.platform.Service.AccessoryInformation)!
|
|
94
|
+
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
|
|
95
|
+
.setCharacteristic(this.platform.Characteristic.Model, 'W2701600')
|
|
96
|
+
.setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
|
|
97
|
+
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision(accessory, device))
|
|
98
|
+
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
99
|
+
.updateValue(this.FirmwareRevision(accessory, device));
|
|
100
|
+
|
|
101
|
+
// get the WindowCovering service if it exists, otherwise create a new WindowCovering service
|
|
102
|
+
// you can create multiple services for each accessory
|
|
103
|
+
(this.windowCoveringService =
|
|
104
|
+
accessory.getService(this.platform.Service.WindowCovering) || accessory.addService(this.platform.Service.WindowCovering)),
|
|
105
|
+
`${device.deviceName} ${device.deviceType}`;
|
|
106
|
+
|
|
107
|
+
// To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
|
|
108
|
+
// when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
|
|
109
|
+
// accessory.getService('NAME') ?? accessory.addService(this.platform.Service.WindowCovering, 'NAME', 'USER_DEFINED_SUBTYPE');
|
|
110
|
+
|
|
111
|
+
// set the service name, this is what is displayed as the default name on the Home app
|
|
112
|
+
// in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
|
|
113
|
+
this.windowCoveringService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
|
|
114
|
+
if (!this.windowCoveringService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
|
|
115
|
+
this.windowCoveringService.addCharacteristic(this.platform.Characteristic.ConfiguredName, accessory.displayName);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// each service must implement at-minimum the "required characteristics" for the given service type
|
|
119
|
+
// see https://developers.homebridge.io/#/service/WindowCovering
|
|
120
|
+
|
|
121
|
+
// create handlers for required characteristics
|
|
122
|
+
this.windowCoveringService.setCharacteristic(this.platform.Characteristic.PositionState, this.PositionState);
|
|
123
|
+
|
|
124
|
+
this.windowCoveringService
|
|
125
|
+
.getCharacteristic(this.platform.Characteristic.CurrentPosition)
|
|
126
|
+
.setProps({
|
|
127
|
+
minStep: this.minStep(device),
|
|
128
|
+
minValue: 0,
|
|
129
|
+
maxValue: 100,
|
|
130
|
+
validValueRanges: [0, 100],
|
|
131
|
+
})
|
|
132
|
+
.onGet(() => {
|
|
133
|
+
return this.CurrentPosition;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
this.windowCoveringService
|
|
137
|
+
.getCharacteristic(this.platform.Characteristic.TargetPosition)
|
|
138
|
+
.setProps({
|
|
139
|
+
minStep: this.minStep(device),
|
|
140
|
+
validValueRanges: [0, 100],
|
|
141
|
+
})
|
|
142
|
+
.onSet(this.TargetPositionSet.bind(this));
|
|
143
|
+
|
|
144
|
+
this.windowCoveringService
|
|
145
|
+
.getCharacteristic(this.platform.Characteristic.CurrentHorizontalTiltAngle)
|
|
146
|
+
.setProps({
|
|
147
|
+
minStep: this.minStep(device),
|
|
148
|
+
minValue: -90,
|
|
149
|
+
maxValue: 90,
|
|
150
|
+
validValueRanges: [-90, 90],
|
|
151
|
+
})
|
|
152
|
+
.onGet(() => {
|
|
153
|
+
return this.CurrentHorizontalTiltAngle;
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
this.windowCoveringService
|
|
157
|
+
.getCharacteristic(this.platform.Characteristic.TargetHorizontalTiltAngle)
|
|
158
|
+
.setProps({
|
|
159
|
+
minStep: this.minStep(device),
|
|
160
|
+
minValue: -90,
|
|
161
|
+
maxValue: 90,
|
|
162
|
+
validValueRanges: [-90, 90],
|
|
163
|
+
})
|
|
164
|
+
.onSet(this.TargetHorizontalTiltAngleSet.bind(this));
|
|
165
|
+
|
|
166
|
+
// Battery Service
|
|
167
|
+
if (!this.BLE) {
|
|
168
|
+
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
|
|
169
|
+
this.batteryService = this.accessory.getService(this.platform.Service.Battery);
|
|
170
|
+
accessory.removeService(this.batteryService!);
|
|
171
|
+
} else if (this.BLE && !this.batteryService) {
|
|
172
|
+
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
|
|
173
|
+
(this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
|
|
174
|
+
`${accessory.displayName} Battery`;
|
|
175
|
+
|
|
176
|
+
this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
|
|
177
|
+
if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
|
|
178
|
+
this.batteryService.addCharacteristic(this.platform.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Battery Service Not Added`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Update Homekit
|
|
185
|
+
this.updateHomeKitCharacteristics();
|
|
186
|
+
|
|
187
|
+
// Start an update interval
|
|
188
|
+
interval(this.deviceRefreshRate * 1000)
|
|
189
|
+
.pipe(skipWhile(() => this.blindTiltUpdateInProgress))
|
|
190
|
+
.subscribe(async () => {
|
|
191
|
+
await this.refreshStatus();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// update slide progress
|
|
195
|
+
interval(this.updateRate * 1000)
|
|
196
|
+
//.pipe(skipWhile(() => this.blindTiltUpdateInProgress))
|
|
197
|
+
.subscribe(async () => {
|
|
198
|
+
if (this.PositionState === this.platform.Characteristic.PositionState.STOPPED) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Refresh Status When Moving, PositionState: ${this.PositionState}`);
|
|
202
|
+
await this.refreshStatus();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Watch for BlindTilt change events
|
|
206
|
+
// We put in a debounce of 100ms so we don't make duplicate calls
|
|
207
|
+
this.doBlindTiltUpdate
|
|
208
|
+
.pipe(
|
|
209
|
+
tap(() => {
|
|
210
|
+
this.blindTiltUpdateInProgress = true;
|
|
211
|
+
}),
|
|
212
|
+
debounceTime(this.platform.config.options!.pushRate! * 1000),
|
|
213
|
+
)
|
|
214
|
+
.subscribe(async () => {
|
|
215
|
+
try {
|
|
216
|
+
await this.pushChanges();
|
|
217
|
+
} catch (e: any) {
|
|
218
|
+
this.apiError(e);
|
|
219
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
|
|
220
|
+
+ ` Error Message: ${superStringify(e.message)}`);
|
|
221
|
+
}
|
|
222
|
+
this.blindTiltUpdateInProgress = false;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Parse the device status from the SwitchBot api
|
|
228
|
+
*/
|
|
229
|
+
async parseStatus(): Promise<void> {
|
|
230
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
231
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
232
|
+
} else if (this.BLE) {
|
|
233
|
+
await this.BLEparseStatus();
|
|
234
|
+
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
235
|
+
await this.openAPIparseStatus();
|
|
236
|
+
} else {
|
|
237
|
+
await this.offlineOff();
|
|
238
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
239
|
+
+ ` ${this.device.connectionType}, parseStatus will not happen.`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async BLEparseStatus(): Promise<void> {
|
|
244
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
245
|
+
// CurrentPosition
|
|
246
|
+
this.CurrentPosition = 100 - Number(this.position);
|
|
247
|
+
await this.setMinMax();
|
|
248
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition ${this.CurrentPosition}`);
|
|
249
|
+
if (this.setNewTarget) {
|
|
250
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Checking Status ...`);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (this.setNewTarget && this.inMotion) {
|
|
254
|
+
await this.setMinMax();
|
|
255
|
+
if (this.TargetPosition > this.CurrentPosition) {
|
|
256
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition}`);
|
|
257
|
+
this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
|
|
258
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
259
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} INCREASING PositionState: ${this.PositionState}`);
|
|
260
|
+
} else if (this.TargetPosition < this.CurrentPosition) {
|
|
261
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Opening, CurrentPosition: ${this.CurrentPosition}`);
|
|
262
|
+
this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
|
|
263
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
264
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} DECREASING PositionState: ${this.PositionState}`);
|
|
265
|
+
} else {
|
|
266
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} Standby, CurrentPosition: ${this.CurrentPosition}`);
|
|
267
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
268
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
269
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} STOPPED PositionState: ${this.PositionState}`);
|
|
270
|
+
}
|
|
271
|
+
} else {
|
|
272
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Standby, CurrentPosition: ${this.CurrentPosition}`);
|
|
273
|
+
this.TargetPosition = this.CurrentPosition;
|
|
274
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
275
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Stopped`);
|
|
276
|
+
}
|
|
277
|
+
this.debugLog(
|
|
278
|
+
`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition},` +
|
|
279
|
+
` TargetPosition: ${this.TargetPosition}, PositionState: ${this.PositionState},`,
|
|
280
|
+
);
|
|
281
|
+
// CurrentHorizontalTiltAngle
|
|
282
|
+
this.CurrentHorizontalTiltAngle = 100 - Number(this.position);
|
|
283
|
+
await this.setMinMax();
|
|
284
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentHorizontalTiltAngle ${this.CurrentHorizontalTiltAngle}`);
|
|
285
|
+
if (this.setNewTarget) {
|
|
286
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Checking Status ...`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (this.setNewTarget && this.inMotion) {
|
|
290
|
+
await this.setMinMax();
|
|
291
|
+
if (this.TargetHorizontalTiltAngle > this.CurrentHorizontalTiltAngle) {
|
|
292
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing,`+
|
|
293
|
+
` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
294
|
+
this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
|
|
295
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
296
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentHorizontalTiltAngle} INCREASING PositionState: ${this.PositionState}`);
|
|
297
|
+
} else if (this.TargetHorizontalTiltAngle < this.CurrentHorizontalTiltAngle) {
|
|
298
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Opening,`+
|
|
299
|
+
` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
300
|
+
this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
|
|
301
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
302
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentHorizontalTiltAngle} DECREASING PositionState: ${this.PositionState}`);
|
|
303
|
+
} else {
|
|
304
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentHorizontalTiltAngle} Standby,`+
|
|
305
|
+
` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
306
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
307
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
308
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentHorizontalTiltAngle} STOPPED PositionState: ${this.PositionState}`);
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Standby,`+
|
|
312
|
+
` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
313
|
+
this.TargetHorizontalTiltAngle = this.CurrentHorizontalTiltAngle;
|
|
314
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
315
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Stopped`);
|
|
316
|
+
}
|
|
317
|
+
this.debugLog(
|
|
318
|
+
`${this.device.deviceType}: ${this.accessory.displayName} CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle},` +
|
|
319
|
+
` TargetHorizontalTiltAngle: ${this.TargetHorizontalTiltAngle}, PositionState: ${this.PositionState},`,
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
// Battery
|
|
323
|
+
this.BatteryLevel = Number(this.battery);
|
|
324
|
+
if (this.BatteryLevel < 10) {
|
|
325
|
+
this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
326
|
+
} else {
|
|
327
|
+
this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
328
|
+
}
|
|
329
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
|
|
330
|
+
+` StatusLowBattery: ${this.StatusLowBattery}`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async openAPIparseStatus(): Promise<void> {
|
|
334
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
335
|
+
// CurrentPosition
|
|
336
|
+
this.CurrentPosition = 100 - Number(this.slidePosition);
|
|
337
|
+
await this.setMinMax();
|
|
338
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition}`);
|
|
339
|
+
if (this.setNewTarget) {
|
|
340
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Checking Status ...`);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (this.setNewTarget && this.moving) {
|
|
344
|
+
await this.setMinMax();
|
|
345
|
+
if (this.TargetPosition > this.CurrentPosition) {
|
|
346
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing, CurrentPosition: ${this.CurrentPosition} `);
|
|
347
|
+
this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
|
|
348
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
349
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} INCREASING PositionState: ${this.PositionState}`);
|
|
350
|
+
} else if (this.TargetPosition < this.CurrentPosition) {
|
|
351
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Opening, CurrentPosition: ${this.CurrentPosition} `);
|
|
352
|
+
this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
|
|
353
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
354
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} DECREASING PositionState: ${this.PositionState}`);
|
|
355
|
+
} else {
|
|
356
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} Standby, CurrentPosition: ${this.CurrentPosition}`);
|
|
357
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
358
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
359
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentPosition} STOPPED PositionState: ${this.PositionState}`);
|
|
360
|
+
}
|
|
361
|
+
} else {
|
|
362
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Standby, CurrentPosition: ${this.CurrentPosition}`);
|
|
363
|
+
this.TargetPosition = this.CurrentPosition;
|
|
364
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
365
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Stopped`);
|
|
366
|
+
}
|
|
367
|
+
this.debugLog(
|
|
368
|
+
`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition},` +
|
|
369
|
+
` TargetPosition: ${this.TargetPosition}, PositionState: ${this.PositionState},`,
|
|
370
|
+
);
|
|
371
|
+
// CurrentHorizontalTiltAngle
|
|
372
|
+
this.CurrentHorizontalTiltAngle = 100 - Number(this.slidePosition);
|
|
373
|
+
await this.setMinMax();
|
|
374
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
375
|
+
if (this.setNewTarget) {
|
|
376
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Checking Status ...`);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (this.setNewTarget && this.moving) {
|
|
380
|
+
await this.setMinMax();
|
|
381
|
+
if (this.TargetHorizontalTiltAngle > this.CurrentHorizontalTiltAngle) {
|
|
382
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Closing,`
|
|
383
|
+
+ ` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle} `);
|
|
384
|
+
this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
|
|
385
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
386
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentHorizontalTiltAngle} INCREASING PositionState: ${this.PositionState}`);
|
|
387
|
+
} else if (this.TargetHorizontalTiltAngle < this.CurrentHorizontalTiltAngle) {
|
|
388
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Opening,`
|
|
389
|
+
+ ` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle} `);
|
|
390
|
+
this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
|
|
391
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
392
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentHorizontalTiltAngle} DECREASING PositionState: ${this.PositionState}`);
|
|
393
|
+
} else {
|
|
394
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentHorizontalTiltAngle} Standby,`
|
|
395
|
+
+ ` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
396
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
397
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
398
|
+
this.debugLog(`${this.device.deviceType}: ${this.CurrentHorizontalTiltAngle} STOPPED PositionState: ${this.PositionState}`);
|
|
399
|
+
}
|
|
400
|
+
} else {
|
|
401
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Standby,`
|
|
402
|
+
+ ` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
403
|
+
this.TargetPosition = this.CurrentHorizontalTiltAngle;
|
|
404
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
405
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Stopped`);
|
|
406
|
+
}
|
|
407
|
+
this.debugLog(
|
|
408
|
+
`${this.device.deviceType}: ${this.accessory.displayName} CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle},` +
|
|
409
|
+
` TargetPosition: ${this.TargetPosition}, PositionState: ${this.PositionState},`,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async refreshStatus(): Promise<void> {
|
|
414
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
415
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
416
|
+
} else if (this.BLE) {
|
|
417
|
+
await this.BLERefreshStatus();
|
|
418
|
+
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
419
|
+
await this.openAPIRefreshStatus();
|
|
420
|
+
} else {
|
|
421
|
+
await this.offlineOff();
|
|
422
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
423
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
async BLERefreshStatus(): Promise<void> {
|
|
428
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
429
|
+
const switchbot = await this.platform.connectBLE();
|
|
430
|
+
// Convert to BLE Address
|
|
431
|
+
this.device.bleMac = this.device
|
|
432
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
433
|
+
.join(':')
|
|
434
|
+
.toLowerCase();
|
|
435
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
436
|
+
this.getCustomBLEAddress(switchbot);
|
|
437
|
+
// Start to monitor advertisement packets
|
|
438
|
+
if (switchbot !== false) {
|
|
439
|
+
switchbot
|
|
440
|
+
.startScan({
|
|
441
|
+
model: 'c',
|
|
442
|
+
id: this.device.bleMac,
|
|
443
|
+
})
|
|
444
|
+
.then(async () => {
|
|
445
|
+
// Set an event hander
|
|
446
|
+
switchbot.onadvertisement = async (ad: any) => {
|
|
447
|
+
this.address = ad.address;
|
|
448
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
|
|
449
|
+
+ ` BLE Address Found: ${this.address}`);
|
|
450
|
+
this.serviceData = ad.serviceData;
|
|
451
|
+
this.calibration = ad.serviceData.calibration;
|
|
452
|
+
this.battery = ad.serviceData.battery;
|
|
453
|
+
this.inMotion = ad.serviceData.inMotion;
|
|
454
|
+
this.position = ad.serviceData.position;
|
|
455
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
|
|
456
|
+
this.debugLog(
|
|
457
|
+
`${this.device.deviceType}: ${this.accessory.displayName} calibration: ${ad.serviceData.calibration}, ` +
|
|
458
|
+
`position: ${ad.serviceData.position}, inMotion: ${ad.serviceData.inMotion}, battery: ${ad.serviceData.battery}`);
|
|
459
|
+
|
|
460
|
+
if (this.serviceData) {
|
|
461
|
+
this.connected = true;
|
|
462
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
463
|
+
await this.stopScanning(switchbot);
|
|
464
|
+
} else {
|
|
465
|
+
this.connected = false;
|
|
466
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
// Wait
|
|
470
|
+
return await switchbot.wait(this.scanDuration * 1000);
|
|
471
|
+
})
|
|
472
|
+
.then(async () => {
|
|
473
|
+
// Stop to monitor
|
|
474
|
+
await this.stopScanning(switchbot);
|
|
475
|
+
})
|
|
476
|
+
.catch(async (e: any) => {
|
|
477
|
+
this.apiError(e);
|
|
478
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
|
|
479
|
+
+ ` Connection, Error Message: ${superStringify(e.message)}`);
|
|
480
|
+
await this.BLERefreshConnection(switchbot);
|
|
481
|
+
});
|
|
482
|
+
} else {
|
|
483
|
+
await this.BLERefreshConnection(switchbot);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
async openAPIRefreshStatus(): Promise<void> {
|
|
488
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
489
|
+
try {
|
|
490
|
+
const t = Date.now();
|
|
491
|
+
const nonce = 'requestID';
|
|
492
|
+
const data = this.platform.config.credentials?.token + t + nonce;
|
|
493
|
+
const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
|
|
494
|
+
const sign = signTerm.toString('base64');
|
|
495
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
496
|
+
const options = {
|
|
497
|
+
hostname: HostDomain,
|
|
498
|
+
port: 443,
|
|
499
|
+
path: `${DevicePath}/${this.device.deviceId}/status`,
|
|
500
|
+
method: 'GET',
|
|
501
|
+
headers: {
|
|
502
|
+
Authorization: this.platform.config.credentials?.token,
|
|
503
|
+
sign: sign,
|
|
504
|
+
nonce: nonce,
|
|
505
|
+
t: t,
|
|
506
|
+
'Content-Type': 'application/json',
|
|
507
|
+
},
|
|
508
|
+
};
|
|
509
|
+
const req = https.request(options, (res) => {
|
|
510
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
|
|
511
|
+
let rawData = '';
|
|
512
|
+
res.on('data', (d) => {
|
|
513
|
+
rawData += d;
|
|
514
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
515
|
+
});
|
|
516
|
+
res.on('end', () => {
|
|
517
|
+
try {
|
|
518
|
+
this.deviceStatus = JSON.parse(rawData);
|
|
519
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${superStringify(this.deviceStatus)}`);
|
|
520
|
+
this.slidePosition = this.deviceStatus.body.slidePosition;
|
|
521
|
+
this.moving = this.deviceStatus.body.moving;
|
|
522
|
+
this.brightness = this.deviceStatus.body.brightness;
|
|
523
|
+
this.openAPIparseStatus();
|
|
524
|
+
this.updateHomeKitCharacteristics();
|
|
525
|
+
} catch (e: any) {
|
|
526
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
req.on('error', (e: any) => {
|
|
531
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
532
|
+
});
|
|
533
|
+
req.end();
|
|
534
|
+
} catch (e: any) {
|
|
535
|
+
this.apiError(e);
|
|
536
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
537
|
+
+ ` Connection, Error Message: ${superStringify(e.message)}`);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
async pushChanges(): Promise<void> {
|
|
542
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
543
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
|
|
544
|
+
} else if (this.BLE) {
|
|
545
|
+
await this.BLEpushChanges();
|
|
546
|
+
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
547
|
+
await this.openAPIpushChanges();
|
|
548
|
+
} else {
|
|
549
|
+
await this.offlineOff();
|
|
550
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
551
|
+
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
552
|
+
}
|
|
553
|
+
// Refresh the status from the API
|
|
554
|
+
interval(15000)
|
|
555
|
+
.pipe(skipWhile(() => this.blindTiltUpdateInProgress))
|
|
556
|
+
.pipe(take(1))
|
|
557
|
+
.subscribe(async () => {
|
|
558
|
+
await this.refreshStatus();
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
async BLEpushChanges(): Promise<void> {
|
|
563
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
|
|
564
|
+
if (this.TargetPosition !== this.CurrentPosition) {
|
|
565
|
+
const switchbot = await this.platform.connectBLE();
|
|
566
|
+
// Convert to BLE Address
|
|
567
|
+
this.device.bleMac = this.device
|
|
568
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
569
|
+
.join(':')
|
|
570
|
+
.toLowerCase();
|
|
571
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
572
|
+
this.SilentPerformance();
|
|
573
|
+
const adjustedMode = this.setPositionMode || null;
|
|
574
|
+
if (adjustedMode === null) {
|
|
575
|
+
this.Mode = 'Default Mode';
|
|
576
|
+
}
|
|
577
|
+
this.debugLog(`${this.accessory.displayName} Mode: ${this.Mode}`);
|
|
578
|
+
if (switchbot !== false) {
|
|
579
|
+
switchbot
|
|
580
|
+
.discover({ model: 'c', quick: true, id: this.device.bleMac })
|
|
581
|
+
.then(async (device_list: any) => {
|
|
582
|
+
this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
|
|
583
|
+
return await this.retry({
|
|
584
|
+
max: await this.maxRetry(),
|
|
585
|
+
fn: () => {
|
|
586
|
+
return device_list[0].runToPos(100 - Number(this.TargetPosition), adjustedMode);
|
|
587
|
+
},
|
|
588
|
+
});
|
|
589
|
+
})
|
|
590
|
+
.then(() => {
|
|
591
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
592
|
+
})
|
|
593
|
+
.catch(async (e: any) => {
|
|
594
|
+
this.apiError(e);
|
|
595
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
596
|
+
+ ` Connection, Error Message: ${superStringify(e.message)}`);
|
|
597
|
+
await this.BLEPushConnection();
|
|
598
|
+
});
|
|
599
|
+
} else {
|
|
600
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
601
|
+
await this.BLEPushConnection();
|
|
602
|
+
}
|
|
603
|
+
} else {
|
|
604
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges, CurrentPosition & TargetPosition Are the Same.` +
|
|
605
|
+
` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`,
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
|
|
611
|
+
return fn().catch(async (err: any) => {
|
|
612
|
+
if (max === 0) {
|
|
613
|
+
throw err;
|
|
614
|
+
}
|
|
615
|
+
this.infoLog(err);
|
|
616
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
|
|
617
|
+
await sleep(1000);
|
|
618
|
+
return this.retry({ max: max - 1, fn });
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
async maxRetry(): Promise<number> {
|
|
623
|
+
let maxRetry: number;
|
|
624
|
+
if (this.device.maxRetry) {
|
|
625
|
+
maxRetry = this.device.maxRetry;
|
|
626
|
+
} else {
|
|
627
|
+
maxRetry = 5;
|
|
628
|
+
}
|
|
629
|
+
return maxRetry;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
async openAPIpushChanges(): Promise<void> {
|
|
633
|
+
try {
|
|
634
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI pushChanges`);
|
|
635
|
+
if ((this.TargetPosition !== this.CurrentPosition) || this.device.disableCaching) {
|
|
636
|
+
// Make Push On request to the API
|
|
637
|
+
const t = Date.now();
|
|
638
|
+
const nonce = 'requestID';
|
|
639
|
+
const data = this.platform.config.credentials?.token + t + nonce;
|
|
640
|
+
const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
|
|
641
|
+
.update(Buffer.from(data, 'utf-8'))
|
|
642
|
+
.digest();
|
|
643
|
+
const sign = signTerm.toString('base64');
|
|
644
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
645
|
+
this.debugLog(`Pushing ${this.TargetPosition}`);
|
|
646
|
+
const adjustedTargetPosition = 100 - Number(this.TargetPosition);
|
|
647
|
+
if (this.TargetPosition > 50) {
|
|
648
|
+
this.setPositionMode = this.device.curtain?.setOpenMode;
|
|
649
|
+
} else {
|
|
650
|
+
this.setPositionMode = this.device.curtain?.setCloseMode;
|
|
651
|
+
}
|
|
652
|
+
if (this.setPositionMode === '1') {
|
|
653
|
+
this.Mode = 'Silent Mode';
|
|
654
|
+
} else if (this.setPositionMode === '0') {
|
|
655
|
+
this.Mode = 'Performance Mode';
|
|
656
|
+
} else {
|
|
657
|
+
this.Mode = 'Default Mode';
|
|
658
|
+
}
|
|
659
|
+
this.debugLog(`${this.accessory.displayName} Mode: ${this.Mode}`);
|
|
660
|
+
const adjustedMode = this.setPositionMode || 'ff';
|
|
661
|
+
const body = superStringify({
|
|
662
|
+
'command': 'setPosition',
|
|
663
|
+
'parameter': `0,${adjustedMode},${adjustedTargetPosition}`,
|
|
664
|
+
'commandType': 'command',
|
|
665
|
+
});
|
|
666
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
|
|
667
|
+
const options = {
|
|
668
|
+
hostname: HostDomain,
|
|
669
|
+
port: 443,
|
|
670
|
+
path: `${DevicePath}/${this.device.deviceId}/commands`,
|
|
671
|
+
method: 'POST',
|
|
672
|
+
headers: {
|
|
673
|
+
'Authorization': this.platform.config.credentials?.token,
|
|
674
|
+
'sign': sign,
|
|
675
|
+
'nonce': nonce,
|
|
676
|
+
't': t,
|
|
677
|
+
'Content-Type': 'application/json',
|
|
678
|
+
'Content-Length': body.length,
|
|
679
|
+
},
|
|
680
|
+
};
|
|
681
|
+
const req = https.request(options, res => {
|
|
682
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
|
|
683
|
+
this.statusCode({ res });
|
|
684
|
+
res.on('data', d => {
|
|
685
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
|
+
req.on('error', (e: any) => {
|
|
689
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
690
|
+
});
|
|
691
|
+
req.write(body);
|
|
692
|
+
req.end();
|
|
693
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${superStringify(req)}`);
|
|
694
|
+
} else {
|
|
695
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No OpenAPI Changes, CurrentPosition & TargetPosition Are the Same.`
|
|
696
|
+
+` CurrentPosition: ${this.CurrentPosition}, TargetPosition ${this.TargetPosition}`,
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
} catch (e: any) {
|
|
700
|
+
this.apiError(e);
|
|
701
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
702
|
+
+ ` Connection, Error Message: ${superStringify(e.message)}`,
|
|
703
|
+
);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Handle requests to set the value of the "Target Position" characteristic
|
|
709
|
+
*/
|
|
710
|
+
async TargetPositionSet(value: CharacteristicValue): Promise<void> {
|
|
711
|
+
if (this.TargetPosition === this.accessory.context.TargetPosition) {
|
|
712
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set TargetPosition: ${value}`);
|
|
713
|
+
} else {
|
|
714
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetPosition: ${value}`);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
this.TargetPosition = value;
|
|
718
|
+
if (this.device.mqttURL) {
|
|
719
|
+
this.mqttPublish('TargetPosition', this.TargetPosition);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
await this.setMinMax();
|
|
723
|
+
if (value > this.CurrentPosition) {
|
|
724
|
+
this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
|
|
725
|
+
this.setNewTarget = true;
|
|
726
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`);
|
|
727
|
+
} else if (value < this.CurrentPosition) {
|
|
728
|
+
this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
|
|
729
|
+
this.setNewTarget = true;
|
|
730
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`);
|
|
731
|
+
} else {
|
|
732
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
733
|
+
this.setNewTarget = false;
|
|
734
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${value}, CurrentPosition: ${this.CurrentPosition}`);
|
|
735
|
+
}
|
|
736
|
+
this.windowCoveringService.setCharacteristic(this.platform.Characteristic.PositionState, this.PositionState);
|
|
737
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* If Blind Tilt movement time is short, the moving flag from backend is always false.
|
|
741
|
+
* The minimum time depends on the network control latency.
|
|
742
|
+
*/
|
|
743
|
+
clearTimeout(this.setNewTargetTimer);
|
|
744
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateRate: ${this.updateRate}`);
|
|
745
|
+
if (this.setNewTarget) {
|
|
746
|
+
this.setNewTargetTimer = setTimeout(() => {
|
|
747
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} setNewTarget ${this.setNewTarget} timeout`);
|
|
748
|
+
this.setNewTarget = false;
|
|
749
|
+
}, this.updateRate * 1000);
|
|
750
|
+
}
|
|
751
|
+
this.doBlindTiltUpdate.next();
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Handle requests to set the value of the "Target Position" characteristic
|
|
756
|
+
*/
|
|
757
|
+
async TargetHorizontalTiltAngleSet(value: CharacteristicValue): Promise<void> {
|
|
758
|
+
if (this.TargetHorizontalTiltAngle === this.accessory.context.TargetHorizontalTiltAngle) {
|
|
759
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set TargetHorizontalTiltAngle: ${value}`);
|
|
760
|
+
} else {
|
|
761
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHorizontalTiltAngle: ${value}`);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
this.TargetHorizontalTiltAngle = value;
|
|
765
|
+
if (this.device.mqttURL) {
|
|
766
|
+
this.mqttPublish('TargetHorizontalTiltAngle', this.TargetHorizontalTiltAngle);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
await this.setMinMax();
|
|
770
|
+
if (value > this.CurrentHorizontalTiltAngle) {
|
|
771
|
+
this.PositionState = this.platform.Characteristic.PositionState.INCREASING;
|
|
772
|
+
this.setNewTarget = true;
|
|
773
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${value},`
|
|
774
|
+
+ ` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
775
|
+
} else if (value < this.CurrentHorizontalTiltAngle) {
|
|
776
|
+
this.PositionState = this.platform.Characteristic.PositionState.DECREASING;
|
|
777
|
+
this.setNewTarget = true;
|
|
778
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${value},`
|
|
779
|
+
+ ` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
780
|
+
} else {
|
|
781
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
782
|
+
this.setNewTarget = false;
|
|
783
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} value: ${value},`
|
|
784
|
+
+ ` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
785
|
+
}
|
|
786
|
+
this.windowCoveringService.setCharacteristic(this.platform.Characteristic.PositionState, this.PositionState);
|
|
787
|
+
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState).updateValue(this.PositionState);
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* If Blind Tilt movement time is short, the moving flag from backend is always false.
|
|
791
|
+
* The minimum time depends on the network control latency.
|
|
792
|
+
*/
|
|
793
|
+
clearTimeout(this.setNewTargetTimer);
|
|
794
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateRate: ${this.updateRate}`);
|
|
795
|
+
if (this.setNewTarget) {
|
|
796
|
+
this.setNewTargetTimer = setTimeout(() => {
|
|
797
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} setNewTarget ${this.setNewTarget} timeout`);
|
|
798
|
+
this.setNewTarget = false;
|
|
799
|
+
}, this.updateRate * 1000);
|
|
800
|
+
}
|
|
801
|
+
this.doBlindTiltUpdate.next();
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
async updateHomeKitCharacteristics(): Promise<void> {
|
|
805
|
+
await this.setMinMax();
|
|
806
|
+
if (this.CurrentPosition === undefined || Number.isNaN(this.CurrentPosition)) {
|
|
807
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentPosition: ${this.CurrentPosition}`);
|
|
808
|
+
} else {
|
|
809
|
+
if (this.device.mqttURL) {
|
|
810
|
+
this.mqttPublish('CurrentPosition', this.CurrentPosition);
|
|
811
|
+
}
|
|
812
|
+
this.accessory.context.CurrentPosition = this.CurrentPosition;
|
|
813
|
+
this.windowCoveringService.updateCharacteristic(this.platform.Characteristic.CurrentPosition, Number(this.CurrentPosition));
|
|
814
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentPosition: ${this.CurrentPosition}`);
|
|
815
|
+
}
|
|
816
|
+
if (this.PositionState === undefined) {
|
|
817
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} PositionState: ${this.PositionState}`);
|
|
818
|
+
} else {
|
|
819
|
+
if (this.device.mqttURL) {
|
|
820
|
+
this.mqttPublish('PositionState', this.PositionState);
|
|
821
|
+
}
|
|
822
|
+
this.accessory.context.PositionState = this.PositionState;
|
|
823
|
+
this.windowCoveringService.updateCharacteristic(this.platform.Characteristic.PositionState, Number(this.PositionState));
|
|
824
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic PositionState: ${this.PositionState}`);
|
|
825
|
+
}
|
|
826
|
+
if (this.TargetPosition === undefined || Number.isNaN(this.TargetPosition)) {
|
|
827
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetPosition: ${this.TargetPosition}`);
|
|
828
|
+
} else {
|
|
829
|
+
if (this.device.mqttURL) {
|
|
830
|
+
this.mqttPublish('TargetPosition', this.TargetPosition);
|
|
831
|
+
}
|
|
832
|
+
this.accessory.context.TargetPosition = this.TargetPosition;
|
|
833
|
+
this.windowCoveringService.updateCharacteristic(this.platform.Characteristic.TargetPosition, Number(this.TargetPosition));
|
|
834
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic TargetPosition: ${this.TargetPosition}`);
|
|
835
|
+
}
|
|
836
|
+
if (this.CurrentHorizontalTiltAngle === undefined || Number.isNaN(this.CurrentHorizontalTiltAngle)) {
|
|
837
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
838
|
+
} else {
|
|
839
|
+
if (this.device.mqttURL) {
|
|
840
|
+
this.mqttPublish('CurrentHorizontalTiltAngle', this.CurrentHorizontalTiltAngle);
|
|
841
|
+
}
|
|
842
|
+
this.accessory.context.CurrentHorizontalTiltAngle = this.CurrentHorizontalTiltAngle;
|
|
843
|
+
this.windowCoveringService.updateCharacteristic(this.platform.Characteristic.CurrentHorizontalTiltAngle, Number(this.CurrentPosition));
|
|
844
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
845
|
+
+` CurrentHorizontalTiltAngle: ${this.CurrentHorizontalTiltAngle}`);
|
|
846
|
+
}
|
|
847
|
+
if (this.TargetHorizontalTiltAngle === undefined || Number.isNaN(this.TargetHorizontalTiltAngle)) {
|
|
848
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetHorizontalTiltAngle: ${this.TargetHorizontalTiltAngle}`);
|
|
849
|
+
} else {
|
|
850
|
+
if (this.device.mqttURL) {
|
|
851
|
+
this.mqttPublish('TargetHorizontalTiltAngle', this.TargetHorizontalTiltAngle);
|
|
852
|
+
}
|
|
853
|
+
this.accessory.context.TargetHorizontalTiltAngle = this.TargetHorizontalTiltAngle;
|
|
854
|
+
this.windowCoveringService.updateCharacteristic(this.platform.Characteristic.TargetHorizontalTiltAngle, Number(this.TargetHorizontalTiltAngle));
|
|
855
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
856
|
+
+ ` TargetHorizontalTiltAngle: ${this.TargetHorizontalTiltAngle}`);
|
|
857
|
+
}
|
|
858
|
+
if (this.BLE) {
|
|
859
|
+
if (this.BatteryLevel === undefined) {
|
|
860
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
861
|
+
} else {
|
|
862
|
+
if (this.device.mqttURL) {
|
|
863
|
+
this.mqttPublish('BatteryLevel', this.BatteryLevel);
|
|
864
|
+
}
|
|
865
|
+
this.accessory.context.BatteryLevel = this.BatteryLevel;
|
|
866
|
+
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
|
|
867
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
|
|
868
|
+
}
|
|
869
|
+
if (this.StatusLowBattery === undefined) {
|
|
870
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
|
|
871
|
+
} else {
|
|
872
|
+
if (this.device.mqttURL) {
|
|
873
|
+
this.mqttPublish('StatusLowBattery', this.StatusLowBattery);
|
|
874
|
+
}
|
|
875
|
+
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
|
|
876
|
+
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
|
|
877
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/*
|
|
883
|
+
* Publish MQTT message for topics of
|
|
884
|
+
* 'homebridge-switchbot/blindtilt/xx:xx:xx:xx:xx:xx'
|
|
885
|
+
*/
|
|
886
|
+
mqttPublish(topic: string, message: any) {
|
|
887
|
+
const mac = this.device.deviceId
|
|
888
|
+
?.toLowerCase()
|
|
889
|
+
.match(/[\s\S]{1,2}/g)
|
|
890
|
+
?.join(':');
|
|
891
|
+
const options = this.device.mqttPubOptions || {};
|
|
892
|
+
this.mqttClient?.publish(`homebridge-switchbot/blindtilt/${mac}/${topic}`, `${message}`, options);
|
|
893
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${topic}/${message} options:${superStringify(options)}`);
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
/*
|
|
897
|
+
* Setup MQTT hadler if URL is specifed.
|
|
898
|
+
*/
|
|
899
|
+
async setupMqtt(device: device & devicesConfig): Promise<void> {
|
|
900
|
+
if (device.mqttURL) {
|
|
901
|
+
try {
|
|
902
|
+
this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
|
|
903
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
|
|
904
|
+
this.mqttClient.on('error', (e: Error) => {
|
|
905
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
|
|
906
|
+
});
|
|
907
|
+
} catch (e) {
|
|
908
|
+
this.mqttClient = null;
|
|
909
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
async stopScanning(switchbot: any) {
|
|
915
|
+
await switchbot.stopScan();
|
|
916
|
+
if (this.connected) {
|
|
917
|
+
await this.BLEparseStatus();
|
|
918
|
+
await this.updateHomeKitCharacteristics();
|
|
919
|
+
} else {
|
|
920
|
+
await this.BLERefreshConnection(switchbot);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
async getCustomBLEAddress(switchbot: any) {
|
|
925
|
+
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
926
|
+
(async () => {
|
|
927
|
+
// Start to monitor advertisement packets
|
|
928
|
+
await switchbot.startScan({
|
|
929
|
+
model: 'c',
|
|
930
|
+
});
|
|
931
|
+
// Set an event handler
|
|
932
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
933
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
|
|
934
|
+
};
|
|
935
|
+
await switchbot.wait(10000);
|
|
936
|
+
// Stop to monitor
|
|
937
|
+
switchbot.stopScan();
|
|
938
|
+
})();
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
async BLEPushConnection() {
|
|
943
|
+
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
944
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
|
|
945
|
+
await this.openAPIpushChanges();
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
async BLERefreshConnection(switchbot: any): Promise<void> {
|
|
950
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
951
|
+
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
952
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
|
|
953
|
+
await this.openAPIRefreshStatus();
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
async SilentPerformance() {
|
|
958
|
+
if (this.TargetPosition > 50) {
|
|
959
|
+
if (this.device.curtain?.setOpenMode === '1') {
|
|
960
|
+
this.setPositionMode = 1;
|
|
961
|
+
this.Mode = 'Silent Mode';
|
|
962
|
+
} else {
|
|
963
|
+
this.setPositionMode = 0;
|
|
964
|
+
this.Mode = 'Performance Mode';
|
|
965
|
+
}
|
|
966
|
+
} else {
|
|
967
|
+
if (this.device.curtain?.setCloseMode === '1') {
|
|
968
|
+
this.setPositionMode = 1;
|
|
969
|
+
this.Mode = 'Silent Mode';
|
|
970
|
+
} else {
|
|
971
|
+
this.setPositionMode = 0;
|
|
972
|
+
this.Mode = 'Performance Mode';
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
async setMinMax(): Promise<void> {
|
|
978
|
+
if (this.device.curtain?.set_min) {
|
|
979
|
+
if (this.CurrentPosition <= this.device.curtain?.set_min) {
|
|
980
|
+
this.CurrentPosition = 0;
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
if (this.device.curtain?.set_max) {
|
|
984
|
+
if (this.CurrentPosition >= this.device.curtain?.set_max) {
|
|
985
|
+
this.CurrentPosition = 100;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
minStep(device: device & devicesConfig): number {
|
|
991
|
+
if (device.curtain?.set_minStep) {
|
|
992
|
+
this.set_minStep = device.curtain?.set_minStep;
|
|
993
|
+
} else {
|
|
994
|
+
this.set_minStep = 1;
|
|
995
|
+
}
|
|
996
|
+
return this.set_minStep;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
minLux(): number {
|
|
1000
|
+
if (this.device.curtain?.set_minLux) {
|
|
1001
|
+
this.set_minLux = this.device.curtain?.set_minLux;
|
|
1002
|
+
} else {
|
|
1003
|
+
this.set_minLux = 1;
|
|
1004
|
+
}
|
|
1005
|
+
return this.set_minLux;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
maxLux(): number {
|
|
1009
|
+
if (this.device.curtain?.set_maxLux) {
|
|
1010
|
+
this.set_maxLux = this.device.curtain?.set_maxLux;
|
|
1011
|
+
} else {
|
|
1012
|
+
this.set_maxLux = 6001;
|
|
1013
|
+
}
|
|
1014
|
+
return this.set_maxLux;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
async scan(device: device & devicesConfig): Promise<void> {
|
|
1018
|
+
if (device.scanDuration) {
|
|
1019
|
+
if (this.updateRate > device.scanDuration) {
|
|
1020
|
+
this.scanDuration = this.updateRate;
|
|
1021
|
+
if (this.BLE) {
|
|
1022
|
+
this.warnLog(`${this.device.deviceType}: `
|
|
1023
|
+
+ `${this.accessory.displayName} scanDuration is less then updateRate, overriding scanDuration with updateRate`);
|
|
1024
|
+
}
|
|
1025
|
+
} else {
|
|
1026
|
+
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
1027
|
+
}
|
|
1028
|
+
if (this.BLE) {
|
|
1029
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
1030
|
+
}
|
|
1031
|
+
} else {
|
|
1032
|
+
if (this.updateRate > 1) {
|
|
1033
|
+
this.scanDuration = this.updateRate;
|
|
1034
|
+
if (this.BLE) {
|
|
1035
|
+
this.warnLog(`${this.device.deviceType}: `
|
|
1036
|
+
+ `${this.accessory.displayName} scanDuration is less then updateRate, overriding scanDuration with updateRate`);
|
|
1037
|
+
}
|
|
1038
|
+
} else {
|
|
1039
|
+
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
1040
|
+
}
|
|
1041
|
+
if (this.BLE) {
|
|
1042
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
async statusCode({ res }: { res: IncomingMessage }): Promise<void> {
|
|
1048
|
+
switch (res.statusCode) {
|
|
1049
|
+
case 151:
|
|
1050
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
|
|
1051
|
+
break;
|
|
1052
|
+
case 152:
|
|
1053
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
|
|
1054
|
+
break;
|
|
1055
|
+
case 160:
|
|
1056
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
|
|
1057
|
+
break;
|
|
1058
|
+
case 161:
|
|
1059
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
|
|
1060
|
+
this.offlineOff();
|
|
1061
|
+
break;
|
|
1062
|
+
case 171:
|
|
1063
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
|
|
1064
|
+
this.offlineOff();
|
|
1065
|
+
break;
|
|
1066
|
+
case 190:
|
|
1067
|
+
this.errorLog(
|
|
1068
|
+
`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
1069
|
+
` Or command: ${superStringify(res)} format is invalid`,
|
|
1070
|
+
);
|
|
1071
|
+
break;
|
|
1072
|
+
case 100:
|
|
1073
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
|
|
1074
|
+
break;
|
|
1075
|
+
default:
|
|
1076
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
async offlineOff(): Promise<void> {
|
|
1081
|
+
if (this.device.offline) {
|
|
1082
|
+
await this.context();
|
|
1083
|
+
await this.updateHomeKitCharacteristics();
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
async apiError(e: any): Promise<void> {
|
|
1088
|
+
this.windowCoveringService.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
|
|
1089
|
+
this.windowCoveringService.updateCharacteristic(this.platform.Characteristic.PositionState, e);
|
|
1090
|
+
this.windowCoveringService.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
|
|
1091
|
+
if (this.BLE) {
|
|
1092
|
+
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
|
|
1093
|
+
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
|
|
1094
|
+
}
|
|
1095
|
+
//throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
|
|
1099
|
+
let FirmwareRevision: string;
|
|
1100
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
1101
|
+
+ ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
|
|
1102
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
|
|
1103
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
|
|
1104
|
+
if (accessory.context.FirmwareRevision) {
|
|
1105
|
+
FirmwareRevision = accessory.context.FirmwareRevision;
|
|
1106
|
+
} else if (device.firmware) {
|
|
1107
|
+
FirmwareRevision = device.firmware;
|
|
1108
|
+
} else {
|
|
1109
|
+
FirmwareRevision = this.platform.version;
|
|
1110
|
+
}
|
|
1111
|
+
return FirmwareRevision;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
async context() {
|
|
1115
|
+
if (this.CurrentPosition === undefined) {
|
|
1116
|
+
this.CurrentPosition = 0;
|
|
1117
|
+
} else {
|
|
1118
|
+
this.CurrentPosition = this.accessory.context.CurrentPosition;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
if (this.TargetPosition === undefined) {
|
|
1122
|
+
this.TargetPosition = 0;
|
|
1123
|
+
} else {
|
|
1124
|
+
this.TargetPosition = this.accessory.context.TargetPosition;
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
if (this.CurrentHorizontalTiltAngle === undefined) {
|
|
1128
|
+
this.CurrentHorizontalTiltAngle = 0;
|
|
1129
|
+
} else {
|
|
1130
|
+
this.CurrentHorizontalTiltAngle = this.accessory.context.CurrentHorizontalTiltAngle;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
if (this.TargetHorizontalTiltAngle === undefined) {
|
|
1134
|
+
this.TargetHorizontalTiltAngle = 0;
|
|
1135
|
+
} else {
|
|
1136
|
+
this.TargetHorizontalTiltAngle = this.accessory.context.TargetHorizontalTiltAngle;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
if (this.PositionState === undefined) {
|
|
1140
|
+
this.PositionState = this.platform.Characteristic.PositionState.STOPPED;
|
|
1141
|
+
} else {
|
|
1142
|
+
this.PositionState = this.accessory.context.PositionState;
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
async refreshRate(device: device & devicesConfig): Promise<void> {
|
|
1147
|
+
// refreshRate
|
|
1148
|
+
if (device.refreshRate) {
|
|
1149
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
1150
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1151
|
+
} else if (this.platform.config.options!.refreshRate) {
|
|
1152
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
|
|
1153
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
1154
|
+
}
|
|
1155
|
+
// updateRate
|
|
1156
|
+
if (device?.curtain?.updateRate) {
|
|
1157
|
+
this.updateRate = device?.curtain?.updateRate;
|
|
1158
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Blind Tilt updateRate: ${this.updateRate}`);
|
|
1159
|
+
} else {
|
|
1160
|
+
this.updateRate = 7;
|
|
1161
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default Blind Tilt updateRate: ${this.updateRate}`);
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
async config(device: device & devicesConfig): Promise<void> {
|
|
1166
|
+
let config = {};
|
|
1167
|
+
if (device.curtain) {
|
|
1168
|
+
config = device.curtain;
|
|
1169
|
+
}
|
|
1170
|
+
if (device.connectionType !== undefined) {
|
|
1171
|
+
config['connectionType'] = device.connectionType;
|
|
1172
|
+
}
|
|
1173
|
+
if (device.external !== undefined) {
|
|
1174
|
+
config['external'] = device.external;
|
|
1175
|
+
}
|
|
1176
|
+
if (device.mqttURL !== undefined) {
|
|
1177
|
+
config['mqttURL'] = device.mqttURL;
|
|
1178
|
+
}
|
|
1179
|
+
if (device.logging !== undefined) {
|
|
1180
|
+
config['logging'] = device.logging;
|
|
1181
|
+
}
|
|
1182
|
+
if (device.refreshRate !== undefined) {
|
|
1183
|
+
config['refreshRate'] = device.refreshRate;
|
|
1184
|
+
}
|
|
1185
|
+
if (device.scanDuration !== undefined) {
|
|
1186
|
+
config['scanDuration'] = device.scanDuration;
|
|
1187
|
+
}
|
|
1188
|
+
if (device.maxRetry !== undefined) {
|
|
1189
|
+
config['maxRetry'] = device.maxRetry;
|
|
1190
|
+
}
|
|
1191
|
+
if (Object.entries(config).length !== 0) {
|
|
1192
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${superStringify(config)}`);
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
async logs(device: device & devicesConfig): Promise<void> {
|
|
1197
|
+
if (this.platform.debugMode) {
|
|
1198
|
+
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
1199
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|
|
1200
|
+
} else if (device.logging) {
|
|
1201
|
+
this.deviceLogging = this.accessory.context.logging = device.logging;
|
|
1202
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
|
|
1203
|
+
} else if (this.platform.config.options?.logging) {
|
|
1204
|
+
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
1205
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
|
|
1206
|
+
} else {
|
|
1207
|
+
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
1208
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* Logging for Device
|
|
1214
|
+
*/
|
|
1215
|
+
infoLog(...log: any[]): void {
|
|
1216
|
+
if (this.enablingDeviceLogging()) {
|
|
1217
|
+
this.platform.log.info(String(...log));
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
warnLog(...log: any[]): void {
|
|
1222
|
+
if (this.enablingDeviceLogging()) {
|
|
1223
|
+
this.platform.log.warn(String(...log));
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
debugWarnLog(...log: any[]): void {
|
|
1228
|
+
if (this.enablingDeviceLogging()) {
|
|
1229
|
+
if (this.deviceLogging?.includes('debug')) {
|
|
1230
|
+
this.platform.log.warn('[DEBUG]', String(...log));
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
errorLog(...log: any[]): void {
|
|
1236
|
+
if (this.enablingDeviceLogging()) {
|
|
1237
|
+
this.platform.log.error(String(...log));
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
debugErrorLog(...log: any[]): void {
|
|
1242
|
+
if (this.enablingDeviceLogging()) {
|
|
1243
|
+
if (this.deviceLogging?.includes('debug')) {
|
|
1244
|
+
this.platform.log.error('[DEBUG]', String(...log));
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
debugLog(...log: any[]): void {
|
|
1250
|
+
if (this.enablingDeviceLogging()) {
|
|
1251
|
+
if (this.deviceLogging === 'debug') {
|
|
1252
|
+
this.platform.log.info('[DEBUG]', String(...log));
|
|
1253
|
+
} else {
|
|
1254
|
+
this.platform.log.debug(String(...log));
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
enablingDeviceLogging(): boolean {
|
|
1260
|
+
return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
|
|
1261
|
+
}
|
|
1262
|
+
}
|