-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddd.js
More file actions
407 lines (377 loc) · 17.2 KB
/
Copy pathddd.js
File metadata and controls
407 lines (377 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Pure DDD/VU normalization - turns raw flespi tacho-file-parse JSON into the
// normalized record shape the compliance engine and UI consume. Framework-free
// (no Vue/Pinia); the Pinia store (src/stores/ddd.js) imports from here and owns
// only the reactive state/orchestration.
function resolveRegistration(c) {
const regObj = c.VehicleRegistrationIdentification?.[0]
if (regObj) return regObj
const num = c.VehicleRegistrationNumber?.[0]
if (num) return { vehicleRegistrationNumber: num, vehicleRegistrationNation: '' }
return {}
}
function normalizeVuDaily(items) {
const activityDailyRecords = items.map((item) => {
const c = item.content
const currentDateTime = c.CurrentDateTime?.[0] || 0
const dayTs = currentDateTime - (currentDateTime % 86400)
return {
activityRecordDate: dayTs,
activityChangeInfo: c.ActivityChangeInfo || [],
activityDayDistance: 0,
}
})
const firstMeta = items[0].meta || {}
const companyLocksMap = new Map()
const downloadActivityMap = new Map()
for (const item of items) {
const c = item.content
for (const lock of (c.VuCompanyLocksData || [])) {
const key = `${lock.lockInTime}:${lock.companyName}`
if (!companyLocksMap.has(key)) companyLocksMap.set(key, lock)
}
for (const dl of (c.VuDownloadActivityData || [])) {
const key = `${dl.downloadingTime}:${dl.companyOrWorkshopName}`
if (!downloadActivityMap.has(key)) downloadActivityMap.set(key, dl)
}
}
return {
EF_Driver_Activity_Data: {
CardDriverActivity: { activityDailyRecords },
},
EF_Identification: {
CardIdentification: {
cardNumber: firstMeta.plate_number || '',
cardIssuingMemberState: firstMeta.region || '',
},
DriverCardHolderIdentification: {
cardHolderName: {
holderSurname: firstMeta.vin || firstMeta.plate_number || 'Vehicle Unit',
holderFirstNames: '',
},
},
},
EF_Vehicles_Used: {
cardVehicleRecords: items.map((item) => {
const c = item.content
const ts = c.CurrentDateTime?.[0] || 0
const dayTs = ts - (ts % 86400)
return {
vehicleRegistration: resolveRegistration(c),
vehicleFirstUse: dayTs,
vehicleLastUse: dayTs + 86399,
vehicleOdometerBegin: c.OdometerValueMidnight?.[0] ?? 0,
vehicleOdometerEnd: c.OdometerValueMidnight?.[0] ?? 0,
}
}),
},
VU_Company_Locks: [...companyLocksMap.values()],
VU_Download_Activity: [...downloadActivityMap.values()],
}
}
// Vehicle-unit (mass memory) download. Unlike a driver card, where the days sit
// in one contiguous structure, a VU file carries them as separate per-day blocks
// under VuActivities - each with its own activity list, date, odometer and
// per-day sub-records. Note the per-day fields are scalars here, not the
// single-element arrays used at the top level of the file.
function normalizeVuGen2(content, meta = {}) {
const days = [...(content.VuActivities || [])]
.filter((d) => d && d.DateOfDayDownloaded)
.sort((a, b) => a.DateOfDayDownloaded - b.DateOfDayDownloaded)
// Distance per day is the gap between consecutive midnight odometer readings.
// The last day has no following reading, so it stays 0 rather than guessing.
const activityDailyRecords = days.map((d, i) => {
const odo = d.OdometerValueMidnight ?? 0
const nextOdo = days[i + 1]?.OdometerValueMidnight
return {
activityRecordDate: d.DateOfDayDownloaded,
activityChangeInfo: d.ActivityChangeInfo || [],
activityDayDistance: odo && nextOdo != null ? Math.max(0, nextOdo - odo) : 0,
}
})
const collect = (key) => days.flatMap((d) => d[key] || [])
// A VU file names the same records differently from a driver card. Places wrap
// the fields in `placeRecord`, and the GNSS position sits under
// `GNSSPlaceAuthRecord` where a card calls it `gnssPlaceRecord`. Map both to
// the card shape so the UI and the compliance engine stay card-agnostic.
// Written tolerantly: a record already in the card shape passes through.
const unwrapPlace = (r) => ({ ...(r.placeRecord || r) })
// The GNSS block is called GNSSPlaceAuthRecord in a VU file. A card calls it
// gnssPlaceRecord in GNSS records but gnssPlaceAuthRecord in border crossings,
// so the target name differs per record type. Load/unload records already use
// GNSSPlaceAuthRecord on both sides and need no remapping.
const renameGnss = (target) => (r) => {
const place = r[target] || r.GNSSPlaceAuthRecord || r.gnssPlaceRecord
return place ? { ...r, [target]: place } : r
}
const first = days[0]
const last = days[days.length - 1]
const vin = meta.vin || content.VehicleIdentificationNumber?.[0] || ''
return {
EF_Driver_Activity_Data: {
CardDriverActivity: { activityDailyRecords },
},
EF_Identification: {
CardIdentification: {
cardNumber: meta.plate_number || resolveRegistration(content).vehicleRegistrationNumber || '',
cardIssuingMemberState: meta.region || resolveRegistration(content).vehicleRegistrationNation || '',
},
DriverCardHolderIdentification: {
cardHolderName: {
holderSurname: vin || meta.plate_number || 'Vehicle Unit',
holderFirstNames: '',
},
},
},
// One vehicle for the whole download, spanning the days it covers, rather
// than one pseudo-vehicle per day as the previous VU handling produced.
EF_Vehicles_Used: {
cardVehicleRecords: first ? [{
vehicleRegistration: resolveRegistration(content),
vehicleFirstUse: first.DateOfDayDownloaded,
vehicleLastUse: last.DateOfDayDownloaded + 86399,
vehicleOdometerBegin: first.OdometerValueMidnight ?? 0,
vehicleOdometerEnd: last.OdometerValueMidnight ?? 0,
}] : [],
},
EF_Places: { placeRecords: collect('VuPlaceDailyWorkPeriodData').map(unwrapPlace) },
EF_GNSS_Places: { gnssAccumulatedDrivingRecords: collect('VuGNSSADRecords').map(renameGnss('gnssPlaceRecord')) },
EF_Specific_Conditions: { specificConditionRecords: collect('VuSpecificConditionData') },
EF_Border_Crossings: { cardBorderCrossingRecords: collect('VuBorderCrossingRecords').map(renameGnss('gnssPlaceAuthRecord')) },
EF_Load_Unload_Operations: { cardLoadUnloadRecords: collect('VuLoadUnloadRecords') },
EF_Control_Activity_Data: content.VuControlActivityData?.[0] || null,
VU_Company_Locks: content.VuCompanyLocksData || [],
VU_Download_Activity: content.VuDownloadActivityData || [],
}
}
function normalizeVuTechnical(items) {
const firstMeta = items[0].meta || {}
const first = items[0].content || {}
const companyLocksMap = new Map()
const downloadActivityMap = new Map()
for (const item of items) {
const c = item.content
for (const lock of (c.VuCompanyLocksData || [])) {
const key = `${lock.lockInTime}:${lock.companyName}`
if (!companyLocksMap.has(key)) companyLocksMap.set(key, lock)
}
for (const dl of (c.VuDownloadActivityData || [])) {
const key = `${dl.downloadingTime}:${dl.companyOrWorkshopName}`
if (!downloadActivityMap.has(key)) downloadActivityMap.set(key, dl)
}
}
return {
EF_Driver_Activity_Data: {
CardDriverActivity: { activityDailyRecords: [] },
},
EF_Identification: {
CardIdentification: {
cardNumber: firstMeta.plate_number || '',
cardIssuingMemberState: firstMeta.region || '',
},
DriverCardHolderIdentification: {
cardHolderName: {
holderSurname: firstMeta.vin || firstMeta.plate_number || 'Vehicle Unit',
holderFirstNames: '',
},
},
},
EF_Vehicles_Used: {
cardVehicleRecords: [{
vehicleRegistration: resolveRegistration(first),
vehicleFirstUse: first.CurrentDateTime?.[0] || 0,
vehicleLastUse: first.CurrentDateTime?.[0] || 0,
vehicleOdometerBegin: 0,
vehicleOdometerEnd: 0,
}],
},
VU_Company_Locks: [...companyLocksMap.values()],
VU_Download_Activity: [...downloadActivityMap.values()],
}
}
export function detectAndNormalize(json) {
if (json.result && Array.isArray(json.result)) {
const items = json.result
if (!items.length) return null
const first = items[0]
const content = first.content
if (content?.DF_Tachograph || content?.DF_Tachograph_G2) {
const cardNumber = content.DF_Tachograph?.EF_Identification?.CardIdentification?.cardNumber
|| content.DF_Tachograph_G2?.EF_Identification?.CardIdentification?.cardNumber || ''
const downloadTs = content.DF_Tachograph?.EF_Card_Download?.LastCardDownload || 0
return {
type: 'driver-card',
uuid: first.uuid || null,
key: `card:${cardNumber}`,
downloadTs,
meta: first.meta || {},
name: first.name || '',
g1: content.DF_Tachograph || null,
g2: content.DF_Tachograph_G2 || null,
enabled: true,
}
}
// Current VU (mass memory) layout: one item holding every day under
// VuActivities. Must be checked before the technical-only branch below,
// which would otherwise swallow the file and report zero activity.
if (content?.VuActivities?.length) {
const vin = first.meta?.vin || content.VehicleIdentificationNumber?.[0] || ''
const plate = first.meta?.plate_number || resolveRegistration(content).vehicleRegistrationNumber || ''
return {
type: 'vu-daily',
uuid: first.uuid || null,
key: `vu:${vin || plate}`,
downloadTs: content.CurrentDateTime?.[0] || 0,
meta: first.meta || {},
name: first.name || '',
g1: normalizeVuGen2(content, first.meta || {}),
g2: null,
enabled: true,
}
}
// Legacy VU layout: one item per day, activity directly under content.
// Kept for JSON files saved from earlier versions of the app.
if (content?.ActivityChangeInfo?.length) {
const vin = first.meta?.vin || content.VehicleIdentificationNumber?.[0] || ''
const plate = first.meta?.plate_number || content.VehicleRegistrationIdentification?.[0]?.vehicleRegistrationNumber || content.VehicleRegistrationNumber?.[0] || ''
return {
type: 'vu-daily',
uuid: first.uuid || null,
key: `vu:${vin || plate}`,
downloadTs: content.CurrentDateTime?.[0] || 0,
meta: first.meta || {},
name: first.name || '',
g1: normalizeVuDaily(items),
g2: null,
enabled: true,
}
}
if (content?.VehicleIdentificationNumber || content?.VuCompanyLocksData || content?.VuDownloadActivityData) {
const vin = first.meta?.vin || content.VehicleIdentificationNumber?.[0] || ''
const plate = first.meta?.plate_number || resolveRegistration(content).vehicleRegistrationNumber || ''
return {
type: 'vu-daily',
uuid: first.uuid || null,
key: `vu:${vin || plate}`,
downloadTs: content.CurrentDateTime?.[0] || 0,
meta: first.meta || {},
name: first.name || '',
g1: normalizeVuTechnical(items),
g2: null,
enabled: true,
warning: 'VU file contains no driver activity data — only technical/admin data loaded',
}
}
}
if (json.EF_Application_Identification || json.EF_Driver_Activity_Data) {
const cardNumber = json.EF_Identification?.CardIdentification?.cardNumber || ''
const downloadTs = json.EF_Card_Download?.LastCardDownload || 0
return {
type: 'driver-card',
uuid: null,
key: `card:${cardNumber || 'local'}`,
downloadTs,
meta: {},
name: '',
g1: json,
g2: null,
enabled: true,
}
}
return null
}
export function extractRecords(data) {
const empty = { activityRecords: [], vehicleRecords: [], placeRecords: [], eventRecords: [], faultRecords: [], conditionRecords: [], gnssRecords: [], vehicleUnitsUsed: [], companyLocksRecords: [], downloadActivityRecords: [], borderCrossingRecords: [], loadUnloadRecords: [], loadTypeRecords: [], controlActivityRecords: [], gnssAuthRecords: [], placesAuthRecords: [] }
if (!data) return empty
return {
activityRecords: data.EF_Driver_Activity_Data?.CardDriverActivity?.activityDailyRecords || [],
vehicleRecords: data.EF_Vehicles_Used?.cardVehicleRecords || [],
placeRecords: data.EF_Places?.placeRecords || [],
eventRecords: data.EF_Events_Data?.CardEventData?.cardEventRecords || [],
faultRecords: data.EF_Faults_Data?.CardFaultData?.cardFaultRecords || [],
conditionRecords: data.EF_Specific_Conditions?.specificConditionRecords || [],
gnssRecords: data.EF_GNSS_Places?.gnssAccumulatedDrivingRecords || [],
vehicleUnitsUsed: data.EF_Vehicle_Units_Used?.cardVehicleUnitRecords || [],
companyLocksRecords: data.VU_Company_Locks || [],
downloadActivityRecords: data.VU_Download_Activity || [],
borderCrossingRecords: data.EF_Border_Crossings?.cardBorderCrossingRecords || [],
loadUnloadRecords: data.EF_Load_Unload_Operations?.cardLoadUnloadRecords || [],
loadTypeRecords: data.EF_Load_Type_Entries?.cardLoadTypeEntryRecords || [],
controlActivityRecords: data.EF_Control_Activity_Data ? [data.EF_Control_Activity_Data].filter((r) => r.controlTime > 0) : [],
gnssAuthRecords: data.EF_GNSS_Places_Authentication?.gnssAuthStatusADRecords || [],
placesAuthRecords: data.EF_Places_Authentication?.placeAuthStatusRecords || [],
}
}
function dedup(arr, keyFn) {
const seen = new Map()
for (const item of arr) {
const k = keyFn(item)
if (!seen.has(k)) seen.set(k, item)
}
return [...seen.values()]
}
// Dedup keeping, per key, the item ranked best by `better` (a < b -> a wins).
// Used where two sources can carry the same key with different richness and the
// first-wins of dedup() would silently drop the better record.
function dedupBest(arr, keyFn, better) {
const seen = new Map()
for (const item of arr) {
const k = keyFn(item)
const prev = seen.get(k)
if (!prev || better(item, prev) < 0) seen.set(k, item)
}
return [...seen.values()]
}
export function mergeRecordSets(sources, gen) {
const all = { activityRecords: [], vehicleRecords: [], placeRecords: [], eventRecords: [], faultRecords: [], conditionRecords: [], gnssRecords: [], vehicleUnitsUsed: [], companyLocksRecords: [], downloadActivityRecords: [], borderCrossingRecords: [], loadUnloadRecords: [], loadTypeRecords: [], controlActivityRecords: [], gnssAuthRecords: [], placesAuthRecords: [] }
for (const src of sources) {
const data = gen === 'g2' && src.g2 ? src.g2 : src.g1
const r = extractRecords(data)
all.activityRecords.push(...r.activityRecords)
all.vehicleRecords.push(...r.vehicleRecords)
all.placeRecords.push(...r.placeRecords)
all.eventRecords.push(...r.eventRecords)
all.faultRecords.push(...r.faultRecords)
all.conditionRecords.push(...r.conditionRecords)
all.gnssRecords.push(...r.gnssRecords)
all.vehicleUnitsUsed.push(...r.vehicleUnitsUsed)
all.companyLocksRecords.push(...r.companyLocksRecords)
all.downloadActivityRecords.push(...r.downloadActivityRecords)
all.borderCrossingRecords.push(...r.borderCrossingRecords)
all.loadUnloadRecords.push(...r.loadUnloadRecords)
all.loadTypeRecords.push(...r.loadTypeRecords)
all.controlActivityRecords.push(...r.controlActivityRecords)
all.gnssAuthRecords.push(...r.gnssAuthRecords)
all.placesAuthRecords.push(...r.placesAuthRecords)
}
// Activity is keyed on the calendar day, but two sources (e.g. driver card + VU,
// or overlapping downloads) can both carry that day with different content -
// keep the richer record (more activity changes) rather than first-wins, so a
// day's driving isn't silently dropped before compliance runs.
all.activityRecords = dedupBest(
all.activityRecords,
(r) => r.activityRecordDate,
(a, b) => (b.activityChangeInfo?.length || 0) - (a.activityChangeInfo?.length || 0),
)
all.gnssRecords = dedup(all.gnssRecords, (r) => r.gnssPlaceRecord?.timeStamp || r.timeStamp)
all.conditionRecords = dedup(all.conditionRecords, (r) => `${r.entryTime}:${r.specificConditionType}`)
all.eventRecords = dedup(all.eventRecords, (r) => `${r.eventBeginTime}:${r.eventType}`)
all.faultRecords = dedup(all.faultRecords, (r) => `${r.faultBeginTime}:${r.faultType}`)
all.placeRecords = dedup(all.placeRecords, (r) => `${r.entryTime}:${r.entryTypeDailyWorkPeriod}`)
all.vehicleRecords = dedup(all.vehicleRecords, (r) => `${r.vehicleFirstUse}:${r.vehicleLastUse}`)
all.companyLocksRecords = dedup(all.companyLocksRecords, (r) => `${r.lockInTime}:${r.companyName}`)
all.downloadActivityRecords = dedup(all.downloadActivityRecords, (r) => `${r.downloadingTime}:${r.companyOrWorkshopName}`)
all.borderCrossingRecords = dedup(all.borderCrossingRecords, (r) => `${r.gnssPlaceAuthRecord?.timeStamp}`)
all.loadUnloadRecords = dedup(all.loadUnloadRecords, (r) => `${r.timeStamp}:${r.operationType}`)
all.loadTypeRecords = dedup(all.loadTypeRecords, (r) => `${r.timeStamp}:${r.loadTypeEntered}`)
return all
}
export function isCompatible(src, enabledSources) {
if (!enabledSources.length) return true
for (const existing of enabledSources) {
// Don't mix VU and driver card sources
if (src.type !== existing.type) return false
if (src.type === 'driver-card' && src.key !== existing.key) return false
if (src.type === 'vu-daily' && src.key !== existing.key) return false
}
return true
}