|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Validator; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use App\Device; |
|
8
|
|
|
|
|
9
|
|
|
class ApiController extends Controller |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Creates a json response for all the devices. |
|
13
|
|
|
* |
|
14
|
|
|
* @return Response |
|
15
|
|
|
*/ |
|
16
|
|
|
public function index() |
|
17
|
|
|
{ |
|
18
|
|
|
$devices = Device::all(); |
|
19
|
|
|
return response()->json($devices, 200); |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Creates a json response for a specifc device. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Device $device |
|
26
|
|
|
* @return Response |
|
27
|
|
|
*/ |
|
28
|
|
|
public function show(Device $device) |
|
29
|
|
|
{ |
|
30
|
|
|
return response()->json($device, 200); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Updates the status of a device. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Request $request |
|
37
|
|
|
* @return Response |
|
38
|
|
|
*/ |
|
39
|
|
|
public function update(Request $request) |
|
40
|
|
|
{ |
|
41
|
|
|
// Validate the request |
|
42
|
|
|
$validator = Validator::make($request->all(), [ |
|
43
|
|
|
'uuid' => 'required|string|max:255|exists:devices,uuid', |
|
44
|
|
|
'token' => 'required|string|max:60', |
|
45
|
|
|
'version' => 'nullable|string|max:32', |
|
46
|
|
|
'hostname' => 'nullable|string|alpha_num|max:255', |
|
47
|
|
|
'ip' => 'nullable|ip', |
|
48
|
|
|
'mac_address' => 'nullable|string|min:16|max:16', |
|
49
|
|
|
'time' => 'nullable|date', |
|
50
|
|
|
'cover_status' => 'nullable|string|max:32', |
|
51
|
|
|
'error_msg' => 'nullable|string', |
|
52
|
|
|
'limitsw_open' => 'nullable|boolean', |
|
53
|
|
|
'limitsw_closed' => 'nullable|boolean', |
|
54
|
|
|
'light_in' => 'nullable|numeric', |
|
55
|
|
|
'light_out' => 'nullable|numeric', |
|
56
|
|
|
'cpu_temp' => 'nullable|numeric', |
|
57
|
|
|
'temperature' => 'nullable|numeric', |
|
58
|
|
|
'humidity' => 'nullable|numeric', |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
// If validation fails, send the validation error back with status 400 |
|
62
|
|
|
if ($validator->fails()) { |
|
63
|
|
|
return response()->json(['data' => $validator->toArray()], 400); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Get the device record |
|
67
|
|
|
$device = Device::where('uuid', $request->input('uuid'))->first(); |
|
68
|
|
|
|
|
69
|
|
|
// If token doesnt match then send 401 unauthorized. |
|
70
|
|
|
if ($request->input('token') != $device->token) { |
|
71
|
|
|
return response()->json(['data' => 'Bad token.'], 401); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Update the device |
|
75
|
|
|
$device->version = $request->input('version'); |
|
76
|
|
|
$device->hostname = $request->input('hostname'); |
|
77
|
|
|
$device->ip = $request->input('ip'); |
|
78
|
|
|
$device->mac_address = $request->input('mac_address'); |
|
79
|
|
|
$device->time = $request->input('time'); |
|
80
|
|
|
$device->cover_status = $request->input('cover_status'); |
|
81
|
|
|
$device->error_msg = $request->input('error_msg'); |
|
82
|
|
|
$device->limitsw_open = $request->input('limitsw_open'); |
|
83
|
|
|
$device->limitsw_closed = $request->input('limitsw_closed'); |
|
84
|
|
|
$device->light_in = $request->input('light_in'); |
|
85
|
|
|
$device->light_out = $request->input('light_out'); |
|
86
|
|
|
$device->cpu_temp = $request->input('cpu_temp'); |
|
87
|
|
|
$device->temperature = $request->input('temperature'); |
|
88
|
|
|
$device->humidity = $request->input('humidity'); |
|
89
|
|
|
|
|
90
|
|
|
$device->save(); |
|
91
|
|
|
|
|
92
|
|
|
// Create an api token for the new device. |
|
93
|
|
|
$device->generateToken(); |
|
94
|
|
|
|
|
95
|
|
|
// A 'Registered' event is created and will trigger any relevant |
|
96
|
|
|
// observers, such as sending a confirmation email or any |
|
97
|
|
|
// code that needs to be run as soon as the device is created. |
|
98
|
|
|
//event(new Registered(true)); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
// Return the new device info including the token. |
|
101
|
|
|
return response()->json(['data' => $device->toArray()], 201); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Registers a new device. |
|
106
|
|
|
* |
|
107
|
|
|
* @param Request $request |
|
108
|
|
|
* @return Response |
|
109
|
|
|
*/ |
|
110
|
|
|
public function register(Request $request) |
|
111
|
|
|
{ |
|
112
|
|
|
// Validate the request |
|
113
|
|
|
$validator = Validator::make($request->all(), [ |
|
114
|
|
|
'uuid' => 'required|string|max:255', |
|
115
|
|
|
'challenge' => 'required|string|min:6', |
|
116
|
|
|
]); |
|
117
|
|
|
|
|
118
|
|
|
// If validation fails, send the validation error back with status 400 |
|
119
|
|
|
if ($validator->fails()) { |
|
120
|
|
|
return response()->json(['data' => $validator->toArray()], 400); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// If challenge string doesnt match then send 401 unauthorized. |
|
124
|
|
|
if ($request->input('challenge') != 'temppass') { |
|
125
|
|
|
return response()->json(['data' => 'Bad challenge.'], 401); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// If the uuid already exists then just send them the record. |
|
129
|
|
|
if ($device = Device::where('uuid', $request->input('uuid'))->first()) { |
|
130
|
|
|
return response()->json([ 'data' => [ |
|
131
|
|
|
'name' => $device->name, |
|
132
|
|
|
'uuid' => $device->uuid, |
|
133
|
|
|
'id' => $device->id, |
|
134
|
|
|
'token' => $device->token, |
|
135
|
|
|
]], 200); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// Create the new device |
|
139
|
|
|
$device = new Device; |
|
140
|
|
|
$device->name = 'New Device'; |
|
|
|
|
|
|
141
|
|
|
$device->uuid = $request->input('uuid'); |
|
|
|
|
|
|
142
|
|
|
$device->save(); |
|
143
|
|
|
|
|
144
|
|
|
// Create an api token for the new device. |
|
145
|
|
|
$device->generateToken(); |
|
146
|
|
|
|
|
147
|
|
|
// A 'Registered' event is created and will trigger any relevant |
|
148
|
|
|
// observers, such as sending a confirmation email or any |
|
149
|
|
|
// code that needs to be run as soon as the device is created. |
|
150
|
|
|
//event(new Registered(true)); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
// Return the new device info including the token. |
|
153
|
|
|
return response()->json([ 'data' => [ |
|
154
|
|
|
'name' => $device->name, |
|
|
|
|
|
|
155
|
|
|
'uuid' => $device->uuid, |
|
|
|
|
|
|
156
|
|
|
'id' => $device->id, |
|
|
|
|
|
|
157
|
|
|
'token' => $device->token, |
|
|
|
|
|
|
158
|
|
|
]], 201); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// HTTP STATUS CODES: |
|
163
|
|
|
// 200: OK. The standard success code and default option. |
|
164
|
|
|
// 201: Object created. Useful for the store actions. |
|
165
|
|
|
// 204: No content. When an action was executed successfully, but there is no content to return. |
|
166
|
|
|
// 206: Partial content. Useful when you have to return a paginated list of resources. |
|
167
|
|
|
// 400: Bad request. The standard option for requests that fail to pass validation. |
|
168
|
|
|
// 401: Unauthorized. The user needs to be authenticated. |
|
169
|
|
|
// 403: Forbidden. The user is authenticated, but does not have the permissions to perform an action. |
|
170
|
|
|
// 404: Not found. This will be returned automatically by Laravel when the resource is not found. |
|
171
|
|
|
// 500: Internal server error. Ideally you're not going to be explicitly returning this, but if something unexpected breaks, this is what your user is going to receive. |
|
172
|
|
|
// 503: Service unavailable. Pretty self explanatory, but also another code that is not going to be returned explicitly by the application. |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.