myenergi-api 0.0.7-0 → 0.2.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.7-0",
3
+ "version": "0.2.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/Digest.ts DELETED
@@ -1,138 +0,0 @@
1
- import * as https from 'https';
2
- import * as crypto from 'crypto';
3
- import { RequestOptions } from 'https';
4
- import * as url from "url";
5
-
6
- export class Digest {
7
- private _username: string;
8
- private _password: string;
9
- private _authorization: string | undefined;
10
-
11
- constructor(username: string, password: string) {
12
- this._username = username;
13
- this._password = password;
14
- this._authorization = '';
15
- }
16
-
17
- private md5(data: string) {
18
- let md5 = crypto.createHash('md5');
19
- let result = md5.update(data).digest('hex');
20
- return result;
21
- }
22
-
23
- private request(options: RequestOptions, data?: any): Promise<any> {
24
- return new Promise<any>((resolve, reject) => {
25
- const req = https.request(options, (res: any) => {
26
- let resData: string = '';
27
-
28
- res.setEncoding('utf8');
29
- res.on('data', (chunk: string) => {
30
- resData += chunk;
31
- });
32
- res.on('end', () => {
33
- resolve(resData);
34
- });
35
- });
36
-
37
- req.on('error', (e: any) => {
38
- console.error(`problem with request: ${e.message}`);
39
- reject(e);
40
- });
41
-
42
- if (data) {
43
- req.write(data);
44
- }
45
-
46
- req.end();
47
- });
48
- }
49
-
50
- private requestDigest(options: RequestOptions, data?: any): Promise<any> {
51
- return new Promise<any>((resolve, reject) => {
52
- let resData: string = '';
53
- const req = https.request(options, (res: any) => {
54
-
55
- if (res.statusCode == 401) {
56
- let cnonce = this.md5(String(new Date().getTime()));
57
- let auth = res.headers["www-authenticate"];
58
- let realm, nonce, qop;
59
- let authSplit = auth?.split(",") as string[];
60
-
61
- for (let item of authSplit) {
62
- if (item.indexOf("realm=") >= 0) {
63
- let realmSplit = item.split("=\"");
64
- realm = realmSplit[realmSplit.length - 1];
65
- realm = realm.substring(0, realm.length - 1);
66
- }
67
-
68
- if (item.indexOf("nonce=") >= 0) {
69
- let nonceSplit = item.split("=\"");
70
- nonce = nonceSplit[nonceSplit.length - 1];
71
- nonce = nonce.substring(0, nonce.length - 1);
72
- }
73
-
74
- if (item.indexOf("qop=") >= 0) {
75
- let qopSplit = item.split("=\"");
76
- qop = qopSplit[qopSplit.length - 1];
77
- qop = qop.substring(0, qop.length - 1);
78
- }
79
- }
80
-
81
- let HA1 = this.md5(this._username + ":" + realm + ":" + this._password);
82
- let HA2 = this.md5(options.method + ":" + options.path);
83
- let response = this.md5(HA1 + ":" + nonce + ":00000001:" + cnonce + ":" + qop + ":" + HA2);
84
- if (!options.headers)
85
- options.headers = {};
86
- this._authorization = "Digest username=\"" + this._username + "\",realm=\"" + realm + "\",nonce=\"" + nonce + "\",uri=\"" + options.path + "\",cnonce=\"" + cnonce + "\",nc=00000001,algorithm=MD5,response=\"" + response + "\",qop=\"" + qop + "\"";
87
- options.headers.Authorization = this._authorization;
88
- return this.requestDigest(options, data).then(value => {
89
- resolve(value);
90
- }).catch(resaon => {
91
- reject(resaon);
92
- });
93
- } else if (res.statusCode >= 200 && res.statusCode < 300) {
94
- res.setEncoding('utf8');
95
- res.on('data', (chunk: string) => {
96
- resData += chunk;
97
- });
98
- res.on('end', () => {
99
- resolve(resData);
100
- });
101
- } else {
102
- console.error("status code failed!!");
103
- reject("status code failed!!");
104
- return;
105
- }
106
- });
107
-
108
- req.on('error', (e: any) => {
109
- console.error(`problem with request: ${e.message}`);
110
- reject(e);
111
- });
112
-
113
- if (data) {
114
- req.write(data);
115
- }
116
-
117
- req.end();
118
- });
119
- }
120
-
121
- public get(requestUrl: string, data?: any): Promise<any> {
122
- const uri = url.parse(requestUrl);
123
- const options: RequestOptions = {
124
- hostname: uri.hostname,
125
- port: uri.port,
126
- path: uri.pathname,
127
- method: 'GET',
128
- headers: {
129
- 'Connection': 'Keep-Alive',
130
- 'Content-Type': 'text/plain',
131
- 'Host': uri.hostname as string,
132
- 'Authorization': this._authorization
133
- }
134
- };
135
- return this.requestDigest(options, data);
136
- }
137
-
138
- }
package/src/MyEnergi.ts DELETED
@@ -1,53 +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
- private _client: any;
27
- private _asn: any;
28
-
29
- constructor(username: string, password: string) {
30
- this._config.username = username;
31
- this._config.password = password;
32
- }
33
-
34
- public async getStatus(): Promise<any> {
35
- const digest = new Digest(this._config.username, this._config.password);
36
- const data = await digest.get(this._config.status_url);
37
- return data;
38
- }
39
-
40
- public async getZappi(): Promise<any> {
41
- const digest = new Digest(this._config.username, this._config.password);
42
- const data = await digest.get(this._config.zappi_url);
43
- return data;
44
- }
45
-
46
- public async setZappiChargeMode(serialNo: string, chargeMode: ZappiChargeMode): Promise<any> {
47
- const digest = new Digest(this._config.username, this._config.password);
48
- const url = `${this._config.zappi_mode_url}${serialNo}-${chargeMode}-0-0-0000`;
49
- const data = await digest.get(url);
50
- return data;
51
- }
52
-
53
- }
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,18 +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
- });
17
- });
18
- });
@@ -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
- }