fleetmap-reports 1.0.485 → 1.0.486

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.486",
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']
@@ -119,7 +120,9 @@ async function getEvents (traccarInstance, from, to, devices, userData, deviceCo
119
120
 
120
121
  const events = []
121
122
  if (userData.roadSpeedLimits) {
122
- events.push(...await getHereEvents(devices, routes, userData.maxSpeedThreshold))
123
+ const hereResults = await getHereEvents(devices, routes, userData.maxSpeedThreshold)
124
+ console.log('got', hereResults.length, 'from here')
125
+ events.push(...hereResults)
123
126
  } else if (!userData.useVehicleSpeedLimit && userData.customSpeed) {
124
127
  events.push(...await getCustomSpeedLimitEvents(devices, routes, userData.customSpeed))
125
128
  } else {
@@ -281,47 +284,57 @@ async function getCustomSpeedLimitEvents (devices, routes, customSpeed) {
281
284
  }
282
285
 
283
286
  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)
287
+ try {
288
+ console.log('env', process.env.PROCESS_HERE_EVENTS)
289
+ if (process.env.PROCESS_HERE_EVENTS && process.env.PROCESS_HERE_EVENTS === 'server') {
290
+ const url = `${process.env.REPORTS_SERVER}/reports/here`
291
+ return axios.post(url, { devices, routes, threshold }).then(d => d.data)
292
+ } else {
293
+ console.log('here speed limit events')
294
+ const events = []
295
+ const promises = devices.map(d => new Promise((resolve) => {
296
+ const positions = routes.filter(p => p.deviceId === d.id)
297
+ if (!positions.length) {
298
+ console.log('no positions on device', d.name)
299
+ resolve()
309
300
  }
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
301
+ here.routeMatch(positions).then(results => {
302
+ const reduced = results.reduce((acc, cur, idx, src) => {
303
+ cur.overSpeeding = cur.currentSpeedKmh > parseInt(cur.speedLimit) + (threshold || 0)
304
+ const last = acc.length && acc.slice(-1)[0]
305
+ cur.position = positions.find(p => new Date(p.fixTime).getTime() === cur.timestamp)
306
+ if (cur.overSpeeding && last && src[idx - 1].overSpeeding && last.speedLimit === cur.speedLimit) {
307
+ last.eventTime = cur.timestamp - last.timestamp
308
+ last.attributes.maxSpeed = Math.max(last.attributes.maxSpeed, cur.position.speed)
309
+ last.distance = distance.default(point([cur.position.longitude, cur.position.latitude]), point([last.position.longitude, last.position.latitude]))
310
+ } else if (cur.overSpeeding) {
311
+ cur.positionId = cur.position && cur.position.id
312
+ cur.roadSpeedLimit = cur.speedLimit
313
+ cur.deviceId = d.id
314
+ cur.attributes = { speedLimit: cur.speedLimit, speed: cur.currentSpeedKmh / 1.85200, maxSpeed: cur.position.speed }
315
+ cur.eventTime = 0
316
+ cur.distance = 0
317
+ acc.push(cur)
318
+ }
319
+ return acc
320
+ }, [])
321
+ if (!reduced.length) {
322
+ console.log('empty array after filter on device', d.name)
323
+ resolve()
324
+ }
325
+ events.push(...reduced)
326
+ resolve()
327
+ }).catch(e => {
328
+ console.error('error on route match, moving on', e)
329
+ resolve()
330
+ })
331
+ }))
332
+ await Promise.all(promises)
333
+ return events
334
+ }
335
+ } catch (e) {
336
+ console.error(e)
337
+ }
325
338
  }
326
339
 
327
340
  function calculateEventData (positions, pIndex, alert, device, geofence, userData) {