myenergi-api 0.0.8-0 → 0.3.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/package.json CHANGED
@@ -1,12 +1,17 @@
1
1
  {
2
2
  "name": "myenergi-api",
3
- "version": "0.0.8-0",
3
+ "version": "0.3.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"
10
15
  },
11
16
  "repository": {
12
17
  "type": "git",
@@ -25,15 +30,15 @@
25
30
  },
26
31
  "homepage": "https://github.com/bisand/myenergi-api#readme",
27
32
  "dependencies": {
28
- "dotenv": "^10.0.0",
29
- "node-digest-auth-client": "^1.0.6"
33
+ "dotenv": "^10.0.0"
30
34
  },
31
35
  "devDependencies": {
36
+ "@types/chai": "^4.2.22",
32
37
  "@types/mocha": "^9.0.0",
33
38
  "@types/node": "^16.11.9",
34
- "assert": "^2.0.0",
35
39
  "chai": "^4.3.4",
36
40
  "mocha": "^9.1.3",
41
+ "prettier": "^2.4.1",
37
42
  "ts-node": "^10.4.0",
38
43
  "tslint": "^6.1.3",
39
44
  "typescript": "^4.5.2"
package/src/AuthDigest.ts DELETED
@@ -1,119 +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 realm, nonce, qop, opaque;
67
- let authSplit = wwwAuthHeader.split(',') as string[];
68
-
69
- for (let item of authSplit) {
70
- if (item.indexOf('realm=') >= 0) {
71
- let realmSplit = item.split('="');
72
- this._realm = realmSplit[realmSplit.length - 1];
73
- this._realm = this._realm.substring(0, this._realm.length - 1);
74
- }
75
-
76
- if (item.indexOf('nonce=') >= 0) {
77
- let nonceSplit = item.split('="');
78
- this._nonce = nonceSplit[nonceSplit.length - 1];
79
- this._nonce = this._nonce.substring(0, this._nonce.length - 1);
80
- }
81
-
82
- if (item.indexOf('qop=') >= 0) {
83
- let qopSplit = item.split('="');
84
- this._qop = qopSplit[qopSplit.length - 1];
85
- this._qop = this._qop.substring(0, this._qop.length - 1);
86
- }
87
-
88
- if (item.indexOf('opaque=') >= 0) {
89
- let opaqueSplit = item.split('="');
90
- this._opaque = opaqueSplit[opaqueSplit.length - 1];
91
- this._opaque = this._opaque.substring(0, this._opaque.length - 1);
92
- }
93
- }
94
- this._initialized = true;
95
- }
96
-
97
- public getAuthorization(httpMethod: string, path: string): string {
98
- if(!this._initialized)
99
- return '';
100
-
101
- const nc = this.nc;
102
- const cnonce = this.cnonce;
103
- let HA1 = this.md5(this.username + ":" + this.realm + ":" + this._password);
104
- let HA2 = this.md5(httpMethod + ":" + path);
105
- let response = this.md5(HA1 + ":" + this.nonce + ":" + nc + ":" + cnonce + ":" + this.qop + ":" + HA2);
106
-
107
- let res = `Digest username="${this._username}",`;
108
- res += `realm="${this.realm}",`;
109
- res += `nonce="${this.nonce}",`;
110
- res += `uri="${path}",`;
111
- res += `cnonce="${cnonce}",`;
112
- res += `nc=${nc},`;
113
- res += `algorithm=MD5,`;
114
- res += `response="${response}",`;
115
- res += `qop="${this.qop}",`;
116
- res += `opaque="${this.opaque}"`;
117
- return res;
118
- }
119
- }
package/src/Digest.ts DELETED
@@ -1,81 +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 _authorization?: AuthDigest;
8
-
9
- constructor(username: string, password: string) {
10
- this._authorization = 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._authorization?.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
- let auth = res.headers["www-authenticate"];
28
- this._authorization?.init(auth);
29
- if (!options.headers)
30
- options.headers = {};
31
- options.headers.Authorization = this._authorization?.getAuthorization(options.method as string, options.path as string);
32
- return this.request(options, data, retryCount).then(value => {
33
- resolve(value);
34
- }).catch(resaon => {
35
- reject(resaon);
36
- });
37
- } else if (res.statusCode >= 200 && res.statusCode < 300) {
38
- res.setEncoding('utf8');
39
- res.on('data', (chunk: string) => {
40
- resData += chunk;
41
- });
42
- res.on('end', () => {
43
- resolve(resData);
44
- });
45
- } else {
46
- console.error("status code failed!!");
47
- reject("status code failed!!");
48
- return;
49
- }
50
- });
51
-
52
- req.on('error', (e: any) => {
53
- console.error(`problem with request: ${e.message}`);
54
- reject(e);
55
- });
56
-
57
- if (data) {
58
- req.write(data);
59
- }
60
-
61
- req.end();
62
- });
63
- }
64
-
65
- public get(requestUrl: string, data?: any): Promise<any> {
66
- const uri = url.parse(requestUrl, true);
67
- const options: RequestOptions = {
68
- hostname: uri.hostname,
69
- port: uri.port,
70
- path: uri.pathname,
71
- method: 'GET',
72
- headers: {
73
- 'Connection': 'Keep-Alive',
74
- 'Content-Type': 'application/json',
75
- 'Host': uri.hostname as string,
76
- }
77
- };
78
- return this.request(options, data);
79
- }
80
-
81
- }
package/src/MyEnergi.ts DELETED
@@ -1,51 +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
- export enum ZappiBoostMode {
10
- Fast = 0,
11
- Eco = 1,
12
- EcoPlus = 2
13
- }
14
- export class MyEnergi {
15
- private _config = {
16
- username: '',
17
- password: '',
18
- eddi_url: 'https://s18.myenergi.net/cgi-jstatus-E',
19
- zappi_url: 'https://s18.myenergi.net/cgi-jstatus-Z',
20
- harvi_url: 'https://s18.myenergi.net/cgi-jstatus-H',
21
- status_url: 'https://s18.myenergi.net/cgi-jstatus-*',
22
- dayhour_url: 'https://s18.myenergi.net/cgi-jdayhour-',
23
- zappi_mode_url: 'https://s18.myenergi.net/cgi-zappi-mode-Z'
24
- //https://s18.myenergi.net/cgi-jdayhour-Znnnnnnnn-YYYY-MM-DD
25
- }
26
-
27
- private _digest: Digest;
28
-
29
- constructor(username: string, password: string) {
30
- this._config.username = username;
31
- this._config.password = password;
32
- this._digest = new Digest(this._config.username, this._config.password);
33
- }
34
-
35
- public async getStatus(): Promise<any> {
36
- const data = await this._digest.get(this._config.status_url);
37
- return data;
38
- }
39
-
40
- public async getZappi(): Promise<any> {
41
- const data = await this._digest.get(this._config.zappi_url);
42
- return data;
43
- }
44
-
45
- public async setZappiChargeMode(serialNo: string, chargeMode: ZappiChargeMode): Promise<any> {
46
- const url = `${this._config.zappi_mode_url}${serialNo}-${chargeMode}-0-0-0000`;
47
- const data = await this._digest.get(url);
48
- return data;
49
- }
50
-
51
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- import { MyEnergi } from './MyEnergi';
2
- export { MyEnergi };
@@ -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,27 +0,0 @@
1
- import { exit } from 'process';
2
- import { MyEnergi } from '../src';
3
- import * as dotenv from 'dotenv';
4
- import { 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
- exit();
16
- }).catch(err => {
17
- console.error(err);
18
- exit();
19
- });
20
- }).catch(err => {
21
- console.error(err);
22
- exit();
23
- });
24
- }).catch(err => {
25
- console.error(err);
26
- exit();
27
- });
@@ -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
- }