@aceplus/smartace-api 2.12.0 → 2.13.0-alpha.1
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 +162 -158
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,12 +19,16 @@ To use the SmartAce Api in node you will need the following dependencies
|
|
|
19
19
|
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
Frontend: [https://saas.tkf-aceplus.com/](https://saas.tkf-aceplus.com/)
|
|
23
|
+
Auth: auth.tkf-aceplus.com
|
|
24
|
+
Asset Platform: saas.tkf-aceplus.com
|
|
25
|
+
|
|
22
26
|
## Auth
|
|
23
27
|
|
|
24
28
|
Authentication happens on the auth cluster which is located on the following URL
|
|
25
29
|
|
|
26
30
|
```typescript
|
|
27
|
-
var AuthAddress = "https://
|
|
31
|
+
var AuthAddress = "https://auth.tkf-aceplus.com";
|
|
28
32
|
```
|
|
29
33
|
|
|
30
34
|
### Login
|
|
@@ -38,29 +42,29 @@ import { AuthnProto } from "@aceplus/smartace-api/auth.authn_pb";
|
|
|
38
42
|
import { AuthnGRPC } from "@aceplus/smartace-api/auth.authn_pb_service";
|
|
39
43
|
|
|
40
44
|
function login(username: string, password: string): Promise<AuthnProto.AsObject> {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
45
|
+
return new Promise(async (resolve, reject) => {
|
|
46
|
+
let request = new AuthnProto.LoginRequest();
|
|
47
|
+
|
|
48
|
+
request.setUsername(username);
|
|
49
|
+
request.setPassword(password);
|
|
50
|
+
|
|
51
|
+
grpc.invoke(AuthnGRPC.Login, {
|
|
52
|
+
request,
|
|
53
|
+
transport: NodeHttpTransport(),
|
|
54
|
+
host: AuthAddress,
|
|
55
|
+
onMessage: (res) => {
|
|
56
|
+
resolve(res.toObject() as AuthnProto.AsObject);
|
|
57
|
+
},
|
|
58
|
+
onEnd: (code, message) => {
|
|
59
|
+
if (code === 0) {
|
|
60
|
+
console.log("Ok");
|
|
61
|
+
} else {
|
|
62
|
+
console.log({ code, message });
|
|
63
|
+
reject({ code, message });
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
});
|
|
64
68
|
}
|
|
65
69
|
|
|
66
70
|
var auth = await login("test@email.com", "password"); // fill in your credentials here
|
|
@@ -81,29 +85,29 @@ import { AuthnProto } from "@aceplus/smartace-api/auth.authn_pb";
|
|
|
81
85
|
import { AuthnGRPC } from "@aceplus/smartace-api/auth.authn_pb_service";
|
|
82
86
|
|
|
83
87
|
function refresh(accessToken: string, refreshToken: string): Promise<AuthnProto.AsObject> {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
88
|
+
return new Promise(async (resolve, reject) => {
|
|
89
|
+
let request = new AuthnProto.RefreshRequest();
|
|
90
|
+
|
|
91
|
+
request.setAccessToken(accessToken);
|
|
92
|
+
request.setRefreshToken(refreshToken);
|
|
93
|
+
|
|
94
|
+
grpc.invoke(AuthnGRPC.Refresh, {
|
|
95
|
+
request,
|
|
96
|
+
transport: NodeHttpTransport(),
|
|
97
|
+
host: AuthAddress,
|
|
98
|
+
onMessage: (res) => {
|
|
99
|
+
resolve(res.toObject() as AuthnProto.AsObject);
|
|
100
|
+
},
|
|
101
|
+
onEnd: (code, message) => {
|
|
102
|
+
if (code === 0) {
|
|
103
|
+
console.log("Ok");
|
|
104
|
+
} else {
|
|
105
|
+
console.log({ code, message });
|
|
106
|
+
reject({ code, message });
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
});
|
|
107
111
|
}
|
|
108
112
|
|
|
109
113
|
var auth = await login("test@email.com", "password");
|
|
@@ -116,36 +120,36 @@ auth = await refresh(auth.accessToken, auth.refreshToken);
|
|
|
116
120
|
To access the spic data, we need to access the operator cluster on the following URL
|
|
117
121
|
|
|
118
122
|
```typescript
|
|
119
|
-
var OperatorAddress = "https://
|
|
123
|
+
var OperatorAddress = "https://saas.tkf-aceplus.com";
|
|
120
124
|
|
|
121
125
|
// grpc call helper function
|
|
122
126
|
function BaseOperatorRequest<TRequest extends grpc.ProtobufMessage, TResponse extends grpc.ProtobufMessage, TMethod extends grpc.MethodDefinition<TRequest, TResponse>>(
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
accessToken: string,
|
|
128
|
+
method: TMethod,
|
|
129
|
+
request: TRequest
|
|
126
130
|
): Promise<TResponse> {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
131
|
+
return new Promise(async (resolve, reject) => {
|
|
132
|
+
// set accesssToken in header for authorization
|
|
133
|
+
const metadata = new grpc.Metadata();
|
|
134
|
+
metadata.set("authorization", "Bearer " + accessToken);
|
|
135
|
+
|
|
136
|
+
grpc.invoke(method, {
|
|
137
|
+
metadata,
|
|
138
|
+
request,
|
|
139
|
+
host: OperatorAddress,
|
|
140
|
+
transport: NodeHttpTransport(), // required for Node
|
|
141
|
+
onMessage: (res) => {
|
|
142
|
+
resolve(res as TResponse);
|
|
143
|
+
},
|
|
144
|
+
onEnd: (code, message) => {
|
|
145
|
+
if (code === 0) {
|
|
146
|
+
console.log("Ok");
|
|
147
|
+
} else {
|
|
148
|
+
reject({ code, message });
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
});
|
|
149
153
|
}
|
|
150
154
|
```
|
|
151
155
|
|
|
@@ -155,18 +159,18 @@ Searching cabinets with pagination
|
|
|
155
159
|
|
|
156
160
|
```typescript
|
|
157
161
|
function searchCabinets(accessToken: string, name: string, count: number, page: number): Promise<CabinetProto.SearchReply> {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
162
|
+
return new Promise(async (resolve, reject) => {
|
|
163
|
+
const request = new CabinetProto.SearchRequest();
|
|
164
|
+
name && name.length >= 3 && request.setName(name);
|
|
165
|
+
request.setCount(count);
|
|
166
|
+
request.setPage(page);
|
|
167
|
+
|
|
168
|
+
BaseOperatorRequest(accessToken, CabinetGRPC.Search, request)
|
|
169
|
+
.then((res) => {
|
|
170
|
+
resolve(res as CabinetProto.SearchReply);
|
|
171
|
+
})
|
|
172
|
+
.catch((err) => reject(err));
|
|
173
|
+
});
|
|
170
174
|
}
|
|
171
175
|
|
|
172
176
|
var cabinets = await searchCabinets(auth.accessToken, "", 10, 0);
|
|
@@ -183,29 +187,29 @@ Or retrieve cabinet by id or Uid
|
|
|
183
187
|
|
|
184
188
|
```typescript
|
|
185
189
|
function retrieveCabinetById(accessToken: string, id: number): Promise<CabinetWithLocation> {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
190
|
+
return new Promise(async (resolve, reject) => {
|
|
191
|
+
const request = new CabinetProto.RetrieveRequest();
|
|
192
|
+
request.setId(id);
|
|
193
|
+
|
|
194
|
+
BaseOperatorRequest(accessToken, CabinetGRPC.Retrieve, request)
|
|
195
|
+
.then((res) => {
|
|
196
|
+
resolve(res as CabinetWithLocation);
|
|
197
|
+
})
|
|
198
|
+
.catch((err) => reject(err));
|
|
199
|
+
});
|
|
196
200
|
}
|
|
197
201
|
|
|
198
202
|
function retrieveCabinetByUid(accessToken: string, uid: string): Promise<CabinetWithLocation> {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
203
|
+
return new Promise(async (resolve, reject) => {
|
|
204
|
+
const request = new CabinetProto.RetrieveRequest();
|
|
205
|
+
request.setUid(uid);
|
|
206
|
+
|
|
207
|
+
BaseOperatorRequest(accessToken, CabinetGRPC.Retrieve, request)
|
|
208
|
+
.then((res) => {
|
|
209
|
+
resolve(res as CabinetWithLocation);
|
|
210
|
+
})
|
|
211
|
+
.catch((err) => reject(err));
|
|
212
|
+
});
|
|
209
213
|
}
|
|
210
214
|
|
|
211
215
|
let cabinet1 = await retrieveCabinetById(auth.accessToken, 1);
|
|
@@ -218,20 +222,20 @@ console.log(cabinet2.toObject());
|
|
|
218
222
|
|
|
219
223
|
```typescript
|
|
220
224
|
function retrieveEvents(accessToken: string, cabinetId: number, count: number, page: number, descendingOrder: boolean): Promise<CabinetProto.Event.SearchReply> {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
225
|
+
return new Promise(async (resolve, reject) => {
|
|
226
|
+
const request = new CabinetProto.Event.SearchRequest();
|
|
227
|
+
|
|
228
|
+
request.setCabinetId(cabinetId);
|
|
229
|
+
request.setCount(count);
|
|
230
|
+
request.setPage(page);
|
|
231
|
+
request.setDescorder(descendingOrder);
|
|
232
|
+
|
|
233
|
+
BaseOperatorRequest(accessToken, CabinetGRPC.SearchEvents, request)
|
|
234
|
+
.then((res) => {
|
|
235
|
+
resolve(res as CabinetProto.Event.SearchReply);
|
|
236
|
+
})
|
|
237
|
+
.catch((err) => reject(err));
|
|
238
|
+
});
|
|
235
239
|
}
|
|
236
240
|
let events = await retrieveEvents(auth.accessToken, cabinet2.getCabinet()!.getId(), 10, 0, false);
|
|
237
241
|
|
|
@@ -242,23 +246,23 @@ console.log(events.toObject());
|
|
|
242
246
|
|
|
243
247
|
```typescript
|
|
244
248
|
function retrieveStates(accessToken: string, uid: string, count: number, page: number, descendingOrder: boolean): Promise<CabinetProto.State.SearchReply> {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
249
|
+
return new Promise(async (resolve, reject) => {
|
|
250
|
+
const request = new CabinetProto.State.SearchRequest();
|
|
251
|
+
|
|
252
|
+
request.setUid(uid);
|
|
253
|
+
request.setCount(count);
|
|
254
|
+
request.setPage(page);
|
|
255
|
+
request.setDescorder(descendingOrder);
|
|
256
|
+
|
|
257
|
+
request.setBatteryTo(4000);
|
|
258
|
+
request.setTemperatureTo(100);
|
|
259
|
+
|
|
260
|
+
BaseOperatorRequest(accessToken, CabinetGRPC.SearchStates, request)
|
|
261
|
+
.then((res) => {
|
|
262
|
+
resolve(res as CabinetProto.State.SearchReply);
|
|
263
|
+
})
|
|
264
|
+
.catch((err) => reject(err));
|
|
265
|
+
});
|
|
262
266
|
}
|
|
263
267
|
let states = await retrieveStates(auth.accessToken, cabinet2.getCabinet()!.getUid(), 10, 0, false);
|
|
264
268
|
|
|
@@ -273,36 +277,36 @@ Here you have option for how long the opening is valid and if it is a single use
|
|
|
273
277
|
|
|
274
278
|
```typescript
|
|
275
279
|
function Open(accessToken: string, cabinetId: number): Promise<CabinetProto.OpenRequest> {
|
|
276
|
-
|
|
277
|
-
|
|
280
|
+
return new Promise(async (resolve, reject) => {
|
|
281
|
+
const request = new CabinetProto.OpenRequest.CreateRequest();
|
|
278
282
|
|
|
279
|
-
|
|
283
|
+
request.setCabinetId(cabinetId);
|
|
280
284
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
285
|
+
function DateToTimestamp(date: Date): Timestamp {
|
|
286
|
+
let ts = new Timestamp();
|
|
287
|
+
let seconds = Math.round(date.getTime() / 1000);
|
|
288
|
+
ts.setSeconds(seconds);
|
|
289
|
+
return ts;
|
|
290
|
+
}
|
|
287
291
|
|
|
288
|
-
|
|
289
|
-
|
|
292
|
+
var now = new Date(Date.now());
|
|
293
|
+
request.setRequestTime(DateToTimestamp(now));
|
|
290
294
|
|
|
291
|
-
|
|
292
|
-
|
|
295
|
+
var expirationDate = new Date(Date.now());
|
|
296
|
+
expirationDate.setTime(expirationDate.getTime() + 60 * 60 * 1000);
|
|
293
297
|
|
|
294
|
-
|
|
298
|
+
request.setValidUntil(DateToTimestamp(expirationDate));
|
|
295
299
|
|
|
296
|
-
|
|
300
|
+
request.setType(CabinetProto.OpenRequest.OpenRequestType.ADMIN_OPENING);
|
|
297
301
|
|
|
298
|
-
|
|
302
|
+
request.setDeactivateafteropening(true);
|
|
299
303
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
304
|
+
BaseOperatorRequest(accessToken, CabinetGRPC.CreateOpenRequest, request)
|
|
305
|
+
.then((res) => {
|
|
306
|
+
resolve(res as CabinetProto.OpenRequest);
|
|
307
|
+
})
|
|
308
|
+
.catch((err) => reject(err));
|
|
309
|
+
});
|
|
306
310
|
}
|
|
307
311
|
|
|
308
312
|
let openRequest = await Open(auth.accessToken, cabinet2.getCabinet()!.getId()).catch((err) => console.log(err));
|