|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Validator; |
|
8
|
|
|
use App\Device; |
|
9
|
|
|
use App\Site; |
|
10
|
|
|
use App\Location; |
|
11
|
|
|
|
|
12
|
|
|
class DashboardController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Create a new controller instance. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->middleware('auth'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Show the application dashboard. |
|
25
|
|
|
* |
|
26
|
|
|
* @return \Illuminate\Http\Response |
|
27
|
|
|
*/ |
|
28
|
|
|
public function index() |
|
29
|
|
|
{ |
|
30
|
|
|
//TODO use the users preferred device |
|
31
|
|
|
$device_id = Device::firstOrFail()->id; |
|
32
|
|
|
|
|
33
|
|
|
$deviceQuery = Device::publicDashData()->where('id', '=', $device_id)->with('location')->get(); |
|
34
|
|
|
$site_id = $deviceQuery[0]->location->site_id ?? 0; |
|
35
|
|
|
$location_id = $deviceQuery[0]->location->id ?? 0; |
|
36
|
|
|
|
|
37
|
|
|
//Get all sites with the selected site first |
|
38
|
|
|
$sites = Site::select('id', 'name') |
|
39
|
|
|
->orderByRaw("id = ? DESC", $site_id) |
|
40
|
|
|
->orderBy('name', 'ASC') |
|
41
|
|
|
->get(); |
|
42
|
|
|
//Get all locations for the selected site with the selected location first |
|
43
|
|
|
$locations = Location::select('id', 'name', 'site_id') |
|
44
|
|
|
->where('site_id', '=', $sites[0]->id ?? 0) |
|
45
|
|
|
->orderByRaw("id = ? DESC", $location_id) |
|
46
|
|
|
->orderBy('name', 'ASC') |
|
47
|
|
|
->get(); |
|
48
|
|
|
//Get all devices for the selected location |
|
49
|
|
|
$devices = Device::publicDashData() |
|
50
|
|
|
->where('location_id', '=', $locations[0]->id ?? 0) |
|
51
|
|
|
->orderBy('name', 'ASC') |
|
52
|
|
|
->limit(1) |
|
53
|
|
|
->get(); |
|
54
|
|
|
|
|
55
|
|
|
//Get the active device |
|
56
|
|
|
$active_device = $devices->where('id', $device_id)->first(); |
|
57
|
|
|
//Set the active device to the first device in $devices if it is not empty and the original active device wasn't found |
|
58
|
|
|
if (!$devices->isEmpty() && $active_device == null) |
|
59
|
|
|
$active_device = $devices[0]; |
|
60
|
|
|
|
|
61
|
|
|
//Store the active site, location, and device in a collection |
|
62
|
|
|
$active_data = collect([ 'device' => $active_device, 'location' => $locations[0] ?? null, 'site' => $sites[0] ?? null ]); |
|
63
|
|
|
|
|
64
|
|
|
return view('dashboard.index', [ 'active_data' => $active_data, 'devices' => $devices, |
|
65
|
|
|
'locations' => $locations, 'sites' => $sites ]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Refresh all the data on the dashboard except the image and sensor graphs |
|
70
|
|
|
* |
|
71
|
|
|
* @param Request $request |
|
72
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
73
|
|
|
*/ |
|
74
|
|
|
public function refreshPage(Request $request) |
|
75
|
|
|
{ |
|
76
|
|
|
$request->validate([ |
|
77
|
|
|
'site_id' => 'required|integer|digits_between:1,7', |
|
78
|
|
|
'location_id' => 'sometimes|required|integer|digits_between:1,7', |
|
79
|
|
|
'device_id' => 'sometimes|required|integer|digits_between:1,7', |
|
80
|
|
|
'offset' => 'sometimes|required|integer|digits_between:1,7', |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
//Todo limit the amount of queries based on if there is changes |
|
84
|
|
|
//Check for changes in database since last update |
|
85
|
|
|
|
|
86
|
|
|
//Limit the number of devices to be loaded |
|
87
|
|
|
$limit = 4; |
|
88
|
|
|
|
|
89
|
|
|
//Set the offset for device pagination |
|
90
|
|
|
$offset = $request->offset ?? 0; |
|
91
|
|
|
|
|
92
|
|
|
//Get the active site, location, and device ids |
|
93
|
|
|
$site_id = $request->site_id ?? 0; |
|
94
|
|
|
$location_id = $request->location_id ?? 0; |
|
95
|
|
|
$device_id = $request->device_id ?? 0; |
|
96
|
|
|
|
|
97
|
|
|
//TODO always have a un-deletable site and location and there will never be an error |
|
98
|
|
|
//Get all sites with the selected site first |
|
99
|
|
|
$sites = Site::select('id', 'name') |
|
100
|
|
|
->orderByRaw("id = ? DESC", $site_id) |
|
101
|
|
|
->orderBy('name', 'ASC') |
|
102
|
|
|
->get(); |
|
103
|
|
|
//Get all locations for the selected site with the selected location first |
|
104
|
|
|
$locations = Location::select('id', 'name', 'site_id') |
|
105
|
|
|
->where('site_id', '=', $sites[0]->id ?? 0) |
|
106
|
|
|
->orderByRaw("id = ? DESC", $location_id) |
|
107
|
|
|
->orderBy('name', 'ASC') |
|
108
|
|
|
->get(); |
|
109
|
|
|
//Get all devices for the selected location |
|
110
|
|
|
$devices = Device::publicDashData() |
|
111
|
|
|
->where('location_id', '=', $locations[0]->id ?? 0) |
|
112
|
|
|
->orderBy('name', 'ASC') |
|
113
|
|
|
->limit($limit) |
|
114
|
|
|
->offset($offset * $limit) |
|
115
|
|
|
->get(); |
|
116
|
|
|
//Get the total device count for the given location |
|
117
|
|
|
$deviceCount = Device::where('location_id', '=', $locations[0]->id ?? 0)->count(); |
|
118
|
|
|
|
|
119
|
|
|
//Get the active device |
|
120
|
|
|
$active_device = $devices->where('id', $device_id)->first(); |
|
121
|
|
|
//Set the active device to the first device in $devices if it is not empty and the original active device wasn't found |
|
122
|
|
|
if (!$devices->isEmpty() && $active_device == null) |
|
123
|
|
|
$active_device = $devices[0]; |
|
124
|
|
|
|
|
125
|
|
|
//Store the active site, location, and device in a collection |
|
126
|
|
|
$active_data = collect(['device' => $active_device, 'location' => $locations[0] ?? null, 'site' => $sites[0] ?? null]); |
|
127
|
|
|
|
|
128
|
|
|
//Get the total amount of pages for pagination |
|
129
|
|
|
$page_count = ceil($deviceCount / $limit) - 1; |
|
130
|
|
|
|
|
131
|
|
|
//Store pagination data |
|
132
|
|
|
$pag_data = collect(['offset' => $offset, 'page_count' => $page_count]); |
|
133
|
|
|
|
|
134
|
|
|
return response()->json([ 'active_data' => $active_data, 'devices' => $devices, 'locations' => $locations, |
|
135
|
|
|
'sites' => $sites, 'pag_data' => $pag_data]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Show the development layouts for dashboard. |
|
140
|
|
|
* |
|
141
|
|
|
* @return \Illuminate\Http\Response |
|
142
|
|
|
*/ |
|
143
|
|
|
public function dev_layout() |
|
144
|
|
|
{ |
|
145
|
|
|
return view('dashboard.dev_layout'); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|