fleetmap-reports 1.0.675 → 1.0.677

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/zone-report.js +39 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.675",
3
+ "version": "1.0.677",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -18,7 +18,7 @@ const distance = require('@turf/distance')
18
18
  const axios = require('axios')
19
19
  const { processServerSide } = require('./util')
20
20
  const { filterPositions } = require('./util/route')
21
- const sliceSize = 10
21
+ const sliceSize = 1
22
22
  const deviceChunk = 1
23
23
  const fileName = 'ZoneReport'
24
24
 
@@ -71,9 +71,8 @@ async function createZoneReport (from, to, userData, traccar) {
71
71
  const route = filterPositions(data.route)
72
72
  const alerts = getInAndOutEvents(slice, route, userData)
73
73
 
74
- if (alerts.length) {
75
- allData.devices.push(...await processDevices(from, to, devices, userData, { alerts, route }))
76
- }
74
+ allData.devices.push(...await processDevices(from, to, devices, userData, { alerts, route }))
75
+
77
76
  deviceCount = deviceCount + slice.length
78
77
  }
79
78
 
@@ -97,11 +96,11 @@ async function processDevices (from, to, devices, userData, data) {
97
96
  const alerts = data.alerts.filter(t => t.deviceId === d.id)
98
97
  const deviceRoute = data.route.filter(p => p.deviceId === d.id)
99
98
 
100
- if (alerts.length > 0) {
101
- const zoneInOutData = userData.groupByDay
102
- ? analyseAlertsGroupGeofences(alerts, deviceRoute, from, to)
103
- : analyseAlerts(alerts, deviceRoute, userData)
99
+ const zoneInOutData = userData.groupByDay
100
+ ? analyseAlertsGroupGeofences(alerts, deviceRoute, from, to)
101
+ : analyseAlerts(alerts, deviceRoute, userData)
104
102
 
103
+ if (zoneInOutData.length > 0) {
105
104
  const result = userData.onlyWithStop ? zoneInOutData.filter(d => d.stopped) : zoneInOutData
106
105
 
107
106
  if (userData.groupByDay) {
@@ -272,6 +271,15 @@ function analyseAlertsGroupGeofences (alerts, deviceRoute, from, to) {
272
271
  })
273
272
  }
274
273
 
274
+ if (!zoneInOutData.length) {
275
+ const geofenceIn = alerts.find(a => a.type === 'geofenceIn')
276
+ zoneInOutData.push({
277
+ distanceIn: geofenceIn ? calculateDistance(deviceRoute) : 0,
278
+ distanceOut: !geofenceIn ? calculateDistance(deviceRoute) : 0,
279
+ totalTime: to.getTime() - from.getTime()
280
+ })
281
+ }
282
+
275
283
  return zoneInOutData
276
284
  }
277
285
 
@@ -344,40 +352,50 @@ function analyseAlerts (alerts, deviceRoute, userData) {
344
352
  }
345
353
 
346
354
  function checkGeofenceEnter (p1, p2, g) {
355
+ return !checkGeofenceIn(p1, g) && checkGeofenceIn(p2, g)
356
+ }
357
+
358
+ function checkGeofenceExit (p1, p2, g) {
359
+ return checkGeofenceEnter(p2, p1, g)
360
+ }
361
+
362
+ function checkGeofenceIn (p, g) {
347
363
  switch (g.geometry.type) {
348
364
  case 'Polygon':
349
- return !booleanPointInPolygon.default(p1, g) && booleanPointInPolygon.default(p2, g)
365
+ return booleanPointInPolygon.default(p, g)
350
366
  case 'Point':
351
- return distance.default(p1, g, { units: 'meters' }) >= g.properties.distance &&
352
- distance.default(p2, g, { units: 'meters' }) < g.properties.distance
367
+ return distance.default(p, g, { units: 'meters' }) >= g.properties.distance
353
368
  case 'LineString':
354
- return pointToLineDistance.default(p1, g, { units: 'meters' }) > (g.properties.geofence.attributes.polylineDistance || 25) &&
355
- pointToLineDistance.default(p2, g, { units: 'meters' }) <= (g.properties.geofence.attributes.polylineDistance || 25)
369
+ return pointToLineDistance.default(p, g, { units: 'meters' }) > (g.properties.geofence.attributes.polylineDistance || 25)
356
370
  }
357
371
  }
358
372
 
359
- function checkGeofenceExit (p1, p2, g) {
360
- return checkGeofenceEnter(p2, p1, g)
361
- }
362
-
363
373
  function getInAndOutEvents (devices, route, userData) {
364
374
  const events = []
365
375
  devices.forEach(d => {
366
376
  const deviceRoute = route.filter(p => p.deviceId === d.id)
367
377
  const routePoints = deviceRoute.sort(sortPositions).map(p => convertPositionToFeature(p))
368
378
  const geofencesFeatures = userData.geofences.map(g => convertToFeature(g))
369
-
379
+ const deviceEvents = []
370
380
  routePoints.filter(p => p.properties.position.valid).forEach((p, i, array) => {
371
- if (!i) { return }
381
+ if (!i) {
382
+ for (const g of geofencesFeatures) {
383
+ if (checkGeofenceIn(p, g)) {
384
+ deviceEvents.push(createEvent('geofenceIn', d.id, p, g))
385
+ }
386
+ }
387
+ return
388
+ }
372
389
  const previous = array[i - 1]
373
390
  for (const g of geofencesFeatures) {
374
391
  if (checkGeofenceEnter(previous, p, g)) {
375
- events.push(createEvent('geofenceEnter', d.id, p, g))
392
+ deviceEvents.push(createEvent('geofenceEnter', d.id, p, g))
376
393
  } else if (checkGeofenceExit(previous, p, g)) {
377
- events.push(createEvent('geofenceExit', d.id, p, g))
394
+ deviceEvents.push(createEvent('geofenceExit', d.id, p, g))
378
395
  }
379
396
  }
380
397
  })
398
+ events.push(...deviceEvents)
381
399
  })
382
400
  return events
383
401
  }