fleetmap-reports 1.0.385 → 1.0.388

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.385",
3
+ "version": "1.0.388",
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
  })
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,22 @@ 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
+ deviceCount = deviceCount + slice.length
70
+ }
68
71
 
69
72
  reportData.push(allData)
70
73
 
@@ -77,14 +80,14 @@ async function processDevices(from, to, devices, drivers, geofences, data, tracc
77
80
  for (const d of devices) {
78
81
  const alerts = data.filter(t => t.deviceId===d.id)
79
82
  if(alerts.length > 0) {
80
- const response = await traccar.reports.reportsRouteGet(from, to, [d.id])
83
+ //const response = await traccar.reports.reportsRouteGet(from, to, [d.id])
81
84
 
82
85
  const zoneInOutData = []
83
86
  const zoneInData = {}
84
- const positions = response.data
87
+ //const positions = response.data
85
88
 
86
89
  alerts.forEach(a => {
87
- a.position = positions.find(p => p.id === a.positionId)
90
+ //a.position = positions.find(p => p.id === a.positionId)
88
91
  if (a.position) {
89
92
  if (a.type === 'geofenceEnter') {
90
93
  const driver = drivers.find(d => a.position.attributes && d.uniqueId === a.position.attributes.driverUniqueId)
@@ -144,6 +147,47 @@ async function processDevices(from, to, devices, drivers, geofences, data, tracc
144
147
  return devicesResult
145
148
  }
146
149
 
150
+
151
+ function getInAndOutEvents(devices, route, userData) {
152
+ const events = []
153
+ devices.forEach(d => {
154
+ const deviceRoute = route.filter(p => p.deviceId === d.id)
155
+
156
+ const routePoints = deviceRoute.map(p => convertPositionToFeature(p))
157
+ const geofencesFeatures = userData.geofences.filter(g => g.area.startsWith('POLYGON')).map(g => convertToFeature(g))
158
+
159
+ const entryMap = []
160
+ routePoints.forEach(p => {
161
+ geofencesFeatures.forEach(g => {
162
+ if(turf.inside(p, g)) {
163
+ if(!entryMap.includes(g)) {
164
+ events.push({
165
+ type: "geofenceEnter",
166
+ position: p.properties.position,
167
+ geofenceId: g.properties.geofence.id,
168
+ deviceId: d.id
169
+ })
170
+ entryMap.push(g)
171
+ }
172
+ } else {
173
+ if(entryMap.includes(g)) {
174
+ events.push({
175
+ type: "geofenceExit",
176
+ position: p.properties.position,
177
+ geofenceId: g.properties.geofence.id,
178
+ deviceId: d.id
179
+ })
180
+ const index = entryMap.indexOf(g)
181
+ entryMap.splice(index, 1)
182
+ }
183
+ }
184
+ })
185
+ })
186
+ })
187
+
188
+ return events
189
+ }
190
+
147
191
  async function exportZoneReportToPDF(userData, reportData) {
148
192
  console.log('Export to PDF')
149
193