homebridge-solaxcloud-api 1.5.0 → 2.0.2

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.
Files changed (37) hide show
  1. package/README.md +28 -18
  2. package/config.schema.json +28 -0
  3. package/dist/lightSensorAccessory.d.ts +36 -0
  4. package/dist/lightSensorAccessory.d.ts.map +1 -0
  5. package/dist/lightSensorAccessory.js +60 -0
  6. package/dist/lightSensorAccessory.js.map +1 -0
  7. package/dist/outletAccessory.d.ts +23 -4
  8. package/dist/outletAccessory.d.ts.map +1 -1
  9. package/dist/outletAccessory.js +70 -18
  10. package/dist/outletAccessory.js.map +1 -1
  11. package/dist/platform.d.ts +45 -14
  12. package/dist/platform.d.ts.map +1 -1
  13. package/dist/platform.js +231 -67
  14. package/dist/platform.js.map +1 -1
  15. package/dist/platformAccessory.d.ts +5 -0
  16. package/dist/platformAccessory.d.ts.map +1 -1
  17. package/dist/platformAccessory.js +10 -3
  18. package/dist/platformAccessory.js.map +1 -1
  19. package/dist/platformMeters.d.ts +53 -0
  20. package/dist/platformMeters.d.ts.map +1 -0
  21. package/dist/platformMeters.js +3 -0
  22. package/dist/platformMeters.js.map +1 -0
  23. package/dist/solaxcloudfetch.d.ts +2 -0
  24. package/dist/solaxcloudfetch.d.ts.map +1 -0
  25. package/dist/solaxcloudfetch.js +19 -0
  26. package/dist/solaxcloudfetch.js.map +1 -0
  27. package/dist/statistics.d.ts +36 -1
  28. package/dist/statistics.d.ts.map +1 -1
  29. package/dist/statistics.js +86 -16
  30. package/dist/statistics.js.map +1 -1
  31. package/images/homebridge-ui.png +0 -0
  32. package/images/plugin-accessories-home-pure.png +0 -0
  33. package/images/plugin-accessories-home.png +0 -0
  34. package/images/plugin-outlet-consumption.png +0 -0
  35. package/package.json +3 -3
  36. package/test/solaxcloudapi.test.ts +1 -1
  37. package/test/statistics.test.ts +51 -0
@@ -1,19 +1,89 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- /* eslint-disable no-console */
7
- const solaxcloudapi_1 = require("./solaxcloudapi");
8
- const fs_1 = __importDefault(require("fs"));
9
- const config = JSON.parse(fs_1.default.readFileSync('config.json').toString());
10
- const api = new solaxcloudapi_1.SolaxCloudAPI(config.platforms[0].tokenId, config.platforms[0].sn);
11
- const result = api.getAPIData();
12
- console.log(`PV Power: ${solaxcloudapi_1.SolaxCloudAPI.getPVPower(result.result)}W`);
13
- console.log(`Inverter AC Power: ${solaxcloudapi_1.SolaxCloudAPI.getInverterACPower(result.result)}W`);
14
- console.log(`Grid Power to House: ${solaxcloudapi_1.SolaxCloudAPI.getGridPowerToHouse(result.result)}W`);
15
- console.log(`Inverter Power to Grid: ${solaxcloudapi_1.SolaxCloudAPI.getInverterPowerToGrid(result.result)}W`);
16
- console.log(`Inverter Power to House: ${solaxcloudapi_1.SolaxCloudAPI.getInverterPowerToHouse(result.result)}W`);
17
- console.log(`Yield Today: ${solaxcloudapi_1.SolaxCloudAPI.getYieldToday(result.result)}KWh`);
18
- console.log(`Yield Total: ${solaxcloudapi_1.SolaxCloudAPI.getYieldTotal(result.result)}KWh`);
3
+ exports.Statistics = void 0;
4
+ /**
5
+ * Statistics.
6
+ * This class is used for statistical calculations for the purpose of smoothing a series of value.
7
+ */
8
+ class Statistics {
9
+ /**
10
+ * Simple moving average calculation from a series of values.
11
+ * Formula is
12
+ * SMA = (V1 + V2 + ... + Vn) / n
13
+ * where
14
+ * Vn = Series value for period n
15
+ * n = number values in period
16
+ * @param {number[]} series Array with series of values.
17
+ * @param {number} windowSize Size of moving average window.
18
+ * @param {number} maxCalculations Maximum calculations to be performed.
19
+ * @returns {number[]} Array with simple moving averages from series.
20
+ */
21
+ static simpleMovingAverage(series, windowSize, maxCalculations = Infinity) {
22
+ const result = [];
23
+ // sanity check
24
+ if (!series || !windowSize || series.length < windowSize) {
25
+ return result;
26
+ }
27
+ // first index to compute
28
+ let index = windowSize - 1;
29
+ // max series lenght
30
+ const length = series.length + 1;
31
+ // number of simple moving averages calculated
32
+ let numberOfSMAsCalculated = 0;
33
+ // loop over series array
34
+ while (++index < length && numberOfSMAsCalculated++ < maxCalculations) {
35
+ // create array with window slice only
36
+ const windowSlice = series.slice(index - windowSize, index);
37
+ // compute slice sum
38
+ const sum = windowSlice.reduce((prev, curr) => prev + curr, 0);
39
+ // push new moving average to result
40
+ result.push(sum / windowSize);
41
+ }
42
+ return result;
43
+ }
44
+ /**
45
+ * Exponential moving average calculation from a series of values.
46
+ * Formula is
47
+ * EMAn = (Vn - EMAn-1) * weight + EMAn-1
48
+ * where
49
+ * EMAn = Exponential Moving Average for period n
50
+ * EMAn-1 = Exponential Moving Average for period n - 1
51
+ * Vn = Series value for period n
52
+ * weight = multiplier to use lower weights for older value
53
+ * w = window size
54
+ * and
55
+ * weight = 2 / (w + 1)
56
+ * @param {number[]} series Array with series of values.
57
+ * @param {number} windowSize Size of moving average window.
58
+ * @returns {number[]} Array with exponential moving averages from series.
59
+ */
60
+ static exponentialMovingAverage(series, windowSize) {
61
+ const result = [];
62
+ // sanity check
63
+ if (!series || !windowSize || series.length < windowSize) {
64
+ return result;
65
+ }
66
+ // first index to compute
67
+ let index = windowSize - 1;
68
+ // set previous EMA index
69
+ let previousEmaIndex = 0;
70
+ // setup smoothing factor
71
+ const smoothingFactor = 2 / (windowSize + 1);
72
+ // calculate first EMA using SMA
73
+ const [SMA] = Statistics.simpleMovingAverage(series, windowSize, 1);
74
+ result.push(SMA);
75
+ // loop over series array
76
+ while (++index < series.length) {
77
+ // get current value
78
+ const value = series[index];
79
+ // get previous EMA
80
+ const previousEMA = result[previousEmaIndex++];
81
+ // calculate current EMA
82
+ const currentEMA = (value - previousEMA) * smoothingFactor + previousEMA;
83
+ result.push(currentEMA);
84
+ }
85
+ return result;
86
+ }
87
+ }
88
+ exports.Statistics = Statistics;
19
89
  //# sourceMapp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AAO,MAAM,CAAC;SACf;QAED,yBAAyB;QACzB,IAAI,KAAK,GAAG,UAAU,GAAG,CAAC,CAAC;QAE3B,yBAAyB;QACzB,IAAI,gBAAgB,GAAG,CAAC,CAAC;QAEzB,yBAAyB;QACzB,MAAM,eAAe,GAAG,CAAC,GAAG,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;QAE7C,gCAAgC;QAChC,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,mBAAmB,CAAC,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC;QACpE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAEjB,yBAAyB;QACzB,OAAO,EAAE,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE;YAC9B,oBAAoB;YACpB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;YAE5B,mBAAmB;YACnB,MAAM,WAAW,GAAG,MAAM,CAAC,gBAAgB,EAAE,CAAC,CAAC;YAE/C,wBAAwB;YACxB,MAAM,UAAU,GAAG,CAAC,KAAK,GAAG,WAAW,CAAC,GAAG,eAAe,GAAG,WAAW,CAAC;YAEzE,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SACzB;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CAEF;AApGD,gCAoGC"}
Binary file
Binary file
package/package.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "private": false,
3
3
  "displayName": "Homebridge Solax Cloud API",
4
4
  "name": "homebridge-solaxcloud-api",
5
- "version": "1.5.0",
6
- "description": "Integrates solar production info using public Solax Cloud API into Homebrigde acessories.",
5
+ "version": "2.0.2",
6
+ "description": "Integrates solar production info using public Solax Cloud API into Homebrigde accessories.",
7
7
  "license": "Apache-2.0",
8
8
  "repository": {
9
9
  "type": "git",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "main": "dist/index.js",
20
20
  "scripts": {
21
- "test": "mocha --timeout 0",
21
+ "test": "mocha --require ts-node/register test/**/*.test.ts",
22
22
  "lint": "eslint src/**.ts --max-warnings=0",
23
23
  "watch": "npm run build && npm link && nodemon",
24
24
  "build": "rimraf ./dist && tsc",
@@ -2,7 +2,7 @@ import { SolaxCloudAPI, SolaxCloudAPIResponse } from '../src/solaxcloudapi';
2
2
 
3
3
  import assert = require('assert');
4
4
 
5
- describe('Static methods', () => {
5
+ describe('SolaxCloudAPI: Static methods', () => {
6
6
 
7
7
  const sampleReply: SolaxCloudAPIResponse = {
8
8
  success: true,
@@ -0,0 +1,51 @@
1
+ import { Statistics } from '../src/statistics';
2
+
3
+ import assert = require('assert');
4
+
5
+ describe('Statistics: Smoothing methods', () => {
6
+ const sampleSeries = [1, 3, 5, 3, 7, 6, 8, 5, 8, 8, 4, 5];
7
+
8
+ it('Simple Moving Average (sanity checks)', () => {
9
+ // window size too big
10
+ assert.deepEqual(Statistics.simpleMovingAverage(sampleSeries, 100), []);
11
+ });
12
+
13
+ it('Simple Moving Average', () => {
14
+ assert.deepEqual(
15
+ Statistics.simpleMovingAverage(sampleSeries, 4),
16
+ [
17
+ 3,
18
+ 4.5,
19
+ 5.25,
20
+ 6,
21
+ 6.5,
22
+ 6.75,
23
+ 7.25,
24
+ 6.25,
25
+ 6.25],
26
+ );
27
+ });
28
+
29
+ it('Exponential Moving Average (sanity checks)', () => {
30
+ // window size too big
31
+ assert.deepEqual(Statistics.exponentialMovingAverage(sampleSeries, 100), []);
32
+ });
33
+
34
+ it('Exponential Moving Average', () => {
35
+ assert.deepEqual(
36
+ Statistics.exponentialMovingAverage(sampleSeries, 4),
37
+ [
38
+ 3,
39
+ 4.6,
40
+ 5.16,
41
+ 6.296,
42
+ 5.7776000000000005,
43
+ 6.6665600000000005,
44
+ 7.199936,
45
+ 5.9199616,
46
+ 5.55197696,
47
+ ],
48
+ );
49
+ });
50
+
51
+ });