edilkamin 1.7.2 → 1.7.3
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/.github/workflows/publish.yml +1 -1
- package/README.md +1 -1
- package/dist/esm/browser-bundle.test.d.ts +1 -0
- package/dist/esm/browser-bundle.test.js +29 -0
- package/dist/esm/configureAmplify.test.d.ts +1 -0
- package/dist/esm/configureAmplify.test.js +37 -0
- package/dist/esm/index.d.ts +1 -2
- package/dist/esm/index.js +0 -1
- package/dist/esm/library.d.ts +4 -4
- package/dist/esm/library.js +59 -51
- package/dist/esm/library.test.js +167 -190
- package/package.json +14 -2
- package/src/browser-bundle.test.ts +21 -0
- package/src/configureAmplify.test.ts +47 -0
- package/src/index.ts +0 -1
- package/src/library.test.ts +195 -197
- package/src/library.ts +76 -62
package/src/library.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { strict as assert } from "assert";
|
|
|
2
2
|
import { Amplify } from "aws-amplify";
|
|
3
3
|
import * as amplifyAuth from "aws-amplify/auth";
|
|
4
4
|
import { cognitoUserPoolsTokenProvider } from "aws-amplify/auth/cognito";
|
|
5
|
-
import axios, { AxiosInstance } from "axios";
|
|
6
5
|
|
|
7
6
|
import { processResponse } from "./buffer-utils";
|
|
8
7
|
import { API_URL } from "./constants";
|
|
@@ -14,6 +13,22 @@ import {
|
|
|
14
13
|
EditDeviceAssociationBody,
|
|
15
14
|
} from "./types";
|
|
16
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Makes a fetch request and returns parsed JSON response.
|
|
18
|
+
* Throws an error for non-2xx status codes.
|
|
19
|
+
*/
|
|
20
|
+
const fetchJson = async <T>(
|
|
21
|
+
baseURL: string,
|
|
22
|
+
path: string,
|
|
23
|
+
options: RequestInit = {},
|
|
24
|
+
): Promise<T> => {
|
|
25
|
+
const response = await fetch(`${baseURL}${path}`, options);
|
|
26
|
+
if (!response.ok) {
|
|
27
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
28
|
+
}
|
|
29
|
+
return response.json();
|
|
30
|
+
};
|
|
31
|
+
|
|
17
32
|
const amplifyconfiguration = {
|
|
18
33
|
aws_project_region: "eu-central-1",
|
|
19
34
|
aws_user_pools_id: "eu-central-1_BYmQ2VBlo",
|
|
@@ -111,7 +126,7 @@ const createAuthService = (auth: typeof amplifyAuth) => {
|
|
|
111
126
|
const { signIn, getSession } = createAuthService(amplifyAuth);
|
|
112
127
|
|
|
113
128
|
const deviceInfo =
|
|
114
|
-
(
|
|
129
|
+
(baseURL: string) =>
|
|
115
130
|
/**
|
|
116
131
|
* Retrieves information about a device by its MAC address.
|
|
117
132
|
* Automatically decompresses any gzip-compressed Buffer fields in the response.
|
|
@@ -121,28 +136,33 @@ const deviceInfo =
|
|
|
121
136
|
* @returns {Promise<DeviceInfoType>} - A promise that resolves to the device info.
|
|
122
137
|
*/
|
|
123
138
|
async (jwtToken: string, macAddress: string): Promise<DeviceInfoType> => {
|
|
124
|
-
const
|
|
139
|
+
const data = await fetchJson<DeviceInfoRawType>(
|
|
140
|
+
baseURL,
|
|
125
141
|
`device/${macAddress}/info`,
|
|
126
142
|
{
|
|
143
|
+
method: "GET",
|
|
127
144
|
headers: headers(jwtToken),
|
|
128
145
|
},
|
|
129
146
|
);
|
|
130
147
|
// Process response to decompress any gzipped Buffer fields
|
|
131
|
-
return processResponse(
|
|
148
|
+
return processResponse(data) as DeviceInfoType;
|
|
132
149
|
};
|
|
133
150
|
|
|
134
151
|
const mqttCommand =
|
|
135
|
-
(
|
|
152
|
+
(baseURL: string) =>
|
|
136
153
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
137
154
|
(jwtToken: string, macAddress: string, payload: any) =>
|
|
138
|
-
|
|
139
|
-
"
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
155
|
+
fetchJson(baseURL, "mqtt/command", {
|
|
156
|
+
method: "PUT",
|
|
157
|
+
headers: {
|
|
158
|
+
"Content-Type": "application/json",
|
|
159
|
+
...headers(jwtToken),
|
|
160
|
+
},
|
|
161
|
+
body: JSON.stringify({ mac_address: macAddress, ...payload }),
|
|
162
|
+
});
|
|
143
163
|
|
|
144
164
|
const setPower =
|
|
145
|
-
(
|
|
165
|
+
(baseURL: string) =>
|
|
146
166
|
/**
|
|
147
167
|
* Sends a command to set the power state of a device.
|
|
148
168
|
*
|
|
@@ -152,10 +172,10 @@ const setPower =
|
|
|
152
172
|
* @returns {Promise<string>} - A promise that resolves to the command response.
|
|
153
173
|
*/
|
|
154
174
|
(jwtToken: string, macAddress: string, value: number) =>
|
|
155
|
-
mqttCommand(
|
|
175
|
+
mqttCommand(baseURL)(jwtToken, macAddress, { name: "power", value });
|
|
156
176
|
|
|
157
177
|
const setPowerOn =
|
|
158
|
-
(
|
|
178
|
+
(baseURL: string) =>
|
|
159
179
|
/**
|
|
160
180
|
* Turns a device ON by setting its power state.
|
|
161
181
|
*
|
|
@@ -168,10 +188,10 @@ const setPowerOn =
|
|
|
168
188
|
* console.log(response);
|
|
169
189
|
*/
|
|
170
190
|
(jwtToken: string, macAddress: string) =>
|
|
171
|
-
setPower(
|
|
191
|
+
setPower(baseURL)(jwtToken, macAddress, 1);
|
|
172
192
|
|
|
173
193
|
const setPowerOff =
|
|
174
|
-
(
|
|
194
|
+
(baseURL: string) =>
|
|
175
195
|
/**
|
|
176
196
|
* Turns a device OFF by setting its power state.
|
|
177
197
|
*
|
|
@@ -184,10 +204,10 @@ const setPowerOff =
|
|
|
184
204
|
* console.log(response);
|
|
185
205
|
*/
|
|
186
206
|
(jwtToken: string, macAddress: string) =>
|
|
187
|
-
setPower(
|
|
207
|
+
setPower(baseURL)(jwtToken, macAddress, 0);
|
|
188
208
|
|
|
189
209
|
const getPower =
|
|
190
|
-
(
|
|
210
|
+
(baseURL: string) =>
|
|
191
211
|
/**
|
|
192
212
|
* Retrieves the power status of the device.
|
|
193
213
|
*
|
|
@@ -196,12 +216,12 @@ const getPower =
|
|
|
196
216
|
* @returns {Promise<boolean>} - A promise that resolves to the power status.
|
|
197
217
|
*/
|
|
198
218
|
async (jwtToken: string, macAddress: string): Promise<boolean> => {
|
|
199
|
-
const info = await deviceInfo(
|
|
219
|
+
const info = await deviceInfo(baseURL)(jwtToken, macAddress);
|
|
200
220
|
return info.status.commands.power;
|
|
201
221
|
};
|
|
202
222
|
|
|
203
223
|
const getEnvironmentTemperature =
|
|
204
|
-
(
|
|
224
|
+
(baseURL: string) =>
|
|
205
225
|
/**
|
|
206
226
|
* Retrieves the environment temperature from the device's sensors.
|
|
207
227
|
*
|
|
@@ -210,12 +230,12 @@ const getEnvironmentTemperature =
|
|
|
210
230
|
* @returns {Promise<number>} - A promise that resolves to the temperature value.
|
|
211
231
|
*/
|
|
212
232
|
async (jwtToken: string, macAddress: string): Promise<number> => {
|
|
213
|
-
const info = await deviceInfo(
|
|
233
|
+
const info = await deviceInfo(baseURL)(jwtToken, macAddress);
|
|
214
234
|
return info.status.temperatures.enviroment;
|
|
215
235
|
};
|
|
216
236
|
|
|
217
237
|
const getTargetTemperature =
|
|
218
|
-
(
|
|
238
|
+
(baseURL: string) =>
|
|
219
239
|
/**
|
|
220
240
|
* Retrieves the target temperature value set on the device.
|
|
221
241
|
*
|
|
@@ -224,12 +244,12 @@ const getTargetTemperature =
|
|
|
224
244
|
* @returns {Promise<number>} - A promise that resolves to the target temperature (degree celsius).
|
|
225
245
|
*/
|
|
226
246
|
async (jwtToken: string, macAddress: string): Promise<number> => {
|
|
227
|
-
const info = await deviceInfo(
|
|
247
|
+
const info = await deviceInfo(baseURL)(jwtToken, macAddress);
|
|
228
248
|
return info.nvm.user_parameters.enviroment_1_temperature;
|
|
229
249
|
};
|
|
230
250
|
|
|
231
251
|
const setTargetTemperature =
|
|
232
|
-
(
|
|
252
|
+
(baseURL: string) =>
|
|
233
253
|
/**
|
|
234
254
|
* Sends a command to set the target temperature (degree celsius) of a device.
|
|
235
255
|
*
|
|
@@ -239,13 +259,13 @@ const setTargetTemperature =
|
|
|
239
259
|
* @returns {Promise<string>} - A promise that resolves to the command response.
|
|
240
260
|
*/
|
|
241
261
|
(jwtToken: string, macAddress: string, temperature: number) =>
|
|
242
|
-
mqttCommand(
|
|
262
|
+
mqttCommand(baseURL)(jwtToken, macAddress, {
|
|
243
263
|
name: "enviroment_1_temperature",
|
|
244
264
|
value: temperature,
|
|
245
265
|
});
|
|
246
266
|
|
|
247
267
|
const registerDevice =
|
|
248
|
-
(
|
|
268
|
+
(baseURL: string) =>
|
|
249
269
|
/**
|
|
250
270
|
* Registers a device with the user's account.
|
|
251
271
|
* This must be called before other device operations will work on the new API.
|
|
@@ -270,16 +290,18 @@ const registerDevice =
|
|
|
270
290
|
deviceRoom,
|
|
271
291
|
serialNumber,
|
|
272
292
|
};
|
|
273
|
-
|
|
274
|
-
"
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
293
|
+
return fetchJson<DeviceAssociationResponse>(baseURL, "device", {
|
|
294
|
+
method: "POST",
|
|
295
|
+
headers: {
|
|
296
|
+
"Content-Type": "application/json",
|
|
297
|
+
...headers(jwtToken),
|
|
298
|
+
},
|
|
299
|
+
body: JSON.stringify(body),
|
|
300
|
+
});
|
|
279
301
|
};
|
|
280
302
|
|
|
281
303
|
const editDevice =
|
|
282
|
-
(
|
|
304
|
+
(baseURL: string) =>
|
|
283
305
|
/**
|
|
284
306
|
* Updates a device's name and room.
|
|
285
307
|
*
|
|
@@ -300,12 +322,18 @@ const editDevice =
|
|
|
300
322
|
deviceName,
|
|
301
323
|
deviceRoom,
|
|
302
324
|
};
|
|
303
|
-
|
|
325
|
+
return fetchJson<DeviceAssociationResponse>(
|
|
326
|
+
baseURL,
|
|
304
327
|
`device/${normalizedMac}`,
|
|
305
|
-
|
|
306
|
-
|
|
328
|
+
{
|
|
329
|
+
method: "PUT",
|
|
330
|
+
headers: {
|
|
331
|
+
"Content-Type": "application/json",
|
|
332
|
+
...headers(jwtToken),
|
|
333
|
+
},
|
|
334
|
+
body: JSON.stringify(body),
|
|
335
|
+
},
|
|
307
336
|
);
|
|
308
|
-
return response.data;
|
|
309
337
|
};
|
|
310
338
|
|
|
311
339
|
/**
|
|
@@ -319,32 +347,18 @@ const editDevice =
|
|
|
319
347
|
* const api = configure();
|
|
320
348
|
* const power = await api.getPower(jwtToken, macAddress);
|
|
321
349
|
*/
|
|
322
|
-
const configure = (baseURL: string = API_URL) => {
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
const setTargetTemperatureInstance = setTargetTemperature(axiosInstance);
|
|
335
|
-
return {
|
|
336
|
-
deviceInfo: deviceInfoInstance,
|
|
337
|
-
registerDevice: registerDeviceInstance,
|
|
338
|
-
editDevice: editDeviceInstance,
|
|
339
|
-
setPower: setPowerInstance,
|
|
340
|
-
setPowerOff: setPowerOffInstance,
|
|
341
|
-
setPowerOn: setPowerOnInstance,
|
|
342
|
-
getPower: getPowerInstance,
|
|
343
|
-
getEnvironmentTemperature: getEnvironmentTemperatureInstance,
|
|
344
|
-
getTargetTemperature: getTargetTemperatureInstance,
|
|
345
|
-
setTargetTemperature: setTargetTemperatureInstance,
|
|
346
|
-
};
|
|
347
|
-
};
|
|
350
|
+
const configure = (baseURL: string = API_URL) => ({
|
|
351
|
+
deviceInfo: deviceInfo(baseURL),
|
|
352
|
+
registerDevice: registerDevice(baseURL),
|
|
353
|
+
editDevice: editDevice(baseURL),
|
|
354
|
+
setPower: setPower(baseURL),
|
|
355
|
+
setPowerOff: setPowerOff(baseURL),
|
|
356
|
+
setPowerOn: setPowerOn(baseURL),
|
|
357
|
+
getPower: getPower(baseURL),
|
|
358
|
+
getEnvironmentTemperature: getEnvironmentTemperature(baseURL),
|
|
359
|
+
getTargetTemperature: getTargetTemperature(baseURL),
|
|
360
|
+
setTargetTemperature: setTargetTemperature(baseURL),
|
|
361
|
+
});
|
|
348
362
|
|
|
349
363
|
export {
|
|
350
364
|
configure,
|