@jakobgoerke/ac-infinity-client 0.1.1 → 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/README.md CHANGED
@@ -1 +1 @@
1
- # ac-infinity-client
1
+ # ac-infinity-client
@@ -1,22 +1,36 @@
1
- import { Controller, User, DeviceModeSettings, DeviceSettings } from './types';
1
+ import type { Controller, DeviceModeSettings, DeviceModeSettingsChanges, DeviceSettings, DeviceSettingsChanges, User } from './types';
2
2
  export declare enum Url {
3
3
  AUTH = "/user/appUserLogin",
4
- CONTROLLERS = "/user/devInfoListAll",
5
- DEVICE_SETTINGS = "/dev/getDevSetting",
6
- DEVICE_MODE_SETTINGS = "/dev/getdevModeSettingList"
4
+ GET_CONTROLLERS = "/user/devInfoListAll",
5
+ GET_SHARED_CONTROLLERS = "/user/shareDevList",
6
+ GET_CONTROLLER = "/dev/getDeviceInfo",
7
+ GET_DEVICE_SETTINGS = "/dev/getDevSetting",
8
+ SET_DEVICE_SETTINGS = "/dev/updateAdvSetting",
9
+ GET_DEVICE_MODE_SETTINGS = "/dev/getdevModeSettingList",
10
+ SET_DEVICE_MODE_SETTINGS = "/dev/addDevMode"
7
11
  }
8
12
  export interface Response<T> {
9
13
  code: number;
14
+ msg?: string;
10
15
  data: T;
11
16
  }
12
17
  export interface AuthParams {
13
18
  email: string;
14
19
  password: string;
15
20
  }
16
- interface GetDeviceParams {
17
- deviceId: string;
21
+ export interface ControllerParams {
22
+ controllerId: string;
23
+ }
24
+ export interface GetDeviceParams extends ControllerParams {
25
+ /** 0 addresses the controller itself, 1..n the devices plugged into its ports */
18
26
  port: number;
19
27
  }
28
+ export interface SetDeviceModeSettingsParams extends GetDeviceParams {
29
+ settings: DeviceModeSettingsChanges;
30
+ }
31
+ export interface SetDeviceSettingsParams extends GetDeviceParams {
32
+ settings: DeviceSettingsChanges;
33
+ }
20
34
  declare class AcInfinityClient {
21
35
  constructor(args: AuthParams);
22
36
  private api;
@@ -24,11 +38,17 @@ declare class AcInfinityClient {
24
38
  private email;
25
39
  private password;
26
40
  authenticate(): Promise<User>;
41
+ private request;
27
42
  getControllers(): Promise<Controller[]>;
28
- getDeviceSettings({ deviceId, port }: GetDeviceParams): Promise<DeviceSettings>;
29
- getDeviceModeSettings({ deviceId, port }: GetDeviceParams): Promise<DeviceModeSettings>;
43
+ getSharedControllers(): Promise<Controller[]>;
44
+ getController({ controllerId }: ControllerParams): Promise<Controller>;
45
+ getDeviceSettings({ controllerId, port }: GetDeviceParams): Promise<DeviceSettings>;
46
+ setDeviceSettings({ controllerId, port, settings }: SetDeviceSettingsParams): Promise<void>;
47
+ private getDeviceName;
48
+ getDeviceModeSettings({ controllerId, port }: GetDeviceParams): Promise<DeviceModeSettings>;
49
+ setDeviceModeSettings({ controllerId, port, settings }: SetDeviceModeSettingsParams): Promise<void>;
30
50
  static build(args: AuthParams): Promise<AcInfinityClient>;
31
51
  }
52
+ export type { Controller, DeviceModeSettings, DeviceModeSettingsChanges, DeviceSettings, DeviceSettingsChanges, User };
32
53
  export { AcInfinityClient };
33
- export type { Controller, User, DeviceModeSettings, DeviceSettings };
34
54
  //# sourceMappingURL=AcInfinityClient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AcInfinityClient.d.ts","sourceRoot":"","sources":["../../src/AcInfinityClient.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,UAAU,EAEV,IAAI,EAGJ,kBAAkB,EAClB,cAAc,EAEf,MAAM,SAAS,CAAC;AAGjB,oBAAY,GAAG;IACb,IAAI,uBAAuB;IAC3B,WAAW,yBAAyB;IACpC,eAAe,uBAAuB;IACtC,oBAAoB,+BAA+B;CACpD;AAED,MAAM,WAAW,QAAQ,CAAC,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,CAAC,CAAC;CACT;AAED,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB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IAAI,CAAC;WAYnG,KAAK,CAAC,IAAI,EAAE,UAAU;CAKpC;AAED,YAAY,EAAE,UAAU,EAAE,kBAAkB,EAAE,yBAAyB,EAAE,cAAc,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAAC;AACvH,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -5,15 +5,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.AcInfinityClient = exports.Url = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
- const types_1 = require("./types");
9
8
  const errors_1 = require("./errors");
9
+ const types_1 = require("./types");
10
10
  var Url;
11
11
  (function (Url) {
12
12
  Url["AUTH"] = "/user/appUserLogin";
13
- Url["CONTROLLERS"] = "/user/devInfoListAll";
14
- Url["DEVICE_SETTINGS"] = "/dev/getDevSetting";
15
- Url["DEVICE_MODE_SETTINGS"] = "/dev/getdevModeSettingList";
13
+ Url["GET_CONTROLLERS"] = "/user/devInfoListAll";
14
+ Url["GET_SHARED_CONTROLLERS"] = "/user/shareDevList";
15
+ Url["GET_CONTROLLER"] = "/dev/getDeviceInfo";
16
+ Url["GET_DEVICE_SETTINGS"] = "/dev/getDevSetting";
17
+ Url["SET_DEVICE_SETTINGS"] = "/dev/updateAdvSetting";
18
+ Url["GET_DEVICE_MODE_SETTINGS"] = "/dev/getdevModeSettingList";
19
+ Url["SET_DEVICE_MODE_SETTINGS"] = "/dev/addDevMode";
16
20
  })(Url || (exports.Url = Url = {}));
21
+ // fahrenheit values below freezing are raised to 32 before sending mode settings
22
+ const FAHRENHEIT_FIELDS = ['devHtf', 'devLtf', 'targetTempF', 'waterTempHighValueF', 'waterTempLowValueF', 'waterTempTargetValueF'];
23
+ // only send flat values: nested objects and empty fields are left out
24
+ const toFormPayload = (values) => Object.fromEntries(Object.entries(values).filter(([, value]) => value !== null && value !== undefined && typeof value !== 'object'));
25
+ const clampFahrenheit = (payload) => {
26
+ for (const field of FAHRENHEIT_FIELDS) {
27
+ const value = payload[field];
28
+ if (typeof value === 'number' && value < 32) {
29
+ payload[field] = 32;
30
+ }
31
+ }
32
+ return payload;
33
+ };
17
34
  class AcInfinityClient {
18
35
  constructor(args) {
19
36
  const { email, password } = args;
@@ -41,29 +58,72 @@ class AcInfinityClient {
41
58
  throw new errors_1.AuthenticationError();
42
59
  }
43
60
  const user = types_1.UserSchema.parse(response.data.data);
44
- this.token = user.appId;
45
- this.api.defaults.headers.common['token'] = user.appId;
61
+ this.token = user.id;
62
+ this.api.defaults.headers.common.token = user.id;
46
63
  return user;
47
64
  }
65
+ async request(url, params) {
66
+ const response = await this.api.post(url, params);
67
+ if (response.data.code !== 200) {
68
+ throw new errors_1.ApiError(response.data.code, response.data.msg);
69
+ }
70
+ return response.data.data;
71
+ }
48
72
  async getControllers() {
49
- const response = await this.api.post(Url.CONTROLLERS, {
73
+ const controllers = await this.request(Url.GET_CONTROLLERS, {
74
+ userId: this.token,
75
+ });
76
+ return controllers.map((device) => types_1.ControllerSchema.parse(device));
77
+ }
78
+ async getSharedControllers() {
79
+ const controllers = await this.request(Url.GET_SHARED_CONTROLLERS, {
50
80
  userId: this.token,
51
81
  });
52
- return response.data.data.map((device) => types_1.ControllerSchema.parse(device));
82
+ return controllers.map((device) => types_1.ControllerSchema.parse(device));
83
+ }
84
+ async getController({ controllerId }) {
85
+ const controller = await this.request(Url.GET_CONTROLLER, {
86
+ devId: controllerId,
87
+ });
88
+ return types_1.ControllerSchema.parse(controller);
89
+ }
90
+ async getDeviceSettings({ controllerId, port }) {
91
+ const settings = await this.request(Url.GET_DEVICE_SETTINGS, {
92
+ devId: controllerId,
93
+ port,
94
+ });
95
+ return types_1.DeviceSettingsSchema.parse(settings);
96
+ }
97
+ async setDeviceSettings({ controllerId, port, settings }) {
98
+ const vendorChanges = (0, types_1.encodeDeviceSettings)(settings);
99
+ const currentSettings = await this.request(Url.GET_DEVICE_SETTINGS, {
100
+ devId: controllerId,
101
+ port,
102
+ });
103
+ // the name is required, but often not stored in the settings themselves
104
+ const devName = vendorChanges.devName ?? currentSettings.devName ?? (await this.getDeviceName({ controllerId, port }));
105
+ const payload = toFormPayload({ ...currentSettings, ...vendorChanges, devName });
106
+ await this.request(Url.SET_DEVICE_SETTINGS, payload);
107
+ }
108
+ async getDeviceName({ controllerId, port }) {
109
+ const controller = await this.getController({ controllerId });
110
+ return port === 0 ? controller.name : controller.deviceInfo.ports.find((p) => p.port === port)?.deviceName;
53
111
  }
54
- async getDeviceSettings({ deviceId, port }) {
55
- const response = await this.api.post(Url.DEVICE_SETTINGS, {
56
- devId: deviceId,
112
+ async getDeviceModeSettings({ controllerId, port }) {
113
+ const settings = await this.request(Url.GET_DEVICE_MODE_SETTINGS, {
114
+ devId: controllerId,
57
115
  port,
58
116
  });
59
- return types_1.DeviceSettingsSchema.parse(response.data.data);
117
+ return types_1.DeviceModeSettingsSchema.parse(settings);
60
118
  }
61
- async getDeviceModeSettings({ deviceId, port }) {
62
- const response = await this.api.post(Url.DEVICE_MODE_SETTINGS, {
63
- devId: deviceId,
119
+ async setDeviceModeSettings({ controllerId, port, settings }) {
120
+ const vendorChanges = (0, types_1.encodeDeviceModeSettings)(settings);
121
+ const currentSettings = await this.request(Url.GET_DEVICE_MODE_SETTINGS, {
122
+ devId: controllerId,
64
123
  port,
65
124
  });
66
- return types_1.DeviceModeSettingsSchema.parse(response.data.data);
125
+ const payload = clampFahrenheit(toFormPayload({ ...currentSettings, ...vendorChanges, onlyUpdateSpeed: 0 }));
126
+ await this.request(Url.SET_DEVICE_MODE_SETTINGS, payload);
67
127
  }
68
128
  static async build(args) {
69
129
  const instance = new AcInfinityClient(args);
@@ -1,5 +1,10 @@
1
1
  declare class AuthenticationError extends Error {
2
2
  constructor(message?: string);
3
3
  }
4
- export { AuthenticationError };
4
+ declare class ApiError extends Error {
5
+ readonly code: number;
6
+ readonly msg?: string | undefined;
7
+ constructor(code: number, msg?: string | undefined);
8
+ }
9
+ export { ApiError, AuthenticationError };
5
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/errors/index.ts"],"names":[],"mappings":"AAAA,cAAM,mBAAoB,SAAQ,KAAK;gBACzB,OAAO,CAAC,EAAE,MAAM;CAQ7B;AAED,OAAO,EAAE,mBAAmB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/errors/index.ts"],"names":[],"mappings":"AAAA,cAAM,mBAAoB,SAAQ,KAAK;gBACzB,OAAO,CAAC,EAAE,MAAM;CAQ7B;AAED,cAAM,QAAS,SAAQ,KAAK;aAER,IAAI,EAAE,MAAM;aACZ,GAAG,CAAC,EAAE,MAAM;gBADZ,IAAI,EAAE,MAAM,EACZ,GAAG,CAAC,EAAE,MAAM,YAAA;CAS/B;AAED,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AuthenticationError = void 0;
3
+ exports.AuthenticationError = exports.ApiError = void 0;
4
4
  class AuthenticationError extends Error {
5
5
  constructor(message) {
6
6
  super(message);
@@ -10,3 +10,16 @@ class AuthenticationError extends Error {
10
10
  }
11
11
  }
12
12
  exports.AuthenticationError = AuthenticationError;
13
+ class ApiError extends Error {
14
+ code;
15
+ msg;
16
+ constructor(code, msg) {
17
+ super(`API request failed [code="${code}", msg="${msg}"]`);
18
+ this.code = code;
19
+ this.msg = msg;
20
+ this.name = this.constructor.name;
21
+ Error.captureStackTrace(this, this.constructor);
22
+ Object.setPrototypeOf(this, ApiError.prototype);
23
+ }
24
+ }
25
+ exports.ApiError = ApiError;
@@ -1,2 +1,5 @@
1
1
  export * from './AcInfinityClient';
2
+ export * from './errors';
3
+ export type { ControllerDeviceInfo, ControllerPort, ControllerSensor } from './types';
4
+ export { ControllerDeviceType, DeviceMode, DeviceType, SensorType, TemperatureUnit } from './types';
2
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,UAAU,CAAC;AACzB,YAAY,EAAE,oBAAoB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AACtF,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC"}
@@ -14,4 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.TemperatureUnit = exports.SensorType = exports.DeviceType = exports.DeviceMode = exports.ControllerDeviceType = void 0;
17
18
  __exportStar(require("./AcInfinityClient"), exports);
19
+ __exportStar(require("./errors"), exports);
20
+ var types_1 = require("./types");
21
+ Object.defineProperty(exports, "ControllerDeviceType", { enumerable: true, get: function () { return types_1.ControllerDeviceType; } });
22
+ Object.defineProperty(exports, "DeviceMode", { enumerable: true, get: function () { return types_1.DeviceMode; } });
23
+ Object.defineProperty(exports, "DeviceType", { enumerable: true, get: function () { return types_1.DeviceType; } });
24
+ Object.defineProperty(exports, "SensorType", { enumerable: true, get: function () { return types_1.SensorType; } });
25
+ Object.defineProperty(exports, "TemperatureUnit", { enumerable: true, get: function () { return types_1.TemperatureUnit; } });