fleetmap-reports 1.0.391 → 1.0.398
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 +10 -4
- package/src/index.test.js +340 -328
- package/src/refueling-report.js +149 -147
- package/src/speeding-report.js +112 -111
- package/src/tests/index.js +14 -16
- package/src/util/utils.js +152 -135
- package/src/zone-report.js +34 -30
package/src/speeding-report.js
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
const messages = require('../lang')
|
|
2
2
|
const jsPDF = require('jspdf')
|
|
3
|
-
const {convertMS, convertToLocaleString} = require(
|
|
4
|
-
const {headerFromUser, AmiriRegular} = require(
|
|
3
|
+
const { convertMS, convertToLocaleString } = require('./util/utils')
|
|
4
|
+
const { headerFromUser, AmiriRegular } = require('./util/pdfDocument')
|
|
5
5
|
require('jspdf-autotable')
|
|
6
6
|
const { parse } = require('wkt')
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const drivers = require("./util/driver")
|
|
10
|
-
const {distance, point} = require('turf')
|
|
7
|
+
const { getStyle } = require('./reportStyle')
|
|
8
|
+
const drivers = require('./util/driver')
|
|
11
9
|
const here = require('./here')
|
|
12
|
-
const {getUserPartner} = require(
|
|
13
|
-
const traccarHelper = require(
|
|
14
|
-
const {devicesToProcess} = require(
|
|
10
|
+
const { getUserPartner } = require('fleetmap-partners')
|
|
11
|
+
const traccarHelper = require('./util/traccar')
|
|
12
|
+
const { devicesToProcess } = require('./util/device')
|
|
13
|
+
const turf = require('@turf/distance')
|
|
14
|
+
const { point } = require('@turf/helpers')
|
|
15
|
+
const distance = require('@turf/distance');
|
|
15
16
|
|
|
16
17
|
const fileName = 'SpeedingReport'
|
|
17
18
|
const eventTypes = ['deviceOverspeed']
|
|
18
19
|
|
|
19
|
-
async function createSpeedingReport(from, to, userData, traccarInstance) {
|
|
20
|
+
async function createSpeedingReport (from, to, userData, traccarInstance) {
|
|
20
21
|
const reportData = []
|
|
21
22
|
|
|
22
23
|
if (userData.byDriver) {
|
|
23
|
-
console.log(
|
|
24
|
+
console.log('ByDriver')
|
|
24
25
|
const allData = await createSpeedingReportByDriver(from, to, userData, traccarInstance)
|
|
25
26
|
reportData.push(allData)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
console.log("ByGroup")
|
|
27
|
+
} else if (userData.byGroup) {
|
|
28
|
+
console.log('ByGroup')
|
|
29
29
|
const allData = await createSpeedingReportByGroup(from, to, userData, traccarInstance)
|
|
30
30
|
reportData.push(...allData)
|
|
31
31
|
} else {
|
|
32
|
-
console.log(
|
|
32
|
+
console.log('createSpeedingReportByDevice')
|
|
33
33
|
const allData = await createSpeedingReportByDevice(from, to, userData, traccarInstance)
|
|
34
34
|
reportData.push(allData)
|
|
35
35
|
}
|
|
@@ -37,12 +37,12 @@ async function createSpeedingReport(from, to, userData, traccarInstance) {
|
|
|
37
37
|
return reportData
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
async function createSpeedingReportByGroup(from, to, userData, traccarInstance){
|
|
40
|
+
async function createSpeedingReportByGroup (from, to, userData, traccarInstance) {
|
|
41
41
|
const reportData = []
|
|
42
42
|
for (const g of userData.groups) {
|
|
43
43
|
const devices = userData.devices.filter(d => d.groupId === g.id)
|
|
44
44
|
console.log(g.name + ' devices:' + devices.length)
|
|
45
|
-
if(devices.length > 0) {
|
|
45
|
+
if (devices.length > 0) {
|
|
46
46
|
const groupData = {
|
|
47
47
|
devices: [],
|
|
48
48
|
group: g,
|
|
@@ -67,24 +67,24 @@ async function createSpeedingReportByGroup(from, to, userData, traccarInstance){
|
|
|
67
67
|
return reportData
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
async function createSpeedingReportByDevice(from, to, userData, traccarInstance) {
|
|
70
|
+
async function createSpeedingReportByDevice (from, to, userData, traccarInstance) {
|
|
71
71
|
const devices = devicesToProcess(userData)
|
|
72
72
|
|
|
73
73
|
const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false, false, false)
|
|
74
74
|
const routes = allInOne.route
|
|
75
75
|
const events = []
|
|
76
|
-
if(!userData.useVehicleSpeedLimit && userData.customSpeed){
|
|
76
|
+
if (!userData.useVehicleSpeedLimit && userData.customSpeed) {
|
|
77
77
|
events.push(...await getCustomSpeedLimitEvents(devices, routes, userData.customSpeed))
|
|
78
78
|
} else {
|
|
79
79
|
events.push(...await traccarHelper.getEvents(traccarInstance, from, to, devices, eventTypes))
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
if(userData.roadSpeedLimits){
|
|
82
|
+
if (userData.roadSpeedLimits) {
|
|
83
83
|
events.push(...await getHereEvents(devices, routes, userData.maxSpeedThreshold))
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
if(!events.length) {
|
|
87
|
-
//empty report
|
|
86
|
+
if (!events.length) {
|
|
87
|
+
// empty report
|
|
88
88
|
return { devices: [] }
|
|
89
89
|
}
|
|
90
90
|
|
|
@@ -94,12 +94,12 @@ async function createSpeedingReportByDevice(from, to, userData, traccarInstance)
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
async function createSpeedingReportByDriver(from, to, userData, traccarInstance) {
|
|
97
|
+
async function createSpeedingReportByDriver (from, to, userData, traccarInstance) {
|
|
98
98
|
const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
|
|
99
|
-
console.log('Devices with driver',devices.length)
|
|
99
|
+
console.log('Devices with driver', devices.length)
|
|
100
100
|
|
|
101
|
-
if(!devices.length) {
|
|
102
|
-
//empty report
|
|
101
|
+
if (!devices.length) {
|
|
102
|
+
// empty report
|
|
103
103
|
return { drivers: [] }
|
|
104
104
|
}
|
|
105
105
|
|
|
@@ -107,25 +107,25 @@ async function createSpeedingReportByDriver(from, to, userData, traccarInstance)
|
|
|
107
107
|
const routes = allInOne.route
|
|
108
108
|
|
|
109
109
|
const events = []
|
|
110
|
-
if(!userData.useVehicleSpeedLimit && userData.customSpeed){
|
|
110
|
+
if (!userData.useVehicleSpeedLimit && userData.customSpeed) {
|
|
111
111
|
events.push(...await getCustomSpeedLimitEvents(devices, routes, userData.customSpeed))
|
|
112
112
|
} else {
|
|
113
113
|
events.push(...await traccarHelper.getEvents(traccarInstance, from, to, devices, eventTypes))
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
if(userData.roadSpeedLimits){
|
|
116
|
+
if (userData.roadSpeedLimits) {
|
|
117
117
|
events.push(...await getHereEvents(devices, routes, userData.maxSpeedThreshold))
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
if(!events.length) {
|
|
121
|
-
//empty report
|
|
120
|
+
if (!events.length) {
|
|
121
|
+
// empty report
|
|
122
122
|
return { drivers: [] }
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
return {drivers: await processDrivers(from, to, events, routes, userData)}
|
|
125
|
+
return { drivers: await processDrivers(from, to, events, routes, userData) }
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
async function processDrivers(from, to, events, routes, userData) {
|
|
128
|
+
async function processDrivers (from, to, events, routes, userData) {
|
|
129
129
|
const driversResult = []
|
|
130
130
|
|
|
131
131
|
await findEventsPosition(from, to, userData.devices, events, routes, userData)
|
|
@@ -136,12 +136,12 @@ async function processDrivers(from, to, events, routes, userData) {
|
|
|
136
136
|
driverEvents.sort((a, b) => a.positionId - b.positionId)
|
|
137
137
|
driversResult.push({
|
|
138
138
|
driver: d,
|
|
139
|
-
from
|
|
140
|
-
to
|
|
139
|
+
from,
|
|
140
|
+
to,
|
|
141
141
|
alerts: driverEvents,
|
|
142
142
|
maxSpeed: driverEvents.reduce((a, b) => {
|
|
143
143
|
return a > b.attributes.maxSpeed ? a : b.attributes.maxSpeed
|
|
144
|
-
}, 0)
|
|
144
|
+
}, 0)
|
|
145
145
|
})
|
|
146
146
|
}
|
|
147
147
|
})
|
|
@@ -149,7 +149,7 @@ async function processDrivers(from, to, events, routes, userData) {
|
|
|
149
149
|
return driversResult
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
async function processDevices(from, to, devices, events, routes, userData) {
|
|
152
|
+
async function processDevices (from, to, devices, events, routes, userData) {
|
|
153
153
|
const devicesResult = []
|
|
154
154
|
await findEventsPosition(from, to, devices, events, routes, userData)
|
|
155
155
|
|
|
@@ -159,12 +159,12 @@ async function processDevices(from, to, devices, events, routes, userData) {
|
|
|
159
159
|
deviceEvents.sort((a, b) => a.positionId - b.positionId)
|
|
160
160
|
devicesResult.push({
|
|
161
161
|
device: d,
|
|
162
|
-
from
|
|
163
|
-
to
|
|
162
|
+
from,
|
|
163
|
+
to,
|
|
164
164
|
alerts: deviceEvents,
|
|
165
165
|
maxSpeed: deviceEvents.reduce((a, b) => {
|
|
166
166
|
return a > (b && b.attributes && b.attributes.maxSpeed) ? a : (b && b.attributes && b.attributes.maxSpeed)
|
|
167
|
-
}, 0)
|
|
167
|
+
}, 0)
|
|
168
168
|
})
|
|
169
169
|
}
|
|
170
170
|
}
|
|
@@ -172,11 +172,11 @@ async function processDevices(from, to, devices, events, routes, userData) {
|
|
|
172
172
|
return devicesResult
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
async function findEventsPosition(from, to, devices, events, routes, userData){
|
|
175
|
+
async function findEventsPosition (from, to, devices, events, routes, userData) {
|
|
176
176
|
for (const d of devices) {
|
|
177
|
-
|
|
177
|
+
const deviceEvents = events.filter(e => e.deviceId === d.id && e.positionId)
|
|
178
178
|
if (deviceEvents.length > 0) {
|
|
179
|
-
deviceEvents.sort((a,b) => a.positionId-b.positionId
|
|
179
|
+
deviceEvents.sort((a, b) => a.positionId - b.positionId)
|
|
180
180
|
const positions = routes.filter(p => p.deviceId === d.id)
|
|
181
181
|
|
|
182
182
|
for (const a of deviceEvents) {
|
|
@@ -199,7 +199,7 @@ async function findEventsPosition(from, to, devices, events, routes, userData){
|
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
async function getCustomSpeedLimitEvents(devices, routes, customSpeed){
|
|
202
|
+
async function getCustomSpeedLimitEvents (devices, routes, customSpeed) {
|
|
203
203
|
console.log('custom speed limit events')
|
|
204
204
|
const events = []
|
|
205
205
|
const maxSpeed = customSpeed / 1.85200
|
|
@@ -209,7 +209,7 @@ async function getCustomSpeedLimitEvents(devices, routes, customSpeed){
|
|
|
209
209
|
let eventIsActive = false
|
|
210
210
|
|
|
211
211
|
for (const position of positions) {
|
|
212
|
-
if(position.speed > maxSpeed && !eventIsActive){
|
|
212
|
+
if (position.speed > maxSpeed && !eventIsActive) {
|
|
213
213
|
const event = {
|
|
214
214
|
positionId: position && position.id,
|
|
215
215
|
deviceId: d.id,
|
|
@@ -229,7 +229,7 @@ async function getCustomSpeedLimitEvents(devices, routes, customSpeed){
|
|
|
229
229
|
return events
|
|
230
230
|
}
|
|
231
231
|
|
|
232
|
-
async function getHereEvents(devices, routes, threshold){
|
|
232
|
+
async function getHereEvents (devices, routes, threshold) {
|
|
233
233
|
console.log('here speed limit events')
|
|
234
234
|
const events = []
|
|
235
235
|
for (const d of devices) {
|
|
@@ -239,23 +239,25 @@ async function getHereEvents(devices, routes, threshold){
|
|
|
239
239
|
continue
|
|
240
240
|
}
|
|
241
241
|
let hereAlerts = null
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
}
|
|
253
|
-
})
|
|
254
|
-
if (!hereAlerts.length) {
|
|
255
|
-
console.log('empty array after filter on device', d.name)
|
|
256
|
-
continue
|
|
242
|
+
const results = await here.routeMatch(positions)
|
|
243
|
+
hereAlerts = results.filter(r => r.currentSpeedKmh > (parseInt(r.speedLimit) + threshold)).map(r => {
|
|
244
|
+
const position = positions.find(p => new Date(p.fixTime).getTime() === r.timestamp)
|
|
245
|
+
return {
|
|
246
|
+
...r,
|
|
247
|
+
roadSpeedLimit: r.speedLimit,
|
|
248
|
+
deviceId: d.id,
|
|
249
|
+
position,
|
|
250
|
+
positionId: position && position.id,
|
|
251
|
+
attributes: { speedLimit: r.speedLimit, speed: r.currentSpeedKmh / 1.85200 }
|
|
257
252
|
}
|
|
258
|
-
|
|
253
|
+
})
|
|
254
|
+
if (!hereAlerts.length) {
|
|
255
|
+
console.log('empty array after filter on device', d.name)
|
|
256
|
+
continue
|
|
257
|
+
}
|
|
258
|
+
const reduced = hereAlerts.length < 2
|
|
259
|
+
? hereAlerts
|
|
260
|
+
: hereAlerts.reduce((acc, cur, idx, src) => {
|
|
259
261
|
if (idx === 1) {
|
|
260
262
|
return [cur]
|
|
261
263
|
}
|
|
@@ -264,20 +266,19 @@ async function getHereEvents(devices, routes, threshold){
|
|
|
264
266
|
}
|
|
265
267
|
return acc
|
|
266
268
|
})
|
|
267
|
-
|
|
269
|
+
events.push(...reduced)
|
|
268
270
|
}
|
|
269
271
|
return events
|
|
270
272
|
}
|
|
271
273
|
|
|
272
|
-
function calculateEventData(positions, pIndex, alert, device, geofence, userData){
|
|
274
|
+
function calculateEventData (positions, pIndex, alert, device, geofence, userData) {
|
|
273
275
|
let endEventPosition = alert.position
|
|
274
276
|
let maxSpeed = alert.position.speed
|
|
275
277
|
let dist = 0
|
|
276
278
|
while (pIndex + 1 < positions.length) {
|
|
277
279
|
pIndex++
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
point([positions[pIndex].longitude, positions[pIndex].latitude]))
|
|
280
|
+
dist += distance.default(point([endEventPosition.longitude, endEventPosition.latitude]),
|
|
281
|
+
point([positions[pIndex].longitude, positions[pIndex].latitude]))
|
|
281
282
|
endEventPosition = positions[pIndex]
|
|
282
283
|
if (endEventPosition.speed <= alert.attributes.speedLimit || (geofence && isOutsideGeofence(geofence, endEventPosition))) {
|
|
283
284
|
alert.eventTime = new Date(endEventPosition.fixTime) - new Date(alert.position.fixTime)
|
|
@@ -291,7 +292,6 @@ function calculateEventData(positions, pIndex, alert, device, geofence, userData
|
|
|
291
292
|
}
|
|
292
293
|
}
|
|
293
294
|
|
|
294
|
-
|
|
295
295
|
if (alert.position.attributes.driverUniqueId) {
|
|
296
296
|
const driver = userData.drivers.find(d => d.uniqueId === alert.position.attributes.driverUniqueId)
|
|
297
297
|
alert.driver = driver && driver.name
|
|
@@ -302,13 +302,13 @@ function calculateEventData(positions, pIndex, alert, device, geofence, userData
|
|
|
302
302
|
return pIndex
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
-
function isOutsideGeofence(geofence, endEventPosition) {
|
|
305
|
+
function isOutsideGeofence (geofence, endEventPosition) {
|
|
306
306
|
const v = turf.feature(turf.flip(parse(geofence.area)))
|
|
307
307
|
const point = turf.point([endEventPosition.longitude, endEventPosition.latitude])
|
|
308
308
|
return turf.inside(point, v)
|
|
309
309
|
}
|
|
310
310
|
|
|
311
|
-
async function exportSpeedingReportToPDF(userData, reportData) {
|
|
311
|
+
async function exportSpeedingReportToPDF (userData, reportData) {
|
|
312
312
|
console.log('Export to PDF')
|
|
313
313
|
|
|
314
314
|
const lang = userData.user.attributes.lang
|
|
@@ -323,10 +323,10 @@ async function exportSpeedingReportToPDF(userData, reportData) {
|
|
|
323
323
|
translations.report.overspeed,
|
|
324
324
|
translations.report.distance,
|
|
325
325
|
translations.report.maxSpeed,
|
|
326
|
-
translations.report.duration
|
|
326
|
+
translations.report.duration
|
|
327
327
|
]
|
|
328
328
|
|
|
329
|
-
if(userData.roadSpeedLimits) {
|
|
329
|
+
if (userData.roadSpeedLimits) {
|
|
330
330
|
headers.splice(2, 0, translations.report.roadSpeedLimit)
|
|
331
331
|
}
|
|
332
332
|
|
|
@@ -338,43 +338,43 @@ async function exportSpeedingReportToPDF(userData, reportData) {
|
|
|
338
338
|
|
|
339
339
|
if (overspeedData) {
|
|
340
340
|
let first = true
|
|
341
|
-
const doc = new jsPDF.jsPDF('l',undefined, undefined, true)
|
|
341
|
+
const doc = new jsPDF.jsPDF('l', undefined, undefined, true)
|
|
342
342
|
await headerFromUser(doc, translations.report.titleSpeedingReport, userData.user)
|
|
343
343
|
|
|
344
344
|
overspeedData.forEach(d => {
|
|
345
345
|
try {
|
|
346
|
-
|
|
346
|
+
const data = []
|
|
347
347
|
|
|
348
348
|
const name = userData.byDriver ? d.driver.name : deviceName(d.device)
|
|
349
349
|
const group = userData.byDriver ? userData.groups.find(g => g.drivers.includes(d.id)) : userData.groups.find(g => d.device.groupId === g.id)
|
|
350
350
|
|
|
351
351
|
let space = 0
|
|
352
|
-
if(!first) {
|
|
352
|
+
if (!first) {
|
|
353
353
|
doc.addPage()
|
|
354
354
|
} else {
|
|
355
355
|
first = false
|
|
356
356
|
space = 10
|
|
357
357
|
}
|
|
358
358
|
doc.setFontSize(13)
|
|
359
|
-
doc.text(name, 20, space+20
|
|
359
|
+
doc.text(name, 20, space + 20)
|
|
360
360
|
doc.setFontSize(11)
|
|
361
|
-
doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space+20
|
|
362
|
-
doc.text(convertToLocaleString(d.from, lang, timezone) + ' - ' + convertToLocaleString(d.to, lang, timezone), 20, space+25
|
|
361
|
+
doc.text(group ? translations.report.group + ': ' + group.name : '', 150, space + 20)
|
|
362
|
+
doc.text(convertToLocaleString(d.from, lang, timezone) + ' - ' + convertToLocaleString(d.to, lang, timezone), 20, space + 25)
|
|
363
363
|
if (d.alerts[0] && !userData.byDriver) {
|
|
364
|
-
doc.text(translations.report.speedLimit +
|
|
364
|
+
doc.text(translations.report.speedLimit + ': ' + ((userData.useVehicleSpeedLimit || !userData.customSpeed) ? Math.round(d.alerts[0].attributes.speedLimit * 1.85200) : userData.customSpeed) + ' Km/h', 20, space + 30)
|
|
365
365
|
}
|
|
366
366
|
d.alerts.map(a => {
|
|
367
367
|
const temp = [
|
|
368
368
|
getAlertDate(a, userData.user),
|
|
369
|
-
a.position.address
|
|
369
|
+
a.position.address + (a.geofenceName ? ' - ' + a.geofenceName : ''),
|
|
370
370
|
Math.round(a.attributes.speed * 1.85200),
|
|
371
|
-
a.distance && new Intl.NumberFormat(userData.user.attributes.lang, {maximumSignificantDigits: 1}).format(a.distance),
|
|
371
|
+
a.distance && new Intl.NumberFormat(userData.user.attributes.lang, { maximumSignificantDigits: 1 }).format(a.distance),
|
|
372
372
|
a.attributes.maxSpeed ? Math.round(a.attributes.maxSpeed * 1.85200) : Math.round(a.attributes.speed * 1.85200),
|
|
373
373
|
convertMS(a.eventTime, true),
|
|
374
374
|
userData.byDriver ? a.deviceName : a.driver
|
|
375
375
|
]
|
|
376
376
|
|
|
377
|
-
if(userData.roadSpeedLimits){
|
|
377
|
+
if (userData.roadSpeedLimits) {
|
|
378
378
|
temp.splice(2, 0, a.roadSpeedLimit)
|
|
379
379
|
}
|
|
380
380
|
|
|
@@ -390,13 +390,13 @@ async function exportSpeedingReportToPDF(userData, reportData) {
|
|
|
390
390
|
convertMS(d.alerts.reduce((a, b) => a + b.eventTime, 0), true)
|
|
391
391
|
]
|
|
392
392
|
|
|
393
|
-
if(userData.roadSpeedLimits){
|
|
393
|
+
if (userData.roadSpeedLimits) {
|
|
394
394
|
footValues.splice(2, 0, '')
|
|
395
395
|
}
|
|
396
396
|
|
|
397
|
-
if(timezone === 'Asia/Qatar') {
|
|
398
|
-
doc.addFileToVFS(
|
|
399
|
-
doc.addFont(
|
|
397
|
+
if (timezone === 'Asia/Qatar') {
|
|
398
|
+
doc.addFileToVFS('Amiri-Regular.ttf', AmiriRegular())
|
|
399
|
+
doc.addFont('Amiri-Regular.ttf', 'Amiri', 'normal')
|
|
400
400
|
}
|
|
401
401
|
|
|
402
402
|
const style = getStyle(getUserPartner(userData.user))
|
|
@@ -414,14 +414,15 @@ async function exportSpeedingReportToPDF(userData, reportData) {
|
|
|
414
414
|
fillColor: style.pdfBodyColor,
|
|
415
415
|
textColor: style.pdfBodyTextColor,
|
|
416
416
|
fontSize: 8,
|
|
417
|
-
font: timezone === 'Asia/Qatar' ?
|
|
417
|
+
font: timezone === 'Asia/Qatar' ? 'Amiri' : ''
|
|
418
418
|
},
|
|
419
419
|
footStyles: {
|
|
420
420
|
fillColor: style.pdfFooterColor,
|
|
421
421
|
textColor: style.pdfFooterTextColor,
|
|
422
422
|
fontSize: 9
|
|
423
423
|
},
|
|
424
|
-
startY: space+35
|
|
424
|
+
startY: space + 35
|
|
425
|
+
})
|
|
425
426
|
} catch (e) {
|
|
426
427
|
console.error(e, 'moving on...')
|
|
427
428
|
}
|
|
@@ -431,7 +432,7 @@ async function exportSpeedingReportToPDF(userData, reportData) {
|
|
|
431
432
|
}
|
|
432
433
|
}
|
|
433
434
|
|
|
434
|
-
function exportSpeedingReportToExcel(userData, reportData) {
|
|
435
|
+
function exportSpeedingReportToExcel (userData, reportData) {
|
|
435
436
|
console.log('Export to Excel')
|
|
436
437
|
|
|
437
438
|
const lang = userData.user.attributes.lang
|
|
@@ -441,21 +442,21 @@ function exportSpeedingReportToExcel(userData, reportData) {
|
|
|
441
442
|
|
|
442
443
|
const settings = {
|
|
443
444
|
sheetName: translations.report.titleSpeedingReport.substring(0, 31), // The name of the sheet
|
|
444
|
-
fileName
|
|
445
|
+
fileName // The name of the spreadsheet
|
|
445
446
|
}
|
|
446
447
|
const headers = [
|
|
447
|
-
userData.byDriver ? {label: translations.report.driver, value:'driver'} : {label: translations.report.vehicle, value:'name'},
|
|
448
|
-
{label: translations.report.date, value:'fixTime'},
|
|
449
|
-
{label: translations.report.address, value:'address'},
|
|
450
|
-
{label: translations.report.overspeed, value:'speed'},
|
|
451
|
-
{label: translations.report.distance, value:'distance'},
|
|
452
|
-
{label: translations.report.maxSpeed, value:'maxSpeed'},
|
|
453
|
-
{label: translations.report.duration, value:'duration'},
|
|
454
|
-
userData.byDriver ? {label: translations.report.vehicle, value:'name'} : {label: translations.report.driver, value:'driver'}
|
|
448
|
+
userData.byDriver ? { label: translations.report.driver, value: 'driver' } : { label: translations.report.vehicle, value: 'name' },
|
|
449
|
+
{ label: translations.report.date, value: 'fixTime' },
|
|
450
|
+
{ label: translations.report.address, value: 'address' },
|
|
451
|
+
{ label: translations.report.overspeed, value: 'speed' },
|
|
452
|
+
{ label: translations.report.distance, value: 'distance' },
|
|
453
|
+
{ label: translations.report.maxSpeed, value: 'maxSpeed' },
|
|
454
|
+
{ label: translations.report.duration, value: 'duration' },
|
|
455
|
+
userData.byDriver ? { label: translations.report.vehicle, value: 'name' } : { label: translations.report.driver, value: 'driver' }
|
|
455
456
|
]
|
|
456
457
|
|
|
457
|
-
if (userData.roadSpeedLimits){
|
|
458
|
-
headers.splice(3, 0, {label: translations.report.roadSpeedLimit, value:'roadSpeedLimit'})
|
|
458
|
+
if (userData.roadSpeedLimits) {
|
|
459
|
+
headers.splice(3, 0, { label: translations.report.roadSpeedLimit, value: 'roadSpeedLimit' })
|
|
459
460
|
}
|
|
460
461
|
|
|
461
462
|
let data = []
|
|
@@ -466,9 +467,9 @@ function exportSpeedingReportToExcel(userData, reportData) {
|
|
|
466
467
|
return {
|
|
467
468
|
driver: userData.byDriver ? d.driver.name : a.driver,
|
|
468
469
|
speed: Math.round(a.attributes.speed * 1.85200),
|
|
469
|
-
distance: a.distance && new Intl.NumberFormat(lang, {maximumSignificantDigits: 1}).format(a.distance),
|
|
470
|
+
distance: a.distance && new Intl.NumberFormat(lang, { maximumSignificantDigits: 1 }).format(a.distance),
|
|
470
471
|
maxSpeed: a.attributes.maxSpeed ? Math.round(a.attributes.maxSpeed * 1.85200) : Math.round(a.attributes.speed * 1.85200),
|
|
471
|
-
duration:
|
|
472
|
+
duration: convertMS(a.eventTime, true),
|
|
472
473
|
fixTime: getAlertDate(a, userData.user),
|
|
473
474
|
name: userData.byDriver ? a.deviceName : d.device.name,
|
|
474
475
|
address: a.position.address + (a.geofenceName ? ' - ' + a.geofenceName : ''),
|
|
@@ -484,24 +485,24 @@ function exportSpeedingReportToExcel(userData, reportData) {
|
|
|
484
485
|
}
|
|
485
486
|
}
|
|
486
487
|
|
|
487
|
-
function deviceName(device){
|
|
488
|
-
return device.name + (device.attributes.license_plate ? ', ' +device.attributes.license_plate : '') + (device.model ? ', ' + device.model : '')
|
|
488
|
+
function deviceName (device) {
|
|
489
|
+
return device.name + (device.attributes.license_plate ? ', ' + device.attributes.license_plate : '') + (device.model ? ', ' + device.model : '')
|
|
489
490
|
}
|
|
490
491
|
|
|
491
|
-
function getAlertDate(row, user) {
|
|
492
|
+
function getAlertDate (row, user) {
|
|
492
493
|
return convertToLocaleString(row.position.fixTime, user.attributes.lang, user.attributes.timezone)
|
|
493
494
|
}
|
|
494
495
|
|
|
495
|
-
function getMaxSpeed(data) {
|
|
496
|
+
function getMaxSpeed (data) {
|
|
496
497
|
const values = data.map(item => {
|
|
497
498
|
return item.attributes.maxSpeed
|
|
498
499
|
})
|
|
499
|
-
const max = values.reduce(function(a, b) {
|
|
500
|
-
return Math.max(a, b)
|
|
501
|
-
})
|
|
500
|
+
const max = values.reduce(function (a, b) {
|
|
501
|
+
return Math.max(a, b)
|
|
502
|
+
})
|
|
502
503
|
return Math.round(max * 1.85200)
|
|
503
504
|
}
|
|
504
505
|
|
|
505
|
-
exports.createSpeedingReport = createSpeedingReport
|
|
506
|
-
exports.exportSpeedingReportToPDF = exportSpeedingReportToPDF
|
|
507
|
-
exports.exportSpeedingReportToExcel = exportSpeedingReportToExcel
|
|
506
|
+
exports.createSpeedingReport = createSpeedingReport
|
|
507
|
+
exports.exportSpeedingReportToPDF = exportSpeedingReportToPDF
|
|
508
|
+
exports.exportSpeedingReportToExcel = exportSpeedingReportToExcel
|
package/src/tests/index.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
const {SessionApi} = require(
|
|
2
|
-
const axiosCookieJarSupport = require('axios-cookiejar-support').default
|
|
3
|
-
const tough = require('tough-cookie')
|
|
4
|
-
const Index = require('../index')
|
|
5
|
-
const cookieJar = new tough.CookieJar()
|
|
1
|
+
const { SessionApi } = require('traccar-api')
|
|
2
|
+
const axiosCookieJarSupport = require('axios-cookiejar-support').default
|
|
3
|
+
const tough = require('tough-cookie')
|
|
4
|
+
const Index = require('../index')
|
|
5
|
+
const cookieJar = new tough.CookieJar()
|
|
6
6
|
const traccarConfig = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
basePath: 'https://api.pinme.io/api',
|
|
8
|
+
baseOptions: {
|
|
9
|
+
withCredentials: true,
|
|
10
|
+
jar: cookieJar
|
|
11
|
+
}
|
|
12
12
|
}
|
|
13
|
-
const axios = require('axios').create({...traccarConfig.baseOptions, baseURL: traccarConfig.basePath})
|
|
13
|
+
const axios = require('axios').create({ ...traccarConfig.baseOptions, baseURL: traccarConfig.basePath })
|
|
14
14
|
|
|
15
15
|
axiosCookieJarSupport(axios)
|
|
16
16
|
|
|
17
|
-
const getReports = async() => {
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
const getReports = async () => {
|
|
18
|
+
await new SessionApi(traccarConfig, null, axios).sessionPost(process.env.email, process.env.password)
|
|
19
|
+
return new Index(traccarConfig, axios, cookieJar)
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
exports.getReports = getReports
|
|
23
|
-
|
|
24
|
-
|