fleetmap-reports 1.0.473 → 1.0.474

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.473",
3
+ "version": "1.0.474",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/here.js CHANGED
@@ -10,11 +10,11 @@ const calcRoute = 'https://fleet.ls.hereapi.com/2/calculateroute.json?&detailedT
10
10
 
11
11
  const util = require('util')
12
12
 
13
- function mpsToKmh(mps) {
13
+ function mpsToKmh (mps) {
14
14
  return mps * 3.6
15
15
  }
16
16
 
17
- function getSpeedLimit(li) {
17
+ function getSpeedLimit (li) {
18
18
  if (li && li.attributes && li.attributes.SPEED_LIMITS_FCN) {
19
19
  const from = li.attributes.SPEED_LIMITS_FCN[0].FROM_REF_SPEED_LIMIT
20
20
  const to = li.attributes.SPEED_LIMITS_FCN[0].TO_REF_SPEED_LIMIT
@@ -26,7 +26,7 @@ function getSpeedLimit(li) {
26
26
  return 999
27
27
  }
28
28
 
29
- function getRoadName(li) {
29
+ function getRoadName (li) {
30
30
  if (li.attributes.ROAD_GEOM_FCN) {
31
31
  const result = li.attributes.ROAD_GEOM_FCN[0].NAME
32
32
  if (result && result.length > 0) { return result }
@@ -34,14 +34,14 @@ function getRoadName(li) {
34
34
  return ''
35
35
  }
36
36
 
37
- function getTruckSpeedLimits(li) {
37
+ function getTruckSpeedLimits (li) {
38
38
  if (li.attributes.TRUCK_SPEED_LIMITS_FCN) {
39
39
  return li.attributes.TRUCK_SPEED_LIMITS_FCN
40
40
  }
41
41
  return null
42
42
  }
43
43
 
44
- function getDistanceMarkers(li) {
44
+ function getDistanceMarkers (li) {
45
45
  if (li.attributes.DISTANCE_MARKERS_FCN) {
46
46
  const dMarkers = li.attributes.DISTANCE_MARKERS_FCN
47
47
  return util.format('km %d', dMarkers[0].DISTANCE_VALUE)
@@ -49,7 +49,7 @@ function getDistanceMarkers(li) {
49
49
  return ''
50
50
  }
51
51
 
52
- function generateCSV(rows) {
52
+ function generateCSV (rows) {
53
53
  const route = []
54
54
  route.push('latitude, longitude, speed_kmh, heading, timestamp')
55
55
  for (let i = 0; i < rows.length; i++) {
@@ -68,7 +68,6 @@ function generateCoordsCSV(rows) {
68
68
  return route.join('\n')
69
69
  }
70
70
 
71
-
72
71
  function fillGeocoding(rows) {
73
72
  return new Promise((resolve) => {
74
73
  const csv = generateCoordsCSV(rows.slice(0, 300))
@@ -94,12 +93,12 @@ function fillGeocoding(rows) {
94
93
  console.error(e)
95
94
  })
96
95
  })
97
- }*/
96
+ } */
98
97
 
99
98
  exports.routeMatch = async (rows) => {
100
99
  console.log('here sending', rows.length)
101
100
  const csv = generateCSV(rows)
102
- const response = await axios.post(calcRoute, csv, { headers: { 'Content-Type': 'application/binary' }})
101
+ const response = await axios.post(calcRoute, csv, { headers: { 'Content-Type': 'application/binary' } })
103
102
  const hereData = response.data
104
103
  const route = hereData.response.route[0]
105
104
  const results = []
@@ -109,32 +108,30 @@ exports.routeMatch = async (rows) => {
109
108
  for (let i = 0; i < wayPoints.length; i++) {
110
109
  const tp = wayPoints[i]
111
110
  const li = links[tp.routeLinkSeqNrMatched]
112
- if (mpsToKmh(tp.speedMps) > getSpeedLimit(li)) {
113
- results.push({
114
- linkId: tp.routeLinkSeqNrMatched,
115
- timestamp: tp.timestamp,
116
- currentSpeedKmh: mpsToKmh(tp.speedMps),
117
- latitude: tp.originalPosition.latitude,
118
- longitude: tp.originalPosition.longitude,
119
- latMatched: tp.mappedPosition.latitude,
120
- lonMatched: tp.mappedPosition.longitude,
121
- heading: tp.headingDegreeNorthClockwise,
122
- headingMatched: tp.headingMatched,
123
- speedLimit: getSpeedLimit(li),
124
- roadName: getRoadName(li),
125
- truckSpeedLimits: getTruckSpeedLimits(li),
126
- distanceMarkers: getDistanceMarkers(li),
127
- geocoding: ''
128
- })
129
- }
111
+ results.push({
112
+ linkId: tp.routeLinkSeqNrMatched,
113
+ timestamp: tp.timestamp,
114
+ currentSpeedKmh: mpsToKmh(tp.speedMps),
115
+ latitude: tp.originalPosition.latitude,
116
+ longitude: tp.originalPosition.longitude,
117
+ latMatched: tp.mappedPosition.latitude,
118
+ lonMatched: tp.mappedPosition.longitude,
119
+ heading: tp.headingDegreeNorthClockwise,
120
+ headingMatched: tp.headingMatched,
121
+ speedLimit: getSpeedLimit(li),
122
+ roadName: getRoadName(li),
123
+ truckSpeedLimits: getTruckSpeedLimits(li),
124
+ distanceMarkers: getDistanceMarkers(li),
125
+ geocoding: ''
126
+ })
130
127
  }
131
128
  }
132
129
  return results
133
130
  }
134
131
 
135
- exports.tollsMatch = async(rows, result) => {
132
+ exports.tollsMatch = async (rows, result) => {
136
133
  const csv = generateCSV(rows)
137
- axios.post(calcRoute, csv, { headers: { 'Content-Type': 'application/binary' }}).then((response) => {
134
+ axios.post(calcRoute, csv, { headers: { 'Content-Type': 'application/binary' } }).then((response) => {
138
135
  const hereData = response.data
139
136
  const results = []
140
137
  const tollData = hereData.response.route[0].tollCost.routeTollItems
package/src/index.test.js CHANGED
@@ -3,11 +3,11 @@ const assert = require('assert')
3
3
 
4
4
  async function getSpeedingReport (report, userData) {
5
5
  const data = await report.speedingReport(
6
- new Date(Date.UTC(2021, 11, 6, 0, 0, 0, 0)),
7
- new Date(Date.UTC(2021, 11, 8, 23, 59, 59, 0)),
6
+ new Date(Date.UTC(2022, 7, 15, 0, 0, 0, 0)),
7
+ new Date(Date.UTC(2022, 7, 15, 23, 59, 59, 0)),
8
8
  userData)
9
9
  assert.equal(data.length, 1)
10
- const device = data[0].devices.find(d => d.device.id === 22326)
10
+ const device = data[0].devices[0]
11
11
  const totalDistance = device.alerts.reduce((a, b) => a + b.distance, 0)
12
12
  const totalEventTime = device.alerts.reduce((a, b) => a + b.eventTime, 0)
13
13
  return { device, totalDistance, totalEventTime }
@@ -19,14 +19,14 @@ describe('Test_Reports', function () {
19
19
  it('Speeding by device', async () => {
20
20
  const report = await getReports()
21
21
  const userData = await report.getUserData()
22
- userData.roadSpeedLimit = true
22
+ userData.roadSpeedLimits = true
23
23
  const { device, totalDistance, totalEventTime } = await getSpeedingReport(report, userData)
24
24
  assert.equal(device.alerts.length, 15) // Total Alerts
25
25
  assert.equal(totalDistance, 19.59984677533689) // Total Kms
26
26
  assert.equal(totalEventTime, 562000) // Total Duration
27
27
  }, 200000)
28
28
  // eslint-disable-next-line no-undef
29
- it('Speeding by device with custom speed', async () => {
29
+ it('Speeding with custom speed', async () => {
30
30
  const report = await getReports()
31
31
  const userData = await report.getUserData()
32
32
 
@@ -280,34 +280,29 @@ async function getHereEvents (devices, routes, threshold) {
280
280
  console.log('no positions on device', d.name)
281
281
  resolve()
282
282
  }
283
- let hereAlerts = null
284
283
  here.routeMatch(positions).then(results => {
285
- hereAlerts = results.filter(r => r.currentSpeedKmh > (parseInt(r.speedLimit) + threshold)).map(r => {
286
- const position = positions.find(p => new Date(p.fixTime).getTime() === r.timestamp)
287
- return {
288
- ...r,
289
- roadSpeedLimit: r.speedLimit,
290
- deviceId: d.id,
291
- position,
292
- positionId: position && position.id,
293
- attributes: { speedLimit: r.speedLimit, speed: r.currentSpeedKmh / 1.85200 }
284
+ const reduced = results.reduce((acc, cur, idx, src) => {
285
+ const previous = acc.length && acc.slice(-1)[0]
286
+ if (cur.currentSpeedKmh > parseInt(cur.speedLimit) + (threshold || 0)) {
287
+ cur.position = positions.find(p => new Date(p.fixTime).getTime() === cur.timestamp)
288
+ if (previous && previous.speedLimit === cur.speedLimit) {
289
+ previous.eventTime += cur.timestamp - previous.timestamp
290
+ previous.attributes.maxSpeed = Math.max(previous.attributes.maxSpeed, cur.currentSpeedKmh)
291
+ previous.distance += distance.default(point([cur.position.longitude, cur.position.latitude]), point([previous.position.longitude, previous.position.latitude]))
292
+ } else {
293
+ cur.positionId = cur.position && cur.position.id
294
+ cur.roadSpeedLimit = cur.speedLimit
295
+ cur.deviceId = d.id
296
+ cur.attributes = { speedLimit: cur.speedLimit, speed: cur.currentSpeedKmh / 1.85200 }
297
+ return acc.concat(cur)
298
+ }
294
299
  }
295
- })
296
- if (!hereAlerts.length) {
300
+ return acc
301
+ }, [])
302
+ if (!reduced.length) {
297
303
  console.log('empty array after filter on device', d.name)
298
304
  resolve()
299
305
  }
300
- const reduced = hereAlerts.length < 2
301
- ? hereAlerts
302
- : hereAlerts.reduce((acc, cur, idx, src) => {
303
- if (idx === 1) {
304
- return [acc]
305
- }
306
- if (cur.timestamp - src[idx - 1].timestamp > 300000 || cur.roadSpeedLimit !== src[idx - 1].roadSpeedLimit) {
307
- return acc.concat(cur)
308
- }
309
- return acc
310
- })
311
306
  events.push(...reduced)
312
307
  resolve()
313
308
  }).catch(e => {
@@ -323,6 +318,7 @@ function calculateEventData (positions, pIndex, alert, device, geofence, userDat
323
318
  let endEventPosition = alert.position
324
319
  let maxSpeed = alert.position.speed
325
320
  let dist = 0
321
+ if (alert.eventTime) { return -1 }
326
322
  while (pIndex + 1 < positions.length) {
327
323
  pIndex++
328
324
  dist += distance.default(point([endEventPosition.longitude, endEventPosition.latitude]),