Skip to content

Commit

Permalink
Merge pull request #437 from opentaps/main
Browse files Browse the repository at this point in the history
Calculate estimated emissions -- placeholder
  • Loading branch information
sichen1234 committed Feb 10, 2022
2 parents 875f30f + 23fbd7e commit 634e9e3
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions supply-chain/ups.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,30 @@ function calc_distance(o, d) {
const c = 2 * Math.asin(Math.sqrt(a));

// Radius of earth
const use_miles = conf.imperial;
const r = (use_miles ? 3958.8 : 6371) * c;
return r;
return 6371.0 * c;
}

ups.track(trackingNumber, {latest: false}, (err, res) => {
if (err) console.error('An error occurred: ', err);
else {
const isGround = is_ground(res);
const output = { ups: res };
let weight = 0.0;
if (res.Shipment && res.Shipment.ShipmentWeight) {
const w = res.Shipment.ShipmentWeight;
weight = w.Weight;
if (w.UnitOfMeasurement.Code === 'LBS') {
weight *= 0.453592;
}
output.weight = {
value: w.Weight,
unit: w.UnitOfMeasurement.Code
value: weight,
unit: 'kg'
}
let emissions = weight * 0.001 * (isGround ? 0.52218 : 2.37968);
output.emissions = { value: emissions, unit: 'kgCO2e' }
}
const addresses = get_addresses(res);
const isGround = is_ground(res);
if (addresses) {
console.log('Calculating distances : ', addresses);
const client = new Client({});
const address_o = addresses.origin.join(' ');
const address_d = addresses.dest.join(' ');
Expand All @@ -117,8 +121,8 @@ ups.track(trackingNumber, {latest: false}, (err, res) => {
destination: {
address: address_d,
},
value: (conf.imperial ? (dist_m * 0.621371) : dist_m),
unit: (conf.imperial ? 'mi' : 'km')
value: dist_m,
unit: 'km'
};
console.log(JSON.stringify(output, null, 4));
}).catch((err)=>{
Expand Down Expand Up @@ -150,7 +154,7 @@ ups.track(trackingNumber, {latest: false}, (err, res) => {
coords: dest_r
},
value: calc_distance(origin_r, dest_r),
unit: (conf.imperial ? 'mi' : 'km')
unit: 'km'
};
console.log(JSON.stringify(output, null, 4));
}).catch((err)=>{
Expand Down

0 comments on commit 634e9e3

Please sign in to comment.