fleetmap-reports 1.0.384 → 1.0.387

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.384",
3
+ "version": "1.0.387",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.test.js CHANGED
@@ -324,4 +324,19 @@ describe('Test_Reports', function() {
324
324
  assert.equal(device.days.length, 25)
325
325
 
326
326
  }, 30000)
327
+ /* it('Zone Report', async () => {
328
+ const report = await getReports()
329
+ const userData = await report.getUserData()
330
+ const data = await report.zoneReport(new Date(2022, 3, 25, 0, 0, 0, 0),
331
+ new Date(2022, 3, 28, 23, 59, 59, 0),
332
+ userData)
333
+
334
+ assert.equal(data.length, 1)
335
+ const device = data[0].devices.find(d => d.device.id===22326)
336
+ assert.equal(device.geofences.length, 11)
337
+ assert.equal(device.geofences[0].inTime.fixTime, '2022-04-25T23:48:05.000+0000')
338
+ assert.equal(device.geofences[0].outTime.fixTime, '2022-04-26T07:47:35.000+0000')
339
+ assert.equal(device.geofences[0].totalTime, '28770')
340
+ assert.equal(device.geofences[0].geofenceName, 'Casa João')
341
+ }, 30000)*/
327
342
  })
@@ -46,7 +46,7 @@ async function createTripReportByDevice(from, to, userData, traccar) {
46
46
 
47
47
  let deviceCount = 0
48
48
  for(const slice of sliced) {
49
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, true, false)
49
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, true, false, deviceCount, devices.length)
50
50
  const tripsData = allInOne.trips
51
51
  const stopsData = allInOne.stops
52
52
  const routeData = allInOne.route
@@ -62,7 +62,6 @@ async function createTripReportByDevice(from, to, userData, traccar) {
62
62
  }, userData))
63
63
  }
64
64
  deviceCount = deviceCount + slice.length
65
- console.log(`PROGRESS_PERC:${deviceCount / devices.length * 100}`)
66
65
  }
67
66
 
68
67
  return allData
@@ -65,7 +65,7 @@ async function getSummary(traccar, from, to, devices){
65
65
  return result.map(r => r.data).flat()
66
66
  }
67
67
 
68
- async function getAllInOne(traccar, from, to, devices, getRoutes, getTrips, getStops, getSummary){
68
+ async function getAllInOne(traccar, from, to, devices, getRoutes, getTrips, getStops, getSummary, currentDeviceCount = 0, totalDevices = devices.length){
69
69
  let url = `/reports/allinone?&from=${from.toISOString()}&to=${to.toISOString()}`
70
70
  if(getRoutes) url += `&type=route`
71
71
  if(getTrips) url += `&type=trips`
@@ -74,6 +74,7 @@ async function getAllInOne(traccar, from, to, devices, getRoutes, getTrips, getS
74
74
 
75
75
  const sliced = automaticReports.sliceArray(devices, 5)
76
76
  const result = []
77
+ let deviceCount = currentDeviceCount
77
78
  for(const chunk of sliced) {
78
79
  const requests = chunk.map(d =>
79
80
  traccar.axios.get(url + '&' + `deviceId=${d.id}`, {
@@ -81,6 +82,9 @@ async function getAllInOne(traccar, from, to, devices, getRoutes, getTrips, getS
81
82
  withCredentials: true
82
83
  }).then(r => r.data).then(x => {
83
84
  console.log('LOADING_MESSAGE:' + d.name)
85
+
86
+ deviceCount = deviceCount + 1
87
+ console.log(`PROGRESS_PERC:${deviceCount / totalDevices * 100}`)
84
88
  return x
85
89
  }))
86
90
  result.push(...(await Promise.all(requests)))
package/src/util/utils.js CHANGED
@@ -36,6 +36,34 @@ function coordsDistance(lon1, lat1, lon2, lat2) {
36
36
  return (turf.distance(from, to, 'meters'))
37
37
  }
38
38
 
39
+ function convertPositionToFeature(position) {
40
+ const point = turf.point([position.longitude,position.latitude])
41
+ point.properties.position = position
42
+ return point
43
+ }
44
+
45
+ function convertToFeature(geofence) {
46
+ if(geofence.area.startsWith('POLYGON')) {
47
+ const feature = {
48
+ "type": "Feature",
49
+ "properties": {
50
+ "geofence": geofence
51
+ },
52
+ "geometry": {
53
+ "type": "Polygon",
54
+ "coordinates": [[]]
55
+ }
56
+ }
57
+ const str = geofence.area.substring('POLYGON(('.length, geofence.area.length - 2)
58
+ const coord_list = str.split(',')
59
+ for (const i in coord_list) {
60
+ const coord = coord_list[i].trim().split(' ')
61
+ feature.geometry.coordinates[0].push([parseFloat(coord[1]), parseFloat(coord[0])])
62
+ }
63
+ return feature
64
+ }
65
+ }
66
+
39
67
  function getDates(startDate, endDate) {
40
68
  const dates = []
41
69
  let currentDate = new Date(startDate.toISOString().split('T')[0] + ' 12:00:00.000 PM')
@@ -155,3 +183,5 @@ exports.getLogo = (user) => {
155
183
  const host = window ? window.location.hostname : getUserPartner(user).host
156
184
  return getImage(getLogoUrl(host))
157
185
  }
186
+ exports.convertToFeature = convertToFeature
187
+ exports.convertPositionToFeature = convertPositionToFeature
@@ -1,12 +1,14 @@
1
1
  const moment = require('moment')
2
- const automaticReports = require("./automaticReports");
3
- const {convertMS, convertToLocaleString, getTranslations} = require("./util/utils")
2
+ const automaticReports = require("./automaticReports")
3
+ const {convertMS, convertToLocaleString, getTranslations, convertToFeature, convertPositionToFeature} = require("./util/utils")
4
4
  const jsPDF = require('jspdf')
5
5
  require('jspdf-autotable')
6
- const {headerFromUser} = require("./util/pdfDocument");
6
+ const {headerFromUser} = require("./util/pdfDocument")
7
7
  const {getStyle} = require("./reportStyle")
8
- const {getUserPartner} = require("fleetmap-partners");
9
- const {devicesToProcess} = require("./util/device");
8
+ const {getUserPartner} = require("fleetmap-partners")
9
+ const {devicesToProcess} = require("./util/device")
10
+ const traccarHelper = require("./util/traccar")
11
+ const turf = require('turf')
10
12
 
11
13
  const fileName = 'ZoneReport'
12
14
 
@@ -50,21 +52,21 @@ async function createZoneReport(from, to, userData, traccar) {
50
52
  xpert: devices.filter(d => d.attributes.xpert).length > 0
51
53
  }
52
54
 
55
+ const sliced = automaticReports.sliceArray(devices, 5)
53
56
 
54
- const arrayOfArrays = automaticReports.sliceArray(devices)
55
- let data = []
56
- for (const a of arrayOfArrays) {
57
- const response = await traccar.reports.reportsEventsGet(from, to, a.map(d => d.id), null, types)
58
- data = data.concat(response.data)
59
- }
57
+ let deviceCount = 0
58
+ for(const slice of sliced) {
59
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, false, false, false, deviceCount, devices.length)
60
+ const routeData = allInOne.route
60
61
 
61
- console.log('Geofence Enter/Exit Alerts:'+data.length)
62
+ const data = getInAndOutEvents(slice, routeData, userData)
62
63
 
63
- if(data.length === 0) {
64
- return reportData
65
- }
64
+ console.log('Geofence Enter/Exit Alerts:' + data.length)
66
65
 
67
- allData.devices = await processDevices(from, to, devices, userData.drivers, userData.geofences, data, traccar)
66
+ if (data.length) {
67
+ allData.devices.push(...await processDevices(from, to, devices, userData.drivers, userData.geofences, data, traccar))
68
+ }
69
+ }
68
70
 
69
71
  reportData.push(allData)
70
72
 
@@ -77,14 +79,14 @@ async function processDevices(from, to, devices, drivers, geofences, data, tracc
77
79
  for (const d of devices) {
78
80
  const alerts = data.filter(t => t.deviceId===d.id)
79
81
  if(alerts.length > 0) {
80
- const response = await traccar.reports.reportsRouteGet(from, to, [d.id])
82
+ //const response = await traccar.reports.reportsRouteGet(from, to, [d.id])
81
83
 
82
84
  const zoneInOutData = []
83
85
  const zoneInData = {}
84
- const positions = response.data
86
+ //const positions = response.data
85
87
 
86
88
  alerts.forEach(a => {
87
- a.position = positions.find(p => p.id === a.positionId)
89
+ //a.position = positions.find(p => p.id === a.positionId)
88
90
  if (a.position) {
89
91
  if (a.type === 'geofenceEnter') {
90
92
  const driver = drivers.find(d => a.position.attributes && d.uniqueId === a.position.attributes.driverUniqueId)
@@ -144,6 +146,47 @@ async function processDevices(from, to, devices, drivers, geofences, data, tracc
144
146
  return devicesResult
145
147
  }
146
148
 
149
+
150
+ function getInAndOutEvents(devices, route, userData) {
151
+ const events = []
152
+ devices.forEach(d => {
153
+ const deviceRoute = route.filter(p => p.deviceId === d.id)
154
+
155
+ const routePoints = deviceRoute.map(p => convertPositionToFeature(p))
156
+ const geofencesFeatures = userData.geofences.filter(g => g.area.startsWith('POLYGON')).map(g => convertToFeature(g))
157
+
158
+ const entryMap = []
159
+ routePoints.forEach(p => {
160
+ geofencesFeatures.forEach(g => {
161
+ if(turf.inside(p, g)) {
162
+ if(!entryMap.includes(g)) {
163
+ events.push({
164
+ type: "geofenceEnter",
165
+ position: p.properties.position,
166
+ geofenceId: g.properties.geofence.id,
167
+ deviceId: d.id
168
+ })
169
+ entryMap.push(g)
170
+ }
171
+ } else {
172
+ if(entryMap.includes(g)) {
173
+ events.push({
174
+ type: "geofenceExit",
175
+ position: p.properties.position,
176
+ geofenceId: g.properties.geofence.id,
177
+ deviceId: d.id
178
+ })
179
+ const index = entryMap.indexOf(g)
180
+ entryMap.splice(index, 1)
181
+ }
182
+ }
183
+ })
184
+ })
185
+ })
186
+
187
+ return events
188
+ }
189
+
147
190
  async function exportZoneReportToPDF(userData, reportData) {
148
191
  console.log('Export to PDF')
149
192