fleetmap-reports 1.0.386 → 1.0.389

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.386",
3
+ "version": "1.0.389",
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,45 @@ 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
+ if(geofence.area.startsWith('CIRCLE')) {
67
+ const str = geofence.area.substring('CIRCLE(('.length, geofence.area.length - 2)
68
+ const coord_list = str.split(',')
69
+ const coord = coord_list[0].trim().split(' ')
70
+ const point = turf.point([parseFloat(coord[1]), parseFloat(coord[0])])
71
+ point.properties.geofence = geofence
72
+ point.properties.distance = coord_list[1].trim()
73
+ console.log(point)
74
+ return point
75
+ }
76
+ }
77
+
39
78
  function getDates(startDate, endDate) {
40
79
  const dates = []
41
80
  let currentDate = new Date(startDate.toISOString().split('T')[0] + ' 12:00:00.000 PM')
@@ -155,3 +194,5 @@ exports.getLogo = (user) => {
155
194
  const host = window ? window.location.hostname : getUserPartner(user).host
156
195
  return getImage(getLogoUrl(host))
157
196
  }
197
+ exports.convertToFeature = convertToFeature
198
+ 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, 100)
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,73 @@ 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.map(g => convertToFeature(g))
158
+
159
+ const entryMap = []
160
+ routePoints.forEach(p => {
161
+ geofencesFeatures.forEach(g => {
162
+ if(g.geometry.type === 'Polygon') {
163
+ if (turf.inside(p, g)) {
164
+ if (!entryMap.includes(g)) {
165
+ events.push({
166
+ type: "geofenceEnter",
167
+ position: p.properties.position,
168
+ geofenceId: g.properties.geofence.id,
169
+ deviceId: d.id
170
+ })
171
+ entryMap.push(g)
172
+ }
173
+ } else {
174
+ if (entryMap.includes(g)) {
175
+ events.push({
176
+ type: "geofenceExit",
177
+ position: p.properties.position,
178
+ geofenceId: g.properties.geofence.id,
179
+ deviceId: d.id
180
+ })
181
+ const index = entryMap.indexOf(g)
182
+ entryMap.splice(index, 1)
183
+ }
184
+ }
185
+ }
186
+ if(g.geometry.type === 'Point') {
187
+ if (turf.distance(p, g, 'meters') < g.properties.distance) {
188
+ if (!entryMap.includes(g)) {
189
+ events.push({
190
+ type: "geofenceEnter",
191
+ position: p.properties.position,
192
+ geofenceId: g.properties.geofence.id,
193
+ deviceId: d.id
194
+ })
195
+ entryMap.push(g)
196
+ }
197
+ } else {
198
+ if (entryMap.includes(g)) {
199
+ events.push({
200
+ type: "geofenceExit",
201
+ position: p.properties.position,
202
+ geofenceId: g.properties.geofence.id,
203
+ deviceId: d.id
204
+ })
205
+ const index = entryMap.indexOf(g)
206
+ entryMap.splice(index, 1)
207
+ }
208
+ }
209
+ }
210
+ })
211
+ })
212
+ })
213
+
214
+ return events
215
+ }
216
+
147
217
  async function exportZoneReportToPDF(userData, reportData) {
148
218
  console.log('Export to PDF')
149
219