|
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
|
|
|
|
|
10
|
|
|
class Device extends Model |
|
11
|
|
|
{ |
|
12
|
|
|
use SoftDeletes; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The attributes that should be mutated to dates. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $dates = [ |
|
20
|
|
|
'deleted_at' |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The attributes that should be hidden for arrays. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $hidden = ['token']; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The attributes that are mass assignable. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $fillable = [ |
|
36
|
|
|
'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', |
|
37
|
|
|
'time', 'cover_status', 'error_msg', 'limitsw_open', 'limitsw_closed', |
|
38
|
|
|
'light_in', 'light_out', 'cpu_temp', 'temperature', 'humidity', |
|
39
|
|
|
'update_rate', 'image_rate', 'sensor_rate' |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Update the updated_at and created_at timestamps? |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
public $timestamps = true; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the location for the device |
|
51
|
|
|
*/ |
|
52
|
|
|
public function location() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->belongsTo('App\Location', 'location_id'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the site for the device |
|
59
|
|
|
*/ |
|
60
|
|
|
public function site() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->location->site; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Accessor: Get the open time of the device converted to hours and minutes |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $value |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getOpenTimeAttribute($value) |
|
72
|
|
|
{ |
|
73
|
|
|
$time = new Carbon($value, 'UTC'); |
|
74
|
|
|
$time = $time->setTimezone(Auth::user()->timezone); |
|
|
|
|
|
|
75
|
|
|
return $time->format('H:i'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Accessor: Get the close time of the device converted to hours and minutes |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $value |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getCloseTimeAttribute($value) |
|
85
|
|
|
{ |
|
86
|
|
|
$time = new Carbon($value, 'UTC'); |
|
87
|
|
|
$time = $time->setTimezone(Auth::user()->timezone); |
|
|
|
|
|
|
88
|
|
|
return $time->format('H:i'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set the open time to UTC |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $value |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setOpenTimeAttribute($value) |
|
98
|
|
|
{ |
|
99
|
|
|
$time = new Carbon($value, Auth::user()->timezone); |
|
|
|
|
|
|
100
|
|
|
$time = $time->setTimezone('UTC'); |
|
101
|
|
|
$this->attributes['open_time'] = $time; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set the close time to UTC |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $value |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setCloseTimeAttribute($value) |
|
111
|
|
|
{ |
|
112
|
|
|
$time = new Carbon($value, Auth::user()->timezone); |
|
|
|
|
|
|
113
|
|
|
$time = $time->setTimezone('UTC'); |
|
114
|
|
|
$this->attributes['close_time'] = $time; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Scope a query to only include devices belonging to a given location |
|
119
|
|
|
* |
|
120
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
121
|
|
|
* @param int $location_id |
|
122
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
123
|
|
|
*/ |
|
124
|
|
|
public function scopeByLocation($query, $location_id) |
|
125
|
|
|
{ |
|
126
|
|
|
return $query->where('location_id', $location_id); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Scope a query to limit the included columns to only include what is publicly needed to be displayed on the |
|
131
|
|
|
* dashboard |
|
132
|
|
|
* |
|
133
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
134
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
135
|
|
|
*/ |
|
136
|
|
|
public function scopePublicDashData($query) |
|
137
|
|
|
{ |
|
138
|
|
|
return $query->select([ |
|
|
|
|
|
|
139
|
|
|
'id', |
|
140
|
|
|
'name', |
|
141
|
|
|
'location_id', |
|
142
|
|
|
'cover_command', |
|
143
|
|
|
'cover_status', |
|
144
|
|
|
'temperature', |
|
145
|
|
|
'humidity', |
|
146
|
|
|
'light_in', |
|
147
|
|
|
'open_time', |
|
148
|
|
|
'close_time', |
|
149
|
|
|
'update_rate', |
|
150
|
|
|
'image_rate', |
|
151
|
|
|
'sensor_rate', |
|
152
|
|
|
'cpu_temp', |
|
153
|
|
|
]); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Create a new API token for the device. |
|
158
|
|
|
*/ |
|
159
|
|
|
public function generateToken() |
|
160
|
|
|
{ |
|
161
|
|
|
$this->token = str_random(60); |
|
|
|
|
|
|
162
|
|
|
$this->save(); |
|
163
|
|
|
|
|
164
|
|
|
return $this->token; |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get a device by uuid |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $uuid |
|
171
|
|
|
* @return Device|Illuminate\Database\Eloquent\Model |
|
172
|
|
|
*/ |
|
173
|
|
|
public static function getDeviceByUUID($uuid) |
|
174
|
|
|
{ |
|
175
|
|
|
return self::where('uuid', $uuid)->first(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get the deviceimage record associated with the device. |
|
180
|
|
|
*/ |
|
181
|
|
|
public function image() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->hasOne('App\Deviceimage'); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.