myenergi-api 0.0.8-0 → 0.0.9-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,6 +1,6 @@
1
1
  {
2
2
  "name": "myenergi-api",
3
- "version": "0.0.8-0",
3
+ "version": "0.0.9-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",
package/src/AuthDigest.ts CHANGED
@@ -63,7 +63,6 @@ export class AuthDigest {
63
63
 
64
64
  public init(wwwAuthHeader: string) {
65
65
 
66
- //let realm, nonce, qop, opaque;
67
66
  let authSplit = wwwAuthHeader.split(',') as string[];
68
67
 
69
68
  for (let item of authSplit) {
@@ -104,7 +103,7 @@ export class AuthDigest {
104
103
  let HA2 = this.md5(httpMethod + ":" + path);
105
104
  let response = this.md5(HA1 + ":" + this.nonce + ":" + nc + ":" + cnonce + ":" + this.qop + ":" + HA2);
106
105
 
107
- let res = `Digest username="${this._username}",`;
106
+ let res = `Digest username="${this.username}",`;
108
107
  res += `realm="${this.realm}",`;
109
108
  res += `nonce="${this.nonce}",`;
110
109
  res += `uri="${path}",`;
package/src/Digest.ts CHANGED
@@ -4,10 +4,10 @@ import * as url from "url";
4
4
  import { AuthDigest } from './AuthDigest';
5
5
 
6
6
  export class Digest {
7
- private _authorization?: AuthDigest;
7
+ private _authDigest?: AuthDigest;
8
8
 
9
9
  constructor(username: string, password: string) {
10
- this._authorization = new AuthDigest(username, password);
10
+ this._authDigest = new AuthDigest(username, password);
11
11
  }
12
12
 
13
13
  private request(options: RequestOptions, data?: any, retryCount: number = 0): Promise<any> {
@@ -15,7 +15,7 @@ export class Digest {
15
15
  let resData: string = '';
16
16
  if (!options.headers)
17
17
  options.headers = {};
18
- options.headers.Authorization = this._authorization?.getAuthorization(options.method as string, options.path as string);
18
+ options.headers.Authorization = this._authDigest?.getAuthorization(options.method as string, options.path as string);
19
19
  const req = https.request(options, (res: any) => {
20
20
 
21
21
  if (res.statusCode == 401) {
@@ -24,11 +24,16 @@ export class Digest {
24
24
  return;
25
25
  }
26
26
  retryCount++;
27
- let auth = res.headers["www-authenticate"];
28
- this._authorization?.init(auth);
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);
29
34
  if (!options.headers)
30
35
  options.headers = {};
31
- options.headers.Authorization = this._authorization?.getAuthorization(options.method as string, options.path as string);
36
+ options.headers.Authorization = this._authDigest?.getAuthorization(options.method as string, options.path as string);
32
37
  return this.request(options, data, retryCount).then(value => {
33
38
  resolve(value);
34
39
  }).catch(resaon => {
@@ -72,6 +77,7 @@ export class Digest {
72
77
  headers: {
73
78
  'Connection': 'Keep-Alive',
74
79
  'Content-Type': 'application/json',
80
+ 'Accept': 'application/json',
75
81
  'Host': uri.hostname as string,
76
82
  }
77
83
  };
package/src/MyEnergi.ts CHANGED
@@ -6,11 +6,13 @@ export enum ZappiChargeMode {
6
6
  Eco = 2,
7
7
  EcoPlus = 3
8
8
  }
9
+
9
10
  export enum ZappiBoostMode {
10
- Fast = 0,
11
- Eco = 1,
12
- EcoPlus = 2
11
+ Manual = 10,
12
+ Smart = 11,
13
+ Stop = 2
13
14
  }
15
+
14
16
  export class MyEnergi {
15
17
  private _config = {
16
18
  username: '',
@@ -20,7 +22,8 @@ export class MyEnergi {
20
22
  harvi_url: 'https://s18.myenergi.net/cgi-jstatus-H',
21
23
  status_url: 'https://s18.myenergi.net/cgi-jstatus-*',
22
24
  dayhour_url: 'https://s18.myenergi.net/cgi-jdayhour-',
23
- zappi_mode_url: 'https://s18.myenergi.net/cgi-zappi-mode-Z'
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'
24
27
  //https://s18.myenergi.net/cgi-jdayhour-Znnnnnnnn-YYYY-MM-DD
25
28
  }
26
29
 
@@ -48,4 +51,20 @@ export class MyEnergi {
48
51
  return data;
49
52
  }
50
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
+
51
70
  }
package/tests/debug.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { exit } from 'process';
2
2
  import { MyEnergi } from '../src';
3
3
  import * as dotenv from 'dotenv';
4
- import { ZappiChargeMode } from '../src/MyEnergi';
4
+ import { ZappiBoostMode, ZappiChargeMode } from '../src/MyEnergi';
5
5
 
6
6
  dotenv.config();
7
7
  const myenergi = new MyEnergi(process.env.USERNAME as string, process.env.PASSWORD as string);
@@ -12,7 +12,19 @@ myenergi.getStatus().then(val => {
12
12
  const sno: string = JSON.parse(v).zappi[0].sno;
13
13
  myenergi.setZappiChargeMode(sno, ZappiChargeMode.Fast).then(x => {
14
14
  console.log(x);
15
- exit();
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
+ });
16
28
  }).catch(err => {
17
29
  console.error(err);
18
30
  exit();