homebridge-solaxcloud-api 2.3.2 → 2.4.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/CHANGELOG.md +140 -127
- package/LICENSE +175 -175
- package/config.sample +15 -15
- package/config.schema.json +105 -105
- package/dist/batteryAccessory.d.ts +70 -70
- package/dist/batteryAccessory.d.ts.map +1 -1
- package/dist/batteryAccessory.js +124 -123
- package/dist/batteryAccessory.js.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.js +6 -6
- package/dist/lightSensorAccessory.d.ts +34 -34
- package/dist/lightSensorAccessory.js +58 -58
- package/dist/motionAccessory.d.ts +53 -53
- package/dist/motionAccessory.js +93 -93
- package/dist/outletAccessory.d.ts +100 -100
- package/dist/outletAccessory.js +187 -187
- package/dist/platform.d.ts +62 -56
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +294 -265
- package/dist/platform.js.map +1 -1
- package/dist/platformAccessory.d.ts +70 -70
- package/dist/platformAccessory.js +102 -102
- package/dist/platformInverter.d.ts +180 -176
- package/dist/platformInverter.d.ts.map +1 -1
- package/dist/platformInverter.js +348 -343
- package/dist/platformInverter.js.map +1 -1
- package/dist/platformMeters.d.ts +68 -68
- package/dist/platformMeters.js +2 -2
- package/dist/settings.d.ts +8 -8
- package/dist/settings.js +11 -11
- package/dist/solaxcloudfetch.d.ts +1 -1
- package/dist/solaxcloudfetch.js +21 -21
- package/dist/solaxcloudfetch.js.map +1 -1
- package/dist/statistics.d.ts +36 -36
- package/dist/statistics.js +88 -88
- package/dist/util.d.ts +23 -23
- package/dist/util.js +42 -42
- package/package.json +55 -54
- package/config.json +0 -16
- package/dist/solaxcloudapi.d.ts +0 -163
- package/dist/solaxcloudapi.d.ts.map +0 -1
- package/dist/solaxcloudapi.js +0 -226
- package/dist/solaxcloudapi.js.map +0 -1
- package/test/solaxcloudapi.test.ts +0 -164
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
import { Service, AccessoryPlugin, Logging, CharacteristicGetCallback, API } from 'homebridge';
|
|
2
|
-
import { SolaxPlatformAccessory } from './platformAccessory';
|
|
3
|
-
/**
|
|
4
|
-
* Solax Outlet Accessory.
|
|
5
|
-
* Virtual outlet used to monitor and measure both power and total energy consumption from Solax devices.
|
|
6
|
-
* Outlet use is inferred from a positive power comsumption.
|
|
7
|
-
*/
|
|
8
|
-
export declare class SolaxOutletAccessory extends SolaxPlatformAccessory implements AccessoryPlugin {
|
|
9
|
-
/**
|
|
10
|
-
* Eve Homekit types helper
|
|
11
|
-
* */
|
|
12
|
-
private readonly eve;
|
|
13
|
-
/**
|
|
14
|
-
* Eve Service for fakegato.
|
|
15
|
-
*/
|
|
16
|
-
private readonly eveService;
|
|
17
|
-
/**
|
|
18
|
-
* Outlet service with power meter.
|
|
19
|
-
*/
|
|
20
|
-
private readonly outletService;
|
|
21
|
-
/**
|
|
22
|
-
* Current power reading in Watt.
|
|
23
|
-
*/
|
|
24
|
-
private powerConsumption;
|
|
25
|
-
/**
|
|
26
|
-
* Total energy readings in KWh.
|
|
27
|
-
*/
|
|
28
|
-
private totalEnergyConsumption;
|
|
29
|
-
/**
|
|
30
|
-
* Elgato Eve fake history service for energy and status.
|
|
31
|
-
*/
|
|
32
|
-
private readonly loggingService;
|
|
33
|
-
/**
|
|
34
|
-
* Power series for smoothing operations.
|
|
35
|
-
*/
|
|
36
|
-
private readonly powerSeries;
|
|
37
|
-
/**
|
|
38
|
-
* Last Exponential Moving Average.
|
|
39
|
-
*/
|
|
40
|
-
private lastEMA;
|
|
41
|
-
/**
|
|
42
|
-
* Solax virtual outlet class constructor.
|
|
43
|
-
* @param {Logging} log The platform logging for homebridge.
|
|
44
|
-
* @param {API} api The API for Solax Cloud platform.
|
|
45
|
-
* @param {string} name The accessory name.
|
|
46
|
-
* @param {string} serial "Real world" serial number for this accessory.
|
|
47
|
-
* @param {string} model Accessory model.
|
|
48
|
-
*/
|
|
49
|
-
constructor(log: Logging, api: API, name: string, serial: string, model: string);
|
|
50
|
-
/**
|
|
51
|
-
* Adds new power entry to series array of power values.
|
|
52
|
-
* @param {number} powerConsumption Power value to add (in Watts).
|
|
53
|
-
*/
|
|
54
|
-
private addPowerEntry;
|
|
55
|
-
/**
|
|
56
|
-
* Gets array series with power consumption values.
|
|
57
|
-
* @returns {number[]} Array with power consumption values.
|
|
58
|
-
*/
|
|
59
|
-
getPowerSeries(): number[];
|
|
60
|
-
/**
|
|
61
|
-
* Get outlet power from power series by smoothing data using the desired method and window size.
|
|
62
|
-
* @param {string} method Method to smooth data ('SMA' or 'EMA').
|
|
63
|
-
* @param {number} windowSize Window size for SMA or EMA.
|
|
64
|
-
* @returns {number} Power value after smoothing (rounded to nearest integer number).
|
|
65
|
-
*/
|
|
66
|
-
getSmoothPowerConsumption(method: string, windowSize: number): number;
|
|
67
|
-
/**
|
|
68
|
-
* Gets the current power consumption value for this outlet.
|
|
69
|
-
* @returns {number} Outlet power consumption (Watt).
|
|
70
|
-
*/
|
|
71
|
-
getPowerConsumptionValue(): number;
|
|
72
|
-
/**
|
|
73
|
-
* Gets the current power consumpion.
|
|
74
|
-
*/
|
|
75
|
-
getPowerConsumption(callback: CharacteristicGetCallback): void;
|
|
76
|
-
/**
|
|
77
|
-
* Sets the current power consumption.
|
|
78
|
-
* @param {number} powerConsumption The power consumption to set. */
|
|
79
|
-
setPowerConsumption(powerConsumption: number): void;
|
|
80
|
-
/**
|
|
81
|
-
* Gets the total energy consumption value for this outlet.
|
|
82
|
-
* @returns {number} Outlet total energy consumption (Wh).
|
|
83
|
-
*/
|
|
84
|
-
getTotalEnergyConsumptionValue(): number;
|
|
85
|
-
/**
|
|
86
|
-
* Gets the total energy consumpion.
|
|
87
|
-
*/
|
|
88
|
-
getTotalEnergyConsumption(callback: CharacteristicGetCallback): void;
|
|
89
|
-
/**
|
|
90
|
-
* Sets the total energy consumption.
|
|
91
|
-
* @param {number} totalEnergyConsumption The total energy consumption to set.
|
|
92
|
-
*/
|
|
93
|
-
setTotalEnergyConsumption(totalEnergyConsumption: number): void;
|
|
94
|
-
/**
|
|
95
|
-
* Gets the outlet state Characteristic based on its consumed power.
|
|
96
|
-
* @param {CharacteristicGetCallback} callback The callback.
|
|
97
|
-
*/
|
|
98
|
-
getState(callback: CharacteristicGetCallback): void;
|
|
99
|
-
getServices(): Service[];
|
|
100
|
-
}
|
|
1
|
+
import { Service, AccessoryPlugin, Logging, CharacteristicGetCallback, API } from 'homebridge';
|
|
2
|
+
import { SolaxPlatformAccessory } from './platformAccessory';
|
|
3
|
+
/**
|
|
4
|
+
* Solax Outlet Accessory.
|
|
5
|
+
* Virtual outlet used to monitor and measure both power and total energy consumption from Solax devices.
|
|
6
|
+
* Outlet use is inferred from a positive power comsumption.
|
|
7
|
+
*/
|
|
8
|
+
export declare class SolaxOutletAccessory extends SolaxPlatformAccessory implements AccessoryPlugin {
|
|
9
|
+
/**
|
|
10
|
+
* Eve Homekit types helper
|
|
11
|
+
* */
|
|
12
|
+
private readonly eve;
|
|
13
|
+
/**
|
|
14
|
+
* Eve Service for fakegato.
|
|
15
|
+
*/
|
|
16
|
+
private readonly eveService;
|
|
17
|
+
/**
|
|
18
|
+
* Outlet service with power meter.
|
|
19
|
+
*/
|
|
20
|
+
private readonly outletService;
|
|
21
|
+
/**
|
|
22
|
+
* Current power reading in Watt.
|
|
23
|
+
*/
|
|
24
|
+
private powerConsumption;
|
|
25
|
+
/**
|
|
26
|
+
* Total energy readings in KWh.
|
|
27
|
+
*/
|
|
28
|
+
private totalEnergyConsumption;
|
|
29
|
+
/**
|
|
30
|
+
* Elgato Eve fake history service for energy and status.
|
|
31
|
+
*/
|
|
32
|
+
private readonly loggingService;
|
|
33
|
+
/**
|
|
34
|
+
* Power series for smoothing operations.
|
|
35
|
+
*/
|
|
36
|
+
private readonly powerSeries;
|
|
37
|
+
/**
|
|
38
|
+
* Last Exponential Moving Average.
|
|
39
|
+
*/
|
|
40
|
+
private lastEMA;
|
|
41
|
+
/**
|
|
42
|
+
* Solax virtual outlet class constructor.
|
|
43
|
+
* @param {Logging} log The platform logging for homebridge.
|
|
44
|
+
* @param {API} api The API for Solax Cloud platform.
|
|
45
|
+
* @param {string} name The accessory name.
|
|
46
|
+
* @param {string} serial "Real world" serial number for this accessory.
|
|
47
|
+
* @param {string} model Accessory model.
|
|
48
|
+
*/
|
|
49
|
+
constructor(log: Logging, api: API, name: string, serial: string, model: string);
|
|
50
|
+
/**
|
|
51
|
+
* Adds new power entry to series array of power values.
|
|
52
|
+
* @param {number} powerConsumption Power value to add (in Watts).
|
|
53
|
+
*/
|
|
54
|
+
private addPowerEntry;
|
|
55
|
+
/**
|
|
56
|
+
* Gets array series with power consumption values.
|
|
57
|
+
* @returns {number[]} Array with power consumption values.
|
|
58
|
+
*/
|
|
59
|
+
getPowerSeries(): number[];
|
|
60
|
+
/**
|
|
61
|
+
* Get outlet power from power series by smoothing data using the desired method and window size.
|
|
62
|
+
* @param {string} method Method to smooth data ('SMA' or 'EMA').
|
|
63
|
+
* @param {number} windowSize Window size for SMA or EMA.
|
|
64
|
+
* @returns {number} Power value after smoothing (rounded to nearest integer number).
|
|
65
|
+
*/
|
|
66
|
+
getSmoothPowerConsumption(method: string, windowSize: number): number;
|
|
67
|
+
/**
|
|
68
|
+
* Gets the current power consumption value for this outlet.
|
|
69
|
+
* @returns {number} Outlet power consumption (Watt).
|
|
70
|
+
*/
|
|
71
|
+
getPowerConsumptionValue(): number;
|
|
72
|
+
/**
|
|
73
|
+
* Gets the current power consumpion.
|
|
74
|
+
*/
|
|
75
|
+
getPowerConsumption(callback: CharacteristicGetCallback): void;
|
|
76
|
+
/**
|
|
77
|
+
* Sets the current power consumption.
|
|
78
|
+
* @param {number} powerConsumption The power consumption to set. */
|
|
79
|
+
setPowerConsumption(powerConsumption: number): void;
|
|
80
|
+
/**
|
|
81
|
+
* Gets the total energy consumption value for this outlet.
|
|
82
|
+
* @returns {number} Outlet total energy consumption (Wh).
|
|
83
|
+
*/
|
|
84
|
+
getTotalEnergyConsumptionValue(): number;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the total energy consumpion.
|
|
87
|
+
*/
|
|
88
|
+
getTotalEnergyConsumption(callback: CharacteristicGetCallback): void;
|
|
89
|
+
/**
|
|
90
|
+
* Sets the total energy consumption.
|
|
91
|
+
* @param {number} totalEnergyConsumption The total energy consumption to set.
|
|
92
|
+
*/
|
|
93
|
+
setTotalEnergyConsumption(totalEnergyConsumption: number): void;
|
|
94
|
+
/**
|
|
95
|
+
* Gets the outlet state Characteristic based on its consumed power.
|
|
96
|
+
* @param {CharacteristicGetCallback} callback The callback.
|
|
97
|
+
*/
|
|
98
|
+
getState(callback: CharacteristicGetCallback): void;
|
|
99
|
+
getServices(): Service[];
|
|
100
|
+
}
|
|
101
101
|
//# sourceMappingURL=outletAccessory.d.ts.map
|
package/dist/outletAccessory.js
CHANGED
|
@@ -1,188 +1,188 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SolaxOutletAccessory = void 0;
|
|
7
|
-
const platformAccessory_1 = require("./platformAccessory");
|
|
8
|
-
const statistics_1 = require("./statistics");
|
|
9
|
-
const homebridge_lib_1 = require("homebridge-lib");
|
|
10
|
-
const fakegato_history_1 = __importDefault(require("fakegato-history"));
|
|
11
|
-
/**
|
|
12
|
-
* Maximum size for the outlet power series.
|
|
13
|
-
*/
|
|
14
|
-
const MAX_POWER_SERIES_SIZE = 1024;
|
|
15
|
-
/**
|
|
16
|
-
* Solax Outlet Accessory.
|
|
17
|
-
* Virtual outlet used to monitor and measure both power and total energy consumption from Solax devices.
|
|
18
|
-
* Outlet use is inferred from a positive power comsumption.
|
|
19
|
-
*/
|
|
20
|
-
class SolaxOutletAccessory extends platformAccessory_1.SolaxPlatformAccessory {
|
|
21
|
-
/**
|
|
22
|
-
* Solax virtual outlet class constructor.
|
|
23
|
-
* @param {Logging} log The platform logging for homebridge.
|
|
24
|
-
* @param {API} api The API for Solax Cloud platform.
|
|
25
|
-
* @param {string} name The accessory name.
|
|
26
|
-
* @param {string} serial "Real world" serial number for this accessory.
|
|
27
|
-
* @param {string} model Accessory model.
|
|
28
|
-
*/
|
|
29
|
-
constructor(log, api, name, serial, model) {
|
|
30
|
-
super(log, api, name, serial, model);
|
|
31
|
-
/**
|
|
32
|
-
* Current power reading in Watt.
|
|
33
|
-
*/
|
|
34
|
-
this.powerConsumption = 0;
|
|
35
|
-
/**
|
|
36
|
-
* Total energy readings in KWh.
|
|
37
|
-
*/
|
|
38
|
-
this.totalEnergyConsumption = 0.0;
|
|
39
|
-
/**
|
|
40
|
-
* Power series for smoothing operations.
|
|
41
|
-
*/
|
|
42
|
-
this.powerSeries = [];
|
|
43
|
-
/**
|
|
44
|
-
* Last Exponential Moving Average.
|
|
45
|
-
*/
|
|
46
|
-
this.lastEMA = 0;
|
|
47
|
-
this.log.debug(`Creating outlet "${this.name}"`);
|
|
48
|
-
// init fakegato objects
|
|
49
|
-
this.eve = new homebridge_lib_1.EveHomeKitTypes(this.api);
|
|
50
|
-
this.eveService = (0, fakegato_history_1.default)(this.api);
|
|
51
|
-
// create power meter service
|
|
52
|
-
this.outletService = new this.eve.Services.Outlet(this.name);
|
|
53
|
-
// outlet on/off state
|
|
54
|
-
this.outletService.getCharacteristic(this.api.hap.Characteristic.On)
|
|
55
|
-
.on("get" /* GET */, this.getState.bind(this));
|
|
56
|
-
// in use
|
|
57
|
-
//this.outletService.getCharacteristic(hap.Characteristic.OutletInUse).on(hap.CharacteristicEventTypes.GET, this.getState.bind(this));
|
|
58
|
-
// current consumption
|
|
59
|
-
this.outletService.getCharacteristic(this.eve.Characteristics.CurrentConsumption)
|
|
60
|
-
.on("get" /* GET */, this.getPowerConsumption.bind(this));
|
|
61
|
-
// total consumption
|
|
62
|
-
this.outletService.getCharacteristic(this.eve.Characteristics.TotalConsumption)
|
|
63
|
-
.on("get" /* GET */, this.getTotalEnergyConsumption.bind(this));
|
|
64
|
-
// history logging services
|
|
65
|
-
this.loggingService = new this.eveService('energy', this, { storage: 'fs', log: this.log, disableRepeatLastData: true });
|
|
66
|
-
log.info(`Outlet "${name}" created!`);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Adds new power entry to series array of power values.
|
|
70
|
-
* @param {number} powerConsumption Power value to add (in Watts).
|
|
71
|
-
*/
|
|
72
|
-
addPowerEntry(powerConsumption) {
|
|
73
|
-
// check if we reached the limit
|
|
74
|
-
if (this.powerSeries.length === MAX_POWER_SERIES_SIZE) {
|
|
75
|
-
// remove first element
|
|
76
|
-
this.powerSeries.shift();
|
|
77
|
-
}
|
|
78
|
-
// add new element
|
|
79
|
-
this.powerSeries.push(powerConsumption);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Gets array series with power consumption values.
|
|
83
|
-
* @returns {number[]} Array with power consumption values.
|
|
84
|
-
*/
|
|
85
|
-
getPowerSeries() {
|
|
86
|
-
return this.powerSeries;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Get outlet power from power series by smoothing data using the desired method and window size.
|
|
90
|
-
* @param {string} method Method to smooth data ('SMA' or 'EMA').
|
|
91
|
-
* @param {number} windowSize Window size for SMA or EMA.
|
|
92
|
-
* @returns {number} Power value after smoothing (rounded to nearest integer number).
|
|
93
|
-
*/
|
|
94
|
-
getSmoothPowerConsumption(method, windowSize) {
|
|
95
|
-
this.log.debug(`${this.name}: GET Power Smooth (method=${method}, window=${windowSize})`);
|
|
96
|
-
let result = 0;
|
|
97
|
-
// get power series for window size
|
|
98
|
-
const series = this.getPowerSeries().slice(-windowSize);
|
|
99
|
-
// calculate simple moving average from given window size
|
|
100
|
-
const [sma] = statistics_1.Statistics.simpleMovingAverage(series, Math.min(windowSize, series.length));
|
|
101
|
-
// check if we have enough data for averaging
|
|
102
|
-
switch (method) {
|
|
103
|
-
case 'sma':
|
|
104
|
-
result = sma;
|
|
105
|
-
this.log.debug(`${this.name}: simple moving average (window data=[${series}], power=${result}W)`);
|
|
106
|
-
break;
|
|
107
|
-
case 'ema':
|
|
108
|
-
if (series.length < windowSize) {
|
|
109
|
-
result = sma;
|
|
110
|
-
this.log.debug(`${this.name}: exponential moving average (window data=[${series}], last EMA=none, power=${result}W)`);
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
const lastPowerValue = series[series.length - 1];
|
|
114
|
-
const weight = 2 / (windowSize + 1);
|
|
115
|
-
result = (lastPowerValue - this.lastEMA) * weight + this.lastEMA;
|
|
116
|
-
this.log.debug(`${this.name}: exponential moving average (window data=[${series}], last EMA=${this.lastEMA}, power=${result}W)`);
|
|
117
|
-
}
|
|
118
|
-
this.lastEMA = result;
|
|
119
|
-
}
|
|
120
|
-
return result;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Gets the current power consumption value for this outlet.
|
|
124
|
-
* @returns {number} Outlet power consumption (Watt).
|
|
125
|
-
*/
|
|
126
|
-
getPowerConsumptionValue() {
|
|
127
|
-
return this.powerConsumption;
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Gets the current power consumpion.
|
|
131
|
-
*/
|
|
132
|
-
getPowerConsumption(callback) {
|
|
133
|
-
this.log.debug(`${this.name}: GET Power (power=${this.powerConsumption}W)`);
|
|
134
|
-
callback(null, this.powerConsumption);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Sets the current power consumption.
|
|
138
|
-
* @param {number} powerConsumption The power consumption to set. */
|
|
139
|
-
setPowerConsumption(powerConsumption) {
|
|
140
|
-
this.log.debug(`${this.name}: SET Power (power=${powerConsumption}W)`);
|
|
141
|
-
this.powerConsumption = powerConsumption >= 0 ? powerConsumption : 0;
|
|
142
|
-
this.outletService.getCharacteristic(this.eve.Characteristics.CurrentConsumption).updateValue(this.powerConsumption);
|
|
143
|
-
// add entries to history
|
|
144
|
-
this.loggingService.addEntry({ time: Math.round(new Date().valueOf() / 1000), power: this.powerConsumption });
|
|
145
|
-
// add new value to power series
|
|
146
|
-
this.addPowerEntry(this.powerConsumption);
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Gets the total energy consumption value for this outlet.
|
|
150
|
-
* @returns {number} Outlet total energy consumption (Wh).
|
|
151
|
-
*/
|
|
152
|
-
getTotalEnergyConsumptionValue() {
|
|
153
|
-
return this.totalEnergyConsumption;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Gets the total energy consumpion.
|
|
157
|
-
*/
|
|
158
|
-
getTotalEnergyConsumption(callback) {
|
|
159
|
-
this.log.debug(`${this.name}: GET Total Energy (energy=${this.totalEnergyConsumption}kWh)`);
|
|
160
|
-
callback(null, this.totalEnergyConsumption);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Sets the total energy consumption.
|
|
164
|
-
* @param {number} totalEnergyConsumption The total energy consumption to set.
|
|
165
|
-
*/
|
|
166
|
-
setTotalEnergyConsumption(totalEnergyConsumption) {
|
|
167
|
-
this.log.debug(`${this.name}: SET Total Energy (energy=${totalEnergyConsumption}kWh)`);
|
|
168
|
-
this.totalEnergyConsumption = totalEnergyConsumption;
|
|
169
|
-
this.outletService.getCharacteristic(this.eve.Characteristics.TotalConsumption).updateValue(this.totalEnergyConsumption);
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Gets the outlet state Characteristic based on its consumed power.
|
|
173
|
-
* @param {CharacteristicGetCallback} callback The callback.
|
|
174
|
-
*/
|
|
175
|
-
getState(callback) {
|
|
176
|
-
this.log.debug(`${this.name}: GET State (power=${this.powerConsumption}W)`);
|
|
177
|
-
callback(null, this.powerConsumption > 0);
|
|
178
|
-
}
|
|
179
|
-
/*
|
|
180
|
-
* This method is called directly after creation of this instance.
|
|
181
|
-
* It should return all services which should be added to the accessory.
|
|
182
|
-
*/
|
|
183
|
-
getServices() {
|
|
184
|
-
return [this.informationService, this.outletService, this.loggingService];
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
exports.SolaxOutletAccessory = SolaxOutletAccessory;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SolaxOutletAccessory = void 0;
|
|
7
|
+
const platformAccessory_1 = require("./platformAccessory");
|
|
8
|
+
const statistics_1 = require("./statistics");
|
|
9
|
+
const homebridge_lib_1 = require("homebridge-lib");
|
|
10
|
+
const fakegato_history_1 = __importDefault(require("fakegato-history"));
|
|
11
|
+
/**
|
|
12
|
+
* Maximum size for the outlet power series.
|
|
13
|
+
*/
|
|
14
|
+
const MAX_POWER_SERIES_SIZE = 1024;
|
|
15
|
+
/**
|
|
16
|
+
* Solax Outlet Accessory.
|
|
17
|
+
* Virtual outlet used to monitor and measure both power and total energy consumption from Solax devices.
|
|
18
|
+
* Outlet use is inferred from a positive power comsumption.
|
|
19
|
+
*/
|
|
20
|
+
class SolaxOutletAccessory extends platformAccessory_1.SolaxPlatformAccessory {
|
|
21
|
+
/**
|
|
22
|
+
* Solax virtual outlet class constructor.
|
|
23
|
+
* @param {Logging} log The platform logging for homebridge.
|
|
24
|
+
* @param {API} api The API for Solax Cloud platform.
|
|
25
|
+
* @param {string} name The accessory name.
|
|
26
|
+
* @param {string} serial "Real world" serial number for this accessory.
|
|
27
|
+
* @param {string} model Accessory model.
|
|
28
|
+
*/
|
|
29
|
+
constructor(log, api, name, serial, model) {
|
|
30
|
+
super(log, api, name, serial, model);
|
|
31
|
+
/**
|
|
32
|
+
* Current power reading in Watt.
|
|
33
|
+
*/
|
|
34
|
+
this.powerConsumption = 0;
|
|
35
|
+
/**
|
|
36
|
+
* Total energy readings in KWh.
|
|
37
|
+
*/
|
|
38
|
+
this.totalEnergyConsumption = 0.0;
|
|
39
|
+
/**
|
|
40
|
+
* Power series for smoothing operations.
|
|
41
|
+
*/
|
|
42
|
+
this.powerSeries = [];
|
|
43
|
+
/**
|
|
44
|
+
* Last Exponential Moving Average.
|
|
45
|
+
*/
|
|
46
|
+
this.lastEMA = 0;
|
|
47
|
+
this.log.debug(`Creating outlet "${this.name}"`);
|
|
48
|
+
// init fakegato objects
|
|
49
|
+
this.eve = new homebridge_lib_1.EveHomeKitTypes(this.api);
|
|
50
|
+
this.eveService = (0, fakegato_history_1.default)(this.api);
|
|
51
|
+
// create power meter service
|
|
52
|
+
this.outletService = new this.eve.Services.Outlet(this.name);
|
|
53
|
+
// outlet on/off state
|
|
54
|
+
this.outletService.getCharacteristic(this.api.hap.Characteristic.On)
|
|
55
|
+
.on("get" /* GET */, this.getState.bind(this));
|
|
56
|
+
// in use
|
|
57
|
+
//this.outletService.getCharacteristic(hap.Characteristic.OutletInUse).on(hap.CharacteristicEventTypes.GET, this.getState.bind(this));
|
|
58
|
+
// current consumption
|
|
59
|
+
this.outletService.getCharacteristic(this.eve.Characteristics.CurrentConsumption)
|
|
60
|
+
.on("get" /* GET */, this.getPowerConsumption.bind(this));
|
|
61
|
+
// total consumption
|
|
62
|
+
this.outletService.getCharacteristic(this.eve.Characteristics.TotalConsumption)
|
|
63
|
+
.on("get" /* GET */, this.getTotalEnergyConsumption.bind(this));
|
|
64
|
+
// history logging services
|
|
65
|
+
this.loggingService = new this.eveService('energy', this, { storage: 'fs', log: this.log, disableRepeatLastData: true });
|
|
66
|
+
log.info(`Outlet "${name}" created!`);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Adds new power entry to series array of power values.
|
|
70
|
+
* @param {number} powerConsumption Power value to add (in Watts).
|
|
71
|
+
*/
|
|
72
|
+
addPowerEntry(powerConsumption) {
|
|
73
|
+
// check if we reached the limit
|
|
74
|
+
if (this.powerSeries.length === MAX_POWER_SERIES_SIZE) {
|
|
75
|
+
// remove first element
|
|
76
|
+
this.powerSeries.shift();
|
|
77
|
+
}
|
|
78
|
+
// add new element
|
|
79
|
+
this.powerSeries.push(powerConsumption);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Gets array series with power consumption values.
|
|
83
|
+
* @returns {number[]} Array with power consumption values.
|
|
84
|
+
*/
|
|
85
|
+
getPowerSeries() {
|
|
86
|
+
return this.powerSeries;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Get outlet power from power series by smoothing data using the desired method and window size.
|
|
90
|
+
* @param {string} method Method to smooth data ('SMA' or 'EMA').
|
|
91
|
+
* @param {number} windowSize Window size for SMA or EMA.
|
|
92
|
+
* @returns {number} Power value after smoothing (rounded to nearest integer number).
|
|
93
|
+
*/
|
|
94
|
+
getSmoothPowerConsumption(method, windowSize) {
|
|
95
|
+
this.log.debug(`${this.name}: GET Power Smooth (method=${method}, window=${windowSize})`);
|
|
96
|
+
let result = 0;
|
|
97
|
+
// get power series for window size
|
|
98
|
+
const series = this.getPowerSeries().slice(-windowSize);
|
|
99
|
+
// calculate simple moving average from given window size
|
|
100
|
+
const [sma] = statistics_1.Statistics.simpleMovingAverage(series, Math.min(windowSize, series.length));
|
|
101
|
+
// check if we have enough data for averaging
|
|
102
|
+
switch (method) {
|
|
103
|
+
case 'sma':
|
|
104
|
+
result = sma;
|
|
105
|
+
this.log.debug(`${this.name}: simple moving average (window data=[${series}], power=${result}W)`);
|
|
106
|
+
break;
|
|
107
|
+
case 'ema':
|
|
108
|
+
if (series.length < windowSize) {
|
|
109
|
+
result = sma;
|
|
110
|
+
this.log.debug(`${this.name}: exponential moving average (window data=[${series}], last EMA=none, power=${result}W)`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
const lastPowerValue = series[series.length - 1];
|
|
114
|
+
const weight = 2 / (windowSize + 1);
|
|
115
|
+
result = (lastPowerValue - this.lastEMA) * weight + this.lastEMA;
|
|
116
|
+
this.log.debug(`${this.name}: exponential moving average (window data=[${series}], last EMA=${this.lastEMA}, power=${result}W)`);
|
|
117
|
+
}
|
|
118
|
+
this.lastEMA = result;
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Gets the current power consumption value for this outlet.
|
|
124
|
+
* @returns {number} Outlet power consumption (Watt).
|
|
125
|
+
*/
|
|
126
|
+
getPowerConsumptionValue() {
|
|
127
|
+
return this.powerConsumption;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets the current power consumpion.
|
|
131
|
+
*/
|
|
132
|
+
getPowerConsumption(callback) {
|
|
133
|
+
this.log.debug(`${this.name}: GET Power (power=${this.powerConsumption}W)`);
|
|
134
|
+
callback(null, this.powerConsumption);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Sets the current power consumption.
|
|
138
|
+
* @param {number} powerConsumption The power consumption to set. */
|
|
139
|
+
setPowerConsumption(powerConsumption) {
|
|
140
|
+
this.log.debug(`${this.name}: SET Power (power=${powerConsumption}W)`);
|
|
141
|
+
this.powerConsumption = powerConsumption >= 0 ? powerConsumption : 0;
|
|
142
|
+
this.outletService.getCharacteristic(this.eve.Characteristics.CurrentConsumption).updateValue(this.powerConsumption);
|
|
143
|
+
// add entries to history
|
|
144
|
+
this.loggingService.addEntry({ time: Math.round(new Date().valueOf() / 1000), power: this.powerConsumption });
|
|
145
|
+
// add new value to power series
|
|
146
|
+
this.addPowerEntry(this.powerConsumption);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Gets the total energy consumption value for this outlet.
|
|
150
|
+
* @returns {number} Outlet total energy consumption (Wh).
|
|
151
|
+
*/
|
|
152
|
+
getTotalEnergyConsumptionValue() {
|
|
153
|
+
return this.totalEnergyConsumption;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Gets the total energy consumpion.
|
|
157
|
+
*/
|
|
158
|
+
getTotalEnergyConsumption(callback) {
|
|
159
|
+
this.log.debug(`${this.name}: GET Total Energy (energy=${this.totalEnergyConsumption}kWh)`);
|
|
160
|
+
callback(null, this.totalEnergyConsumption);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Sets the total energy consumption.
|
|
164
|
+
* @param {number} totalEnergyConsumption The total energy consumption to set.
|
|
165
|
+
*/
|
|
166
|
+
setTotalEnergyConsumption(totalEnergyConsumption) {
|
|
167
|
+
this.log.debug(`${this.name}: SET Total Energy (energy=${totalEnergyConsumption}kWh)`);
|
|
168
|
+
this.totalEnergyConsumption = totalEnergyConsumption;
|
|
169
|
+
this.outletService.getCharacteristic(this.eve.Characteristics.TotalConsumption).updateValue(this.totalEnergyConsumption);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Gets the outlet state Characteristic based on its consumed power.
|
|
173
|
+
* @param {CharacteristicGetCallback} callback The callback.
|
|
174
|
+
*/
|
|
175
|
+
getState(callback) {
|
|
176
|
+
this.log.debug(`${this.name}: GET State (power=${this.powerConsumption}W)`);
|
|
177
|
+
callback(null, this.powerConsumption > 0);
|
|
178
|
+
}
|
|
179
|
+
/*
|
|
180
|
+
* This method is called directly after creation of this instance.
|
|
181
|
+
* It should return all services which should be added to the accessory.
|
|
182
|
+
*/
|
|
183
|
+
getServices() {
|
|
184
|
+
return [this.informationService, this.outletService, this.loggingService];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.SolaxOutletAccessory = SolaxOutletAccessory;
|
|
188
188
|
//# sourceMappingURL=outletAccessory.js.map
|