fleetmap-reports 2.0.346 → 2.0.347
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/fuel-consumption-report.js +10 -2
- package/src/util/fuel.js +49 -0
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@ const { getStyle } = require('./reportStyle')
|
|
|
7
7
|
const { getUserPartner } = require('fleetmap-partners')
|
|
8
8
|
const { addTable, headerFromUser } = require('./util/pdfDocument')
|
|
9
9
|
const jsPDF = require('jspdf')
|
|
10
|
-
const { calculateConsumption } = require('./util/fuel')
|
|
10
|
+
const { calculateConsumption, calculateTotalFuelFromSensor} = require('./util/fuel')
|
|
11
11
|
const { getSummaryByDay } = require('./util/traccar')
|
|
12
12
|
const { getDataByDay } = require('./util/trips')
|
|
13
13
|
|
|
@@ -53,8 +53,16 @@ async function createFuelConsumptionReport (from, to, userData, traccar) {
|
|
|
53
53
|
const summaryByDay = summary.find(a => a.date === date).summary
|
|
54
54
|
const summaryByDevice = summaryByDay.find(a => a.deviceId === d.id)
|
|
55
55
|
|
|
56
|
+
const firstStopIndex = !stopsByDay.length || !tripsByDay.length ? 0 : (new Date(tripsByDay[0].startTime) < new Date(stopsByDay[0].startTime) ? 0 : 1)
|
|
56
57
|
const distance = summaryByDevice.distance
|
|
57
|
-
|
|
58
|
+
|
|
59
|
+
const hasFuelSensor = 'fuel_low_threshold' in d.attributes &&
|
|
60
|
+
'fuel_high_threshold' in d.attributes &&
|
|
61
|
+
d.attributes.fuel_tank_capacity
|
|
62
|
+
|
|
63
|
+
const spentFuel = hasFuelSensor ?
|
|
64
|
+
tripsByDay.length > 0 ? calculateTotalFuelFromSensor(tripsByDay, stopsByDay, route, d, firstStopIndex) : 0
|
|
65
|
+
: calculateConsumption(d, { trips: tripsByDay, stops: stopsByDay, route: routeByDay, summary: summaryByDevice })
|
|
58
66
|
|
|
59
67
|
const dataRow = {
|
|
60
68
|
date,
|
package/src/util/fuel.js
CHANGED
|
@@ -9,6 +9,54 @@ function getRefuelingLiters (d, position, diff) {
|
|
|
9
9
|
return Math.round(diff * d.attributes.fuel_tank_capacity / 100)
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
function calculateTotalFuelFromSensor(trips, stops, positions, device, firstStopIndex) {
|
|
13
|
+
const lastStopIndex = firstStopIndex + trips.length - 1
|
|
14
|
+
const lastStop = stops.length > lastStopIndex ? stops[lastStopIndex] : null
|
|
15
|
+
const firstTripStart = new Date(trips[0].startTime).getTime()
|
|
16
|
+
const lastEndTime = lastStop ? new Date(lastStop.endTime).getTime() : new Date(trips[trips.length - 1].endTime).getTime()
|
|
17
|
+
|
|
18
|
+
const rangePositions = positions.filter(p => {
|
|
19
|
+
const t = new Date(p.fixTime).getTime()
|
|
20
|
+
return t >= firstTripStart && t <= lastEndTime && p.attributes.fuel != null
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
if (rangePositions.length < 2) return 0
|
|
24
|
+
|
|
25
|
+
const invertedSensor = device.attributes.fuel_low_threshold > device.attributes.fuel_high_threshold
|
|
26
|
+
const sensorRange = Math.abs(device.attributes.fuel_high_threshold - device.attributes.fuel_low_threshold)
|
|
27
|
+
const refuelThreshold = sensorRange * 0.2
|
|
28
|
+
|
|
29
|
+
let totalRawConsumption = 0
|
|
30
|
+
let segmentStartFuel = rangePositions[0].attributes.fuel
|
|
31
|
+
|
|
32
|
+
for (let i = 1; i < rangePositions.length; i++) {
|
|
33
|
+
const prev = rangePositions[i - 1]
|
|
34
|
+
const curr = rangePositions[i]
|
|
35
|
+
// refuel = significant increase for normal sensor, significant decrease for inverted
|
|
36
|
+
const fuelChange = invertedSensor
|
|
37
|
+
? prev.attributes.fuel - curr.attributes.fuel
|
|
38
|
+
: curr.attributes.fuel - prev.attributes.fuel
|
|
39
|
+
|
|
40
|
+
if (fuelChange > refuelThreshold) {
|
|
41
|
+
const segmentRawDiff = invertedSensor
|
|
42
|
+
? prev.attributes.fuel - segmentStartFuel
|
|
43
|
+
: segmentStartFuel - prev.attributes.fuel
|
|
44
|
+
if (segmentRawDiff > 0) totalRawConsumption += segmentRawDiff
|
|
45
|
+
segmentStartFuel = curr.attributes.fuel
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const lastFuel = rangePositions[rangePositions.length - 1].attributes.fuel
|
|
50
|
+
const lastSegmentRawDiff = invertedSensor
|
|
51
|
+
? lastFuel - segmentStartFuel
|
|
52
|
+
: segmentStartFuel - lastFuel
|
|
53
|
+
if (lastSegmentRawDiff > 0) totalRawConsumption += lastSegmentRawDiff
|
|
54
|
+
|
|
55
|
+
if (totalRawConsumption <= 0) return 0
|
|
56
|
+
return (totalRawConsumption / sensorRange) * device.attributes.fuel_tank_capacity
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
12
60
|
function calculateConsumption (d, data) {
|
|
13
61
|
let fuelConsumption = 0
|
|
14
62
|
const withFuelUsed = data.route && data.route.find(p => p.attributes.fuelUsed)
|
|
@@ -51,3 +99,4 @@ exports.calculateFuelDiff = calculateFuelDiff
|
|
|
51
99
|
exports.getRefuelingLiters = getRefuelingLiters
|
|
52
100
|
exports.calculateConsumption = calculateConsumption
|
|
53
101
|
exports.useOdooFuelData = useOdooFuelData
|
|
102
|
+
exports.calculateTotalFuelFromSensor = calculateTotalFuelFromSensor
|