-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathDriverSimulatedLocationChanged.php
More file actions
183 lines (163 loc) · 4.23 KB
/
Copy pathDriverSimulatedLocationChanged.php
File metadata and controls
183 lines (163 loc) · 4.23 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
<?php
namespace Fleetbase\FleetOps\Events;
use Fleetbase\FleetOps\Models\Driver;
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;
use Illuminate\Support\Carbon;
/**
* Event class for when a driver's simulated location changes.
*
* This event is broadcasted to multiple channels and includes details
* about the driver's location, speed, heading, and other attributes.
*/
class DriverSimulatedLocationChanged implements ShouldBroadcast
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;
/**
* The event id.
*
* @var string
*/
public $eventId;
/**
* The datetime instance the broadcast ws triggered.
*
* @var string
*/
public $sentAt;
/**
* The uuid of the driver.
*
* @var string
*/
public $driverUuid;
/**
* The public id of the driver.
*
* @var string
*/
public $driverId;
/**
* The internal id of the driver.
*
* @var string
*/
public $driverInternalId;
/**
* The name of the driver.
*
* @var string
*/
public $driverName;
/**
* The phone of the driver.
*
* @var string
*/
public $driverPhone;
/**
* The new driver location.
*
* @var string
*/
public $location;
/**
* The driver altitude.
*
* @var string
*/
public $altitude;
/**
* The ndriver heading.
*
* @var string
*/
public $heading;
/**
* The driver speed.
*
* @var string
*/
public $speed;
/**
* Optional, additional data.
*
* @var array
*/
public $additionalData = [];
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Driver $driver, Point $location, array $additionalData = [])
{
$this->eventId = uniqid('event_');
$this->sentAt = Carbon::now()->toDateTimeString();
$this->driverUuid = $driver->uuid;
$this->driverId = $driver->public_id;
$this->driverInternalId = $driver->internal_id;
$this->driverName = $driver->name;
$this->driverPhone = $driver->phone;
$this->altitude = $driver->altitude;
// can be set in simulation
$this->heading = data_get($additionalData, 'heading', $driver->heading);
$this->speed = data_get($additionalData, 'speed', $driver->speed);
$this->location = $location;
$this->additionalData = $additionalData;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return [
new Channel('company.' . session('company')),
new Channel('api.' . session('api_credential')),
new Channel('driver.' . $this->driverId),
new Channel('driver.' . $this->driverUuid),
];
}
/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'driver.simulated_location_changed';
}
/**
* Get the data to broadcast.
*
* @return array
*/
public function broadcastWith()
{
return [
'id' => $this->eventId,
'api_version' => config('api.version'),
'event' => $this->broadcastAs(),
'created_at' => $this->sentAt,
'data' => [
'id' => $this->driverId,
'internal_id' => $this->driverInternalId,
'name' => $this->driverName,
'phone' => $this->driverPhone,
'location' => $this->location,
'altitude' => $this->altitude,
'heading' => $this->heading,
'speed' => $this->speed,
'additionalData' => $this->additionalData,
],
];
}
}