-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice.ts
271 lines (228 loc) · 7.8 KB
/
device.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import Device from '../../../sdk/javascript/Device';
import Sdk from '../../../sdk/javascript/Sdk';
import Service from '../../../sdk/javascript/Service';
import ServiceRequest from '../../../sdk/javascript/ServiceRequest';
import ServiceResponse from '../../../sdk/javascript/ServiceResponse';
import moment from '../../../sdk/javascript/moment';
import { fatal, getCredentials, log } from './util';
const ORG_ID = 'Org1MSP';
const ORG_DOMAIN = 'org1.example.com';
const USER_NAME = '[email protected]';
const PEER_NAME = 'peer0.org1.example.com';
const PEER_ENDPOINT = 'localhost:7051';
async function registerDevice(isb: Sdk) {
const expected = new Device(
isb.getDeviceId(),
isb.getOrganizationId(),
'device1',
'My first device',
moment(),
);
await isb.getDeviceRegistry().register(expected);
log(`Registered device ${expected.id}`);
let actual = await isb.getDeviceRegistry().get(isb.getOrganizationId(), expected.id);
if (
expected.name !== actual.name ||
expected.lastUpdateTime.valueOf() !== actual.lastUpdateTime.valueOf()
) {
fatal(
`inconsistent device information after registration: ${actual.serialize()} != ${expected.serialize()}`,
);
}
try {
await isb.getDeviceRegistry().get(isb.getOrganizationId(), 'invalid_id');
fatal('should return error when device is not found');
} catch {}
const devices = await isb.getDeviceRegistry().getAll(isb.getOrganizationId());
if (devices.length != 1) {
fatal(`should return only 1 device from ${isb.getOrganizationId()}`);
}
actual = devices[0];
if (
expected.name !== actual.name ||
expected.lastUpdateTime.valueOf() !== actual.lastUpdateTime.valueOf()
) {
fatal(
`inconsistent device information after registration: ${actual.serialize()} != ${expected.serialize()}`,
);
}
}
async function registerServices(isb: Sdk) {
const services = [
new Service(
'service1',
isb.getDeviceId(),
isb.getOrganizationId(),
1,
'My first service',
moment(),
),
new Service(
'service2',
isb.getDeviceId(),
isb.getOrganizationId(),
1,
'My second service',
moment(),
),
];
for (const service of services) {
await isb.getServiceRegistry().register(service);
log(`Registered service ${service.serialize()}`);
const actual = await isb
.getServiceRegistry()
.get(isb.getOrganizationId(), service.deviceId, service.name);
if (
service.name !== actual.name ||
service.lastUpdateTime.valueOf() !== actual.lastUpdateTime.valueOf() ||
service.version !== actual.version
) {
fatal(
`inconsistent service information after registration: ${actual.serialize()} != ${service.serialize()}`,
);
}
}
try {
await isb.getServiceRegistry().get(isb.getOrganizationId(), isb.getDeviceId(), 'invalid_id');
fatal('should return error when service is not found');
} catch {}
const actuals = await isb.getServiceRegistry().getAll(isb.getOrganizationId(), isb.getDeviceId());
if (services.length !== actuals.length) {
fatal(`should return ${services.length} service from ${isb.getOrganizationId()}`);
}
for (let i = 0; i < services.length; i++) {
const expected = services[i];
const actual = actuals[i];
if (
expected.name !== actual.name ||
expected.lastUpdateTime.valueOf() !== actual.lastUpdateTime.valueOf() ||
expected.version !== actual.version
) {
fatal(
`inconsistent service information after registration: ${actual.serialize()} != ${expected.serialize()}`,
);
}
}
}
async function handleRequests(isb: Sdk) {
const events = await isb.getServiceBroker().registerEvent();
const timeout = setTimeout(() => {
events.close();
fatal('timed out waiting for requests');
}, 120 * 1000);
log('Listening for requests');
let count = 0;
for await (const event of events) {
if (event.action !== 'request') {
continue;
}
count++;
const request = event.payload as ServiceRequest;
log(`Received request ${request.serialize()}`);
const response = new ServiceResponse(
request.id,
moment(),
0,
[request.method, ...request.args].join(','),
);
await isb.getServiceBroker().respond(response);
log(`Sent response ${response.serialize()}`);
if (count === 2) {
break;
}
}
clearTimeout(timeout);
events.close();
}
async function checkAndRemoveRequests(isb: Sdk) {
const before = await isb
.getServiceBroker()
.getAll(isb.getOrganizationId(), isb.getDeviceId(), 'service1');
if (before.length === 0) {
fatal(`request/response for service1 is not found`);
}
const pair = before[0];
await isb.getServiceBroker().get(pair.request.id);
if (pair.response?.requestId) {
if (pair.request.id !== pair.response?.requestId) {
fatal(`request and response ID mismatch, ${pair.request.id} != ${pair.response?.requestId}`);
}
await isb.getServiceBroker().remove(pair.request.id);
}
const after = await isb
.getServiceBroker()
.getAll(isb.getOrganizationId(), isb.getDeviceId(), 'service1');
if (before.length - 1 !== after.length) {
fatal(
`incorrect request/response count after removal: ${before.length} - 1 != ${after.length}`,
);
}
}
async function deregisterOneService(isb: Sdk) {
const before = await isb.getServiceRegistry().getAll(isb.getOrganizationId(), isb.getDeviceId());
const service = new Service('service2', isb.getDeviceId(), isb.getOrganizationId());
await isb.getServiceRegistry().deregister(service);
log(`Deregistered service ${service.serialize()}`);
try {
await isb.getServiceRegistry().get(isb.getOrganizationId(), isb.getDeviceId(), service.name);
fatal('should return error when service is already deregistered');
} catch {}
const after = await isb.getServiceRegistry().getAll(isb.getOrganizationId(), isb.getDeviceId());
if (before.length - 1 !== after.length) {
fatal(`incorrect service count after deregistration: ${before.length} - 1 != ${after.length}`);
}
for (const existing of after) {
if (existing.name === service.name) {
fatal(`service ${service.name} has not been correctly removed`);
}
}
}
async function deregisterDevice(isb: Sdk) {
const before = await isb.getDeviceRegistry().getAll(isb.getOrganizationId());
const device = new Device(isb.getDeviceId(), isb.getOrganizationId());
await isb.getDeviceRegistry().deregister(device);
log(`Deregistered device ${device.serialize()}`);
const after = await isb.getDeviceRegistry().getAll(isb.getOrganizationId());
if (before.length - 1 !== after.length) {
fatal(`incorrect device count after deregistration: ${before.length} - 1 != ${after.length}`);
}
for (const existing of after) {
if (existing.name === device.name) {
fatal(`device ${device.name} has not been correctly removed`);
}
}
const services = await isb
.getServiceRegistry()
.getAll(isb.getOrganizationId(), isb.getDeviceId());
if (services.length !== 0) {
fatal('should have removed all services');
}
}
async function main() {
const [certificate, privateKey, tlsCertificate] = getCredentials(
process.env.FABRIC_ROOT ?? '.',
ORG_DOMAIN,
USER_NAME,
PEER_NAME,
);
const isb = new Sdk({
organizationId: ORG_ID,
certificate,
privateKey,
gatewayPeerEndpoint: PEER_ENDPOINT,
gatewayPeerServerName: PEER_NAME,
gatewayPeerTLSCertificate: tlsCertificate,
networkName: 'mychannel',
chaincodeId: 'iotservice',
});
log(`Organization ID is ${isb.getOrganizationId()}`);
log(`Device ID is ${isb.getDeviceId()}`);
await registerDevice(isb);
await registerServices(isb);
await handleRequests(isb);
await checkAndRemoveRequests(isb);
await deregisterOneService(isb);
await deregisterDevice(isb);
isb.close();
}
main();