fleetmap-reports 2.0.346 → 2.0.348

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.346",
3
+ "version": "2.0.348",
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,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
package/src/util/utils.js CHANGED
@@ -113,11 +113,13 @@ function convertToFeature (geofence) {
113
113
  function getDates (startDate, endDate, timezone) {
114
114
  const dates = []
115
115
  let currentDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), 12, 0, 0, 0)
116
- const _endDate = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(), 23, 59, 59, 0)
116
+ let _endDate = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(), 23, 59, 59, 0)
117
117
 
118
118
  if (!isClientSide() && timezone) {
119
119
  const startDateUserTimezone = convertFromUTC(startDate, timezone)
120
120
  currentDate = new Date(startDateUserTimezone.getFullYear(), startDateUserTimezone.getMonth(), startDateUserTimezone.getDate(), 12, 0, 0, 0)
121
+ const endDateUserTimezone = convertFromUTC(endDate, timezone)
122
+ _endDate = new Date(endDateUserTimezone.getFullYear(), endDateUserTimezone.getMonth(), endDateUserTimezone.getDate(), 23, 59, 59, 0)
121
123
  }
122
124
  const addDays = function (days) {
123
125
  const date = new Date(this.valueOf())
@@ -193,12 +195,12 @@ function convertToLocaleTimeString (value, lang, timezone, user) {
193
195
  }
194
196
  }
195
197
 
196
- function getTimezoneOffset (timezone) {
198
+ function getTimezoneOffset (timezone, date = new Date()) {
197
199
  if (isClientSide()) {
198
- return new Date().getTimezoneOffset()
200
+ return date.getTimezoneOffset()
199
201
  } else {
200
- const utcDate = new Date(new Date().toLocaleString('en-US', { timeZone: 'UTC' }))
201
- const tzDate = new Date(new Date().toLocaleString('en-US', { timeZone: timezone }))
202
+ const utcDate = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' }))
203
+ const tzDate = new Date(date.toLocaleString('en-US', { timeZone: timezone }))
202
204
  return (utcDate.getTime() - tzDate.getTime()) / 6e4
203
205
  }
204
206
  }
@@ -208,16 +210,15 @@ function convertFromUTC (value, timezone) {
208
210
  return new Date(value)
209
211
  }
210
212
 
211
- const offset = getTimezoneOffset(timezone)
212
213
  const valueDate = new Date(value)
214
+ const offset = getTimezoneOffset(timezone, valueDate)
213
215
  valueDate.setTime(valueDate.getTime() - (offset * 60 * 1000))
214
216
  return valueDate
215
217
  }
216
218
 
217
219
  exports.convertFromLocal = (value, timezone) => {
218
- const offset = getTimezoneOffset(timezone)
219
-
220
220
  const valueDate = new Date(value)
221
+ const offset = getTimezoneOffset(timezone, valueDate)
221
222
  valueDate.setTime(valueDate.getTime() + (offset * 60 * 1000))
222
223
  return valueDate
223
224
  }