fleetmap-reports 1.0.776 → 1.0.778

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": "1.0.776",
3
+ "version": "1.0.778",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -2,6 +2,7 @@ const automaticReports = require('../automaticReports')
2
2
  const traccarHelper = require('../util/traccar')
3
3
  const { getUncompletedTrip } = require('../util/trips')
4
4
  const { getEndTripMessages, getCanDriverStyleMessages, parseEndTripMessage, parseCanDriverStyleMessage } = require('../util/xpert')
5
+ const { calculateDistance } = require('../util/utils')
5
6
 
6
7
  async function createPerformanceReport (from, to, userData, traccar) {
7
8
  const reportData = []
@@ -12,7 +13,7 @@ async function createPerformanceReport (from, to, userData, traccar) {
12
13
  const sliced = automaticReports.sliceArray(devices, 5)
13
14
  let deviceCount = 0
14
15
  for (const slice of sliced) {
15
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, false, false, deviceCount, devices.length)
16
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, true, true, deviceCount, devices.length)
16
17
 
17
18
  slice.forEach(d => {
18
19
  const deviceData = getDeviceData(allInOne, d)
@@ -23,7 +24,7 @@ async function createPerformanceReport (from, to, userData, traccar) {
23
24
  deviceCount = deviceCount + slice.length
24
25
  }
25
26
  } else {
26
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, userData.devices, true, true, false, true)
27
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, userData.devices, true, true, true, true)
27
28
 
28
29
  const device = userData.devices[0]
29
30
 
@@ -125,31 +126,48 @@ function getDeviceData (allInOne, d) {
125
126
  }
126
127
  } else {
127
128
  const deviceSummary = allInOne.summary.filter(t => t.deviceId === d.id)[0]
129
+ const deviceStops = allInOne.stops.filter(t => t.deviceId === d.id)
128
130
 
129
131
  const drivingTime = deviceTrips.reduce((a, b) => a + b.duration, 0)
130
132
  const drivingConsumption = deviceTrips.reduce((a, b) => a + b.spentFuel, 0)
131
133
  const drivingDistance = deviceSummary ? deviceSummary.distance / 1000 : 0
132
- const idleTime = deviceSummary && deviceSummary.engineHours > drivingTime ? deviceSummary.engineHours - drivingTime : 0
134
+ const idleTime = deviceTrips.reduce((a, b) => a + b.idleTime, 0) + deviceStops.reduce((a, b) => a + b.engineHours, 0)
133
135
  const idleConsumption = deviceSummary && deviceSummary.spentFuel > drivingConsumption ? deviceSummary.spentFuel - drivingConsumption : 0
134
136
 
135
- const hardBreaking = deviceRoute.filter(p => p.attributes.io253 === 2).length
136
- const hardAcceleration = deviceRoute.filter(p => p.attributes.io253 === 1).length
137
+ const highEngineRPMSections = calculateRPMSections(deviceRoute, d.attributes.highRPM || 1300)
138
+ const highEngineRPM = highEngineRPMSections.map(a => a.length > 1
139
+ ? a.map((p, index) => index === 0 ? 0 : new Date(p.fixTime).getTime() - new Date(a[index - 1].fixTime).getTime()).reduce((a, b) => a + b)
140
+ : 0).reduce((a, b) => a + b, 0)
141
+
142
+ const economicRPMSections = calculateRPMSections(deviceRoute, d.attributes.minEconomicRPM || 1000, d.attributes.maxEconomicRPM || 1250)
143
+ const economicTime = economicRPMSections.map(a => a.length > 1
144
+ ? a.map((p, index) => index === 0 ? 0 : new Date(p.fixTime).getTime() - new Date(a[index - 1].fixTime).getTime()).reduce((a, b) => a + b)
145
+ : 0).reduce((a, b) => a + b, 0)
146
+ const economicDistance = economicRPMSections.map(a => a.length > 1
147
+ ? calculateDistance(a)
148
+ : 0).reduce((a, b) => a + b, 0)
149
+ const economicConsumption = economicRPMSections.map(a => a.length > 1
150
+ ? a[a.length - 1].attributes.fuelUsed - a[0].attributes.fuelUsed
151
+ : 0).reduce((a, b) => a + (b || 0), 0)
152
+
153
+ const hardBreaking = deviceRoute.filter(p => p.attributes.alarm === 'hardBreaking').length
154
+ const hardAcceleration = deviceRoute.filter(p => p.attributes.alarm === 'hardAcceleration').length
137
155
 
138
156
  return {
139
157
  drivingTime: drivingTime / 1000,
140
158
  idleTime: idleTime / 1000,
141
159
  cruiseControlTime: 0,
142
- economicTime: 0,
143
- highEngineRPM: 0,
160
+ economicTime: economicTime / 1000,
161
+ highEngineRPM: highEngineRPM / 1000,
144
162
  hardBreaking,
145
163
  hardAcceleration,
146
164
  drivingConsumption,
147
165
  idleConsumption,
148
166
  cruiseControlConsumption: 0,
149
- economicConsumption: 0,
167
+ economicConsumption,
150
168
  drivingDistance,
151
169
  cruiseControlDistance: 0,
152
- economicDistance: 0
170
+ economicDistance
153
171
  }
154
172
  }
155
173
  }
@@ -169,4 +187,24 @@ function getRowData (index, device, description, time, consumption, distance, in
169
187
  }
170
188
  }
171
189
 
190
+ function calculateRPMSections (route, min, max) {
191
+ const sections = []
192
+ let newSection = true
193
+ route.forEach(p => {
194
+ const rpm = p.attributes.rpm
195
+ if ((!min || rpm > min) && (!max || rpm < max)) {
196
+ if (newSection) {
197
+ sections.push([p])
198
+ } else {
199
+ sections[sections.length - 1].push(p)
200
+ }
201
+ newSection = false
202
+ } else {
203
+ newSection = true
204
+ }
205
+ })
206
+
207
+ return sections
208
+ }
209
+
172
210
  exports.createPerformanceReport = createPerformanceReport
@@ -10,13 +10,14 @@ describe('performance', function () {
10
10
  const userData = await report.getUserData()
11
11
  userData.devices = userData.devices.filter(d => d.id === 22326)
12
12
  const data = await createPerformanceReport(
13
- new Date(Date.UTC(2023, 5, 10, 0, 0, 0, 0)),
14
- new Date(Date.UTC(2023, 5, 20, 23, 59, 59, 0)),
13
+ new Date(Date.UTC(2023, 6, 1, 0, 0, 0, 0)),
14
+ new Date(Date.UTC(2023, 6, 17, 23, 59, 59, 0)),
15
15
  userData,
16
16
  report.traccar)
17
- assert.equal(data[0].consumption, 21.8)
18
- assert.equal(data[0].distance, 381.29861000000005)
19
- assert.equal(convertMS(data[1].time * 1000, false), '10:30')
17
+ console.log(data)
18
+ assert.equal(data[0].consumption, 19.799999999999997)
19
+ assert.equal(data[0].distance, 326.7657)
20
+ assert.equal(convertMS(data[1].time * 1000, false), '08:19')
20
21
  }, 8000000)
21
22
 
22
23
  // eslint-disable-next-line no-undef