fleetmap-reports 1.0.484 → 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 +1 -1
- package/src/index.test.js +1 -1
- package/src/speeding-report.js +55 -42
package/package.json
CHANGED
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,
|
|
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
|
package/src/speeding-report.js
CHANGED
|
@@ -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
|
-
|
|
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 {
|
|
@@ -218,7 +221,7 @@ async function findEventsPosition (from, to, devices, events, routes, userData)
|
|
|
218
221
|
for (const d of devices) {
|
|
219
222
|
const deviceEvents = events.filter(e => e.deviceId === d.id && e.positionId)
|
|
220
223
|
if (deviceEvents.length > 0) {
|
|
221
|
-
deviceEvents.sort((a, b) => a.
|
|
224
|
+
deviceEvents.sort((a, b) => new Date(a.position.fixTime) - new Date(b.position.fixTime))
|
|
222
225
|
const positions = routes.filter(p => p.deviceId === d.id)
|
|
223
226
|
|
|
224
227
|
for (const a of deviceEvents) {
|
|
@@ -281,47 +284,57 @@ async function getCustomSpeedLimitEvents (devices, routes, customSpeed) {
|
|
|
281
284
|
}
|
|
282
285
|
|
|
283
286
|
async function getHereEvents (devices, routes, threshold) {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
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
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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) {
|