|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Illuminate\Support\Facades\Auth; |
|
9
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
|
10
|
|
|
//use Spatie\Activitylog\Traits\CausesActivity; |
|
11
|
|
|
use App\Sensor; |
|
12
|
|
|
use App\SensorData; |
|
13
|
|
|
|
|
14
|
|
|
class Device extends Model |
|
15
|
|
|
{ |
|
16
|
|
|
use SoftDeletes; |
|
17
|
|
|
use LogsActivity; |
|
18
|
|
|
//use CausesActivity; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The attributes that should be mutated to dates. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $dates = [ |
|
26
|
|
|
'deleted_at' |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The attributes that should be hidden for arrays. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $hidden = ['token']; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The attributes that are mass assignable. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $fillable = [ |
|
42
|
|
|
'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', |
|
43
|
|
|
'time', 'cover_status', 'error_msg', 'limitsw_open', 'limitsw_closed', |
|
44
|
|
|
'light_in', 'light_out', 'update_rate', 'image_rate', 'sensor_rate', |
|
45
|
|
|
'open_time', 'close_time' |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The attributes to ignore in the Activity Log |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected static $ignoreChangedAttributes = ['updated_at']; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The attributes to log in the Activity Log |
|
57
|
|
|
* |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected static $logAttributes = [ |
|
61
|
|
|
'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', |
|
62
|
|
|
'time', 'cover_status', 'error_msg', 'limitsw_open', 'limitsw_closed', |
|
63
|
|
|
'light_in', 'light_out', 'update_rate', 'image_rate', 'sensor_rate', |
|
64
|
|
|
'open_time', 'close_time' |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Only log those that have actually changed after the update. |
|
69
|
|
|
* |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected static $logOnlyDirty = true; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Update the updated_at and created_at timestamps? |
|
76
|
|
|
* |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
public $timestamps = true; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the location for the device |
|
83
|
|
|
*/ |
|
84
|
|
|
public function location() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->belongsTo('App\Location', 'location_id'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the site for the device using model accessor |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getSiteAttribute() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->location->site ?? (object)[]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Accessor: Get the open time of the device converted to hours and minutes |
|
99
|
|
|
* If it is a device accessing the time use UTC |
|
100
|
|
|
* If it is a user accessing the time use their preferred timezone |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $value |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
View Code Duplication |
public function getOpenTimeAttribute($value) |
|
106
|
|
|
{ |
|
107
|
|
|
$time = new Carbon($value, 'UTC'); |
|
108
|
|
|
|
|
109
|
|
|
//If the user is logged in then use there preferred timezone |
|
110
|
|
|
if (Auth::check()) |
|
111
|
|
|
$time = $time->setTimezone(Auth::user()->timezone); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
return $time->format('H:i'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Accessor: Get the close time of the device converted to hours and minutes |
|
118
|
|
|
* If it is a device accessing the time use UTC |
|
119
|
|
|
* If it is a user accessing the time use their preferred timezone |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $value |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
View Code Duplication |
public function getCloseTimeAttribute($value) |
|
125
|
|
|
{ |
|
126
|
|
|
$time = new Carbon($value, 'UTC'); |
|
127
|
|
|
|
|
128
|
|
|
//If the user is logged in then use there preferred timezone |
|
129
|
|
|
if (Auth::check()) |
|
130
|
|
|
$time = $time->setTimezone(Auth::user()->timezone); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
return $time->format('H:i'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Set the open time to UTC |
|
137
|
|
|
* If it is a device saving the time use UTC |
|
138
|
|
|
* If it is a user saving the time use their preferred timezone |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $value |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
|
View Code Duplication |
public function setOpenTimeAttribute($value) |
|
144
|
|
|
{ |
|
145
|
|
|
//If the user is logged in then use there preferred timezone |
|
146
|
|
|
if (Auth::check()) |
|
147
|
|
|
{ |
|
148
|
|
|
$time = new Carbon($value, Auth::user()->timezone); |
|
|
|
|
|
|
149
|
|
|
$time = $time->setTimezone('UTC'); |
|
150
|
|
|
} |
|
151
|
|
|
else |
|
152
|
|
|
$time = new Carbon($value, 'UTC'); |
|
153
|
|
|
|
|
154
|
|
|
$this->attributes['open_time'] = $time; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Set the close time to UTC |
|
159
|
|
|
* If it is a device saving the time use UTC |
|
160
|
|
|
* If it is a user saving the time use their preferred timezone |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $value |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
View Code Duplication |
public function setCloseTimeAttribute($value) |
|
166
|
|
|
{ |
|
167
|
|
|
//If the user is logged in then use there preferred timezone |
|
168
|
|
|
if (Auth::check()) |
|
169
|
|
|
{ |
|
170
|
|
|
$time = new Carbon($value, Auth::user()->timezone); |
|
|
|
|
|
|
171
|
|
|
$time = $time->setTimezone('UTC'); |
|
172
|
|
|
} |
|
173
|
|
|
else |
|
174
|
|
|
$time = new Carbon($value, 'UTC'); |
|
175
|
|
|
|
|
176
|
|
|
$this->attributes['close_time'] = $time; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Scope a query to only include devices belonging to a given location |
|
181
|
|
|
* |
|
182
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
183
|
|
|
* @param int $location_id |
|
184
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
185
|
|
|
*/ |
|
186
|
|
|
public function scopeByLocation($query, $location_id) |
|
187
|
|
|
{ |
|
188
|
|
|
return $query->where('location_id', $location_id); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Scope a query to limit the included columns to only include what is publicly needed to be displayed on the |
|
193
|
|
|
* dashboard |
|
194
|
|
|
* |
|
195
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
196
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
197
|
|
|
*/ |
|
198
|
|
|
public function scopePublicDashData($query) |
|
199
|
|
|
{ |
|
200
|
|
|
return $query->select([ |
|
|
|
|
|
|
201
|
|
|
'id', |
|
202
|
|
|
'name', |
|
203
|
|
|
'location_id', |
|
204
|
|
|
'cover_command', |
|
205
|
|
|
'cover_status', |
|
206
|
|
|
'temperature', |
|
207
|
|
|
'humidity', |
|
208
|
|
|
'light_in', |
|
209
|
|
|
'light_out', |
|
210
|
|
|
'open_time', |
|
211
|
|
|
'close_time', |
|
212
|
|
|
'update_rate', |
|
213
|
|
|
'image_rate', |
|
214
|
|
|
'sensor_rate', |
|
215
|
|
|
'cpu_temp', |
|
216
|
|
|
]); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Create a new API token for the device. |
|
221
|
|
|
*/ |
|
222
|
|
|
public function generateToken() |
|
223
|
|
|
{ |
|
224
|
|
|
$this->token = str_random(60); |
|
225
|
|
|
$this->save(); |
|
226
|
|
|
|
|
227
|
|
|
return $this->token; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Get a device by uuid |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $uuid |
|
234
|
|
|
* @return Device|Illuminate\Database\Eloquent\Model |
|
235
|
|
|
*/ |
|
236
|
|
|
public static function getDeviceByUUID($uuid) |
|
237
|
|
|
{ |
|
238
|
|
|
return self::where('uuid', $uuid)->first(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Get the deviceimage record associated with the device. |
|
243
|
|
|
*/ |
|
244
|
|
|
public function image() |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->hasOne('App\Deviceimage'); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get the sensors associated with the device. |
|
252
|
|
|
*/ |
|
253
|
|
|
public function sensors() |
|
254
|
|
|
{ |
|
255
|
|
|
return $this->hasMany('App\Sensor'); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Get the sensor data associated with the device. |
|
260
|
|
|
*/ |
|
261
|
|
|
public function data() |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->hasManyThrough('App\SensorData', 'App\Sensor'); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: