myenergi-api 0.0.9-0 → 0.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/dist/AuthDigest.d.ts +29 -0
- package/dist/AuthDigest.js +128 -0
- package/dist/Digest.d.ts +6 -0
- package/dist/Digest.js +106 -0
- package/dist/MyEnergi.d.ts +23 -0
- package/dist/MyEnergi.js +104 -0
- package/{src/index.ts → dist/index.d.ts} +0 -0
- package/dist/index.js +5 -0
- package/dist/models/Zappi.d.ts +41 -0
- package/dist/models/Zappi.js +2 -0
- package/package.json +14 -5
- package/src/AuthDigest.ts +0 -118
- package/src/Digest.ts +0 -87
- package/src/MyEnergi.ts +0 -70
- package/tests/MyEnergi-spec.ts +0 -13
- package/tests/debug.ts +0 -39
- package/tsconfig.testing.json +0 -14
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare class AuthDigest {
|
|
2
|
+
private _password?;
|
|
3
|
+
private _username?;
|
|
4
|
+
private _initialized;
|
|
5
|
+
private _onError?;
|
|
6
|
+
get username(): string | undefined;
|
|
7
|
+
private _realm?;
|
|
8
|
+
get realm(): string | undefined;
|
|
9
|
+
private _nonce?;
|
|
10
|
+
get nonce(): string | undefined;
|
|
11
|
+
private _uri?;
|
|
12
|
+
get uri(): string | undefined;
|
|
13
|
+
private _algorithm;
|
|
14
|
+
get algorithm(): string;
|
|
15
|
+
private _response?;
|
|
16
|
+
get response(): string | undefined;
|
|
17
|
+
private _opaque?;
|
|
18
|
+
get opaque(): string | undefined;
|
|
19
|
+
private _qop?;
|
|
20
|
+
get qop(): string | undefined;
|
|
21
|
+
private _nc;
|
|
22
|
+
get nc(): string | undefined;
|
|
23
|
+
private _cnonce?;
|
|
24
|
+
get cnonce(): string | undefined;
|
|
25
|
+
private md5;
|
|
26
|
+
constructor(username: string, password: string, errorHandler?: (errorMessage: string) => void);
|
|
27
|
+
init(wwwAuthHeader: string): void;
|
|
28
|
+
getAuthorization(httpMethod: string, path: string): string;
|
|
29
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.AuthDigest = void 0;
|
|
23
|
+
const crypto = __importStar(require("crypto"));
|
|
24
|
+
class AuthDigest {
|
|
25
|
+
constructor(username, password, errorHandler) {
|
|
26
|
+
this._username = username;
|
|
27
|
+
this._password = password;
|
|
28
|
+
this._nc = 0;
|
|
29
|
+
this._initialized = false;
|
|
30
|
+
this._algorithm = 'MD5';
|
|
31
|
+
if (errorHandler)
|
|
32
|
+
this._onError = errorHandler;
|
|
33
|
+
}
|
|
34
|
+
get username() {
|
|
35
|
+
return this._username;
|
|
36
|
+
}
|
|
37
|
+
get realm() {
|
|
38
|
+
return this._realm;
|
|
39
|
+
}
|
|
40
|
+
get nonce() {
|
|
41
|
+
return this._nonce;
|
|
42
|
+
}
|
|
43
|
+
get uri() {
|
|
44
|
+
return this._uri;
|
|
45
|
+
}
|
|
46
|
+
get algorithm() {
|
|
47
|
+
return this._algorithm;
|
|
48
|
+
}
|
|
49
|
+
get response() {
|
|
50
|
+
return this._response;
|
|
51
|
+
}
|
|
52
|
+
get opaque() {
|
|
53
|
+
return this._opaque;
|
|
54
|
+
}
|
|
55
|
+
get qop() {
|
|
56
|
+
return this._qop;
|
|
57
|
+
}
|
|
58
|
+
get nc() {
|
|
59
|
+
var _a;
|
|
60
|
+
this._nc++;
|
|
61
|
+
var myHex = ('0000000' + ((_a = this._nc) === null || _a === void 0 ? void 0 : _a.toString(16))).substr(-8);
|
|
62
|
+
return myHex;
|
|
63
|
+
}
|
|
64
|
+
get cnonce() {
|
|
65
|
+
this._cnonce = this.md5(String(new Date().getTime()));
|
|
66
|
+
return this._cnonce;
|
|
67
|
+
}
|
|
68
|
+
md5(data) {
|
|
69
|
+
let md5 = crypto.createHash('md5');
|
|
70
|
+
let result = md5.update(data).digest('hex');
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
init(wwwAuthHeader) {
|
|
74
|
+
let authSplit = wwwAuthHeader.split(',');
|
|
75
|
+
for (let item of authSplit) {
|
|
76
|
+
if (item.indexOf('realm=') >= 0) {
|
|
77
|
+
let realmSplit = item.split('="');
|
|
78
|
+
this._realm = realmSplit[realmSplit.length - 1];
|
|
79
|
+
this._realm = this._realm.substring(0, this._realm.length - 1);
|
|
80
|
+
}
|
|
81
|
+
if (item.indexOf('nonce=') >= 0) {
|
|
82
|
+
let nonceSplit = item.split('="');
|
|
83
|
+
this._nonce = nonceSplit[nonceSplit.length - 1];
|
|
84
|
+
this._nonce = this._nonce.substring(0, this._nonce.length - 1);
|
|
85
|
+
}
|
|
86
|
+
if (item.indexOf('qop=') >= 0) {
|
|
87
|
+
let qopSplit = item.split('="');
|
|
88
|
+
this._qop = qopSplit[qopSplit.length - 1];
|
|
89
|
+
this._qop = this._qop.substring(0, this._qop.length - 1);
|
|
90
|
+
}
|
|
91
|
+
if (item.indexOf('opaque=') >= 0) {
|
|
92
|
+
let opaqueSplit = item.split('="');
|
|
93
|
+
this._opaque = opaqueSplit[opaqueSplit.length - 1];
|
|
94
|
+
this._opaque = this._opaque.substring(0, this._opaque.length - 1);
|
|
95
|
+
}
|
|
96
|
+
if (item.indexOf('algorithm=') >= 0) {
|
|
97
|
+
let algorithmSplit = item.split('=');
|
|
98
|
+
this._algorithm = algorithmSplit[algorithmSplit.length - 1];
|
|
99
|
+
this._algorithm = this._algorithm.substring(0, this._algorithm.length);
|
|
100
|
+
if (this._onError && this.algorithm !== 'MD5') {
|
|
101
|
+
this._onError(`Algorithm ${this.algorithm} is ont supported. Only MD5 is supportet`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
this._initialized = true;
|
|
106
|
+
}
|
|
107
|
+
getAuthorization(httpMethod, path) {
|
|
108
|
+
if (!this._initialized)
|
|
109
|
+
return '';
|
|
110
|
+
const nc = this.nc;
|
|
111
|
+
const cnonce = this.cnonce;
|
|
112
|
+
let HA1 = this.md5(this.username + ':' + this.realm + ':' + this._password);
|
|
113
|
+
let HA2 = this.md5(httpMethod + ':' + path);
|
|
114
|
+
let response = this.md5(HA1 + ':' + this.nonce + ':' + nc + ':' + cnonce + ':' + this.qop + ':' + HA2);
|
|
115
|
+
let res = `Digest username="${this.username}",`;
|
|
116
|
+
res += `realm="${this.realm}",`;
|
|
117
|
+
res += `nonce="${this.nonce}",`;
|
|
118
|
+
res += `uri="${path}",`;
|
|
119
|
+
res += `cnonce="${cnonce}",`;
|
|
120
|
+
res += `nc=${nc},`;
|
|
121
|
+
res += `algorithm=${this.algorithm},`;
|
|
122
|
+
res += `response="${response}",`;
|
|
123
|
+
res += `qop="${this.qop}",`;
|
|
124
|
+
res += `opaque="${this.opaque}"`;
|
|
125
|
+
return res;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.AuthDigest = AuthDigest;
|
package/dist/Digest.d.ts
ADDED
package/dist/Digest.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.Digest = void 0;
|
|
23
|
+
const https = __importStar(require("https"));
|
|
24
|
+
const url = __importStar(require("url"));
|
|
25
|
+
const AuthDigest_1 = require("./AuthDigest");
|
|
26
|
+
class Digest {
|
|
27
|
+
constructor(username, password) {
|
|
28
|
+
this._authDigest = new AuthDigest_1.AuthDigest(username, password, (err) => {
|
|
29
|
+
console.error(err);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
request(options, data, retryCount = 0) {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
var _a;
|
|
35
|
+
let resData = '';
|
|
36
|
+
if (!options.headers)
|
|
37
|
+
options.headers = {};
|
|
38
|
+
options.headers.Authorization = (_a = this._authDigest) === null || _a === void 0 ? void 0 : _a.getAuthorization(options.method, options.path);
|
|
39
|
+
const req = https.request(options, (res) => {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
if (res.statusCode == 401) {
|
|
42
|
+
if (retryCount > 1) {
|
|
43
|
+
reject('Authentication failed');
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
retryCount++;
|
|
47
|
+
const wwwAuth = res.headers['www-authenticate'];
|
|
48
|
+
if (!wwwAuth.startsWith('Digest')) {
|
|
49
|
+
reject('Unsupported authentication method. Supported authentication schemes: Digest');
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
(_a = this._authDigest) === null || _a === void 0 ? void 0 : _a.init(wwwAuth);
|
|
53
|
+
if (!options.headers)
|
|
54
|
+
options.headers = {};
|
|
55
|
+
options.headers.Authorization = (_b = this._authDigest) === null || _b === void 0 ? void 0 : _b.getAuthorization(options.method, options.path);
|
|
56
|
+
return this.request(options, data, retryCount)
|
|
57
|
+
.then((value) => {
|
|
58
|
+
resolve(value);
|
|
59
|
+
})
|
|
60
|
+
.catch((resaon) => {
|
|
61
|
+
reject(resaon);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
65
|
+
res.setEncoding('utf8');
|
|
66
|
+
res.on('data', (chunk) => {
|
|
67
|
+
resData += chunk;
|
|
68
|
+
});
|
|
69
|
+
res.on('end', () => {
|
|
70
|
+
resolve(resData);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
console.error('status code failed!!');
|
|
75
|
+
reject('status code failed!!');
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
req.on('error', (e) => {
|
|
80
|
+
console.error(`problem with request: ${e.message}`);
|
|
81
|
+
reject(e);
|
|
82
|
+
});
|
|
83
|
+
if (data) {
|
|
84
|
+
req.write(data);
|
|
85
|
+
}
|
|
86
|
+
req.end();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
get(requestUrl, data) {
|
|
90
|
+
const uri = url.parse(requestUrl, true);
|
|
91
|
+
const options = {
|
|
92
|
+
hostname: uri.hostname,
|
|
93
|
+
port: uri.port,
|
|
94
|
+
path: uri.pathname,
|
|
95
|
+
method: 'GET',
|
|
96
|
+
headers: {
|
|
97
|
+
Connection: 'Keep-Alive',
|
|
98
|
+
'Content-Type': 'application/json',
|
|
99
|
+
Accept: 'application/json',
|
|
100
|
+
Host: uri.hostname,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
return this.request(options, data);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.Digest = Digest;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Zappi } from './models/Zappi';
|
|
2
|
+
export declare enum ZappiChargeMode {
|
|
3
|
+
Off = 4,
|
|
4
|
+
Fast = 1,
|
|
5
|
+
Eco = 2,
|
|
6
|
+
EcoPlus = 3
|
|
7
|
+
}
|
|
8
|
+
export declare enum ZappiBoostMode {
|
|
9
|
+
Manual = 10,
|
|
10
|
+
Smart = 11,
|
|
11
|
+
Stop = 2
|
|
12
|
+
}
|
|
13
|
+
export declare class MyEnergi {
|
|
14
|
+
private _config;
|
|
15
|
+
private _digest;
|
|
16
|
+
constructor(username: string, password: string);
|
|
17
|
+
getStatusAll(): Promise<any>;
|
|
18
|
+
getStatusZappiAll(): Promise<Zappi[]>;
|
|
19
|
+
getStatusZappi(serialNumber: string): Promise<Zappi | null>;
|
|
20
|
+
setZappiChargeMode(serialNo: string, chargeMode: ZappiChargeMode): Promise<any>;
|
|
21
|
+
setZappiBoostMode(serialNo: string, boostMode: ZappiBoostMode, kwh?: number, completeTime?: string): Promise<any>;
|
|
22
|
+
setZappiGreenLevel(serialNo: string, percentage: number): Promise<any>;
|
|
23
|
+
}
|
package/dist/MyEnergi.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MyEnergi = exports.ZappiBoostMode = exports.ZappiChargeMode = void 0;
|
|
13
|
+
const Digest_1 = require("./Digest");
|
|
14
|
+
var ZappiChargeMode;
|
|
15
|
+
(function (ZappiChargeMode) {
|
|
16
|
+
ZappiChargeMode[ZappiChargeMode["Off"] = 4] = "Off";
|
|
17
|
+
ZappiChargeMode[ZappiChargeMode["Fast"] = 1] = "Fast";
|
|
18
|
+
ZappiChargeMode[ZappiChargeMode["Eco"] = 2] = "Eco";
|
|
19
|
+
ZappiChargeMode[ZappiChargeMode["EcoPlus"] = 3] = "EcoPlus";
|
|
20
|
+
})(ZappiChargeMode = exports.ZappiChargeMode || (exports.ZappiChargeMode = {}));
|
|
21
|
+
var ZappiBoostMode;
|
|
22
|
+
(function (ZappiBoostMode) {
|
|
23
|
+
ZappiBoostMode[ZappiBoostMode["Manual"] = 10] = "Manual";
|
|
24
|
+
ZappiBoostMode[ZappiBoostMode["Smart"] = 11] = "Smart";
|
|
25
|
+
ZappiBoostMode[ZappiBoostMode["Stop"] = 2] = "Stop";
|
|
26
|
+
})(ZappiBoostMode = exports.ZappiBoostMode || (exports.ZappiBoostMode = {}));
|
|
27
|
+
class MyEnergi {
|
|
28
|
+
constructor(username, password) {
|
|
29
|
+
this._config = {
|
|
30
|
+
username: '',
|
|
31
|
+
password: '',
|
|
32
|
+
eddi_url: 'https://s18.myenergi.net/cgi-jstatus-E',
|
|
33
|
+
zappi_url: 'https://s18.myenergi.net/cgi-jstatus-Z',
|
|
34
|
+
harvi_url: 'https://s18.myenergi.net/cgi-jstatus-H',
|
|
35
|
+
status_url: 'https://s18.myenergi.net/cgi-jstatus-*',
|
|
36
|
+
dayhour_url: 'https://s18.myenergi.net/cgi-jdayhour-',
|
|
37
|
+
zappi_mode_url: 'https://s18.myenergi.net/cgi-zappi-mode-Z',
|
|
38
|
+
zappi_min_green_url: 'https://s18.myenergi.net/cgi-set-min-green-Z',
|
|
39
|
+
//https://s18.myenergi.net/cgi-jdayhour-Znnnnnnnn-YYYY-MM-DD
|
|
40
|
+
};
|
|
41
|
+
this._config.username = username;
|
|
42
|
+
this._config.password = password;
|
|
43
|
+
this._digest = new Digest_1.Digest(this._config.username, this._config.password);
|
|
44
|
+
}
|
|
45
|
+
getStatusAll() {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const data = yield this._digest.get(this._config.status_url);
|
|
48
|
+
return data;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
getStatusZappiAll() {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const data = yield this._digest.get(this._config.zappi_url);
|
|
54
|
+
const jsonData = JSON.parse(data);
|
|
55
|
+
if (jsonData.zappi)
|
|
56
|
+
return Object.assign([], jsonData.zappi);
|
|
57
|
+
else
|
|
58
|
+
return [];
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
getStatusZappi(serialNumber) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const data = yield this._digest.get(this._config.zappi_url);
|
|
64
|
+
const jsonData = JSON.parse(data);
|
|
65
|
+
if (jsonData.zappi) {
|
|
66
|
+
const zappi = Object.assign([], jsonData.zappi).find((zappi) => {
|
|
67
|
+
return zappi.sno === serialNumber;
|
|
68
|
+
});
|
|
69
|
+
if (zappi)
|
|
70
|
+
return Object.assign({}, zappi);
|
|
71
|
+
else
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
else
|
|
75
|
+
return null;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
setZappiChargeMode(serialNo, chargeMode) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
const url = `${this._config.zappi_mode_url}${serialNo}-${chargeMode}-0-0-0000`;
|
|
81
|
+
const data = yield this._digest.get(url);
|
|
82
|
+
return data;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
setZappiBoostMode(serialNo, boostMode, kwh = 0, completeTime = '0000') {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
if (boostMode === ZappiBoostMode.Stop) {
|
|
88
|
+
kwh = 0;
|
|
89
|
+
completeTime = '0000';
|
|
90
|
+
}
|
|
91
|
+
const url = `${this._config.zappi_mode_url}${serialNo}-0-${boostMode}-${kwh}-${completeTime}`;
|
|
92
|
+
const data = yield this._digest.get(url);
|
|
93
|
+
return data;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
setZappiGreenLevel(serialNo, percentage) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
const url = `${this._config.zappi_min_green_url}${serialNo}-${percentage}`;
|
|
99
|
+
const data = yield this._digest.get(url);
|
|
100
|
+
return data;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.MyEnergi = MyEnergi;
|
|
File without changes
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface Zappi {
|
|
2
|
+
sno: string;
|
|
3
|
+
dat: string;
|
|
4
|
+
tim: string;
|
|
5
|
+
ectp1: number;
|
|
6
|
+
ectp2: number;
|
|
7
|
+
ectp3: number;
|
|
8
|
+
ectp4: number;
|
|
9
|
+
ectp5: number;
|
|
10
|
+
ectp6: number;
|
|
11
|
+
ectt1: string;
|
|
12
|
+
ectt2: string;
|
|
13
|
+
ectt3: string;
|
|
14
|
+
ectt4: string;
|
|
15
|
+
ectt5: string;
|
|
16
|
+
ectt6: string;
|
|
17
|
+
bsm: number;
|
|
18
|
+
bst: number;
|
|
19
|
+
cmt: number;
|
|
20
|
+
dst: number;
|
|
21
|
+
div: number;
|
|
22
|
+
frq: number;
|
|
23
|
+
fwv: string;
|
|
24
|
+
gen: number;
|
|
25
|
+
grd: number;
|
|
26
|
+
pha: number;
|
|
27
|
+
pri: number;
|
|
28
|
+
sta: number;
|
|
29
|
+
tz: number;
|
|
30
|
+
vol: number;
|
|
31
|
+
che: number;
|
|
32
|
+
bss: number;
|
|
33
|
+
lck: number;
|
|
34
|
+
pst: string;
|
|
35
|
+
zmo: number;
|
|
36
|
+
zs: number;
|
|
37
|
+
rdc: number;
|
|
38
|
+
rac: number;
|
|
39
|
+
rrac: number;
|
|
40
|
+
zsl: number;
|
|
41
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "myenergi-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/**/*"
|
|
9
|
+
],
|
|
7
10
|
"scripts": {
|
|
8
11
|
"build": "tsc",
|
|
9
|
-
"test": "env TS_NODE_PROJECT='tsconfig.testing.json' mocha -r ts-node/register tests/**/*-spec.ts"
|
|
12
|
+
"test": "env TS_NODE_PROJECT='tsconfig.testing.json' mocha -r ts-node/register tests/**/*-spec.ts",
|
|
13
|
+
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
14
|
+
"lint": "tslint -p tsconfig.json",
|
|
15
|
+
"prepare": "tsc",
|
|
16
|
+
"preversion": "npm test",
|
|
17
|
+
"version": "",
|
|
18
|
+
"postversion": ""
|
|
10
19
|
},
|
|
11
20
|
"repository": {
|
|
12
21
|
"type": "git",
|
|
@@ -25,15 +34,15 @@
|
|
|
25
34
|
},
|
|
26
35
|
"homepage": "https://github.com/bisand/myenergi-api#readme",
|
|
27
36
|
"dependencies": {
|
|
28
|
-
"dotenv": "^10.0.0"
|
|
29
|
-
"node-digest-auth-client": "^1.0.6"
|
|
37
|
+
"dotenv": "^10.0.0"
|
|
30
38
|
},
|
|
31
39
|
"devDependencies": {
|
|
40
|
+
"@types/chai": "^4.2.22",
|
|
32
41
|
"@types/mocha": "^9.0.0",
|
|
33
42
|
"@types/node": "^16.11.9",
|
|
34
|
-
"assert": "^2.0.0",
|
|
35
43
|
"chai": "^4.3.4",
|
|
36
44
|
"mocha": "^9.1.3",
|
|
45
|
+
"prettier": "^2.4.1",
|
|
37
46
|
"ts-node": "^10.4.0",
|
|
38
47
|
"tslint": "^6.1.3",
|
|
39
48
|
"typescript": "^4.5.2"
|
package/src/AuthDigest.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import * as crypto from 'crypto';
|
|
2
|
-
|
|
3
|
-
export class AuthDigest {
|
|
4
|
-
private _password?: string | undefined;
|
|
5
|
-
private _username?: string | undefined;
|
|
6
|
-
private _initialized: boolean;
|
|
7
|
-
|
|
8
|
-
public get username(): string | undefined {
|
|
9
|
-
return this._username;
|
|
10
|
-
}
|
|
11
|
-
private _realm?: string | undefined;
|
|
12
|
-
public get realm(): string | undefined {
|
|
13
|
-
return this._realm;
|
|
14
|
-
}
|
|
15
|
-
private _nonce?: string | undefined;
|
|
16
|
-
public get nonce(): string | undefined {
|
|
17
|
-
return this._nonce;
|
|
18
|
-
}
|
|
19
|
-
private _uri?: string | undefined;
|
|
20
|
-
public get uri(): string | undefined {
|
|
21
|
-
return this._uri;
|
|
22
|
-
}
|
|
23
|
-
private _algorithm?: string | undefined;
|
|
24
|
-
public get algorithm(): string | undefined {
|
|
25
|
-
return this._algorithm;
|
|
26
|
-
}
|
|
27
|
-
private _response?: string | undefined;
|
|
28
|
-
public get response(): string | undefined {
|
|
29
|
-
return this._response;
|
|
30
|
-
}
|
|
31
|
-
private _opaque?: string | undefined;
|
|
32
|
-
public get opaque(): string | undefined {
|
|
33
|
-
return this._opaque;
|
|
34
|
-
}
|
|
35
|
-
private _qop?: string | undefined;
|
|
36
|
-
public get qop(): string | undefined {
|
|
37
|
-
return this._qop;
|
|
38
|
-
}
|
|
39
|
-
private _nc: number;
|
|
40
|
-
public get nc(): string | undefined {
|
|
41
|
-
this._nc++;
|
|
42
|
-
var myHex = ("0000000" + this._nc?.toString(16)).substr(-8);
|
|
43
|
-
return myHex;
|
|
44
|
-
}
|
|
45
|
-
private _cnonce?: string | undefined;
|
|
46
|
-
public get cnonce(): string | undefined {
|
|
47
|
-
this._cnonce = this.md5(String(new Date().getTime()));
|
|
48
|
-
return this._cnonce;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private md5(data: string) {
|
|
52
|
-
let md5 = crypto.createHash('md5');
|
|
53
|
-
let result = md5.update(data).digest('hex');
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
constructor(username: string, password: string) {
|
|
58
|
-
this._username = username;
|
|
59
|
-
this._password = password;
|
|
60
|
-
this._nc = 0;
|
|
61
|
-
this._initialized = false;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public init(wwwAuthHeader: string) {
|
|
65
|
-
|
|
66
|
-
let authSplit = wwwAuthHeader.split(',') as string[];
|
|
67
|
-
|
|
68
|
-
for (let item of authSplit) {
|
|
69
|
-
if (item.indexOf('realm=') >= 0) {
|
|
70
|
-
let realmSplit = item.split('="');
|
|
71
|
-
this._realm = realmSplit[realmSplit.length - 1];
|
|
72
|
-
this._realm = this._realm.substring(0, this._realm.length - 1);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (item.indexOf('nonce=') >= 0) {
|
|
76
|
-
let nonceSplit = item.split('="');
|
|
77
|
-
this._nonce = nonceSplit[nonceSplit.length - 1];
|
|
78
|
-
this._nonce = this._nonce.substring(0, this._nonce.length - 1);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (item.indexOf('qop=') >= 0) {
|
|
82
|
-
let qopSplit = item.split('="');
|
|
83
|
-
this._qop = qopSplit[qopSplit.length - 1];
|
|
84
|
-
this._qop = this._qop.substring(0, this._qop.length - 1);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (item.indexOf('opaque=') >= 0) {
|
|
88
|
-
let opaqueSplit = item.split('="');
|
|
89
|
-
this._opaque = opaqueSplit[opaqueSplit.length - 1];
|
|
90
|
-
this._opaque = this._opaque.substring(0, this._opaque.length - 1);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
this._initialized = true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
public getAuthorization(httpMethod: string, path: string): string {
|
|
97
|
-
if(!this._initialized)
|
|
98
|
-
return '';
|
|
99
|
-
|
|
100
|
-
const nc = this.nc;
|
|
101
|
-
const cnonce = this.cnonce;
|
|
102
|
-
let HA1 = this.md5(this.username + ":" + this.realm + ":" + this._password);
|
|
103
|
-
let HA2 = this.md5(httpMethod + ":" + path);
|
|
104
|
-
let response = this.md5(HA1 + ":" + this.nonce + ":" + nc + ":" + cnonce + ":" + this.qop + ":" + HA2);
|
|
105
|
-
|
|
106
|
-
let res = `Digest username="${this.username}",`;
|
|
107
|
-
res += `realm="${this.realm}",`;
|
|
108
|
-
res += `nonce="${this.nonce}",`;
|
|
109
|
-
res += `uri="${path}",`;
|
|
110
|
-
res += `cnonce="${cnonce}",`;
|
|
111
|
-
res += `nc=${nc},`;
|
|
112
|
-
res += `algorithm=MD5,`;
|
|
113
|
-
res += `response="${response}",`;
|
|
114
|
-
res += `qop="${this.qop}",`;
|
|
115
|
-
res += `opaque="${this.opaque}"`;
|
|
116
|
-
return res;
|
|
117
|
-
}
|
|
118
|
-
}
|
package/src/Digest.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import * as https from 'https';
|
|
2
|
-
import { RequestOptions } from 'https';
|
|
3
|
-
import * as url from "url";
|
|
4
|
-
import { AuthDigest } from './AuthDigest';
|
|
5
|
-
|
|
6
|
-
export class Digest {
|
|
7
|
-
private _authDigest?: AuthDigest;
|
|
8
|
-
|
|
9
|
-
constructor(username: string, password: string) {
|
|
10
|
-
this._authDigest = new AuthDigest(username, password);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
private request(options: RequestOptions, data?: any, retryCount: number = 0): Promise<any> {
|
|
14
|
-
return new Promise<any>((resolve, reject) => {
|
|
15
|
-
let resData: string = '';
|
|
16
|
-
if (!options.headers)
|
|
17
|
-
options.headers = {};
|
|
18
|
-
options.headers.Authorization = this._authDigest?.getAuthorization(options.method as string, options.path as string);
|
|
19
|
-
const req = https.request(options, (res: any) => {
|
|
20
|
-
|
|
21
|
-
if (res.statusCode == 401) {
|
|
22
|
-
if (retryCount > 1) {
|
|
23
|
-
reject("Authentication failed");
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
retryCount++;
|
|
27
|
-
const wwwAuth = res.headers["www-authenticate"] as string;
|
|
28
|
-
if(!wwwAuth.startsWith('Digest'))
|
|
29
|
-
{
|
|
30
|
-
reject('Unsupported authentication method. Supported authentication schemes: Digest');
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
this._authDigest?.init(wwwAuth);
|
|
34
|
-
if (!options.headers)
|
|
35
|
-
options.headers = {};
|
|
36
|
-
options.headers.Authorization = this._authDigest?.getAuthorization(options.method as string, options.path as string);
|
|
37
|
-
return this.request(options, data, retryCount).then(value => {
|
|
38
|
-
resolve(value);
|
|
39
|
-
}).catch(resaon => {
|
|
40
|
-
reject(resaon);
|
|
41
|
-
});
|
|
42
|
-
} else if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
43
|
-
res.setEncoding('utf8');
|
|
44
|
-
res.on('data', (chunk: string) => {
|
|
45
|
-
resData += chunk;
|
|
46
|
-
});
|
|
47
|
-
res.on('end', () => {
|
|
48
|
-
resolve(resData);
|
|
49
|
-
});
|
|
50
|
-
} else {
|
|
51
|
-
console.error("status code failed!!");
|
|
52
|
-
reject("status code failed!!");
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
req.on('error', (e: any) => {
|
|
58
|
-
console.error(`problem with request: ${e.message}`);
|
|
59
|
-
reject(e);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (data) {
|
|
63
|
-
req.write(data);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
req.end();
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
public get(requestUrl: string, data?: any): Promise<any> {
|
|
71
|
-
const uri = url.parse(requestUrl, true);
|
|
72
|
-
const options: RequestOptions = {
|
|
73
|
-
hostname: uri.hostname,
|
|
74
|
-
port: uri.port,
|
|
75
|
-
path: uri.pathname,
|
|
76
|
-
method: 'GET',
|
|
77
|
-
headers: {
|
|
78
|
-
'Connection': 'Keep-Alive',
|
|
79
|
-
'Content-Type': 'application/json',
|
|
80
|
-
'Accept': 'application/json',
|
|
81
|
-
'Host': uri.hostname as string,
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
return this.request(options, data);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
}
|
package/src/MyEnergi.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { Digest } from './Digest';
|
|
2
|
-
|
|
3
|
-
export enum ZappiChargeMode {
|
|
4
|
-
Off = 4,
|
|
5
|
-
Fast = 1,
|
|
6
|
-
Eco = 2,
|
|
7
|
-
EcoPlus = 3
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export enum ZappiBoostMode {
|
|
11
|
-
Manual = 10,
|
|
12
|
-
Smart = 11,
|
|
13
|
-
Stop = 2
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export class MyEnergi {
|
|
17
|
-
private _config = {
|
|
18
|
-
username: '',
|
|
19
|
-
password: '',
|
|
20
|
-
eddi_url: 'https://s18.myenergi.net/cgi-jstatus-E',
|
|
21
|
-
zappi_url: 'https://s18.myenergi.net/cgi-jstatus-Z',
|
|
22
|
-
harvi_url: 'https://s18.myenergi.net/cgi-jstatus-H',
|
|
23
|
-
status_url: 'https://s18.myenergi.net/cgi-jstatus-*',
|
|
24
|
-
dayhour_url: 'https://s18.myenergi.net/cgi-jdayhour-',
|
|
25
|
-
zappi_mode_url: 'https://s18.myenergi.net/cgi-zappi-mode-Z',
|
|
26
|
-
zappi_min_green_url: 'https://s18.myenergi.net/cgi-set-min-green-Z'
|
|
27
|
-
//https://s18.myenergi.net/cgi-jdayhour-Znnnnnnnn-YYYY-MM-DD
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
private _digest: Digest;
|
|
31
|
-
|
|
32
|
-
constructor(username: string, password: string) {
|
|
33
|
-
this._config.username = username;
|
|
34
|
-
this._config.password = password;
|
|
35
|
-
this._digest = new Digest(this._config.username, this._config.password);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public async getStatus(): Promise<any> {
|
|
39
|
-
const data = await this._digest.get(this._config.status_url);
|
|
40
|
-
return data;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public async getZappi(): Promise<any> {
|
|
44
|
-
const data = await this._digest.get(this._config.zappi_url);
|
|
45
|
-
return data;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public async setZappiChargeMode(serialNo: string, chargeMode: ZappiChargeMode): Promise<any> {
|
|
49
|
-
const url = `${this._config.zappi_mode_url}${serialNo}-${chargeMode}-0-0-0000`;
|
|
50
|
-
const data = await this._digest.get(url);
|
|
51
|
-
return data;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public async setZappiBoostMode(serialNo: string, boostMode: ZappiBoostMode, kwh: number = 0, completeTime: string = '0000'): Promise<any> {
|
|
55
|
-
if (boostMode === ZappiBoostMode.Stop) {
|
|
56
|
-
kwh = 0;
|
|
57
|
-
completeTime = '0000';
|
|
58
|
-
}
|
|
59
|
-
const url = `${this._config.zappi_mode_url}${serialNo}-0-${boostMode}-${kwh}-${completeTime}`;
|
|
60
|
-
const data = await this._digest.get(url);
|
|
61
|
-
return data;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public async setZappiGreenLevel(serialNo: string, percentage: number): Promise<any> {
|
|
65
|
-
const url = `${this._config.zappi_min_green_url}${serialNo}-${percentage}`;
|
|
66
|
-
const data = await this._digest.get(url);
|
|
67
|
-
return data;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
}
|
package/tests/MyEnergi-spec.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { MyEnergi } from '../src/MyEnergi'
|
|
2
|
-
import { equal } from "assert";
|
|
3
|
-
import { expect } from 'chai'
|
|
4
|
-
|
|
5
|
-
describe("MyEnergi Tests", () => {
|
|
6
|
-
it("should be able to test", () => {
|
|
7
|
-
equal(true, true);
|
|
8
|
-
});
|
|
9
|
-
it("should be able to create instance", () => {
|
|
10
|
-
const myenergi = new MyEnergi('test', 'pwd');
|
|
11
|
-
expect(myenergi).to.be.instanceOf(MyEnergi);
|
|
12
|
-
});
|
|
13
|
-
});
|
package/tests/debug.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { exit } from 'process';
|
|
2
|
-
import { MyEnergi } from '../src';
|
|
3
|
-
import * as dotenv from 'dotenv';
|
|
4
|
-
import { ZappiBoostMode, ZappiChargeMode } from '../src/MyEnergi';
|
|
5
|
-
|
|
6
|
-
dotenv.config();
|
|
7
|
-
const myenergi = new MyEnergi(process.env.USERNAME as string, process.env.PASSWORD as string);
|
|
8
|
-
myenergi.getStatus().then(val => {
|
|
9
|
-
console.log(val);
|
|
10
|
-
myenergi.getZappi().then(v => {
|
|
11
|
-
console.log(v);
|
|
12
|
-
const sno: string = JSON.parse(v).zappi[0].sno;
|
|
13
|
-
myenergi.setZappiChargeMode(sno, ZappiChargeMode.Fast).then(x => {
|
|
14
|
-
console.log(x);
|
|
15
|
-
myenergi.setZappiBoostMode(sno, ZappiBoostMode.Stop).then(x => {
|
|
16
|
-
console.log(x);
|
|
17
|
-
myenergi.setZappiGreenLevel(sno, 0).then(x => {
|
|
18
|
-
console.log(x);
|
|
19
|
-
exit();
|
|
20
|
-
}).catch(err => {
|
|
21
|
-
console.error(err);
|
|
22
|
-
exit();
|
|
23
|
-
});
|
|
24
|
-
}).catch(err => {
|
|
25
|
-
console.error(err);
|
|
26
|
-
exit();
|
|
27
|
-
});
|
|
28
|
-
}).catch(err => {
|
|
29
|
-
console.error(err);
|
|
30
|
-
exit();
|
|
31
|
-
});
|
|
32
|
-
}).catch(err => {
|
|
33
|
-
console.error(err);
|
|
34
|
-
exit();
|
|
35
|
-
});
|
|
36
|
-
}).catch(err => {
|
|
37
|
-
console.error(err);
|
|
38
|
-
exit();
|
|
39
|
-
});
|
package/tsconfig.testing.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "commonjs",
|
|
4
|
-
"target": "es2015",
|
|
5
|
-
"lib": ["es2017"],
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"declaration": false,
|
|
8
|
-
"noImplicitAny": false,
|
|
9
|
-
"removeComments": true,
|
|
10
|
-
"inlineSourceMap": true,
|
|
11
|
-
"moduleResolution": "node",
|
|
12
|
-
},
|
|
13
|
-
"include": ["src/**/*.ts", "src/**/*.d.ts", "tests/**/*.ts"]
|
|
14
|
-
}
|