myenergi-api 1.1.0 → 2.1.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.
@@ -62,7 +62,7 @@ class AuthDigest {
62
62
  get nc() {
63
63
  var _a;
64
64
  this._nc++;
65
- var myHex = ("0000000" + ((_a = this._nc) === null || _a === void 0 ? void 0 : _a.toString(16))).substr(-8);
65
+ const myHex = ("0000000" + ((_a = this._nc) === null || _a === void 0 ? void 0 : _a.toString(16))).substr(-8);
66
66
  return myHex;
67
67
  }
68
68
  get cnonce() {
@@ -70,35 +70,37 @@ class AuthDigest {
70
70
  return this._cnonce;
71
71
  }
72
72
  md5(data) {
73
- let md5 = crypto.createHash("md5");
74
- let result = md5.update(data).digest("hex");
73
+ const md5 = crypto.createHash("md5");
74
+ const result = md5.update(data).digest("hex");
75
75
  return result;
76
76
  }
77
77
  init(wwwAuthHeader) {
78
- let authSplit = wwwAuthHeader.split(",");
79
- for (let item of authSplit) {
80
- if (item.indexOf("realm=") >= 0) {
81
- let realmSplit = item.split('="');
78
+ if (!wwwAuthHeader)
79
+ return;
80
+ const authSplit = wwwAuthHeader === null || wwwAuthHeader === void 0 ? void 0 : wwwAuthHeader.split(",");
81
+ for (const item of authSplit) {
82
+ if (item && item.indexOf("realm=") >= 0) {
83
+ const realmSplit = item.split('="');
82
84
  this._realm = realmSplit[realmSplit.length - 1];
83
85
  this._realm = this._realm.substring(0, this._realm.length - 1);
84
86
  }
85
- if (item.indexOf("nonce=") >= 0) {
86
- let nonceSplit = item.split('="');
87
+ if (item && item.indexOf("nonce=") >= 0) {
88
+ const nonceSplit = item.split('="');
87
89
  this._nonce = nonceSplit[nonceSplit.length - 1];
88
90
  this._nonce = this._nonce.substring(0, this._nonce.length - 1);
89
91
  }
90
- if (item.indexOf("qop=") >= 0) {
91
- let qopSplit = item.split('="');
92
+ if (item && item.indexOf("qop=") >= 0) {
93
+ const qopSplit = item.split('="');
92
94
  this._qop = qopSplit[qopSplit.length - 1];
93
95
  this._qop = this._qop.substring(0, this._qop.length - 1);
94
96
  }
95
- if (item.indexOf("opaque=") >= 0) {
96
- let opaqueSplit = item.split('="');
97
+ if (item && item.indexOf("opaque=") >= 0) {
98
+ const opaqueSplit = item.split('="');
97
99
  this._opaque = opaqueSplit[opaqueSplit.length - 1];
98
100
  this._opaque = this._opaque.substring(0, this._opaque.length - 1);
99
101
  }
100
- if (item.indexOf("algorithm=") >= 0) {
101
- let algorithmSplit = item.split("=");
102
+ if (item && item.indexOf("algorithm=") >= 0) {
103
+ const algorithmSplit = item.split("=");
102
104
  this._algorithm = algorithmSplit[algorithmSplit.length - 1];
103
105
  this._algorithm = this._algorithm.substring(0, this._algorithm.length);
104
106
  if (this._onError && this.algorithm !== "MD5") {
@@ -113,9 +115,9 @@ class AuthDigest {
113
115
  return "";
114
116
  const nc = this.nc;
115
117
  const cnonce = this.cnonce;
116
- let HA1 = this.md5(this.username + ":" + this.realm + ":" + this._password);
117
- let HA2 = this.md5(httpMethod + ":" + path);
118
- let response = this.md5(HA1 + ":" + this.nonce + ":" + nc + ":" + cnonce + ":" + this.qop + ":" + HA2);
118
+ const HA1 = this.md5(this.username + ":" + this.realm + ":" + this._password);
119
+ const HA2 = this.md5(httpMethod + ":" + path);
120
+ const response = this.md5(HA1 + ":" + this.nonce + ":" + nc + ":" + cnonce + ":" + this.qop + ":" + HA2);
119
121
  let res = `Digest username="${this.username}",`;
120
122
  res += `realm="${this.realm}",`;
121
123
  res += `nonce="${this.nonce}",`;
package/dist/Digest.d.ts CHANGED
@@ -5,5 +5,5 @@ export declare class Digest {
5
5
  private _maxRetryCount;
6
6
  constructor(baseUrl: string, username: string, password: string);
7
7
  private request;
8
- get(requestUrl: string, data?: any): Promise<any>;
8
+ get(requestUrl: URL, data?: unknown): Promise<string>;
9
9
  }
package/dist/Digest.js CHANGED
@@ -25,13 +25,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.Digest = void 0;
27
27
  const https = __importStar(require("https"));
28
- const url = __importStar(require("url"));
29
28
  const AuthDigest_1 = require("./AuthDigest");
30
29
  class Digest {
31
30
  constructor(baseUrl, username, password) {
32
31
  this._maxRedirectCount = 3;
33
32
  this._maxRetryCount = 2;
34
- this._baseUrl = url.parse(baseUrl, true);
33
+ this._baseUrl = new URL(baseUrl);
35
34
  this._authDigest = new AuthDigest_1.AuthDigest(username, password, (err) => {
36
35
  console.error(err);
37
36
  });
@@ -87,7 +86,7 @@ class Digest {
87
86
  reject(resaon);
88
87
  });
89
88
  }
90
- else if (res.statusCode >= 200 && res.statusCode < 300) {
89
+ else if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
91
90
  const myenergiAsn = res.headers["x_myenergi-asn"];
92
91
  if (myenergiAsn && myenergiAsn !== "undefined" && myenergiAsn !== this._baseUrl.host) {
93
92
  if (redirectCount > this._maxRedirectCount) {
@@ -115,13 +114,13 @@ class Digest {
115
114
  resolve(resData);
116
115
  });
117
116
  }
118
- else if (res.statusCode >= 300 && res.statusCode < 400) {
117
+ else if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400) {
119
118
  if (redirectCount > this._maxRedirectCount) {
120
119
  reject(`Too many redirects: ${res.headers["location"]}`);
121
120
  return;
122
121
  }
123
122
  const location = res.headers["location"];
124
- const uri = url.parse(location, true);
123
+ const uri = new URL(location);
125
124
  if (uri.host !== this._baseUrl.host) {
126
125
  this._baseUrl.host = uri.host;
127
126
  }
@@ -151,18 +150,17 @@ class Digest {
151
150
  });
152
151
  }
153
152
  get(requestUrl, data) {
154
- const uri = url.parse(requestUrl, true);
155
153
  const options = {
156
154
  hostname: this._baseUrl.hostname,
157
155
  host: this._baseUrl.host,
158
156
  port: this._baseUrl.port,
159
- path: uri.path,
157
+ path: requestUrl.pathname,
160
158
  method: "GET",
161
159
  headers: {
162
160
  Connection: "Keep-Alive",
163
161
  "Content-Type": "application/json",
164
162
  Accept: "application/json",
165
- Host: uri.hostname,
163
+ Host: requestUrl.hostname,
166
164
  },
167
165
  };
168
166
  return this.request(options, data);
@@ -6,16 +6,16 @@ export declare class MyEnergi {
6
6
  private _config;
7
7
  private _digest;
8
8
  constructor(username: string, password: string, apiBaseUrl?: string);
9
- getStatusAll(): Promise<any>;
9
+ getStatusAll(): Promise<unknown>;
10
10
  getStatusZappiAll(): Promise<Zappi[]>;
11
11
  getStatusZappi(serialNumber: string): Promise<Zappi | null>;
12
- setZappiChargeMode(serialNo: string, chargeMode: ZappiChargeMode): Promise<any>;
13
- setZappiBoostMode(serialNo: string, boostMode: ZappiBoostMode, kwh?: number, completeTime?: string): Promise<any>;
14
- setZappiGreenLevel(serialNo: string, percentage: number): Promise<any>;
12
+ setZappiChargeMode(serialNo: string, chargeMode: ZappiChargeMode): Promise<unknown>;
13
+ setZappiBoostMode(serialNo: string, boostMode: ZappiBoostMode, kwh?: number, completeTime?: string): Promise<unknown>;
14
+ setZappiGreenLevel(serialNo: string, percentage: number): Promise<unknown>;
15
15
  getStatusEddiAll(): Promise<Eddi[]>;
16
16
  getStatusEddi(serialNumber: string): Promise<Eddi | null>;
17
- setEddiMode(serialNo: string, mode: EddiMode): Promise<any>;
18
- setEddiBoost(serialNo: string, boost: EddiBoost, minutes?: number): Promise<any>;
17
+ setEddiMode(serialNo: string, mode: EddiMode): Promise<unknown>;
18
+ setEddiBoost(serialNo: string, boost: EddiBoost, minutes?: number): Promise<unknown>;
19
19
  getStatusHarviAll(): Promise<Harvi[]>;
20
20
  getStatusHarvi(serialNumber: string): Promise<Harvi | null>;
21
21
  }
package/dist/MyEnergi.js CHANGED
@@ -36,134 +36,194 @@ class MyEnergi {
36
36
  }
37
37
  getStatusAll() {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
- const data = yield this._digest.get(this._config.status_url);
40
- const jsonData = JSON.parse(data);
41
- return jsonData;
39
+ try {
40
+ const data = yield this._digest.get(new URL(this._config.status_url, this._config.base_url));
41
+ const jsonData = JSON.parse(data);
42
+ return jsonData;
43
+ }
44
+ catch (error) {
45
+ return [];
46
+ }
42
47
  });
43
48
  }
44
49
  getStatusZappiAll() {
45
50
  return __awaiter(this, void 0, void 0, function* () {
46
- const data = yield this._digest.get(this._config.zappi_url);
47
- const jsonData = JSON.parse(data);
48
- if (jsonData.zappi)
49
- return Object.assign([], jsonData.zappi);
50
- else
51
+ try {
52
+ const data = yield this._digest.get(new URL(this._config.zappi_url, this._config.base_url));
53
+ const jsonData = JSON.parse(data);
54
+ if (jsonData.zappi)
55
+ return Object.assign([], jsonData.zappi);
56
+ else
57
+ return [];
58
+ }
59
+ catch (error) {
51
60
  return [];
61
+ }
52
62
  });
53
63
  }
54
64
  getStatusZappi(serialNumber) {
55
65
  return __awaiter(this, void 0, void 0, function* () {
56
- const data = yield this._digest.get(this._config.zappi_url);
57
- const jsonData = JSON.parse(data);
58
- if (jsonData.zappi) {
59
- const zappi = Object.assign([], jsonData.zappi).find((zappi) => {
60
- return zappi.sno === serialNumber;
61
- });
62
- if (zappi)
63
- return Object.assign({}, zappi);
66
+ try {
67
+ const data = yield this._digest.get(new URL(this._config.zappi_url, this._config.base_url));
68
+ const jsonData = JSON.parse(data);
69
+ if (jsonData.zappi) {
70
+ const zappi = Object.assign([], jsonData.zappi).find((zappi) => {
71
+ return zappi.sno == serialNumber;
72
+ });
73
+ if (zappi)
74
+ return Object.assign({}, zappi);
75
+ else
76
+ return null;
77
+ }
64
78
  else
65
79
  return null;
66
80
  }
67
- else
81
+ catch (error) {
68
82
  return null;
83
+ }
69
84
  });
70
85
  }
71
86
  setZappiChargeMode(serialNo, chargeMode) {
72
87
  return __awaiter(this, void 0, void 0, function* () {
73
- const url = `${this._config.zappi_mode_url}${serialNo}-${chargeMode}-0-0-0000`;
74
- const data = yield this._digest.get(url);
75
- const jsonData = JSON.parse(data);
76
- return jsonData;
88
+ try {
89
+ const url = new URL(`${this._config.zappi_mode_url}${serialNo}-${chargeMode}-0-0-0000`, this._config.base_url);
90
+ const data = yield this._digest.get(url);
91
+ const jsonData = JSON.parse(data);
92
+ return jsonData;
93
+ }
94
+ catch (error) {
95
+ return {};
96
+ }
77
97
  });
78
98
  }
79
99
  setZappiBoostMode(serialNo, boostMode, kwh = 0, completeTime = "0000") {
80
100
  return __awaiter(this, void 0, void 0, function* () {
81
- if (boostMode === Types_1.ZappiBoostMode.Stop) {
82
- kwh = 0;
83
- completeTime = "0000";
84
- }
85
- const url = `${this._config.zappi_mode_url}${serialNo}-0-${boostMode}-${kwh}-${completeTime}`;
86
- const data = yield this._digest.get(url);
87
- const jsonData = JSON.parse(data);
88
- return jsonData;
101
+ try {
102
+ if (boostMode === Types_1.ZappiBoostMode.Stop) {
103
+ kwh = 0;
104
+ completeTime = "0000";
105
+ }
106
+ const url = new URL(`${this._config.zappi_mode_url}${serialNo}-0-${boostMode}-${kwh}-${completeTime}`, this._config.base_url);
107
+ const data = yield this._digest.get(url);
108
+ const jsonData = JSON.parse(data);
109
+ return jsonData;
110
+ }
111
+ catch (error) {
112
+ return {};
113
+ }
89
114
  });
90
115
  }
91
116
  setZappiGreenLevel(serialNo, percentage) {
92
117
  return __awaiter(this, void 0, void 0, function* () {
93
- const url = `${this._config.zappi_min_green_url}${serialNo}-${percentage}`;
94
- const data = yield this._digest.get(url);
95
- const jsonData = JSON.parse(data);
96
- return jsonData;
118
+ try {
119
+ const url = new URL(`${this._config.zappi_min_green_url}${serialNo}-${percentage}`, this._config.base_url);
120
+ const data = yield this._digest.get(url);
121
+ const jsonData = JSON.parse(data);
122
+ return jsonData;
123
+ }
124
+ catch (error) {
125
+ return {};
126
+ }
97
127
  });
98
128
  }
99
129
  getStatusEddiAll() {
100
130
  return __awaiter(this, void 0, void 0, function* () {
101
- const data = yield this._digest.get(this._config.eddi_url);
102
- const jsonData = JSON.parse(data);
103
- if (jsonData.eddi)
104
- return Object.assign([], jsonData.eddi);
105
- else
131
+ try {
132
+ const data = yield this._digest.get(new URL(this._config.eddi_url, this._config.base_url));
133
+ const jsonData = JSON.parse(data);
134
+ if (jsonData.eddi)
135
+ return Object.assign([], jsonData.eddi);
136
+ else
137
+ return [];
138
+ }
139
+ catch (error) {
106
140
  return [];
141
+ }
107
142
  });
108
143
  }
109
144
  getStatusEddi(serialNumber) {
110
145
  return __awaiter(this, void 0, void 0, function* () {
111
- const data = yield this._digest.get(this._config.eddi_url);
112
- const jsonData = JSON.parse(data);
113
- if (jsonData.eddi) {
114
- const eddi = Object.assign([], jsonData.eddi).find((eddi) => {
115
- return eddi.sno === serialNumber;
116
- });
117
- if (eddi)
118
- return Object.assign({}, eddi);
146
+ try {
147
+ const data = yield this._digest.get(new URL(this._config.eddi_url, this._config.base_url));
148
+ const jsonData = JSON.parse(data);
149
+ if (jsonData.eddi) {
150
+ const eddi = Object.assign([], jsonData.eddi).find((eddi) => {
151
+ return eddi.sno == serialNumber;
152
+ });
153
+ if (eddi)
154
+ return Object.assign({}, eddi);
155
+ else
156
+ return null;
157
+ }
119
158
  else
120
159
  return null;
121
160
  }
122
- else
161
+ catch (error) {
123
162
  return null;
163
+ }
124
164
  });
125
165
  }
126
166
  setEddiMode(serialNo, mode) {
127
167
  return __awaiter(this, void 0, void 0, function* () {
128
- const url = `${this._config.eddi_mode_url}${serialNo}-${mode}`;
129
- const data = yield this._digest.get(url);
130
- const jsonData = JSON.parse(data);
131
- return jsonData;
168
+ try {
169
+ const url = new URL(`${this._config.eddi_mode_url}${serialNo}-${mode}`, this._config.base_url);
170
+ const data = yield this._digest.get(url);
171
+ const jsonData = JSON.parse(data);
172
+ return jsonData;
173
+ }
174
+ catch (error) {
175
+ return {};
176
+ }
132
177
  });
133
178
  }
134
179
  setEddiBoost(serialNo, boost, minutes = 0) {
135
180
  return __awaiter(this, void 0, void 0, function* () {
136
- const url = `${this._config.eddi_boost_url}${serialNo}-${boost}-${minutes}`;
137
- const data = yield this._digest.get(url);
138
- const jsonData = JSON.parse(data);
139
- return jsonData;
181
+ try {
182
+ const url = new URL(`${this._config.eddi_boost_url}${serialNo}-${boost}-${minutes}`, this._config.base_url);
183
+ const data = yield this._digest.get(url);
184
+ const jsonData = JSON.parse(data);
185
+ return jsonData;
186
+ }
187
+ catch (error) {
188
+ return {};
189
+ }
140
190
  });
141
191
  }
142
192
  getStatusHarviAll() {
143
193
  return __awaiter(this, void 0, void 0, function* () {
144
- const data = yield this._digest.get(this._config.harvi_url);
145
- const jsonData = JSON.parse(data);
146
- if (jsonData.harvi)
147
- return Object.assign([], jsonData.harvi);
148
- else
194
+ try {
195
+ const data = yield this._digest.get(new URL(this._config.harvi_url, this._config.base_url));
196
+ const jsonData = JSON.parse(data);
197
+ if (jsonData.harvi)
198
+ return Object.assign([], jsonData.harvi);
199
+ else
200
+ return [];
201
+ }
202
+ catch (error) {
149
203
  return [];
204
+ }
150
205
  });
151
206
  }
152
207
  getStatusHarvi(serialNumber) {
153
208
  return __awaiter(this, void 0, void 0, function* () {
154
- const data = yield this._digest.get(this._config.harvi_url);
155
- const jsonData = JSON.parse(data);
156
- if (jsonData.harvi) {
157
- const harvi = Object.assign([], jsonData.harvi).find((harvi) => {
158
- return harvi.sno === serialNumber;
159
- });
160
- if (harvi)
161
- return Object.assign({}, harvi);
209
+ try {
210
+ const data = yield this._digest.get(new URL(this._config.harvi_url, this._config.base_url));
211
+ const jsonData = JSON.parse(data);
212
+ if (jsonData.harvi) {
213
+ const harvi = Object.assign([], jsonData.harvi).find((harvi) => {
214
+ return harvi.sno == serialNumber;
215
+ });
216
+ if (harvi)
217
+ return Object.assign({}, harvi);
218
+ else
219
+ return null;
220
+ }
162
221
  else
163
222
  return null;
164
223
  }
165
- else
224
+ catch (error) {
166
225
  return null;
226
+ }
167
227
  });
168
228
  }
169
229
  }
@@ -31,8 +31,8 @@ export interface Zappi {
31
31
  lck: number;
32
32
  pst: string;
33
33
  bsm: number;
34
- bst: number;
35
34
  bss: number;
35
+ bst: number;
36
36
  sbh: number;
37
37
  sbk: number;
38
38
  sbm: number;
@@ -40,6 +40,7 @@ export interface Zappi {
40
40
  tbk: number;
41
41
  tbm: number;
42
42
  zmo: number;
43
+ zsh: number;
43
44
  rdc: number;
44
45
  rac: number;
45
46
  rrac: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "myenergi-api",
3
- "version": "1.1.0",
3
+ "version": "2.1.0",
4
4
  "description": "NodeJS implementation of MyEnergi API for controlling Zappi, Eddi and other MyEnergi products",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,9 +9,9 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "build": "tsc",
12
- "test": "env TS_NODE_PROJECT='tsconfig.testing.json' mocha -r ts-node/register tests/**/*-spec.ts",
12
+ "test": "jest --runInBand --config jestconfig.json",
13
13
  "format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
14
- "lint": "tslint -p tsconfig.json",
14
+ "lint": "npx eslint -c .eslintrc.json '**/*.ts'",
15
15
  "prepare": "tsc",
16
16
  "preversion": "npm test",
17
17
  "version": "",
@@ -35,15 +35,19 @@
35
35
  "homepage": "https://github.com/bisand/myenergi-api#readme",
36
36
  "dependencies": {},
37
37
  "devDependencies": {
38
- "@types/chai": "^4.3.0",
39
- "@types/mocha": "^9.1.0",
40
- "@types/node": "^16.11.12",
41
- "dotenv": "^16.0.0",
42
- "chai": "^4.3.6",
43
- "mocha": "^9.2.1",
44
- "prettier": "^2.5.1",
45
- "ts-node": "^10.7.0",
46
- "tslint": "^6.1.3",
47
- "typescript": "^4.6.2"
38
+ "@tsconfig/node12": "^1.0.11",
39
+ "@jest/test-sequencer": "^28.1.3",
40
+ "@types/jest": "^28.1.3",
41
+ "@types/node": "^12",
42
+ "@typescript-eslint/eslint-plugin": "^5.39.0",
43
+ "@typescript-eslint/parser": "^5.39.0",
44
+ "dotenv": "^16.0.3",
45
+ "jest": "^28.1.3",
46
+ "nock": "^13.2.9",
47
+ "prettier": "^2.7.1",
48
+ "ts-jest": "^28.0.8",
49
+ "ts-node": "^10.9.1",
50
+ "typescript": "^4.8.4",
51
+ "eslint": "^8.24.0"
48
52
  }
49
53
  }