-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
373 lines (316 loc) · 11.7 KB
/
Copy pathserver.js
File metadata and controls
373 lines (316 loc) · 11.7 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const mongoose = require('mongoose');
const Electricity = require('./models/Electricity');
const FuelCombustion = require('./models/FuelCombustion');
const Shipping = require('./models/Shipping');
const Explosion = require('./models/Explosion');
const app = express();
const PORT = 5000;
const Sink=require("./models/Sink")
const ExistingSink=require("./models/ExistingSink")
app.use(cors());
app.use(express.json());
mongoose.connect('mongodb://localhost:27017/carbon-estimation', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));
app.get('/api/electricity-consumption', async (req, res) => {
const { stateName, 'values.EnergyperTime': energyPerTime, 'values.Responsiblearea': responsibleArea, 'values.Totalarea': totalArea } = req.query;
try {
const response = await axios.get('https://api.carbonkit.net/3.6/categories/Electricity_India_By_State/calculation', {
params: {
stateName,
'values.EnergyperTime': energyPerTime,
'values.Responsiblearea': responsibleArea,
'values.Totalarea': totalArea
},
headers: {
'Accept': 'application/json',
'Authorization': 'Basic ' + Buffer.from('sujal6990:sujal9867').toString('base64')
}
});
const result = response.data.output.amounts.reduce((acc, curr) => {
acc[curr.type] = {
value: curr.value,
unit: 'kg/day'
};
return acc;
}, {});
const electricityData = new Electricity({
stateName,
energyPerTime,
responsibleArea,
totalArea,
result
});
await electricityData.save();
res.json({ result });
} catch (error) {
console.error("Error fetching data from CarbonKit API:", error.response ? error.response.data : error.message);
res.status(500).json({ error: "Failed to fetch data" });
}
});
app.get('/api/fuel-combustion', async (req, res) => {
const { fuel, 'values.Volume': Volume } = req.query;
try {
const response = await axios.get('https://api.carbonkit.net/3.6/categories/DEFRA_transport_fuel_methodology/calculation', {
params: {
fuel,
'values.Volume': Volume
},
headers: {
'Accept': 'application/json',
'Authorization': 'Basic ' + Buffer.from('sujal6990:sujal9867').toString('base64')
}
});
const result = response.data.output.amounts.reduce((acc, curr) => {
if (['CO2', 'nitrousOxideCO2e', 'methaneCO2e', 'totalDirectCO2e', 'indirectCO2e', 'lifeCycleCO2e'].includes(curr.type)) {
acc[curr.type] = {
value: curr.value,
unit: curr.unit
};
}
return acc;
}, {});
const fuelCombustionData = new FuelCombustion({
fuel,
quantityFuelConsumed: Volume,
result
});
await fuelCombustionData.save();
res.json({ result });
} catch (error) {
console.error("Error fetching data from CarbonKit API:", error.response ? error.response.data : error.message);
res.status(500).json({ error: "Failed to fetch data" });
}
});
app.post('/api/shipping-emissions', async (req, res) => {
let { weight_unit, weight_value, distance_unit, distance_value, transport_method } = req.body;
weight_value = parseFloat(weight_value);
distance_value = parseFloat(distance_value);
if (!weight_unit || !distance_unit || !transport_method || isNaN(weight_value) || isNaN(distance_value)) {
return res.status(400).json({
error: "Invalid input: Please ensure all fields are filled out correctly."
});
}
try {
const response = await axios.post(
'https://www.carboninterface.com/api/v1/estimates',
{
type: "shipping",
weight_unit,
weight_value,
distance_unit,
distance_value,
transport_method
},
{
headers: {
'Authorization': `Bearer ZguQ1dBUb7qSLQhpZQV3KQ`,
'Content-Type': 'application/json'
}
}
);
const carbonData = response.data.data.attributes;
const result = {
distance: `${carbonData.distance_value} ${carbonData.distance_unit}`,
weight: `${carbonData.weight_value} ${carbonData.weight_unit}`,
carbonEmissions: {
grams: `${carbonData.carbon_g} g`,
kilograms: `${carbonData.carbon_kg} kg`,
metricTonnes: `${carbonData.carbon_mt} mt`
}
};
const shippingData = new Shipping({
weight_unit,
weight_value,
distance_unit,
distance_value,
transport_method,
result
});
await shippingData.save();
res.json(result);
} catch (error) {
console.error("Error fetching data from Carbon Interface API:", error.response ? error.response.data : error.message);
res.status(500).json({ error: "Failed to fetch data" });
}
});
app.post('/api/explosion-emissions', async (req, res) => {
const { explosiveType, amount } = req.body;
const emissionFactors = {
'Black powder': { CO: 85, H2S: 12, CO2: 3100 },
'Smokeless powder': { CO: 38, H2S: 10, CO2: 38 },
'Dynamite, straight': { CO: 141, H2S: 3, CO2: 2320 },
'Dynamite, ammonia': { CO: 32, H2S: 16, CO2: 32 },
'Dynamite, gelatin': { CO: 52, NOx: 26, SO2: 1, CO2: 52 },
'ANFO': { CO: 34, NOx: 8, SO2: 1, CO2: 34 },
'TNT': { CO: 398, NH3: 14, HCN: 13, CO2: 1360 },
'RDX': { CO: 98, NH3: 22, CO2: 1190 },
'PETN': { CO: 149, NH3: 1.3, CO2: 696 }
};
try {
if (!emissionFactors[explosiveType]) {
return res.status(400).json({ error: 'Invalid explosive type' });
}
const factors = emissionFactors[explosiveType];
const emissions = {
CO: (amount * factors.CO / 1000).toFixed(2) + ' kg',
NOx: (amount * (factors.NOx || 0) / 1000).toFixed(2) + ' kg',
NH3: (amount * (factors.NH3 || 0) / 1000).toFixed(2) + ' kg',
HCN: (amount * (factors.HCN || 0) / 1000).toFixed(2) + ' kg',
H2S: (amount * (factors.H2S || 0) / 1000).toFixed(2) + ' kg',
SO2: (amount * (factors.SO2 || 0) / 1000).toFixed(2) + ' kg',
CO2: (amount * factors.CO2 / 1000).toFixed(2) + ' kg'
};
const explosionData = new Explosion({
explosiveType,
amount,
emissions
});
await explosionData.save();
res.json({ emissions, explosiveType, amount });
} catch (error) {
console.error("Error calculating emissions:", error.message);
res.status(500).json({ error: "Failed to calculate emissions" });
}
});
app.post('/api/sinks', async (req, res) => {
try {
const {
name,
vegetationType,
areaCovered,
carbonSequestrationRate,
location,
additionalDetails,
timeframe,
} = req.body;
const dailySequestrationRate = carbonSequestrationRate / 365; // CSR per day
// Calculate total carbon sequestration based on the provided timeframe
const totalSequestration = areaCovered * carbonSequestrationRate * (timeframe || 1); // Assuming timeframe is in years, default to 1 year if not provided
// Create and save the new sink
const newSink = new Sink({
name,
vegetationType,
areaCovered,
carbonSequestrationRate,
dailySequestrationRate, // Save daily sequestration rate
location,
additionalDetails,
});
await newSink.save();
// Respond with the calculated sequestration and saved sink data
res.status(201).json({
message: 'Carbon sink created successfully',
data: {
sink: newSink,
dailySequestrationRate: `${dailySequestrationRate.toFixed(2)} tons of CO2 per hectare per day`,
totalSequestration: `${totalSequestration.toFixed(2)} tons of CO2 over ${timeframe || 1} year(s)`,
},
});
} catch (error) {
console.error('Error saving carbon sink:', error);
res.status(500).json({ message: 'Server error', error });
}
});
app.post('/api/existing-sinks', async (req, res) => {
try {
const {
name,
vegetationType,
areaCovered,
carbonSequestrationRate,
location,
additionalDetails,
timeframe,
} = req.body;
// Calculate total carbon sequestration per day
const dailySequestrationRate = carbonSequestrationRate / 365; // CSR per day
// Calculate total carbon sequestration based on the provided timeframe
const totalSequestration = areaCovered * carbonSequestrationRate * (timeframe || 1); // Assuming timeframe is in years, default to 1 year if not provided
// Create and save the new existing sink
const newExistingSink = new ExistingSink({
name,
vegetationType,
areaCovered,
carbonSequestrationRate,
dailySequestrationRate, // Save daily sequestration rate
location,
additionalDetails,
});
await newExistingSink.save();
// Respond with the calculated sequestration and saved sink data
res.status(201).json({
message: 'Existing carbon sink created successfully',
data: {
sink: newExistingSink,
dailySequestrationRate: `${dailySequestrationRate.toFixed(2)} tons of CO2 per hectare per day`,
totalSequestration: `${totalSequestration.toFixed(2)} tons of CO2 over ${timeframe || 1} year(s)`,
},
});
} catch (error) {
console.error('Error saving existing carbon sink:', error);
res.status(500).json({ message: 'Server error', error });
}
});
app.get('/api/daily-sequestration', async (req, res) => {
try {
// Fetch all sinks and existing sinks from the database
const [sinks, existingSinks] = await Promise.all([
Sink.find(),
ExistingSink.find()
]);
// Initialize total daily sequestration
let totalDailySequestration = 0;
// Calculate total daily carbon sequestration for new sinks
sinks.forEach(sink => {
const areaCovered = Number(sink.areaCovered) || 0; // Default to 0 if undefined or NaN
const dailySequestrationRate = Number(sink.dailySequestrationRate) || 0; // Default to 0 if undefined or NaN
// Add to total daily sequestration
totalDailySequestration += areaCovered * dailySequestrationRate;
});
// Calculate total daily carbon sequestration for existing sinks
existingSinks.forEach(existingSink => {
const areaCovered = Number(existingSink.areaCovered) || 0; // Default to 0 if undefined or NaN
const dailySequestrationRate = Number(existingSink.dailySequestrationRate) || 0; // Default to 0 if undefined or NaN
// Add to total daily sequestration
totalDailySequestration += areaCovered * dailySequestrationRate;
});
// Respond with the total daily sequestration value
res.status(200).json({
message: 'Total daily carbon sequestration calculated successfully',
totalDailySequestration: `${totalDailySequestration} tons of CO2 per day`
});
} catch (error) {
console.error('Error calculating daily carbon sequestration:', error);
res.status(500).json({ message: 'Server error', error });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
app.get('/api/aggregate-data', async (req, res) => {
try {
const electricityData = await Electricity.find();
const fuelData = await FuelCombustion.find();
const shippingData = await Shipping.find();
const explosionData = await Explosion.find();
if (!electricityData || !fuelData || !shippingData || !explosionData) {
throw new Error('One of the collections returned no data.');
}
res.json({
electricity: electricityData,
fuel: fuelData,
shipping: shippingData,
explosion: explosionData,
});
} catch (error) {
console.error('Error fetching aggregated data:', error.message);
res.status(500).json({ error: 'Failed to fetch aggregated data', details: error.message });
}
});