fleetmap-reports 1.0.652 → 1.0.653

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 +41 -40
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.652",
3
+ "version": "1.0.653",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -83,9 +83,10 @@ async function createZoneReport (from, to, userData, traccar) {
83
83
  }
84
84
 
85
85
  function getNextIn (outDate, alerts, geofenceId, deviceRoute) {
86
- return alerts
86
+ const next = alerts
87
87
  .filter(a => a.type === 'geofenceEnter' && a.geofenceId === geofenceId)
88
- .find(a => new Date(a.position.fixTime).getTime() > outDate) ||
88
+ .find(a => new Date(a.position.fixTime).getTime() > outDate)
89
+ return (next && next.position && new Date(next.position.fixTime).getTime()) ||
89
90
  new Date(deviceRoute.slice(-1)[0].fixTime).getTime()
90
91
  }
91
92
 
@@ -101,45 +102,45 @@ async function processDevices (from, to, devices, userData, data) {
101
102
  const zoneInData = {}
102
103
 
103
104
  alerts.forEach(a => {
104
- if (a.position) {
105
- if (a.type === 'geofenceEnter') {
106
- const driver = userData.drivers.find(d => a.position.attributes && d.uniqueId === a.position.attributes.driverUniqueId)
107
- a.position.driverName = driver ? driver.name : ''
108
- zoneInData[a.geofenceId] = a
109
- } else if (a.type === 'geofenceExit') {
110
- const geofence = userData.geofences.find(g => g.id === a.geofenceId)
111
- if (!geofence) { return }
112
- const outDate = new Date(a.position.fixTime).getTime()
113
- const routeAfterOut = deviceRoute.filter(p =>
114
- new Date(p.fixTime).getTime() >= outDate &&
115
- new Date(p.fixTime).getTime() < getNextIn(outDate, alerts, a.geofenceId, deviceRoute)
116
- )
117
- if (zoneInData[a.geofenceId]) {
118
- const totalInTime = moment(a.position.fixTime).diff(moment(zoneInData[a.geofenceId].position.fixTime), 'seconds')
119
- const inDate = new Date(zoneInData[a.geofenceId].position.fixTime).getTime()
120
- const routeIn = deviceRoute.filter(p =>
121
- new Date(p.fixTime).getTime() >= inDate &&
105
+ if (!a.position) { return }
106
+ if (a.type === 'geofenceEnter') {
107
+ const driver = userData.drivers.find(d => a.position.attributes && d.uniqueId === a.position.attributes.driverUniqueId)
108
+ a.position.driverName = driver ? driver.name : ''
109
+ zoneInData[a.geofenceId] = a
110
+ } else if (a.type === 'geofenceExit') {
111
+ const geofence = userData.geofences.find(g => g.id === a.geofenceId)
112
+ if (!geofence) { return }
113
+ const outDate = new Date(a.position.fixTime).getTime()
114
+ const routeAfterOut = deviceRoute.filter(p =>
115
+ new Date(p.fixTime).getTime() >= outDate &&
116
+ new Date(p.fixTime).getTime() < getNextIn(outDate, alerts, a.geofenceId, deviceRoute)
117
+ )
118
+ const distanceOut = calculateDistance(routeAfterOut)
119
+ if (zoneInData[a.geofenceId]) {
120
+ const totalInTime = moment(a.position.fixTime).diff(moment(zoneInData[a.geofenceId].position.fixTime), 'seconds')
121
+ const inDate = new Date(zoneInData[a.geofenceId].position.fixTime).getTime()
122
+ const routeIn = deviceRoute.filter(p =>
123
+ new Date(p.fixTime).getTime() >= inDate &&
122
124
  new Date(p.fixTime).getTime() < outDate
123
- )
124
- zoneInOutData.push({
125
- inTime: zoneInData[a.geofenceId].position,
126
- outTime: a.position,
127
- totalTime: totalInTime,
128
- distanceIn: calculateDistance(routeIn),
129
- distanceOut: calculateDistance(routeAfterOut),
130
- geofenceName: geofence.name,
131
- stopped: routeIn.filter(p => !p.attributes.ignition).length > 0,
132
- driverName: zoneInData[a.geofenceId].position.driverName
133
- })
134
- zoneInData[a.geofenceId] = null
135
- } else {
136
- zoneInOutData.push({
137
- outTime: a.position,
138
- geofenceName: geofence.name,
139
- driverName: '',
140
- distanceOut: calculateDistance(routeAfterOut)
141
- })
142
- }
125
+ )
126
+ zoneInOutData.push({
127
+ inTime: zoneInData[a.geofenceId].position,
128
+ outTime: a.position,
129
+ totalTime: totalInTime,
130
+ distanceIn: calculateDistance(routeIn),
131
+ distanceOut,
132
+ geofenceName: geofence.name,
133
+ stopped: routeIn.filter(p => !p.attributes.ignition).length > 0,
134
+ driverName: zoneInData[a.geofenceId].position.driverName
135
+ })
136
+ zoneInData[a.geofenceId] = null
137
+ } else {
138
+ zoneInOutData.push({
139
+ outTime: a.position,
140
+ geofenceName: geofence.name,
141
+ driverName: '',
142
+ distanceOut
143
+ })
143
144
  }
144
145
  }
145
146
  })