|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
|
|
9
|
|
|
class Device extends Model |
|
10
|
|
|
{ |
|
11
|
|
|
use SoftDeletes; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The attributes that should be mutated to dates. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $dates = [ |
|
19
|
|
|
'deleted_at' |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The attributes that should be hidden for arrays. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $hidden = ['token']; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The attributes that are mass assignable. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $fillable = [ |
|
35
|
|
|
'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', 'time', |
|
36
|
|
|
'cover_status', 'error_msg', 'limitsw_open', 'limitsw_closed', |
|
37
|
|
|
'light_in', 'light_out', 'cpu_temp', 'temperature', 'humidity' |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The attributes that are for timestamps. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
public $timestamps = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the location for the device |
|
49
|
|
|
*/ |
|
50
|
|
|
public function location() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->belongsTo('App\Location', 'location_id'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the site for the device |
|
57
|
|
|
*/ |
|
58
|
|
|
public function site() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->belongsTo('App\Site'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Update the device with the matching id with a new location id |
|
65
|
|
|
* |
|
66
|
|
|
* @param int $id |
|
67
|
|
|
* @param int $location_id |
|
68
|
|
|
*/ |
|
69
|
|
|
public function updateLocationID($id, $location_id) |
|
70
|
|
|
{ |
|
71
|
|
|
|
|
72
|
|
|
$device = $this->findOrFail($id); |
|
|
|
|
|
|
73
|
|
|
$device->location_id = $location_id; |
|
74
|
|
|
$device->save(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get all the devices with the supplied location id |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $location_id |
|
81
|
|
|
* @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getDevicesBasedOnLocation($location_id) |
|
84
|
|
|
{ |
|
85
|
|
|
return self::where('location_id', $location_id)->get(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get the first device with the supplied location id |
|
90
|
|
|
* |
|
91
|
|
|
* @param int $location_id |
|
92
|
|
|
* @return Device|Illuminate\Database\Eloquent\Model |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getFirstDeviceBasedOnLocation($location_id) |
|
95
|
|
|
{ |
|
96
|
|
|
return self::where('location_id', $location_id)->first(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Create a new API token for the device. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function generateToken() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->token = str_random(60); |
|
|
|
|
|
|
105
|
|
|
$this->save(); |
|
106
|
|
|
|
|
107
|
|
|
return $this->token; |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get a device by uuid |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $uuid |
|
114
|
|
|
* @return Device|Illuminate\Database\Eloquent\Model |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getDeviceByUUID($uuid) |
|
117
|
|
|
{ |
|
118
|
|
|
return self::where('uuid', $uuid)->first(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: