This repository has been archived by the owner on May 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathlogic.js
298 lines (262 loc) · 11.6 KB
/
logic.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A shipment has been received by an importer
* @param {org.acme.shipping.perishable.ShipmentReceived} shipmentReceived - the ShipmentReceived transaction
* @transaction
*/
function payOut(shipmentReceived) {
var contract = shipmentReceived.shipment.contract;
var shipment = shipmentReceived.shipment;
var payOut = contract.unitPrice * shipment.unitCount;
//console.log('Received at: ' + shipmentReceived.timestamp);
//console.log('Contract arrivalDateTime: ' + contract.arrivalDateTime);
// set the status of the shipment
shipment.status = 'ARRIVED';
// if the shipment did not arrive on time the payout is zero
if (shipmentReceived.timestamp > contract.arrivalDateTime) {
payOut = 0;
//console.log('Late shipment');
} else {
// find the lowest temperature reading
if (shipment.temperatureReadings) {
// sort the temperatureReadings by celsius
shipment.temperatureReadings.sort(function (a, b) {
return (a.celsius - b.celsius);
});
var lowestReading = shipment.temperatureReadings[0];
var highestReading = shipment.temperatureReadings[shipment.temperatureReadings.length - 1];
var penalty = 0;
//console.log('Lowest temp reading: ' + lowestReading.celsius);
//console.log('Highest temp reading: ' + highestReading.celsius);
// does the lowest temperature violate the contract?
if (lowestReading.celsius < contract.minTemperature) {
penalty += (contract.minTemperature - lowestReading.celsius) * contract.minPenaltyFactor;
//console.log('Min temp penalty: ' + penalty);
}
// does the highest temperature violate the contract?
if (highestReading.celsius > contract.maxTemperature) {
penalty += (highestReading.celsius - contract.maxTemperature) * contract.maxPenaltyFactor;
//console.log('Max temp penalty: ' + penalty);
}
// apply any penalities
payOut -= (penalty * shipment.unitCount);
if (payOut < 0) {
payOut = 0;
}
}
}
//console.log('Payout: ' + payOut);
contract.grower.accountBalance += payOut;
contract.importer.accountBalance -= payOut;
//console.log('Grower: ' + contract.grower.$identifier + ' new balance: ' + contract.grower.accountBalance);
//console.log('Importer: ' + contract.importer.$identifier + ' new balance: ' + contract.importer.accountBalance);
return getParticipantRegistry('org.acme.shipping.perishable.Grower')
.then(function (growerRegistry) {
// update the grower's balance
return growerRegistry.update(contract.grower);
})
.then(function () {
return getParticipantRegistry('org.acme.shipping.perishable.Importer');
})
.then(function (importerRegistry) {
// update the importer's balance
return importerRegistry.update(contract.importer);
})
.then(function () {
return getAssetRegistry('org.acme.shipping.perishable.Shipment');
})
.then(function (shipmentRegistry) {
// update the state of the shipment
return shipmentRegistry.update(shipment);
});
}
/**
* A temperature reading has been received for a shipment
* @param {org.acme.shipping.perishable.TemperatureReading} temperatureReading - the TemperatureReading transaction
* @transaction
*/
function temperatureReading(temperatureReading) {
var shipment = temperatureReading.shipment;
var NS = 'org.acme.shipping.perishable';
var contract = shipment.contract;
var factory = getFactory();
//console.log('Adding temperature ' + temperatureReading.celsius + ' to shipment ' + shipment.$identifier);
if (shipment.temperatureReadings) {
shipment.temperatureReadings.push(temperatureReading);
} else {
shipment.temperatureReadings = [temperatureReading];
}
if (temperatureReading.celsius < contract.minTemperature ||
temperatureReading.celsius > contract.maxTemperature) {
var temperatureEvent = factory.newEvent(NS, 'TemperatureThresholdEvent');
temperatureEvent.shipment = shipment;
temperatureEvent.temperature = temperatureReading.celsius;
temperatureEvent.latitude = temperatureReading.latitude;
temperatureEvent.longitude = temperatureReading.longitude;
temperatureEvent.readingTime = temperatureReading.readingTime;
temperatureEvent.message = 'Temperature threshold violated! Emitting TemperatureEvent for shipment: ' + shipment.$identifier;
emit(temperatureEvent);
}
return getAssetRegistry(NS + '.Shipment')
.then(function (shipmentRegistry) {
// add the temp reading to the shipment
return shipmentRegistry.update(shipment);
});
}
/**
* An Acceleration reading has been received for a shipment
* @param {org.acme.shipping.perishable.AccelReading} AccelReading - the AccelReading transaction
* @transaction
*/
function AccelReading(AccelReading) {
var shipment = AccelReading.shipment;
var NS = 'org.acme.shipping.perishable';
var contract = shipment.contract;
var factory = getFactory();
//console.log('Adding acceleration ' + AccelReading.accel_x + ' to shipment ' + shipment.$identifier);
if (shipment.AccelReadings) {
shipment.AccelReadings.push(AccelReading);
} else {
shipment.AccelReadings = [AccelReading];
}
// Also test for accel_y / accel_z
if (AccelReading.accel_x < contract.maxAccel ) {
var AccelerationEvent = factory.newEvent(NS, 'AccelerationThresholdEvent');
AccelerationEvent.shipment = shipment;
AccelerationEvent.accel_x = AccelReading.accel_x;
AccelerationEvent.accel_y = AccelReading.accel_y;
AccelerationEvent.accel_z = AccelReading.accel_z;
AccelerationEvent.latitude = AccelReading.latitude;
AccelerationEvent.longitude = AccelReading.longitude;
AccelerationEvent.readingTime = AccelReading.readingTime;
AccelerationEvent.message = 'Acceleration threshold violated! Emitting AccelerationEvent for shipment: ' + shipment.$identifier;
emit(AccelerationEvent);
}
return getAssetRegistry(NS + '.Shipment')
.then(function (shipmentRegistry) {
// add the temp reading to the shipment
return shipmentRegistry.update(shipment);
});
}
/**
* A GPS reading has been received for a shipment
* @param {org.acme.shipping.perishable.GpsReading} gpsReading - the GpsReading transaction
* @transaction
*/
function gpsReading(gpsReading) {
var factory = getFactory();
var NS = "org.acme.shipping.perishable";
var shipment = gpsReading.shipment;
var PORT_OF_NEW_YORK = '/LAT:40.6840N/LONG:74.0062W';
if (shipment.gpsReadings) {
shipment.gpsReadings.push(gpsReading);
} else {
shipment.gpsReadings = [gpsReading];
}
var latLong = '/LAT:' + gpsReading.latitude + gpsReading.latitudeDir + '/LONG:' +
gpsReading.longitude + gpsReading.longitudeDir;
if (latLong == PORT_OF_NEW_YORK) {
var shipmentInPortEvent = factory.newEvent(NS, 'ShipmentInPortEvent');
shipmentInPortEvent.shipment = shipment;
var message = 'Shipment has reached the destination port of ' + PORT_OF_NEW_YORK;
shipmentInPortEvent.message = message;
emit(shipmentInPortEvent);
}
return getAssetRegistry(NS + '.Shipment')
.then(function (shipmentRegistry) {
// add the temp reading to the shipment
return shipmentRegistry.update(shipment);
});
}
/**
* Initialize some test assets and participants useful for running a demo.
* @param {org.acme.shipping.perishable.SetupDemo} setupDemo - the SetupDemo transaction
* @transaction
*/
function setupDemo(setupDemo) {
var factory = getFactory();
var NS = 'org.acme.shipping.perishable';
// create the grower
var grower = factory.newResource(NS, 'Grower', '[email protected]');
var growerAddress = factory.newConcept(NS, 'Address');
growerAddress.country = 'USA';
grower.address = growerAddress;
grower.accountBalance = 0;
// create the importer
var importer = factory.newResource(NS, 'Importer', '[email protected]');
var importerAddress = factory.newConcept(NS, 'Address');
importerAddress.country = 'UK';
importer.address = importerAddress;
importer.accountBalance = 0;
// create the shipper
var shipper = factory.newResource(NS, 'Shipper', '[email protected]');
var shipperAddress = factory.newConcept(NS, 'Address');
shipperAddress.country = 'Panama';
shipper.address = shipperAddress;
shipper.accountBalance = 0;
// create the contract
var contract = factory.newResource(NS, 'Contract', 'CON_002');
contract.grower = factory.newRelationship(NS, 'Grower', '[email protected]');
contract.importer = factory.newRelationship(NS, 'Importer', '[email protected]');
contract.shipper = factory.newRelationship(NS, 'Shipper', '[email protected]');
var tomorrow = setupDemo.timestamp;
tomorrow.setDate(tomorrow.getDate() + 1);
contract.arrivalDateTime = tomorrow; // the shipment has to arrive tomorrow
contract.unitPrice = 0.5; // pay 50 cents per unit
contract.minTemperature = 2; // min temperature for the cargo
contract.maxTemperature = 10; // max temperature for the cargo
contract.maxAccel = 15000; // max acceleration for the cargo
contract.minPenaltyFactor = 0.2; // we reduce the price by 20 cents for every degree below the min temp
contract.maxPenaltyFactor = 0.1; // we reduce the price by 10 cents for every degree above the max temp
// create the shipment
var shipment = factory.newResource(NS, 'Shipment', '320022000251363131363432');
shipment.type = 'MEDICINE';
shipment.status = 'IN_TRANSIT';
shipment.unitCount = 5000;
shipment.contract = factory.newRelationship(NS, 'Contract', 'CON_002');
return getParticipantRegistry(NS + '.Grower')
.then(function (growerRegistry) {
// add the growers
return growerRegistry.addAll([grower]);
})
.then(function() {
return getParticipantRegistry(NS + '.Importer');
})
.then(function(importerRegistry) {
// add the importers
return importerRegistry.addAll([importer]);
})
.then(function() {
return getParticipantRegistry(NS + '.Shipper');
})
.then(function(shipperRegistry) {
// add the shippers
return shipperRegistry.addAll([shipper]);
})
.then(function() {
return getAssetRegistry(NS + '.Contract');
})
.then(function(contractRegistry) {
// add the contracts
return contractRegistry.addAll([contract]);
})
.then(function() {
return getAssetRegistry(NS + '.Shipment');
})
.then(function(shipmentRegistry) {
// add the shipments
return shipmentRegistry.addAll([shipment]);
});
}