fleetmap-reports 1.0.485 → 1.0.487

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.485",
3
+ "version": "1.0.487",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.test.js CHANGED
@@ -23,7 +23,7 @@ describe('Test_Reports', function () {
23
23
  const userData = await report.getUserData()
24
24
  userData.roadSpeedLimits = true
25
25
  const { device, totalDistance, totalEventTime } = await getSpeedingReport(report, userData)
26
- assert.equal(device.alerts.length, 11) // Total Alerts
26
+ assert.equal(device.alerts.length, 16) // Total Alerts
27
27
  console.log(device.alerts)
28
28
  assert.equal(totalDistance, 74.71054403083238) // Total Kms
29
29
  assert.equal(totalEventTime, 3009000) // Total Duration
@@ -13,6 +13,7 @@ const distance = require('@turf/distance')
13
13
  const { insideGeofence } = require('./util/geofence')
14
14
  const booleanPointInPolygon = require('@turf/boolean-point-in-polygon')
15
15
  const automaticReports = require('./automaticReports')
16
+ const { default: axios } = require('axios')
16
17
 
17
18
  const fileName = 'SpeedingReport'
18
19
  const eventTypes = ['deviceOverspeed']
@@ -110,28 +111,35 @@ async function createSpeedingReportByDriver (from, to, userData, traccarInstance
110
111
  }
111
112
 
112
113
  async function getEvents (traccarInstance, from, to, devices, userData, deviceCount, totalDevices, sliceSize) {
113
- const geofencesFeatures = userData.geofences.filter(g => g.area.startsWith('POLYGON') &&
114
+ if (process.env.PROCESS_HERE_EVENTS && process.env.PROCESS_HERE_EVENTS === 'server') {
115
+ const url = `${process.env.REPORTS_SERVER}/reports/speeding-report/getEvents`
116
+ return axios.post(url, { from, to, devices, userData, deviceCount, totalDevices, sliceSize }).then(d => d.data)
117
+ } else {
118
+ const geofencesFeatures = userData.geofences.filter(g => g.area.startsWith('POLYGON') &&
114
119
  g.attributes.speedLimit).map(g => convertToFeature(g))
115
120
 
116
- const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false,
117
- false, false, deviceCount, totalDevices, sliceSize)
118
- const routes = allInOne.route
119
-
120
- const events = []
121
- if (userData.roadSpeedLimits) {
122
- events.push(...await getHereEvents(devices, routes, userData.maxSpeedThreshold))
123
- } else if (!userData.useVehicleSpeedLimit && userData.customSpeed) {
124
- events.push(...await getCustomSpeedLimitEvents(devices, routes, userData.customSpeed))
125
- } else {
126
- const traccarEvents = await traccarHelper.getEvents(traccarInstance, from, to, devices, eventTypes)
127
- if (geofencesFeatures.length) {
128
- events.push(...getGeofenceSpeedLimitEvents(geofencesFeatures, routes, devices))
129
- events.push(...traccarEvents.filter(e => !(e.geofenceId)))
121
+ const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false,
122
+ false, false, deviceCount, totalDevices, sliceSize)
123
+ const routes = allInOne.route
124
+
125
+ const events = []
126
+ if (userData.roadSpeedLimits) {
127
+ const hereResults = await getHereEvents(devices, routes, userData.maxSpeedThreshold)
128
+ console.log('got', hereResults.length, 'from here')
129
+ events.push(...hereResults)
130
+ } else if (!userData.useVehicleSpeedLimit && userData.customSpeed) {
131
+ events.push(...await getCustomSpeedLimitEvents(devices, routes, userData.customSpeed))
130
132
  } else {
131
- events.push(...traccarEvents)
133
+ const traccarEvents = await traccarHelper.getEvents(traccarInstance, from, to, devices, eventTypes)
134
+ if (geofencesFeatures.length) {
135
+ events.push(...getGeofenceSpeedLimitEvents(geofencesFeatures, routes, devices))
136
+ events.push(...traccarEvents.filter(e => !(e.geofenceId)))
137
+ } else {
138
+ events.push(...traccarEvents)
139
+ }
132
140
  }
141
+ return { routes, events }
133
142
  }
134
- return { routes, events }
135
143
  }
136
144
 
137
145
  function getGeofenceSpeedLimitEvents (geofencesFeatures, routes, devices) {
@@ -281,47 +289,57 @@ async function getCustomSpeedLimitEvents (devices, routes, customSpeed) {
281
289
  }
282
290
 
283
291
  async function getHereEvents (devices, routes, threshold) {
284
- console.log('here speed limit events')
285
- const events = []
286
- const promises = devices.map(d => new Promise((resolve) => {
287
- const positions = routes.filter(p => p.deviceId === d.id)
288
- if (!positions.length) {
289
- console.log('no positions on device', d.name)
290
- resolve()
291
- }
292
- here.routeMatch(positions).then(results => {
293
- const reduced = results.reduce((acc, cur, idx, src) => {
294
- cur.overSpeeding = cur.currentSpeedKmh > parseInt(cur.speedLimit) + (threshold || 0)
295
- const last = acc.length && acc.slice(-1)[0]
296
- cur.position = positions.find(p => new Date(p.fixTime).getTime() === cur.timestamp)
297
- if (cur.overSpeeding && last && src[idx - 1].overSpeeding && last.speedLimit === cur.speedLimit) {
298
- last.eventTime = cur.timestamp - last.timestamp
299
- last.attributes.maxSpeed = Math.max(last.attributes.maxSpeed, cur.position.speed)
300
- last.distance = distance.default(point([cur.position.longitude, cur.position.latitude]), point([last.position.longitude, last.position.latitude]))
301
- } else if (cur.overSpeeding) {
302
- cur.positionId = cur.position && cur.position.id
303
- cur.roadSpeedLimit = cur.speedLimit
304
- cur.deviceId = d.id
305
- cur.attributes = { speedLimit: cur.speedLimit, speed: cur.currentSpeedKmh / 1.85200, maxSpeed: cur.position.speed }
306
- cur.eventTime = 0
307
- cur.distance = 0
308
- acc.push(cur)
292
+ try {
293
+ console.log('env', process.env.PROCESS_HERE_EVENTS)
294
+ if (process.env.PROCESS_HERE_EVENTS && process.env.PROCESS_HERE_EVENTS === 'server') {
295
+ const url = `${process.env.REPORTS_SERVER}/reports/here`
296
+ return axios.post(url, { devices, routes, threshold }).then(d => d.data)
297
+ } else {
298
+ console.log('here speed limit events')
299
+ const events = []
300
+ const promises = devices.map(d => new Promise((resolve) => {
301
+ const positions = routes.filter(p => p.deviceId === d.id)
302
+ if (!positions.length) {
303
+ console.log('no positions on device', d.name)
304
+ resolve()
309
305
  }
310
- return acc
311
- }, [])
312
- if (!reduced.length) {
313
- console.log('empty array after filter on device', d.name)
314
- resolve()
315
- }
316
- events.push(...reduced)
317
- resolve()
318
- }).catch(e => {
319
- console.error(e)
320
- resolve()
321
- })
322
- }))
323
- await Promise.all(promises)
324
- return events
306
+ here.routeMatch(positions).then(results => {
307
+ const reduced = results.reduce((acc, cur, idx, src) => {
308
+ cur.overSpeeding = cur.currentSpeedKmh > parseInt(cur.speedLimit) + (threshold || 0)
309
+ const last = acc.length && acc.slice(-1)[0]
310
+ cur.position = positions.find(p => new Date(p.fixTime).getTime() === cur.timestamp)
311
+ if (cur.overSpeeding && last && src[idx - 1].overSpeeding && last.speedLimit === cur.speedLimit) {
312
+ last.eventTime = cur.timestamp - last.timestamp
313
+ last.attributes.maxSpeed = Math.max(last.attributes.maxSpeed, cur.position.speed)
314
+ last.distance = distance.default(point([cur.position.longitude, cur.position.latitude]), point([last.position.longitude, last.position.latitude]))
315
+ } else if (cur.overSpeeding) {
316
+ cur.positionId = cur.position && cur.position.id
317
+ cur.roadSpeedLimit = cur.speedLimit
318
+ cur.deviceId = d.id
319
+ cur.attributes = { speedLimit: cur.speedLimit, speed: cur.currentSpeedKmh / 1.85200, maxSpeed: cur.position.speed }
320
+ cur.eventTime = 0
321
+ cur.distance = 0
322
+ acc.push(cur)
323
+ }
324
+ return acc
325
+ }, [])
326
+ if (!reduced.length) {
327
+ console.log('empty array after filter on device', d.name)
328
+ resolve()
329
+ }
330
+ events.push(...reduced)
331
+ resolve()
332
+ }).catch(e => {
333
+ console.error('error on route match, moving on', e)
334
+ resolve()
335
+ })
336
+ }))
337
+ await Promise.all(promises)
338
+ return events
339
+ }
340
+ } catch (e) {
341
+ console.error(e)
342
+ }
325
343
  }
326
344
 
327
345
  function calculateEventData (positions, pIndex, alert, device, geofence, userData) {
@@ -544,3 +562,4 @@ function getMaxSpeed (data) {
544
562
  exports.createSpeedingReport = createSpeedingReport
545
563
  exports.exportSpeedingReportToPDF = exportSpeedingReportToPDF
546
564
  exports.exportSpeedingReportToExcel = exportSpeedingReportToExcel
565
+ exports.getEvents = getEvents