This repository has been archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabric_test_client.js
218 lines (191 loc) · 7.3 KB
/
fabric_test_client.js
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
let moment = require('moment')
let cardName = "admin@hackerton-network"
let composer = require('composer-client');
let BusinessNetworkConnection = composer.BusinessNetworkConnection;
let connection = new BusinessNetworkConnection();
let definition = "";
let factory = "";
main();
async function main() {
definition = await connection.connect(cardName);
factory = definition.getFactory();
// 모두 제거
var before = moment().unix()
console.log("데이터 제거 중..");
await removeAll('org.hackerton.Hospital');
await removeAll('org.hackerton.Requester');
await removeAll('org.hackerton.EmergencyCar');
await removeAll('org.hackerton.EmergencyBed');
var after = moment().unix()
console.log(after - before)
// 요청자 추가
console.log("요청자 추가 중...");
var before = moment().unix()
await addRequester(10, 5);
var after = moment().unix()
console.log(after - before)
console.log("===============")
// 요청자 목록 읽기
var before = moment().unix()
let RequesterList = await getAll('org.hackerton.Requester');
for (let i = 0; i < RequesterList.length; i++) {
console.log(`요청자 : ${RequesterList[i].RequesterID}: ${RequesterList[i].x}, ${RequesterList[i].y}`);
}
var after = moment().unix()
console.log(after - before)
console.log("===============")
// 병원 추가
var before = moment().unix()
await addHospital("A병원", "1");
await addHospital("B병원", "2");
await addHospital("C병원", "3");
await addHospital("D병원", "4");
var after = moment().unix()
console.log(after - before)
console.log("===============")
// 병원 목록 읽기
var before = moment().unix()
let HospitalList = await getAll('org.hackerton.Hospital');
console.log("병원============")
for (let i = 0; i < HospitalList.length; i++) {
console.log(`병원 : ${HospitalList[i].HospitalID}: ${HospitalList[i].name}`);
}
var after = moment().unix()
console.log(after - before)
console.log("===============")
// 관계 추가
let HospitalA = factory.newRelationship('org.hackerton', 'Hospital', '1');
let HospitalB = factory.newRelationship('org.hackerton', 'Hospital', '2');
let HospitalC = factory.newRelationship('org.hackerton', 'Hospital', '3');
let HospitalD = factory.newRelationship('org.hackerton', 'Hospital', '4');
// 구급차 추가
await addCar(1, 10, HospitalA);
// 구급차 읽기
console.log("구급차");
// 응급실 추가
let bedA = await addBed(2, 15, HospitalA);
let bedB = await addBed(10, 15, HospitalB);
let bedC = await addBed(2, 2, HospitalC);
let bedD = await addBed(10, 2, HospitalD);
// 응급실 읽기
let EmergencyBedList = await getAll('org.hackerton.EmergencyBed');
console.log("응급실 좌석=========")
for (let i = 0; i < EmergencyBedList.length; i++) {
console.log(`좌석: ${EmergencyBedList[i].BedID}: ${EmergencyBedList[i].x}, ${EmergencyBedList[i].y}, ${EmergencyBedList[i].owner}`);
}
// 의사 추가
// 스케줄 추가
// 쿼리
console.log("===============")
let find_bed_result = await query_FindEmergencyBed(10,5);
console.log(`결과 2`)
console.log(`${find_bed_result}`)
bedA.isOccupied = true;
bedB.isOccupied = true;
await updateBed(bedA);
await updateBed(bedB);
console.log("===============")
let find_bed_result_after = await query_FindEmergencyBed(10,5);
console.log(`결과 2`)
console.log(`${find_bed_result_after}`)
console.log("===============")
}
async function request(requester, x, y) {
let serializer = definition.getSerializer();
let registry = connection.getAssetRegistry('org.hackerton.Patient');
let currentTime = moment().unix();
patient = factory.newResource('org.hackerton', 'Patient', currentTime)
patient.requester = requester
patient.x = x
patient.y = y
await this.registry.addAll([request]);
let request = serializer.fromJSON({
"$class": 'org.hackerton.requestEmergencyCar',
'patient': patient,
'requester': requester,
'x': x,
'y': y
})
await connection.submitTransaction(request)
}
async function addDoctor(name, major, hospital){
let registry = await connection.getParticipantRegistry('org.hackerton.Doctor');
let currentTime = moment().unix();
let newDoctor = factory.newResource('org.hackerton','Doctor', currentTime.toString());
newDoctor.name = name;
newDoctor.major = major;
newDoctor.hospital = hospital;
await registry.add(newDoctor);
}
async function addRequester(x, y){
let registry = await connection.getParticipantRegistry('org.hackerton.Requester');
let currentTime = moment().unix();
let newData = factory.newResource('org.hackerton','Requester', currentTime.toString());
newData.x = x;
newData.y = y;
await registry.add(newData);
}
async function addHospital(name, id){
let registry = await connection.getParticipantRegistry('org.hackerton.Hospital');
let currentTime = moment().unix();
hospital = factory.newResource('org.hackerton','Hospital', id);
hospital.name = name;
await registry.add(hospital);
return hospital
}
async function addCar(x, y, owner){
let registry = await connection.getParticipantRegistry('org.hackerton.EmergencyCar');
let currentTime = moment().unix();
let newData = factory.newResource('org.hackerton','EmergencyCar', currentTime.toString());
newData.x = x;
newData.y = y;
newData.isOccupied = false;
newData.owner = owner;
await registry.add(newData);
}
async function addBed(x, y, owner){
let registry = await connection.getParticipantRegistry('org.hackerton.EmergencyBed');
let currentTime = moment().unix();
let newData = factory.newResource('org.hackerton','EmergencyBed', currentTime.toString());
newData.x = x;
newData.y = y;
newData.isOccupied = false;
newData.owner = owner;
await registry.add(newData);
return newData;
}
async function addSchedule(doctor, start, end){
let registry = await connection.getAssetRegistry('org.hackerton.Schedule');
let currentTime = moment().unix();
let newData = factory.newResource('org.hackerton','Schedule', currentTime.toString());
newData.doctor = doctor;
newData.start = start;
newData.end = end;
await registry.add(newData);
}
async function addPatient(requester, x, y){
let registry = await connection.getAssetRegistry('org.hackerton.Patient');
let currentTime = moment().unix();
let newData = factory.newResource('org.hackerton','Patient', currentTime.toString());
newData.requester = requester;
newData.x = x;
newData.y = y;
await registry.add(newData);
}
async function getAll(FDQN) {
let registry = await connection.getParticipantRegistry(FDQN);
let list = await registry.getAll();
return list;
}
async function removeAll(FDQN) {
let registry = await connection.getParticipantRegistry(FDQN);
let list = await registry.getAll();
await registry.removeAll(list)
}
async function query_FindEmergencyBed(x,y){
let beds = await connection.query('FindEmergencyBed');
}
async function updateBed(bed){
let registry = await connection.getParticipantRegistry('org.hackerton.EmergencyBed');
await registry.update(bed);
}