fleetmap-reports 2.0.345 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "2.0.345",
3
+ "version": "2.0.347",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -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
- const spentFuel = calculateConsumption(d, { trips: tripsByDay, stops: stopsByDay, route: routeByDay, summary: summaryByDevice })
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,14 +9,61 @@ 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)
15
63
  if (data.summary && withFuelUsed) {
16
64
  fuelConsumption = calculateSpentFuel(data.summary.spentFuel, d, withFuelUsed)
17
65
  } else if (data.trips && data.stops) {
18
- const spentFuel = data.trips.reduce((a, b) => a + (b.spentFuel > 0 ? b.spentFuel : 0), 0) + data.stops.reduce((a, b) => a + (b.spentFuel > 0 ? b.spentFuel : 0), 0)
19
- fuelConsumption = calculateSpentFuel(spentFuel, d, withFuelUsed)
66
+ fuelConsumption = data.trips.reduce((a, b) => a + calculateSpentFuel(b.spentFuel, d, withFuelUsed), 0) + data.stops.reduce((a, b) => a + calculateSpentFuel(b.spentFuel, d, withFuelUsed), 0)
20
67
  } else if (data.route && data.route.length > 1 && data.route[0].attributes.fuelUsed) {
21
68
  fuelConsumption = data.route[data.route.length - 1].attributes.fuelUsed - data.route[0].attributes.fuelUsed
22
69
  } else if (data.summary) {
@@ -41,7 +88,7 @@ function calculateSpentFuel (value, device, withFuelUsed) {
41
88
  return rawFuel > 0 ? (rawFuel / sensorRange) * fuel_tank_capacity : 0
42
89
  }
43
90
 
44
- return value
91
+ return value > 0 ? value : 0
45
92
  }
46
93
 
47
94
  function useOdooFuelData (d, route) {
@@ -52,3 +99,4 @@ exports.calculateFuelDiff = calculateFuelDiff
52
99
  exports.getRefuelingLiters = getRefuelingLiters
53
100
  exports.calculateConsumption = calculateConsumption
54
101
  exports.useOdooFuelData = useOdooFuelData
102
+ exports.calculateTotalFuelFromSensor = calculateTotalFuelFromSensor