@iotready/nextjs-components-library 1.0.0-preview3 → 1.0.0-preview31
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/components/accounts/AccountMenu.js +1 -1
- package/components/accounts/AccountProfile.js +1 -1
- package/components/charts/TrendChart.d.ts +27 -6
- package/components/charts/TrendChart.js +580 -170
- package/components/groups/GroupUpdate.d.ts +4 -8
- package/components/groups/GroupUpdate.js +1 -31
- package/components/groups/GroupsDevices.d.ts +15 -10
- package/components/groups/GroupsDevices.js +92 -79
- package/components/groups/Map.d.ts +3 -8
- package/components/settings/DynamicMenu.d.ts +1 -0
- package/components/settings/DynamicMenu.js +2 -1
- package/components/users/UsersDataGrid.d.ts +5 -2
- package/components/users/UsersDataGrid.js +49 -32
- package/package.json +6 -3
- package/server-actions/annotations.d.ts +4 -0
- package/server-actions/annotations.js +12 -0
- package/server-actions/groups.d.ts +12 -16
- package/server-actions/groups.js +71 -72
- package/server-actions/index.d.ts +1 -0
- package/server-actions/index.js +1 -0
- package/server-actions/influx.d.ts +17 -13
- package/server-actions/influx.js +207 -98
- package/server-actions/trackle.d.ts +10 -4
- package/server-actions/trackle.js +29 -24
- package/server-actions/types.d.ts +16 -0
- package/server-actions/types.js +6 -0
- package/types/device.d.ts +19 -0
- package/types/device.js +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.js +1 -0
- package/types/user.d.ts +1 -0
package/server-actions/influx.js
CHANGED
|
@@ -1,55 +1,180 @@
|
|
|
1
1
|
"use server";
|
|
2
|
+
import { parse } from "csv-parse";
|
|
2
3
|
import moment from "moment";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
import { FilterTagMode } from "./types";
|
|
5
|
+
export async function getInfluxAlerts(influxConfig, fields, limit, offset, sort, filter, timeStart, timeEnd, aggregate = false) {
|
|
6
|
+
const conditions = fields
|
|
7
|
+
.map((field) => `r["valueName"] == "${field}"`)
|
|
8
|
+
.join(" or ");
|
|
9
|
+
let query = `
|
|
10
|
+
import "contrib/tomhollingworth/events"
|
|
11
|
+
from(bucket: "${influxConfig.bucket}")`;
|
|
12
|
+
if (timeStart && timeEnd) {
|
|
13
|
+
query += ` |> range(start: ${timeStart}, stop: ${timeEnd})`;
|
|
14
|
+
}
|
|
15
|
+
query += `
|
|
16
|
+
|> filter(fn: (r) => r["_measurement"] == "${influxConfig.measurement}" and r["${filter.field}"] == "${filter.value}" and (${conditions}))
|
|
17
|
+
|> filter(fn: (r) => r["d"] == "3" or r["d"] !~ /./) // Include only events with tag d == 3 or no tag d
|
|
18
|
+
|> sort(columns: ["_time"]) // Ordina gli eventi cronologicamente
|
|
19
|
+
|> group(columns: ["valueName"]) // Raggruppa per il tag
|
|
20
|
+
`;
|
|
21
|
+
if (aggregate) {
|
|
22
|
+
// Aggregate same consecutive values
|
|
23
|
+
query += `
|
|
24
|
+
|> duplicate(column: "_value", as: "prev_value")
|
|
25
|
+
|> difference(columns: ["prev_value"], keepFirst: true)
|
|
26
|
+
|> filter(fn: (r) => r.prev_value != 0 or not exists r.prev_value)
|
|
27
|
+
`;
|
|
28
|
+
}
|
|
29
|
+
const queryCount = `${query} |> group() |> count()`;
|
|
30
|
+
query += `
|
|
31
|
+
|> events.duration(unit: 1s, stop: 2020-01-02T00:00:00Z)
|
|
32
|
+
|> keep(columns: ["_time", "valueName", "_value", "duration"])
|
|
33
|
+
|> group() // Raggruppa tutti i dati in un unico gruppo
|
|
34
|
+
`;
|
|
35
|
+
if (sort && sort.field === "time" && sort.sort === "desc") {
|
|
36
|
+
query += ` |> sort(columns: ["_time"], desc: true)`;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
query += ` |> sort(columns: ["_time"])`;
|
|
40
|
+
}
|
|
41
|
+
query += ` |> limit(n:${limit}, offset:${offset})`;
|
|
42
|
+
const responses = await Promise.all([
|
|
43
|
+
fetch(encodeURI(`${influxConfig.url}/api/v2/query?org=${influxConfig.orgId}`), {
|
|
44
|
+
method: "POST",
|
|
45
|
+
headers: {
|
|
46
|
+
Authorization: `Token ${influxConfig.accessToken}`,
|
|
47
|
+
"Content-Type": "application/json"
|
|
48
|
+
},
|
|
49
|
+
body: JSON.stringify({
|
|
50
|
+
query: query,
|
|
51
|
+
type: "flux"
|
|
52
|
+
})
|
|
53
|
+
}),
|
|
54
|
+
fetch(encodeURI(`${influxConfig.url}/api/v2/query?org=${influxConfig.orgId}`), {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: {
|
|
57
|
+
Authorization: `Token ${influxConfig.accessToken}`,
|
|
58
|
+
"Content-Type": "application/json"
|
|
59
|
+
},
|
|
60
|
+
body: JSON.stringify({
|
|
61
|
+
query: queryCount,
|
|
62
|
+
type: "flux"
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
]);
|
|
66
|
+
if (!responses[0].ok) {
|
|
67
|
+
throw new Error(`Failed to fetch data from InfluxDB: ${responses[0].statusText}`);
|
|
68
|
+
}
|
|
69
|
+
const data = await responses[0].text();
|
|
70
|
+
const count = await responses[1].text();
|
|
71
|
+
const rows = [];
|
|
72
|
+
const parsedData = parse(data.trim(), { columns: true });
|
|
73
|
+
parsedData.forEach((row) => {
|
|
74
|
+
rows.push({
|
|
75
|
+
time: row["_time"],
|
|
76
|
+
duration: parseInt(row["duration"], 10),
|
|
77
|
+
value: parseInt(row["_value"], 10),
|
|
78
|
+
valueName: row["valueName"]
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
const parsedCount = count.split("\n")[1].split(",");
|
|
82
|
+
let countData = parsedCount[3];
|
|
83
|
+
return {
|
|
84
|
+
data: rows,
|
|
85
|
+
count: countData
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export async function getDwSlots(influxConfig, timeStart, timeEnd, filter) {
|
|
89
|
+
const field = "dw";
|
|
90
|
+
const query = `
|
|
91
|
+
SELECT ("value") FROM "${influxConfig.measurement}"
|
|
92
|
+
WHERE "${filter.field}" = '${filter.value}'
|
|
93
|
+
AND "valueName" = '${field}'
|
|
94
|
+
AND time >= '${moment.unix(timeStart).toISOString()}'
|
|
95
|
+
AND time <= '${moment.unix(timeEnd).toISOString()}'
|
|
12
96
|
`;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
export async function getInfluxDataV1(influxConfig, field, timeStart, timeEnd,
|
|
97
|
+
const response = await fetch(encodeURI(`${influxConfig.url}/query?db=${influxConfig.dbName}&epoch=s&q=${query}`), {
|
|
98
|
+
headers: {
|
|
99
|
+
Authorization: `Basic ${btoa(`${influxConfig.username}:${influxConfig.password}`)}`
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
if (!response.ok) {
|
|
103
|
+
throw new Error(`Failed to fetch DW data: ${response.statusText}`);
|
|
104
|
+
}
|
|
105
|
+
const data = await response.json();
|
|
106
|
+
const series = data?.results?.[0]?.series?.[0];
|
|
107
|
+
if (!series?.values || !Array.isArray(series.values)) {
|
|
108
|
+
return data; // niente da fare, ritorna i dati grezzi
|
|
109
|
+
}
|
|
110
|
+
const values = series.values;
|
|
111
|
+
// Modifiche ai valori
|
|
112
|
+
if (values.length > 0) {
|
|
113
|
+
// Aggiungi punto all'inizio se il primo valore è 0
|
|
114
|
+
if (values[0][1] === 0) {
|
|
115
|
+
values.unshift([moment.unix(timeStart).unix(), 1]);
|
|
116
|
+
}
|
|
117
|
+
// Aggiungi punto alla fine se l'ultimo valore è 1
|
|
118
|
+
if (values[values.length - 1][1] === 1) {
|
|
119
|
+
values.push([moment.unix(timeEnd).unix(), 0]);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return data; // stessa struttura di Influx, con values modificati
|
|
123
|
+
}
|
|
124
|
+
export async function getInfluxDataV1(influxConfig, field, timeStart, timeEnd, filter, timeGroup, raw, fill, filterTag, tagInclude = FilterTagMode.Include) {
|
|
41
125
|
let query;
|
|
126
|
+
let preStartValue = null;
|
|
127
|
+
// Costruzione filtro tag
|
|
128
|
+
let filterTagCondition = "";
|
|
129
|
+
if (filterTag !== undefined && filterTag !== null) {
|
|
130
|
+
switch (tagInclude) {
|
|
131
|
+
case FilterTagMode.Include:
|
|
132
|
+
filterTagCondition = ` AND ("d" = '${filterTag}' OR "d" !~ /./)`;
|
|
133
|
+
break;
|
|
134
|
+
case FilterTagMode.Exclude:
|
|
135
|
+
filterTagCondition = ` AND ("d" != '${filterTag}' OR "d" !~ /./)`;
|
|
136
|
+
break;
|
|
137
|
+
case FilterTagMode.DW:
|
|
138
|
+
filterTagCondition = ` AND "d" = '${filterTag}'`;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
42
142
|
if (raw) {
|
|
43
|
-
query = `SELECT ("value") FROM "${influxConfig.measurement}" WHERE "
|
|
143
|
+
query = `SELECT ("value") FROM "${influxConfig.measurement}" WHERE "${filter.field}" = '${filter.value}' AND "valueName" = '${field}'${filterTagCondition} AND time >= '${moment
|
|
44
144
|
.unix(timeStart)
|
|
45
145
|
.toISOString()}' AND time <= '${moment.unix(timeEnd).toISOString()}'`;
|
|
46
146
|
}
|
|
47
147
|
else {
|
|
48
|
-
query = `SELECT last("value") FROM "${influxConfig.measurement}" WHERE "
|
|
148
|
+
query = `SELECT last("value") FROM "${influxConfig.measurement}" WHERE "${filter.field}" = '${filter.value}' AND "valueName" = '${field}'${filterTagCondition} AND time >= '${moment
|
|
49
149
|
.unix(timeStart)
|
|
50
150
|
.toISOString()}' AND time <= '${moment
|
|
51
151
|
.unix(timeEnd)
|
|
52
|
-
.toISOString()}' GROUP BY time(${timeGroup})
|
|
152
|
+
.toISOString()}' GROUP BY time(${timeGroup})`;
|
|
153
|
+
if (fill === "none") {
|
|
154
|
+
query += ` fill(none)`;
|
|
155
|
+
}
|
|
156
|
+
else if (fill === "previous") {
|
|
157
|
+
query += ` fill(previous)`;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
query += ` fill(null)`;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (fill !== "null") {
|
|
164
|
+
// Query to get the last data point before timeStart
|
|
165
|
+
const preStartQuery = `SELECT last("value") FROM "${influxConfig.measurement}" WHERE "${filter.field}" = '${filter.value}' AND "valueName" = '${field}'${filterTagCondition} AND time < '${moment
|
|
166
|
+
.unix(timeStart)
|
|
167
|
+
.toISOString()}'`;
|
|
168
|
+
const preStartResponse = await fetch(encodeURI(`${influxConfig.url}/query?db=${influxConfig.dbName}&epoch=s&q=${preStartQuery}`), {
|
|
169
|
+
headers: {
|
|
170
|
+
Authorization: `Basic ${btoa(`${influxConfig.username}:${influxConfig.password}`)}`
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
if (!preStartResponse.ok) {
|
|
174
|
+
throw new Error(`Failed to fetch pre-start data from InfluxDB: ${preStartResponse.statusText}`);
|
|
175
|
+
}
|
|
176
|
+
const preStartData = await preStartResponse.json();
|
|
177
|
+
preStartValue = preStartData.results[0].series?.[0]?.values?.[0]?.[1];
|
|
53
178
|
}
|
|
54
179
|
const response = await fetch(encodeURI(`${influxConfig.url}/query?db=${influxConfig.dbName}&epoch=s&q=${query}`), {
|
|
55
180
|
headers: {
|
|
@@ -57,7 +182,6 @@ export async function getInfluxDataV1(influxConfig, field, timeStart, timeEnd, d
|
|
|
57
182
|
}
|
|
58
183
|
});
|
|
59
184
|
if (!response.ok) {
|
|
60
|
-
console.log(response);
|
|
61
185
|
throw new Error(`Failed to fetch data from InfluxDB: ${response.statusText}`);
|
|
62
186
|
}
|
|
63
187
|
const data = await response.json();
|
|
@@ -72,61 +196,34 @@ export async function getInfluxDataV1(influxConfig, field, timeStart, timeEnd, d
|
|
|
72
196
|
}
|
|
73
197
|
];
|
|
74
198
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
.join(" OR ");
|
|
88
|
-
let queryCount = `SELECT count(*) FROM "${influxConfig.measurement}" WHERE "deviceid" = '${deviceID}' AND (${conditions})`;
|
|
89
|
-
let queryPagination = `SELECT "valueName", "value" FROM "${influxConfig.measurement}" WHERE "deviceid" = '${deviceID}' AND (${conditions})`;
|
|
90
|
-
if (timeStart) {
|
|
91
|
-
queryCount += ` AND time >= '${moment.unix(timeStart).toISOString()}'`;
|
|
92
|
-
queryPagination += ` AND time >= '${moment
|
|
93
|
-
.unix(timeStart)
|
|
94
|
-
.toISOString()}'`;
|
|
95
|
-
}
|
|
96
|
-
if (timeEnd) {
|
|
97
|
-
queryCount += ` AND time <= '${moment.unix(timeEnd).toISOString()}'`;
|
|
98
|
-
queryPagination += ` AND time <= '${moment.unix(timeEnd).toISOString()}'`;
|
|
99
|
-
}
|
|
100
|
-
if (sort && sort.field === "time") {
|
|
101
|
-
queryPagination = `${queryPagination} ORDER BY "${sort.field}" ${sort.sort}`;
|
|
102
|
-
}
|
|
103
|
-
queryPagination = `${queryPagination} LIMIT ${limit} OFFSET ${offset}`;
|
|
104
|
-
const responses = await Promise.all([
|
|
105
|
-
fetch(encodeURI(`${influxConfig.url}/query?db=${influxConfig.dbName}&epoch=s&q=${queryPagination}`), {
|
|
106
|
-
headers: {
|
|
107
|
-
Authorization: `Basic ${btoa(`${influxConfig.username}:${influxConfig.password}`)}`
|
|
108
|
-
}
|
|
109
|
-
}),
|
|
110
|
-
fetch(encodeURI(`${influxConfig.url}/query?db=${influxConfig.dbName}&epoch=s&q=${queryCount}`), {
|
|
111
|
-
headers: {
|
|
112
|
-
Authorization: `Basic ${btoa(`${influxConfig.username}:${influxConfig.password}`)}`
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
]);
|
|
116
|
-
if (!responses[0].ok) {
|
|
117
|
-
throw new Error(`Failed to fetch data from InfluxDB: ${responses[0].statusText}`);
|
|
199
|
+
// Always override the name to be the field
|
|
200
|
+
data.results[0].series.forEach((series) => {
|
|
201
|
+
series.name = field; // Force the series name to be the field name
|
|
202
|
+
});
|
|
203
|
+
// 1000000 REMOVED AND ADDED TO MOVE THE POINT AWAY IN THE CHART
|
|
204
|
+
if (fill !== "null") {
|
|
205
|
+
if (preStartValue !== null && preStartValue !== undefined) {
|
|
206
|
+
// Insert the pre-start value at the beginning of the dataset
|
|
207
|
+
data.results[0].series[0].values.unshift([
|
|
208
|
+
timeStart - 1000000,
|
|
209
|
+
preStartValue
|
|
210
|
+
]);
|
|
118
211
|
}
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
212
|
+
// Set the last point as the timeEnd and last value of the dataset
|
|
213
|
+
const lastSeries = data.results[0].series[0];
|
|
214
|
+
const lastValue = lastSeries?.values &&
|
|
215
|
+
lastSeries.values.length > 0 &&
|
|
216
|
+
lastSeries.values.slice(-1)[0].length > 1
|
|
217
|
+
? lastSeries.values.slice(-1)[0][1]
|
|
218
|
+
: null;
|
|
219
|
+
data.results[0].series[0].values.push([timeEnd + 1000000, lastValue]);
|
|
125
220
|
}
|
|
221
|
+
return data;
|
|
126
222
|
}
|
|
127
|
-
export async function
|
|
128
|
-
|
|
129
|
-
|
|
223
|
+
export async function exportDataV1(influxConfig, field, timeStart, timeEnd, filter) {
|
|
224
|
+
const query = `SELECT ("value") FROM "${influxConfig.measurement}" WHERE "${filter.field}" = '${filter.value}' AND "valueName" = '${field}' AND time >= '${moment
|
|
225
|
+
.unix(timeStart)
|
|
226
|
+
.toISOString()}' AND time <= '${moment.unix(timeEnd).toISOString()}'`;
|
|
130
227
|
const response = await fetch(encodeURI(`${influxConfig.url}/query?db=${influxConfig.dbName}&epoch=s&q=${query}`), {
|
|
131
228
|
headers: {
|
|
132
229
|
Authorization: `Basic ${btoa(`${influxConfig.username}:${influxConfig.password}`)}`
|
|
@@ -136,10 +233,22 @@ export async function getFirstTimestamp(influxConfig, deviceID) {
|
|
|
136
233
|
throw new Error(`Failed to fetch data from InfluxDB: ${response.statusText}`);
|
|
137
234
|
}
|
|
138
235
|
const data = await response.json();
|
|
139
|
-
//
|
|
140
|
-
if (data
|
|
141
|
-
//
|
|
142
|
-
|
|
236
|
+
// Ensure the name is manually set to the field
|
|
237
|
+
if (!data.results[0].series) {
|
|
238
|
+
// Set default value with null time and null value
|
|
239
|
+
data.results[0].series = [
|
|
240
|
+
{
|
|
241
|
+
name: field, // Manually set the series name as the field
|
|
242
|
+
columns: ["time", "value"],
|
|
243
|
+
values: [] // Set null point for time and value
|
|
244
|
+
}
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
// Always override the name to be the field
|
|
249
|
+
data.results[0].series.forEach((series) => {
|
|
250
|
+
series.name = field; // Force the series name to be the field name
|
|
251
|
+
});
|
|
143
252
|
}
|
|
144
|
-
return
|
|
253
|
+
return data;
|
|
145
254
|
}
|
|
@@ -1,19 +1,25 @@
|
|
|
1
|
+
import { TrackleDeviceType } from "../types";
|
|
1
2
|
export type TrackleConfig = {
|
|
3
|
+
orgName: string;
|
|
2
4
|
apiUrl: string;
|
|
3
|
-
createCustomerUrl: string;
|
|
4
5
|
clientId: string;
|
|
5
6
|
clientSecret: string;
|
|
6
7
|
tokenUrl: string;
|
|
7
8
|
cookieName: string;
|
|
8
9
|
cookieSecure: boolean;
|
|
9
10
|
apiTimeout: number;
|
|
11
|
+
productID: number;
|
|
10
12
|
};
|
|
11
13
|
export declare function logOut(trackleConfig: TrackleConfig): Promise<void>;
|
|
12
14
|
export declare function createCustomer(trackleConfig: TrackleConfig, uid: string): Promise<{
|
|
13
15
|
organization: string;
|
|
14
16
|
uid: string;
|
|
15
17
|
}>;
|
|
16
|
-
export declare function
|
|
18
|
+
export declare function removeCustomer(trackleConfig: TrackleConfig, uid: string): Promise<{
|
|
19
|
+
organization: string;
|
|
20
|
+
uid: string;
|
|
21
|
+
}>;
|
|
22
|
+
export declare function getDevices(trackleConfig: TrackleConfig, productId?: number, uid?: string, query?: string): Promise<{
|
|
17
23
|
devices: any[];
|
|
18
24
|
}>;
|
|
19
25
|
export declare function getDevice(trackleConfig: TrackleConfig, id: string, productId?: number, uid?: string): Promise<{
|
|
@@ -31,5 +37,5 @@ export declare function post(trackleConfig: TrackleConfig, id: string, endpoint:
|
|
|
31
37
|
export declare function put(trackleConfig: TrackleConfig, id: string, endpoint: string, value: any, productId?: number, uid?: string): Promise<{
|
|
32
38
|
return_value: number;
|
|
33
39
|
}>;
|
|
34
|
-
export declare function addDevicesToGroup(trackleConfig: TrackleConfig, productId: number, uid: string, group: string, devicesToPatch:
|
|
35
|
-
export declare function removeDevicesFromGroup(trackleConfig: TrackleConfig, productId: number, uid: string, group: string, devicesToPatch:
|
|
40
|
+
export declare function addDevicesToGroup(trackleConfig: TrackleConfig, productId: number, uid: string, group: string, devicesToPatch: TrackleDeviceType[]): Promise<TrackleDeviceType[]>;
|
|
41
|
+
export declare function removeDevicesFromGroup(trackleConfig: TrackleConfig, productId: number, uid: string, group: string, devicesToPatch: TrackleDeviceType[]): Promise<TrackleDeviceType[]>;
|
|
@@ -48,34 +48,35 @@ export async function logOut(trackleConfig) {
|
|
|
48
48
|
cookies().delete(trackleConfig.cookieName);
|
|
49
49
|
}
|
|
50
50
|
export async function createCustomer(trackleConfig, uid) {
|
|
51
|
-
return await wretch(trackleConfig.
|
|
51
|
+
return await wretch(`${trackleConfig.apiUrl}/orgs/${trackleConfig.orgName}/customers`)
|
|
52
52
|
.headers({
|
|
53
53
|
Authorization: `Basic ${btoa(`${trackleConfig.clientId}:${trackleConfig.clientSecret}`)}`
|
|
54
54
|
})
|
|
55
55
|
.post({ uid })
|
|
56
56
|
.json();
|
|
57
57
|
}
|
|
58
|
-
export async function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
export async function removeCustomer(trackleConfig, uid) {
|
|
59
|
+
return await wretch(`${trackleConfig.apiUrl}/orgs/${trackleConfig.orgName}/customers/${uid}`)
|
|
60
|
+
.headers({
|
|
61
|
+
Authorization: `Basic ${btoa(`${trackleConfig.clientId}:${trackleConfig.clientSecret}`)}`
|
|
62
|
+
})
|
|
63
|
+
.delete().json();
|
|
64
|
+
}
|
|
65
|
+
export async function getDevices(trackleConfig, productId, uid, query) {
|
|
66
|
+
const api = uid ? wretchApi(trackleConfig, uid) : wretchApi(trackleConfig);
|
|
67
|
+
let response;
|
|
68
|
+
try {
|
|
69
|
+
response = await api
|
|
70
|
+
.get(uid
|
|
71
|
+
? `/devices${query ? "?" + query : ""}`
|
|
72
|
+
: `/products/${productId}/devices${query ? "?" + query : ""}`)
|
|
73
|
+
.setTimeout(trackleConfig.apiTimeout)
|
|
74
|
+
.json();
|
|
65
75
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
queryParams += "&quarantined=false";
|
|
70
|
-
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
console.error("Error fetching devices:", error);
|
|
78
|
+
throw error;
|
|
71
79
|
}
|
|
72
|
-
const api = uid ? wretchApi(trackleConfig, uid) : wretchApi(trackleConfig);
|
|
73
|
-
const response = await api
|
|
74
|
-
.get(uid
|
|
75
|
-
? `/devices?${queryParams}`
|
|
76
|
-
: `/products/${productId}/devices?${queryParams}`)
|
|
77
|
-
.setTimeout(trackleConfig.apiTimeout)
|
|
78
|
-
.json();
|
|
79
80
|
return (uid ? { devices: response } : response);
|
|
80
81
|
}
|
|
81
82
|
export async function getDevice(trackleConfig, id, productId, uid) {
|
|
@@ -123,7 +124,9 @@ export async function put(trackleConfig, id, endpoint, value, productId, uid) {
|
|
|
123
124
|
export async function addDevicesToGroup(trackleConfig, productId, uid, group, devicesToPatch) {
|
|
124
125
|
const devicesPatched = [];
|
|
125
126
|
for (const device of devicesToPatch) {
|
|
126
|
-
const newGroups = device?.groups && device?.groups instanceof Array
|
|
127
|
+
const newGroups = device.state?.groups && device.state?.groups instanceof Array
|
|
128
|
+
? device.state?.groups
|
|
129
|
+
: [];
|
|
127
130
|
if (!newGroups.includes(group)) {
|
|
128
131
|
newGroups.push(group);
|
|
129
132
|
await wretchApi(trackleConfig, uid)
|
|
@@ -133,7 +136,7 @@ export async function addDevicesToGroup(trackleConfig, productId, uid, group, de
|
|
|
133
136
|
})
|
|
134
137
|
.setTimeout(trackleConfig.apiTimeout)
|
|
135
138
|
.res();
|
|
136
|
-
devicesPatched.push(device
|
|
139
|
+
devicesPatched.push(device);
|
|
137
140
|
}
|
|
138
141
|
}
|
|
139
142
|
return devicesPatched;
|
|
@@ -141,7 +144,9 @@ export async function addDevicesToGroup(trackleConfig, productId, uid, group, de
|
|
|
141
144
|
export async function removeDevicesFromGroup(trackleConfig, productId, uid, group, devicesToPatch) {
|
|
142
145
|
const devicesPatched = [];
|
|
143
146
|
for (const device of devicesToPatch) {
|
|
144
|
-
let newGroups = device?.groups && device?.groups instanceof Array
|
|
147
|
+
let newGroups = device.state?.groups && device.state?.groups instanceof Array
|
|
148
|
+
? device.state?.groups
|
|
149
|
+
: [];
|
|
145
150
|
if (newGroups.includes(group)) {
|
|
146
151
|
newGroups = newGroups.filter((newGroup) => newGroup !== group);
|
|
147
152
|
await wretchApi(trackleConfig, uid)
|
|
@@ -151,7 +156,7 @@ export async function removeDevicesFromGroup(trackleConfig, productId, uid, grou
|
|
|
151
156
|
})
|
|
152
157
|
.setTimeout(trackleConfig.apiTimeout)
|
|
153
158
|
.res();
|
|
154
|
-
devicesPatched.push(device
|
|
159
|
+
devicesPatched.push(device);
|
|
155
160
|
}
|
|
156
161
|
}
|
|
157
162
|
return devicesPatched;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type InfluxConfig = {
|
|
2
|
+
url: string;
|
|
3
|
+
accessToken: string;
|
|
4
|
+
bucket: string;
|
|
5
|
+
orgId: string;
|
|
6
|
+
measurement: string;
|
|
7
|
+
dbName: string;
|
|
8
|
+
username: string;
|
|
9
|
+
password: string;
|
|
10
|
+
};
|
|
11
|
+
export declare enum FilterTagMode {
|
|
12
|
+
Include = "include",// include tag + quelli senza tag
|
|
13
|
+
Exclude = "exclude",// esclude il tag
|
|
14
|
+
DW = "dw"
|
|
15
|
+
}
|
|
16
|
+
export type InfluxFillType = "null" | "none" | "previous";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type AssetType = {
|
|
2
|
+
id: string;
|
|
3
|
+
device?: TrackleDeviceType;
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
};
|
|
6
|
+
export type TrackleDeviceType = {
|
|
7
|
+
id: string;
|
|
8
|
+
state: {
|
|
9
|
+
groups: string[] | null;
|
|
10
|
+
};
|
|
11
|
+
managers: string[] | null;
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
};
|
|
14
|
+
export type DevicePositionType = {
|
|
15
|
+
deviceID: string;
|
|
16
|
+
name: string;
|
|
17
|
+
lat: number;
|
|
18
|
+
lng: number;
|
|
19
|
+
};
|
package/types/device.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/types/index.d.ts
CHANGED
package/types/index.js
CHANGED