-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathGeofenceEntered.php
More file actions
207 lines (183 loc) · 6.54 KB
/
Copy pathGeofenceEntered.php
File metadata and controls
207 lines (183 loc) · 6.54 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
<?php
namespace Fleetbase\FleetOps\Events;
use Fleetbase\FleetOps\Models\Driver;
use Fleetbase\FleetOps\Models\Vehicle;
use Fleetbase\LaravelMysqlSpatial\Types\Point;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;
/**
* GeofenceEntered.
*
* Fired when a driver's location update causes them to cross into a
* geofence boundary (Zone or ServiceArea) for the first time.
*
* Listeners:
* - HandleGeofenceEntered (order automation, customer notification, event log)
* - SendResourceLifecycleWebhook (webhook delivery to configured endpoints)
*/
class GeofenceEntered implements ShouldBroadcast
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;
public string $modelHumanName = 'Geofence';
public ?string $modelRecordName = null;
public string $eventName = 'entered';
public $userSession;
public $companySession;
public $requestMethod;
public $apiCredential;
public $apiSecret;
public $apiKey;
public $apiEnvironment;
public $isSandbox;
public array $data = [];
/**
* The driver who entered the geofence.
*/
public ?Driver $driver = null;
/**
* The vehicle associated with the geofence event, if any.
*/
public ?Vehicle $vehicle = null;
/**
* The geofence that was entered (Zone or ServiceArea model instance).
*
* @var \Fleetbase\FleetOps\Models\Zone|\Fleetbase\FleetOps\Models\ServiceArea
*/
public $geofence;
/**
* The type of geofence: 'zone' or 'service_area'.
*/
public string $geofenceType;
/**
* The driver's location at the time of entry.
*/
public Point $location;
/**
* The timestamp when the entry was detected.
*/
public \Carbon\Carbon $timestamp;
/**
* The subject that triggered the event: 'driver' or 'vehicle'.
*/
public string $subjectType;
/**
* Create a new GeofenceEntered event.
*
* @param mixed $geofence Zone or ServiceArea
* @param string $geofenceType 'zone' | 'service_area'
*/
public function __construct(Driver|Vehicle $subject, $geofence, string $geofenceType, Point $location)
{
if ($subject instanceof Driver) {
$this->driver = $subject;
$this->vehicle = $subject->vehicle;
$this->subjectType = 'driver';
} else {
$subject->loadMissing('driver');
$this->vehicle = $subject;
$this->driver = $subject->driver;
$this->subjectType = 'vehicle';
}
$this->geofence = $geofence;
$this->geofenceType = $geofenceType;
$this->location = $location;
$this->timestamp = now();
$this->modelRecordName = $geofence->name ?? $geofence->public_id ?? $geofence->uuid ?? null;
$this->userSession = session('user');
$this->companySession = session('company', $this->getCompanyUuid());
$this->requestMethod = request()?->method() ?? 'CLI';
$this->apiCredential = session('api_credential', 'console');
$this->apiSecret = session('api_secret', 'internal');
$this->apiKey = session('api_key');
$this->apiEnvironment = session('api_environment', 'live');
$this->isSandbox = session('is_sandbox', false);
$this->data = $this->getEventData();
}
/**
* Returns the company UUID for this event.
* Used by the webhook infrastructure to scope delivery.
*/
public function getCompanyUuid(): string
{
return $this->vehicle?->company_uuid ?? $this->driver->company_uuid;
}
public function broadcastOn(): array
{
$channels = [new Channel('company.' . $this->getCompanyUuid())];
if ($this->driver) {
$channels[] = new Channel('driver.' . $this->driver->public_id);
$channels[] = new Channel('driver.' . $this->driver->uuid);
}
if ($this->vehicle) {
$channels[] = new Channel('vehicle.' . $this->vehicle->public_id);
$channels[] = new Channel('vehicle.' . $this->vehicle->uuid);
}
return $channels;
}
public function broadcastAs(): string
{
return 'geofence.entered';
}
/**
* Returns the standardised webhook payload for this event.
*/
public function broadcastWith(): array
{
return $this->getEventData();
}
public function getEventData(): array
{
$driver = $this->driver;
$vehicle = $this->vehicle;
$geofence = $this->geofence;
$subject = $this->subjectType === 'vehicle' ? $vehicle : $driver;
$payload = [
'event' => $this->broadcastAs(),
'event_type' => 'geofence.entered',
'occurred_at' => $this->timestamp->toIso8601String(),
'subject' => [
'type' => $this->subjectType,
'id' => $subject?->public_id ?? null,
'uuid' => $subject?->uuid ?? null,
'name' => $this->subjectType === 'vehicle' ? ($vehicle?->display_name ?? $vehicle?->plate_number) : $driver?->name,
],
'driver' => $driver ? [
'id' => $driver->public_id,
'uuid' => $driver->uuid,
'name' => $driver->name,
'phone' => $driver->phone,
] : null,
'vehicle' => $vehicle ? [
'id' => $vehicle->public_id,
'uuid' => $vehicle->uuid,
'name' => $vehicle->display_name ?? null,
'plate' => $vehicle->plate_number ?? null,
] : null,
'geofence' => [
'id' => $geofence->public_id,
'uuid' => $geofence->uuid,
'name' => $geofence->name,
'type' => $this->geofenceType,
],
'location' => [
'latitude' => $this->location->getLat(),
'longitude' => $this->location->getLng(),
],
];
// Attach active order context if the driver has one
$order = $driver?->getCurrentOrder();
if ($order) {
$payload['order'] = [
'id' => $order->public_id,
'uuid' => $order->uuid,
'status' => $order->status,
];
}
return $payload;
}
}