@fleetbase/fleetops-engine 0.6.55 → 0.6.57
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/addon/components/admin/map-settings.hbs +1 -1
- package/addon/components/ai/create-order-preview.hbs +297 -0
- package/addon/components/ai/create-order-preview.js +362 -0
- package/addon/components/ai/route-preview-map.hbs +79 -0
- package/addon/components/ai/route-preview-map.js +527 -0
- package/addon/components/device/details.js +1 -1
- package/addon/components/device-event/details.hbs +192 -0
- package/addon/components/device-event/details.js +151 -0
- package/addon/components/driver/schedule.js +1 -1
- package/addon/components/fleet/details.hbs +21 -8
- package/addon/components/fleet/details.js +55 -1
- package/addon/components/fleet/hierarchy-tree.hbs +137 -0
- package/addon/components/fleet/hierarchy-tree.js +528 -0
- package/addon/components/layout/fleet-ops-sidebar.js +15 -9
- package/addon/components/map/drawer/device-event-listing.js +1 -0
- package/addon/components/map/drawer/driver-listing.js +1 -0
- package/addon/components/map/drawer/place-listing.js +1 -0
- package/addon/components/map/drawer/vehicle-listing.js +1 -0
- package/addon/components/order/details.hbs +34 -1
- package/addon/components/order/form/details.hbs +4 -0
- package/addon/components/order/form.hbs +9 -1
- package/addon/components/telematic/details.hbs +1 -1
- package/addon/components/telematic/form.hbs +2 -2
- package/addon/components/telematic/settings.hbs +1 -1
- package/addon/components/vehicle/card.hbs +14 -2
- package/addon/components/vehicle/form.hbs +13 -1
- package/addon/controllers/connectivity/devices/index/details/events.js +1 -1
- package/addon/controllers/connectivity/devices/index/details/vehicle.js +2 -2
- package/addon/controllers/connectivity/events/details.js +68 -1
- package/addon/controllers/connectivity/sensors/index.js +4 -0
- package/addon/controllers/connectivity/telematics/details/events.js +1 -1
- package/addon/controllers/connectivity/telematics/details/sensors.js +4 -0
- package/addon/controllers/settings/map.js +19 -0
- package/addon/extension.js +14 -0
- package/addon/instance-initializers/register-vroom-allocation.js +1 -1
- package/addon/routes/connectivity/events/details.js +26 -1
- package/addon/routes/management/fleets/index/details.js +1 -1
- package/addon/routes/management/vehicles/index/details.js +3 -1
- package/addon/services/map-adapter/google.js +66 -1
- package/addon/services/map-adapter/leaflet.js +1 -1
- package/addon/services/map-adapter-interface.js +1 -1
- package/addon/services/map-manager.js +16 -1
- package/addon/services/map-settings.js +15 -0
- package/addon/styles/fleetops-engine.css +487 -1
- package/addon/templates/connectivity/devices/index/details/vehicle.hbs +2 -1
- package/addon/templates/connectivity/events/details.hbs +40 -1
- package/addon/templates/settings/map.hbs +38 -9
- package/addon/utils/leaflet-plugin-loader.js +5 -5
- package/addon/utils/map-drawer-dropdown-position.js +11 -13
- package/addon/utils/route-colors.js +1 -1
- package/app/components/ai/create-order-preview.js +1 -0
- package/app/components/ai/route-preview-map.js +1 -0
- package/app/components/device-event/details.hbs +192 -0
- package/app/components/device-event/details.js +1 -0
- package/app/components/fleet/hierarchy-tree.js +1 -0
- package/app/services/route-engine.js +1 -0
- package/composer.json +1 -1
- package/extension.json +1 -1
- package/package.json +6 -4
- package/scripts/normalize-rgba-alpha.js +90 -0
- package/server/migrations/2026_06_23_000001_delete_bad_afaqy_telematics_sensors.php +34 -0
- package/server/src/Exports/ContactExport.php +12 -5
- package/server/src/Exports/DeviceExport.php +92 -0
- package/server/src/Exports/DriverExport.php +28 -3
- package/server/src/Exports/EquipmentExport.php +94 -0
- package/server/src/Exports/FleetExport.php +22 -7
- package/server/src/Exports/FuelReportExport.php +16 -1
- package/server/src/Exports/IssueExport.php +25 -3
- package/server/src/Exports/MaintenanceExport.php +106 -0
- package/server/src/Exports/MaintenanceScheduleExport.php +99 -0
- package/server/src/Exports/OrderExport.php +24 -5
- package/server/src/Exports/PartExport.php +99 -0
- package/server/src/Exports/PlaceExport.php +33 -7
- package/server/src/Exports/SensorExport.php +94 -0
- package/server/src/Exports/ServiceAreaExport.php +31 -7
- package/server/src/Exports/ServiceRateExport.php +41 -1
- package/server/src/Exports/TelematicExport.php +90 -0
- package/server/src/Exports/VehicleExport.php +274 -8
- package/server/src/Exports/VendorExport.php +15 -2
- package/server/src/Exports/WorkOrderExport.php +92 -0
- package/server/src/Http/Controllers/Api/v1/Concerns/ResolvesFleetOpsApiResources.php +176 -0
- package/server/src/Http/Controllers/Api/v1/DeviceController.php +225 -0
- package/server/src/Http/Controllers/Api/v1/EquipmentController.php +114 -0
- package/server/src/Http/Controllers/Api/v1/FuelTransactionController.php +185 -0
- package/server/src/Http/Controllers/Api/v1/PartController.php +120 -0
- package/server/src/Http/Controllers/Api/v1/SensorController.php +132 -0
- package/server/src/Http/Controllers/Api/v1/WorkOrderController.php +161 -0
- package/server/src/Http/Controllers/Internal/v1/DeviceController.php +18 -0
- package/server/src/Http/Controllers/Internal/v1/EquipmentController.php +17 -0
- package/server/src/Http/Controllers/Internal/v1/MaintenanceController.php +17 -0
- package/server/src/Http/Controllers/Internal/v1/MaintenanceScheduleController.php +17 -0
- package/server/src/Http/Controllers/Internal/v1/PartController.php +17 -0
- package/server/src/Http/Controllers/Internal/v1/SensorController.php +18 -0
- package/server/src/Http/Controllers/Internal/v1/SettingController.php +29 -5
- package/server/src/Http/Controllers/Internal/v1/TelematicController.php +18 -0
- package/server/src/Http/Controllers/Internal/v1/WorkOrderController.php +17 -0
- package/server/src/Http/Filter/DeviceFilter.php +33 -10
- package/server/src/Http/Filter/EquipmentFilter.php +23 -0
- package/server/src/Http/Filter/FuelProviderTransactionFilter.php +57 -2
- package/server/src/Http/Filter/PartFilter.php +47 -0
- package/server/src/Http/Filter/SensorFilter.php +34 -6
- package/server/src/Http/Filter/WorkOrderFilter.php +23 -0
- package/server/src/Http/Requests/CreateDeviceRequest.php +51 -0
- package/server/src/Http/Requests/CreateEquipmentRequest.php +35 -0
- package/server/src/Http/Requests/CreateFuelTransactionRequest.php +50 -0
- package/server/src/Http/Requests/CreatePartRequest.php +40 -0
- package/server/src/Http/Requests/CreateSensorRequest.php +47 -0
- package/server/src/Http/Requests/CreateWorkOrderRequest.php +42 -0
- package/server/src/Http/Requests/UpdateDeviceRequest.php +7 -0
- package/server/src/Http/Requests/UpdateEquipmentRequest.php +7 -0
- package/server/src/Http/Requests/UpdateFuelTransactionRequest.php +7 -0
- package/server/src/Http/Requests/UpdatePartRequest.php +7 -0
- package/server/src/Http/Requests/UpdateSensorRequest.php +7 -0
- package/server/src/Http/Requests/UpdateWorkOrderRequest.php +7 -0
- package/server/src/Http/Resources/v1/Device.php +72 -0
- package/server/src/Http/Resources/v1/Equipment.php +57 -0
- package/server/src/Http/Resources/v1/Fleet.php +8 -2
- package/server/src/Http/Resources/v1/FuelTransaction.php +69 -0
- package/server/src/Http/Resources/v1/Part.php +64 -0
- package/server/src/Http/Resources/v1/Sensor.php +68 -0
- package/server/src/Jobs/SyncTelematicDevicesJob.php +194 -38
- package/server/src/Models/Driver.php +6 -0
- package/server/src/Models/Order.php +20 -7
- package/server/src/Models/Place.php +11 -3
- package/server/src/Observers/PurchaseRateObserver.php +2 -1
- package/server/src/Providers/FleetOpsServiceProvider.php +26 -0
- package/server/src/Support/Ai/Capabilities/AbstractFleetOpsAICapability.php +88 -0
- package/server/src/Support/Ai/Capabilities/AssetStatusCapability.php +108 -0
- package/server/src/Support/Ai/Capabilities/ConsoleNavigationCapability.php +55 -0
- package/server/src/Support/Ai/Capabilities/CreateOrderPreviewCapability.php +508 -0
- package/server/src/Support/Ai/Capabilities/DocsHelpCapability.php +49 -0
- package/server/src/Support/Ai/Capabilities/ImportOrdersPreviewCapability.php +66 -0
- package/server/src/Support/Ai/Capabilities/OperationalQueryCapability.php +319 -0
- package/server/src/Support/Ai/Capabilities/OptimizeOrderRouteCapability.php +240 -0
- package/server/src/Support/Ai/Capabilities/OrderInsightsCapability.php +95 -0
- package/server/src/Support/Ai/Capabilities/SearchResourcesCapability.php +195 -0
- package/server/src/Support/Ai/FleetOpsAiQueryResources.php +150 -0
- package/server/src/Support/Analytics/RevenueTrend.php +3 -8
- package/server/src/Support/Metrics/ActiveRevenueQuery.php +121 -0
- package/server/src/Support/Metrics/AvgOrderValueMetric.php +1 -7
- package/server/src/Support/Metrics/EarningsMetric.php +1 -10
- package/server/src/Support/Telematics/Providers/AfaqyProvider.php +104 -5
- package/server/src/Support/Telematics/Providers/SafeeProvider.php +550 -54
- package/server/src/Support/Telematics/TelematicService.php +28 -9
- package/server/src/Support/Utils.php +52 -0
- package/server/src/routes.php +67 -14
- package/server/tests/AiCreateOrderPreviewCapabilityTest.php +131 -0
- package/server/tests/AiOperationalQueryCapabilityTest.php +75 -0
- package/server/tests/ExportCoverageTest.php +79 -0
- package/server/tests/FleetHierarchyResourceTest.php +12 -0
- package/server/tests/MetricsRegistryTest.php +45 -5
- package/server/tests/OrderPayloadPlaceResolutionTest.php +53 -0
- package/server/tests/PointResolutionTest.php +63 -0
- package/server/tests/PublicFleetOpsRestEndpointsTest.php +191 -0
- package/server/tests/TelematicsHardeningTest.php +636 -1
- package/addon/components/table/cell/dropdown/action-item.hbs +0 -31
- package/addon/components/table/cell/dropdown/action-item.js +0 -62
- package/addon/components/table/cell/dropdown.hbs +0 -28
- package/addon/components/table/cell/dropdown.js +0 -91
- package/app/components/table/cell/dropdown/action-item.js +0 -1
- package/app/components/table/cell/dropdown.js +0 -1
|
@@ -17,6 +17,8 @@ class SafeeProvider extends AbstractProvider
|
|
|
17
17
|
protected int $requestsPerMinute = 3000;
|
|
18
18
|
protected ?string $accessToken = null;
|
|
19
19
|
protected array $authContext = [];
|
|
20
|
+
protected int $dataTimeout = 120;
|
|
21
|
+
protected int $connectTimeout = 15;
|
|
20
22
|
|
|
21
23
|
protected function prepareAuthentication(): void
|
|
22
24
|
{
|
|
@@ -60,24 +62,93 @@ class SafeeProvider extends AbstractProvider
|
|
|
60
62
|
|
|
61
63
|
public function fetchDevices(array $options = []): array
|
|
62
64
|
{
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
$listInfoBody = $this->resolveListInfoPayload($options);
|
|
66
|
+
$response = $this->safeePost('/api/v2/vehicle/list-info', $listInfoBody, true);
|
|
67
|
+
$vehicles = $response['result'] ?? [];
|
|
68
|
+
$vehicleIds = $this->resolveListedVehicleIds($vehicles);
|
|
69
|
+
$identityStats = $this->summarizeVehicleIdentities($vehicles, $vehicleIds);
|
|
70
|
+
$endpointStats = [
|
|
71
|
+
'vehicles_listed' => count($vehicles),
|
|
72
|
+
'unique_vehicle_ids' => $identityStats['unique_vehicle_ids'],
|
|
73
|
+
'missing_vehicle_ids' => $identityStats['missing_vehicle_ids'],
|
|
74
|
+
'duplicate_vehicle_ids' => $identityStats['duplicate_vehicle_ids'],
|
|
75
|
+
'list_info_page_size' => $listInfoBody['pageSize'] ?? null,
|
|
76
|
+
'list_info_requested_unpaginated' => ($listInfoBody['pageSize'] ?? null) === 0,
|
|
77
|
+
'last_state_fetched' => 0,
|
|
78
|
+
'last_info_fetched' => 0,
|
|
79
|
+
'positions_fetched' => 0,
|
|
80
|
+
'events_fetched' => 0,
|
|
81
|
+
'devices_returned_for_ingestion' => count($vehicles),
|
|
82
|
+
'failures' => [],
|
|
83
|
+
];
|
|
84
|
+
$statesById = $this->fetchLastStatesByVehicle($vehicleIds, $endpointStats);
|
|
85
|
+
$endpointStats['last_state_fetched'] = count($statesById);
|
|
86
|
+
|
|
87
|
+
$devices = array_map(function (array $vehicle) use ($statesById, &$endpointStats) {
|
|
88
|
+
$vehicleId = $this->resolveListedVehicleId($vehicle);
|
|
89
|
+
$currentState = $statesById[(string) $vehicleId] ?? null;
|
|
90
|
+
|
|
91
|
+
return array_merge($vehicle, [
|
|
92
|
+
'_safee' => [
|
|
93
|
+
'vehicle_id' => $vehicleId,
|
|
94
|
+
'identity' => $vehicle,
|
|
95
|
+
'current_info' => null,
|
|
96
|
+
'current_state' => $currentState,
|
|
97
|
+
'positions' => [],
|
|
98
|
+
'events' => [],
|
|
99
|
+
'sync_window' => null,
|
|
100
|
+
'diagnostics' => $endpointStats,
|
|
101
|
+
],
|
|
102
|
+
'sensors' => [],
|
|
69
103
|
]);
|
|
70
|
-
|
|
71
|
-
$devices = $response['result'] ?? [];
|
|
72
|
-
} else {
|
|
73
|
-
$response = $this->safeePost('/api/v2/vehicle/list-info', $options['filter'] ?? new \stdClass());
|
|
74
|
-
$devices = $response['result'] ?? [];
|
|
75
|
-
}
|
|
104
|
+
}, $vehicles);
|
|
76
105
|
|
|
77
106
|
return [
|
|
78
107
|
'devices' => $devices,
|
|
79
108
|
'next_cursor' => null,
|
|
80
109
|
'has_more' => false,
|
|
110
|
+
'sync_meta' => [
|
|
111
|
+
'safee_last_endpoint_counts' => array_merge($endpointStats, [
|
|
112
|
+
'failures' => array_slice($endpointStats['failures'], 0, 25),
|
|
113
|
+
]),
|
|
114
|
+
],
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public function fetchDeviceTelemetrySnapshots(array $inventoryPayloads, array $options = []): array
|
|
119
|
+
{
|
|
120
|
+
$window = $this->resolveTelemetryWindow($options);
|
|
121
|
+
$vehicleIds = $this->resolveListedVehicleIds($inventoryPayloads);
|
|
122
|
+
$identityStats = $this->summarizeVehicleIdentities($inventoryPayloads, $vehicleIds);
|
|
123
|
+
$endpointStats = [
|
|
124
|
+
'vehicles_listed' => count($inventoryPayloads),
|
|
125
|
+
'unique_vehicle_ids' => $identityStats['unique_vehicle_ids'],
|
|
126
|
+
'missing_vehicle_ids' => $identityStats['missing_vehicle_ids'],
|
|
127
|
+
'duplicate_vehicle_ids' => $identityStats['duplicate_vehicle_ids'],
|
|
128
|
+
'last_state_fetched' => 0,
|
|
129
|
+
'last_info_fetched' => 0,
|
|
130
|
+
'positions_fetched' => 0,
|
|
131
|
+
'events_fetched' => 0,
|
|
132
|
+
'devices_returned_for_ingestion' => count($inventoryPayloads),
|
|
133
|
+
'failures' => [],
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
$devices = array_map(function (array $vehicle) use ($window, &$endpointStats) {
|
|
137
|
+
return $this->enrichVehicleSnapshot($vehicle, $window, $endpointStats);
|
|
138
|
+
}, $inventoryPayloads);
|
|
139
|
+
|
|
140
|
+
return [
|
|
141
|
+
'devices' => $devices,
|
|
142
|
+
'sync_meta' => [
|
|
143
|
+
'safee_last_telemetry_synced_at' => $window['endDate'],
|
|
144
|
+
'safee_last_sync_window' => $window,
|
|
145
|
+
'safee_last_endpoint_counts' => array_merge($endpointStats, [
|
|
146
|
+
'failures' => array_slice($endpointStats['failures'], 0, 25),
|
|
147
|
+
]),
|
|
148
|
+
'safee_last_enrichment_total' => count($inventoryPayloads),
|
|
149
|
+
'safee_last_enrichment_completed' => count($devices),
|
|
150
|
+
'safee_last_enrichment_failures' => array_slice($endpointStats['failures'], 0, 25),
|
|
151
|
+
],
|
|
81
152
|
];
|
|
82
153
|
}
|
|
83
154
|
|
|
@@ -92,46 +163,61 @@ class SafeeProvider extends AbstractProvider
|
|
|
92
163
|
|
|
93
164
|
public function normalizeDevice(array $payload): array
|
|
94
165
|
{
|
|
95
|
-
$
|
|
96
|
-
$
|
|
166
|
+
$identity = $this->identityPayload($payload);
|
|
167
|
+
$current = $this->currentTelemetryPayload($payload) ?? [];
|
|
168
|
+
$vehicleId = $this->resolveCanonicalVehicleId($payload, $identity, $current);
|
|
169
|
+
$position = $this->extractPosition($current ?: $payload);
|
|
170
|
+
$rawStatus = $current['status'] ?? $identity['status'] ?? $identity['vehicleStatus'] ?? null;
|
|
97
171
|
$status = $this->normalizeVehicleStatus($rawStatus);
|
|
98
172
|
|
|
99
173
|
return [
|
|
100
|
-
'device_id'
|
|
101
|
-
'
|
|
102
|
-
'
|
|
103
|
-
'
|
|
104
|
-
'
|
|
105
|
-
'
|
|
106
|
-
'
|
|
107
|
-
'
|
|
108
|
-
'
|
|
109
|
-
'
|
|
174
|
+
'device_id' => $vehicleId,
|
|
175
|
+
'external_id' => $vehicleId,
|
|
176
|
+
'name' => $this->resolveVehicleName($identity, $current, $vehicleId),
|
|
177
|
+
'provider' => 'safee',
|
|
178
|
+
'model' => $identity['model'] ?? data_get($identity, 'device.model') ?? null,
|
|
179
|
+
'internal_id' => $identity['uuid'] ?? $vehicleId,
|
|
180
|
+
'imei' => data_get($identity, 'device.imei') ?? data_get($current, 'device.imei') ?? null,
|
|
181
|
+
'serial_number' => data_get($identity, 'device.serial') ?? data_get($identity, 'device.id') ?? data_get($current, 'device.serial') ?? null,
|
|
182
|
+
'vin' => $identity['vin'] ?? null,
|
|
183
|
+
'status' => $status,
|
|
184
|
+
'online' => $this->resolveOnline($current ?: $identity),
|
|
185
|
+
'last_seen_at' => $this->parseTimestamp($current['date'] ?? $current['deviceTime'] ?? $current['time'] ?? null),
|
|
186
|
+
'location' => [
|
|
110
187
|
'lat' => $position['lat'] ?? null,
|
|
111
188
|
'lng' => $position['lng'] ?? null,
|
|
112
189
|
],
|
|
113
|
-
'speed' => $
|
|
114
|
-
'heading' => $
|
|
115
|
-
'altitude' => $
|
|
116
|
-
'odometer' => $this->extractOdometer($
|
|
117
|
-
'ignition' => $this->extractIgnition($
|
|
118
|
-
'fuel_level' => $this->extractFuelLevel($
|
|
190
|
+
'speed' => $current['speed'] ?? $current['lastSpeed'] ?? null,
|
|
191
|
+
'heading' => $current['heading'] ?? $current['angle'] ?? null,
|
|
192
|
+
'altitude' => $position['alt'] ?? $current['altitude'] ?? $current['alt'] ?? null,
|
|
193
|
+
'odometer' => $this->extractOdometer($current),
|
|
194
|
+
'ignition' => $this->extractIgnition($current),
|
|
195
|
+
'fuel_level' => $this->extractFuelLevel($current),
|
|
119
196
|
'meta' => [
|
|
120
197
|
'raw' => $payload,
|
|
121
198
|
'provider_status' => array_filter([
|
|
122
199
|
'status' => $rawStatus,
|
|
123
200
|
'normalized' => $status,
|
|
124
|
-
'vehicleStatus' => $
|
|
201
|
+
'vehicleStatus' => $identity['vehicleStatus'] ?? null,
|
|
125
202
|
], fn ($value) => $value !== null),
|
|
126
|
-
'plate_number' => $
|
|
127
|
-
'
|
|
128
|
-
'
|
|
129
|
-
'
|
|
203
|
+
'plate_number' => $identity['plateNo'] ?? $identity['plateNumber'] ?? $identity['plate_number'] ?? $current['plateNo'] ?? null,
|
|
204
|
+
'driver' => $current['driver'] ?? $identity['driver'] ?? null,
|
|
205
|
+
'temperature' => $current['temperature'] ?? null,
|
|
206
|
+
'door' => $current['door'] ?? null,
|
|
207
|
+
'humidity' => $current['humidity'] ?? $current['humidityPerType'] ?? null,
|
|
208
|
+
'mileage' => $current['mileage'] ?? null,
|
|
209
|
+
'last_update' => $current ? $this->normalizeEvent($payload) : null,
|
|
210
|
+
'safee' => [
|
|
211
|
+
'vehicle_id' => $vehicleId,
|
|
212
|
+
'vehicle_uuid' => $identity['uuid'] ?? null,
|
|
213
|
+
'sync_window' => data_get($payload, '_safee.sync_window'),
|
|
214
|
+
'diagnostics' => data_get($payload, '_safee.diagnostics'),
|
|
215
|
+
],
|
|
130
216
|
'capabilities' => [
|
|
131
217
|
'tracking' => isset($position['lat'], $position['lng']),
|
|
132
|
-
'odometer' => $this->extractOdometer($
|
|
133
|
-
'fuel_level' => $this->extractFuelLevel($
|
|
134
|
-
'ignition_state' => $this->extractIgnition($
|
|
218
|
+
'odometer' => $this->extractOdometer($current) !== null,
|
|
219
|
+
'fuel_level' => $this->extractFuelLevel($current) !== null,
|
|
220
|
+
'ignition_state' => $this->extractIgnition($current) !== null,
|
|
135
221
|
],
|
|
136
222
|
],
|
|
137
223
|
];
|
|
@@ -139,37 +225,110 @@ class SafeeProvider extends AbstractProvider
|
|
|
139
225
|
|
|
140
226
|
public function normalizeEvent(array $payload): array
|
|
141
227
|
{
|
|
142
|
-
$
|
|
228
|
+
$eventPayload = $this->currentTelemetryPayload($payload) ?? $payload;
|
|
229
|
+
|
|
230
|
+
return $this->normalizeSafeeTelemetryEvent($eventPayload, 'current', $this->identityPayload($payload));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
public function normalizeEvents(array $payload): array
|
|
234
|
+
{
|
|
235
|
+
$identity = $this->identityPayload($payload);
|
|
236
|
+
$events = [];
|
|
237
|
+
|
|
238
|
+
if ($current = $this->currentTelemetryPayload($payload)) {
|
|
239
|
+
$events[] = $this->normalizeSafeeTelemetryEvent($current, 'current', $identity);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
foreach ((array) data_get($payload, '_safee.positions', []) as $position) {
|
|
243
|
+
if (is_array($position)) {
|
|
244
|
+
$events[] = $this->normalizeSafeeTelemetryEvent($position, 'position', $identity);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
foreach ((array) data_get($payload, '_safee.events', []) as $event) {
|
|
249
|
+
if (is_array($event)) {
|
|
250
|
+
$events[] = $this->normalizeSafeeTelemetryEvent($event, 'event', $identity);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return $events;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
protected function normalizeSafeeTelemetryEvent(array $payload, string $source, array $identity = []): array
|
|
258
|
+
{
|
|
259
|
+
$position = $this->extractPosition($payload);
|
|
260
|
+
$vehicleId = $this->resolveVehicleId($identity) ?? $this->resolveVehicleId($payload);
|
|
261
|
+
$eventCode = data_get($payload, 'event.code') ?? data_get($payload, 'type.key') ?? data_get($payload, 'event.name');
|
|
262
|
+
$eventName = data_get($payload, 'event.name') ?? data_get($payload, 'type.value') ?? $payload['reason'] ?? null;
|
|
263
|
+
$occurredAt = $this->parseTimestamp($payload['date'] ?? $payload['deviceTime'] ?? $payload['time'] ?? null);
|
|
264
|
+
$providerEventId = $source === 'current'
|
|
265
|
+
? (data_get($payload, 'event.id') ?? $payload['id'] ?? null)
|
|
266
|
+
: ($payload['id'] ?? data_get($payload, 'event.id') ?? null);
|
|
143
267
|
|
|
144
268
|
return [
|
|
145
|
-
'external_id'
|
|
146
|
-
'
|
|
147
|
-
'
|
|
148
|
-
'
|
|
149
|
-
'
|
|
150
|
-
'
|
|
151
|
-
'
|
|
269
|
+
'external_id' => implode(':', array_filter(['safee', $source, $vehicleId, $providerEventId, $payload['date'] ?? null])),
|
|
270
|
+
'external_event_id' => $providerEventId,
|
|
271
|
+
'device_id' => $vehicleId,
|
|
272
|
+
'event_type' => $eventCode ?? ($source === 'position' ? 'position_update' : 'telemetry_update'),
|
|
273
|
+
'code' => $eventCode,
|
|
274
|
+
'message' => $eventName,
|
|
275
|
+
'reason' => $payload['reason'] ?? null,
|
|
276
|
+
'state' => $payload['status'] ?? null,
|
|
277
|
+
'occurred_at' => $occurredAt,
|
|
278
|
+
'online' => $this->resolveOnline($payload),
|
|
279
|
+
'location' => [
|
|
152
280
|
'lat' => $position['lat'] ?? null,
|
|
153
281
|
'lng' => $position['lng'] ?? null,
|
|
154
282
|
],
|
|
155
283
|
'speed' => $payload['speed'] ?? $payload['lastSpeed'] ?? null,
|
|
156
284
|
'heading' => $payload['heading'] ?? $payload['angle'] ?? null,
|
|
157
|
-
'altitude' => $payload['altitude'] ?? $payload['alt'] ?? null,
|
|
285
|
+
'altitude' => $position['alt'] ?? $payload['altitude'] ?? $payload['alt'] ?? null,
|
|
158
286
|
'odometer' => $this->extractOdometer($payload),
|
|
159
287
|
'ignition' => $this->extractIgnition($payload),
|
|
160
288
|
'fuel_level' => $this->extractFuelLevel($payload),
|
|
161
|
-
'
|
|
289
|
+
'data' => array_filter([
|
|
290
|
+
'source' => $source,
|
|
291
|
+
'status' => $payload['status'] ?? null,
|
|
292
|
+
'type' => $payload['type'] ?? null,
|
|
293
|
+
'event' => $payload['event'] ?? null,
|
|
294
|
+
'driver' => $payload['driver'] ?? $identity['driver'] ?? null,
|
|
295
|
+
'vehicle' => $payload['vehicle'] ?? null,
|
|
296
|
+
'arguments' => $payload['arguments'] ?? null,
|
|
297
|
+
'temperature' => $payload['temperature'] ?? null,
|
|
298
|
+
'door' => $payload['door'] ?? null,
|
|
299
|
+
'humidity' => $payload['humidity'] ?? $payload['humidityPerType'] ?? null,
|
|
300
|
+
'mileage' => $payload['mileage'] ?? null,
|
|
301
|
+
], fn ($value) => $value !== null),
|
|
302
|
+
'meta' => [
|
|
303
|
+
'raw' => $payload,
|
|
304
|
+
'source' => $source,
|
|
305
|
+
'plate_number' => $payload['plateNo'] ?? $identity['plateNo'] ?? null,
|
|
306
|
+
],
|
|
162
307
|
];
|
|
163
308
|
}
|
|
164
309
|
|
|
165
310
|
public function normalizeSensor(array $payload): array
|
|
166
311
|
{
|
|
312
|
+
$type = $payload['type'] ?? $payload['sensor_type'] ?? 'generic';
|
|
313
|
+
$name = $payload['name'] ?? $payload['sensor_name'] ?? $type;
|
|
314
|
+
|
|
167
315
|
return [
|
|
168
|
-
'
|
|
316
|
+
'internal_id' => $payload['internal_id'] ?? $payload['sensor_id'] ?? $payload['external_id'] ?? null,
|
|
317
|
+
'external_id' => $payload['external_id'] ?? $payload['internal_id'] ?? null,
|
|
318
|
+
'name' => $name,
|
|
319
|
+
'type' => $type,
|
|
320
|
+
'sensor_type' => $type,
|
|
169
321
|
'value' => $payload['value'] ?? $payload['lastValue'] ?? null,
|
|
170
322
|
'unit' => $payload['unit'] ?? null,
|
|
171
|
-
'recorded_at' => $this->parseTimestamp($payload['date'] ?? $payload['deviceTime'] ?? null),
|
|
172
|
-
'
|
|
323
|
+
'recorded_at' => $this->parseTimestamp($payload['recorded_at'] ?? $payload['date'] ?? $payload['deviceTime'] ?? null),
|
|
324
|
+
'status' => $payload['status'] ?? 'active',
|
|
325
|
+
'meta' => array_filter(array_merge($payload['meta'] ?? [], [
|
|
326
|
+
'provider' => 'safee',
|
|
327
|
+
'vehicle_id' => $payload['vehicle_id'] ?? null,
|
|
328
|
+
'plate_no' => $payload['plate_no'] ?? null,
|
|
329
|
+
'source' => $payload['source'] ?? null,
|
|
330
|
+
'raw' => $payload['raw'] ?? $payload,
|
|
331
|
+
]), fn ($value) => $value !== null),
|
|
173
332
|
];
|
|
174
333
|
}
|
|
175
334
|
|
|
@@ -299,6 +458,263 @@ class SafeeProvider extends AbstractProvider
|
|
|
299
458
|
return array_filter($this->authContext, fn ($value) => $value !== null && $value !== '');
|
|
300
459
|
}
|
|
301
460
|
|
|
461
|
+
protected function resolveTelemetryWindow(array $options = []): array
|
|
462
|
+
{
|
|
463
|
+
$endDate = (float) ($options['end_date'] ?? $this->currentSafeeTimestamp());
|
|
464
|
+
$storedStart = data_get($this->telematic?->meta ?? [], 'safee_last_telemetry_synced_at');
|
|
465
|
+
$startDate = $options['start_date'] ?? ($storedStart ? ((float) $storedStart - 120) : $endDate - 900);
|
|
466
|
+
|
|
467
|
+
return [
|
|
468
|
+
'startDate' => max(0, (float) $startDate),
|
|
469
|
+
'endDate' => $endDate,
|
|
470
|
+
];
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
protected function currentSafeeTimestamp(): float
|
|
474
|
+
{
|
|
475
|
+
$now = Carbon::now();
|
|
476
|
+
|
|
477
|
+
return (float) $now->getTimestamp() + ((int) $now->format('u') / 1000000);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
protected function resolveListInfoPayload(array $options = []): array
|
|
481
|
+
{
|
|
482
|
+
$filter = $options['filter'] ?? [];
|
|
483
|
+
|
|
484
|
+
if ($filter instanceof \stdClass) {
|
|
485
|
+
$filter = (array) $filter;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
$payload = is_array($filter) ? $filter : [];
|
|
489
|
+
$payload['pageSize'] = array_key_exists('page_size', $options) ? (int) $options['page_size'] : 0;
|
|
490
|
+
|
|
491
|
+
if (array_key_exists('page_index', $options)) {
|
|
492
|
+
$payload['pageIndex'] = (int) $options['page_index'];
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return $payload;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
protected function resolveListedVehicleIds(array $vehicles): array
|
|
499
|
+
{
|
|
500
|
+
$ids = [];
|
|
501
|
+
|
|
502
|
+
foreach ($vehicles as $vehicle) {
|
|
503
|
+
if (!is_array($vehicle)) {
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
$vehicleId = $this->resolveListedVehicleId($vehicle);
|
|
508
|
+
if ($vehicleId !== null && $vehicleId !== '') {
|
|
509
|
+
$ids[] = $vehicleId;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
return array_values(array_unique($ids, SORT_REGULAR));
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
protected function summarizeVehicleIdentities(array $vehicles, array $uniqueVehicleIds): array
|
|
517
|
+
{
|
|
518
|
+
$counts = [];
|
|
519
|
+
$missing = 0;
|
|
520
|
+
|
|
521
|
+
foreach ($vehicles as $vehicle) {
|
|
522
|
+
if (!is_array($vehicle)) {
|
|
523
|
+
$missing++;
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
$vehicleId = $this->resolveListedVehicleId($vehicle);
|
|
528
|
+
if ($vehicleId === null || $vehicleId === '') {
|
|
529
|
+
$missing++;
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
$key = (string) $vehicleId;
|
|
534
|
+
$counts[$key] = ($counts[$key] ?? 0) + 1;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
$duplicates = [];
|
|
538
|
+
foreach ($counts as $vehicleId => $count) {
|
|
539
|
+
if ($count > 1) {
|
|
540
|
+
$duplicates[$vehicleId] = $count;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
return [
|
|
545
|
+
'unique_vehicle_ids' => count($uniqueVehicleIds),
|
|
546
|
+
'missing_vehicle_ids' => $missing,
|
|
547
|
+
'duplicate_vehicle_ids' => $duplicates,
|
|
548
|
+
];
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
protected function resolveListedVehicleId(array $vehicle): mixed
|
|
552
|
+
{
|
|
553
|
+
return data_get($vehicle, '_safee.vehicle_id')
|
|
554
|
+
?? data_get($vehicle, '_safee.identity.id')
|
|
555
|
+
?? ($vehicle['id'] ?? null);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
protected function fetchLastStatesByVehicle(array $vehicleIds, ?array &$endpointStats = null): array
|
|
559
|
+
{
|
|
560
|
+
if (empty($vehicleIds)) {
|
|
561
|
+
return [];
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
$states = [];
|
|
565
|
+
foreach (array_chunk($vehicleIds, 1000) as $chunk) {
|
|
566
|
+
try {
|
|
567
|
+
$response = $this->safeePost('/api/v2/vehicle/last-state', [
|
|
568
|
+
'live' => true,
|
|
569
|
+
'startDate' => null,
|
|
570
|
+
'endDate' => null,
|
|
571
|
+
'vehicles' => array_values($chunk),
|
|
572
|
+
], true);
|
|
573
|
+
} catch (\Throwable $e) {
|
|
574
|
+
if (is_array($endpointStats)) {
|
|
575
|
+
$endpointStats['failures'][] = [
|
|
576
|
+
'endpoint' => '/api/v2/vehicle/last-state',
|
|
577
|
+
'vehicle_id' => null,
|
|
578
|
+
'message' => $this->sanitizeProviderMessage($e->getMessage()),
|
|
579
|
+
];
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
foreach ($response['result'] ?? [] as $state) {
|
|
586
|
+
if (!is_array($state)) {
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
$vehicleId = $this->resolveVehicleId($state);
|
|
591
|
+
if ($vehicleId !== null) {
|
|
592
|
+
$states[(string) $vehicleId] = $state;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
return $states;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
protected function enrichVehicleSnapshot(array $vehicle, array $window, array &$endpointStats): array
|
|
601
|
+
{
|
|
602
|
+
$vehicleId = $this->resolveListedVehicleId($vehicle);
|
|
603
|
+
|
|
604
|
+
$lastInfo = $this->fetchVehicleEndpoint('/api/v2/vehicle/last-info', ['vehicleId' => $vehicleId], 'last-info', $vehicleId, $endpointStats);
|
|
605
|
+
$positions = $this->fetchVehicleEndpoint('/api/v2/vehicle/positions', [
|
|
606
|
+
'vehicleId' => $vehicleId,
|
|
607
|
+
'startDate' => $window['startDate'],
|
|
608
|
+
'endDate' => $window['endDate'],
|
|
609
|
+
], 'positions', $vehicleId, $endpointStats, []);
|
|
610
|
+
$events = $this->fetchVehicleEndpoint('/api/v2/vehicle/events', [
|
|
611
|
+
'vehicleId' => $vehicleId,
|
|
612
|
+
'startDate' => $window['startDate'],
|
|
613
|
+
'endDate' => $window['endDate'],
|
|
614
|
+
'status' => 'ALL',
|
|
615
|
+
], 'events', $vehicleId, $endpointStats, []);
|
|
616
|
+
|
|
617
|
+
$currentState = data_get($vehicle, '_safee.current_state');
|
|
618
|
+
|
|
619
|
+
return array_merge($vehicle, [
|
|
620
|
+
'_safee' => [
|
|
621
|
+
'vehicle_id' => $vehicleId,
|
|
622
|
+
'identity' => $vehicle,
|
|
623
|
+
'current_info' => $lastInfo,
|
|
624
|
+
'current_state' => $currentState,
|
|
625
|
+
'positions' => is_array($positions) ? $positions : [],
|
|
626
|
+
'events' => is_array($events) ? $events : [],
|
|
627
|
+
'sync_window' => $window,
|
|
628
|
+
'diagnostics' => $endpointStats,
|
|
629
|
+
],
|
|
630
|
+
'sensors' => $this->extractTelemetrySensors($lastInfo ?? []),
|
|
631
|
+
]);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
protected function fetchVehicleEndpoint(string $endpoint, array $payload, string $name, mixed $vehicleId, array &$endpointStats, mixed $default = null): mixed
|
|
635
|
+
{
|
|
636
|
+
if (!$vehicleId) {
|
|
637
|
+
return $default;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
try {
|
|
641
|
+
$response = $this->safeePost($endpoint, $payload, true);
|
|
642
|
+
$result = $response['result'] ?? $default;
|
|
643
|
+
|
|
644
|
+
if ($name === 'last-info' && $result) {
|
|
645
|
+
$endpointStats['last_info_fetched']++;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
if ($name === 'positions' && is_array($result)) {
|
|
649
|
+
$endpointStats['positions_fetched'] += count($result);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
if ($name === 'events' && is_array($result)) {
|
|
653
|
+
$endpointStats['events_fetched'] += count($result);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
return $result;
|
|
657
|
+
} catch (\Throwable $e) {
|
|
658
|
+
$endpointStats['failures'][] = [
|
|
659
|
+
'endpoint' => $endpoint,
|
|
660
|
+
'vehicle_id' => $vehicleId,
|
|
661
|
+
'message' => $this->sanitizeProviderMessage($e->getMessage()),
|
|
662
|
+
];
|
|
663
|
+
|
|
664
|
+
return $default;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
protected function identityPayload(array $payload): array
|
|
669
|
+
{
|
|
670
|
+
return data_get($payload, '_safee.identity') ?? $payload;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
protected function currentTelemetryPayload(array $payload): ?array
|
|
674
|
+
{
|
|
675
|
+
$currentInfo = data_get($payload, '_safee.current_info');
|
|
676
|
+
$currentState = data_get($payload, '_safee.current_state');
|
|
677
|
+
|
|
678
|
+
if (is_array($currentState) && !empty($currentState) && is_array($currentInfo) && !empty($currentInfo)) {
|
|
679
|
+
return array_replace_recursive($currentState, $currentInfo);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
if (is_array($currentInfo) && !empty($currentInfo)) {
|
|
683
|
+
return $currentInfo;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
return is_array($currentState) && !empty($currentState) ? $currentState : null;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
protected function resolveVehicleId(array $payload): mixed
|
|
690
|
+
{
|
|
691
|
+
return data_get($payload, '_safee.vehicle_id')
|
|
692
|
+
?? data_get($payload, '_safee.identity.id')
|
|
693
|
+
?? ($payload['vehicleId'] ?? null)
|
|
694
|
+
?? data_get($payload, 'vehicle.id')
|
|
695
|
+
?? ($payload['id'] ?? null);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
protected function resolveCanonicalVehicleId(array $payload, array $identity, array $current = []): mixed
|
|
699
|
+
{
|
|
700
|
+
return data_get($payload, '_safee.vehicle_id')
|
|
701
|
+
?? data_get($payload, '_safee.identity.id')
|
|
702
|
+
?? data_get($identity, '_safee.vehicle_id')
|
|
703
|
+
?? data_get($identity, '_safee.identity.id')
|
|
704
|
+
?? ($identity['id'] ?? null)
|
|
705
|
+
?? $this->resolveVehicleId($current);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
protected function resolveVehicleName(array $identity, array $current = [], mixed $vehicleId = null): string
|
|
709
|
+
{
|
|
710
|
+
return $identity['plateNo']
|
|
711
|
+
?? $identity['plateNumber']
|
|
712
|
+
?? $identity['name']
|
|
713
|
+
?? $current['plateNo']
|
|
714
|
+
?? data_get($current, 'vehicle.name')
|
|
715
|
+
?? ($vehicleId ? 'Safee Vehicle ' . $vehicleId : 'Unknown Safee Vehicle');
|
|
716
|
+
}
|
|
717
|
+
|
|
302
718
|
protected function safeeGet(string $endpoint): array
|
|
303
719
|
{
|
|
304
720
|
$response = Http::withHeaders($this->headers)
|
|
@@ -312,10 +728,13 @@ class SafeeProvider extends AbstractProvider
|
|
|
312
728
|
return $response->json() ?? [];
|
|
313
729
|
}
|
|
314
730
|
|
|
315
|
-
protected function safeePost(string $endpoint, array|\stdClass $payload = []): array
|
|
731
|
+
protected function safeePost(string $endpoint, array|\stdClass $payload = [], bool $dataEndpoint = false): array
|
|
316
732
|
{
|
|
317
|
-
$
|
|
318
|
-
|
|
733
|
+
$timeout = $dataEndpoint ? $this->dataTimeout : 30;
|
|
734
|
+
$connectTimeout = $dataEndpoint ? $this->connectTimeout : 10;
|
|
735
|
+
$response = Http::withHeaders($this->headers)
|
|
736
|
+
->timeout($timeout)
|
|
737
|
+
->connectTimeout($connectTimeout)
|
|
319
738
|
->post($this->baseUrl . $endpoint, $payload);
|
|
320
739
|
|
|
321
740
|
if ($response->failed()) {
|
|
@@ -325,6 +744,11 @@ class SafeeProvider extends AbstractProvider
|
|
|
325
744
|
return $response->json() ?? [];
|
|
326
745
|
}
|
|
327
746
|
|
|
747
|
+
protected function sanitizeProviderMessage(string $message): string
|
|
748
|
+
{
|
|
749
|
+
return preg_replace('/(access_token|refresh_token|token|password|client_secret)=([^\\s&]+)/i', '$1=[redacted]', $message) ?? 'Safee API request failed';
|
|
750
|
+
}
|
|
751
|
+
|
|
328
752
|
protected function extractPosition(array $payload): array
|
|
329
753
|
{
|
|
330
754
|
$position = $payload['position'] ?? $payload['lastPosition'] ?? $payload['location'] ?? $payload;
|
|
@@ -405,4 +829,76 @@ class SafeeProvider extends AbstractProvider
|
|
|
405
829
|
?? data_get($payload, 'vehicleFuel.level')
|
|
406
830
|
?? null;
|
|
407
831
|
}
|
|
832
|
+
|
|
833
|
+
protected function extractTelemetrySensors(array $payload): array
|
|
834
|
+
{
|
|
835
|
+
$vehicleId = $this->resolveVehicleId($payload);
|
|
836
|
+
$plateNo = $payload['plateNo'] ?? data_get($payload, 'vehicle.name');
|
|
837
|
+
$sensors = [];
|
|
838
|
+
|
|
839
|
+
foreach ($this->extractSensorMap($payload, ['temperature', 'temperaturePerType']) as $name => $value) {
|
|
840
|
+
$sensors[] = $this->makeSafeeSensorPayload($payload, $vehicleId, $plateNo, 'temperature', (string) $name, $value, 'celsius');
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
foreach ($this->extractSensorMap($payload, ['door', 'doorPerType']) as $name => $value) {
|
|
844
|
+
$sensors[] = $this->makeSafeeSensorPayload($payload, $vehicleId, $plateNo, 'door', (string) $name, $value);
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
foreach ($this->extractSensorMap($payload, ['humidity', 'humidityPerType']) as $name => $value) {
|
|
848
|
+
$sensors[] = $this->makeSafeeSensorPayload($payload, $vehicleId, $plateNo, 'humidity', (string) $name, $value, 'percent');
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
return $sensors;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
protected function extractSensorMap(array $payload, array $keys): array
|
|
855
|
+
{
|
|
856
|
+
foreach ($keys as $key) {
|
|
857
|
+
$value = data_get($payload, $key);
|
|
858
|
+
|
|
859
|
+
if (is_array($value) && !empty($value)) {
|
|
860
|
+
return $value;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
return [];
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
protected function makeSafeeSensorPayload(array $sourcePayload, mixed $vehicleId, mixed $plateNo, string $type, string $name, mixed $value, ?string $unit = null): array
|
|
868
|
+
{
|
|
869
|
+
return array_filter([
|
|
870
|
+
'internal_id' => implode(':', ['safee', $vehicleId ?: 'unknown_vehicle', $type, $name]),
|
|
871
|
+
'external_id' => implode(':', ['safee', $vehicleId ?: 'unknown_vehicle', $type, $name]),
|
|
872
|
+
'name' => $name,
|
|
873
|
+
'type' => $type,
|
|
874
|
+
'sensor_type' => $type,
|
|
875
|
+
'value' => $value,
|
|
876
|
+
'unit' => $unit,
|
|
877
|
+
'recorded_at' => $sourcePayload['date'] ?? $sourcePayload['deviceTime'] ?? null,
|
|
878
|
+
'date' => $sourcePayload['date'] ?? null,
|
|
879
|
+
'deviceTime' => $sourcePayload['deviceTime'] ?? null,
|
|
880
|
+
'status' => 'active',
|
|
881
|
+
'vehicle_id' => $vehicleId,
|
|
882
|
+
'plate_no' => $plateNo,
|
|
883
|
+
'source' => $type,
|
|
884
|
+
'raw' => [
|
|
885
|
+
'name' => $name,
|
|
886
|
+
'type' => $type,
|
|
887
|
+
'value' => $value,
|
|
888
|
+
'unit' => $unit,
|
|
889
|
+
],
|
|
890
|
+
'meta' => [
|
|
891
|
+
'provider' => 'safee',
|
|
892
|
+
'vehicle_id' => $vehicleId,
|
|
893
|
+
'plate_no' => $plateNo,
|
|
894
|
+
'source' => $type,
|
|
895
|
+
'raw' => [
|
|
896
|
+
'name' => $name,
|
|
897
|
+
'type' => $type,
|
|
898
|
+
'value' => $value,
|
|
899
|
+
'unit' => $unit,
|
|
900
|
+
],
|
|
901
|
+
],
|
|
902
|
+
], fn ($value) => $value !== null);
|
|
903
|
+
}
|
|
408
904
|
}
|