-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiveraAPI.php
More file actions
70 lines (58 loc) · 2.1 KB
/
Copy pathDiveraAPI.php
File metadata and controls
70 lines (58 loc) · 2.1 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
<?php
class DiveraAPI {
private $baseUrl = "https://app.divera247.com/api/v2/";
private $accessKey;
const FMS_STEIN_MAP = [
1 => 'semiready',
2 => 'ready',
3 => 'inuse',
4 => 'inuse',
6 => 'notready',
'ready' => 2,
'semiready' => 1,
'notready' => 6,
'inuse' => 3,
'maint' => 6
];
public function __construct($accessKey) {
$this->accessKey = $accessKey;
}
private function makeRequest($method, $endpoint, $data = null) {
$url = $this->baseUrl . $endpoint;
if (strpos($url, '?') === false) {
$url .= '?accesskey=' . $this->accessKey;
} else {
$url .= '&accesskey=' . $this->accessKey;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($method === 'POST' && $data) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
throw new Exception("Divera API request failed with code $httpCode");
}
return json_decode($response, true);
}
public function getVehicleStatus() {
$response = $this->makeRequest('GET', 'pull/vehicle-status');
return $response['data'] ?? [];
}
public function setVehicleStatus($vehicleId, $data) {
$payload = [
'status' => self::FMS_STEIN_MAP[$data['status']],
'status_id' => self::FMS_STEIN_MAP[$data['status']]
];
if (!empty($data['comment'])) {
$payload['status_note'] = str_replace("\n", " ", $data['comment']);
}
return $this->makeRequest('POST', "using-vehicles/set-status/{$vehicleId}", $payload);
}
}
?>