/
Devices.ts
213 lines (190 loc) · 5.26 KB
/
Devices.ts
1
2
3
4
5
6
7
8
9
10
11
12
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
GenericID,
GenericToken,
ListTokenQuery,
TokenCreateResponse,
TokenData,
TokenDataList,
} from "../../common/common.types";
import TagoIOModule, { GenericModuleParams } from "../../common/TagoIOModule";
import {
ConfigurationParams,
DeviceCreateInfo,
DeviceCreateResponse,
DeviceInfo,
DeviceQuery,
DeviceListItem,
} from "./devices.types";
class Devices extends TagoIOModule<GenericModuleParams> {
/**
* Retrieves a list with all devices from the account
* @default
* queryObj: {
* page: 1,
* fields: ["id", "name"],
* filter: {},
* amount: 20,
* orderBy: "name,asc",
* resolveBucketName: false
* }
* @param queryObj Search query params
*/
list(queryObj?: DeviceQuery): Promise<DeviceListItem[]> {
const result = this.doRequest<DeviceListItem[]>({
path: "/device",
method: "GET",
params: {
page: queryObj?.page || 1,
fields: queryObj?.fields || ["id", "name"],
filter: queryObj?.filter || {},
amount: queryObj?.amount || 20,
orderBy: queryObj?.orderBy ? `${queryObj.orderBy[0]},${queryObj.orderBy[1]}` : "name,asc",
resolveBucketName: queryObj?.resolveBucketName || false,
},
});
return result;
}
/**
* Generates and retrieves a new action from the Device
* @param deviceObj Object data to create new device
*/
create(deviceObj: DeviceCreateInfo): Promise<DeviceCreateResponse> {
const result = this.doRequest<DeviceCreateResponse>({
path: "/device",
method: "POST",
body: deviceObj,
});
return result;
}
/**
* Modify any property of the device
* @param deviceID Device ID
* @param deviceObj Device object with fields to replace
*/
edit(deviceID: GenericID, deviceObj: Partial<DeviceInfo>): Promise<string> {
const result = this.doRequest<string>({
path: `/device/${deviceID}`,
method: "PUT",
body: deviceObj,
});
return result;
}
/**
* Deletes an device from the account
* @param deviceID Device ID
*/
delete(deviceID: GenericID): Promise<string> {
const result = this.doRequest<string>({
path: `/device/${deviceID}`,
method: "DELETE",
});
return result;
}
/**
* Get Info of the Device
* @param deviceID Device ID
*/
info(deviceID: GenericID): Promise<DeviceInfo> {
const result = this.doRequest<DeviceInfo>({
path: `/device/${deviceID}`,
method: "GET",
});
return result;
}
/**
* Create or edit param for the Device
* @param deviceID Device ID
* @param configObj Configuration Data
* @param paramID Parameter ID
*/
paramSet(deviceID: GenericID, configObj: Partial<ConfigurationParams>, paramID?: GenericID): Promise<string> {
const result = this.doRequest<string>({
path: `/device/${deviceID}/params`,
method: "POST",
body: paramID
? {
id: paramID,
...configObj,
}
: configObj,
});
return result;
}
/**
* List Params for the Device
* @param deviceID Device ID
* @param sentStatus True return only sent=true, False return only sent=false
*/
paramList(deviceID: GenericID, sentStatus?: Boolean): Promise<ConfigurationParams[]> {
const result = this.doRequest<ConfigurationParams[]>({
path: `/device/${deviceID}/params`,
method: "GET",
params: { sent_status: sentStatus },
});
return result;
}
/**
* Remove param for the Device
* @param deviceID Device ID
* @param paramID Parameter ID
*/
paramRemove(deviceID: GenericID, paramID: GenericID): Promise<string> {
const result = this.doRequest<string>({
path: `/device/${deviceID}/params/${paramID}`,
method: "DELETE",
});
return result;
}
/**
* Retrieves a list of all tokens
* @default
* queryObj: {
* page: 1,
* fields: ["name", "token", "permission"],
* filter: {},
* amount: 20,
* orderBy: "created_at,desc",
* }
* @param deviceID Device ID
* @param queryObj Search query params
*/
tokenList(deviceID: GenericID, queryObj?: ListTokenQuery): Promise<Partial<TokenDataList>[]> {
const result = this.doRequest<Partial<TokenDataList>[]>({
path: `/device/token/${deviceID}`,
method: "GET",
params: {
page: queryObj?.page || 1,
fields: queryObj?.fields || ["name", "token", "permission"],
filter: queryObj?.filter || {},
amount: queryObj?.amount || 20,
orderBy: queryObj?.orderBy ? `${queryObj.orderBy[0]},${queryObj.orderBy[1]}` : "created_at,desc",
},
});
return result;
}
/**
* Generates and retrieves a new token
* @param deviceID Device ID
* @param tokenParams Params for new token
*/
tokenCreate(deviceID: GenericID, tokenParams: TokenData): Promise<TokenCreateResponse> {
const result = this.doRequest<TokenCreateResponse>({
path: `/device/token`,
method: "POST",
body: { device: deviceID, ...tokenParams },
});
return result;
}
/**
* Delete a token
* @param token Token
*/
tokenDelete(token: GenericToken): Promise<string> {
const result = this.doRequest<string>({
path: `/device/token/${token}`,
method: "DELETE",
});
return result;
}
}
export default Devices;