1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Gate; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use App\Device; |
9
|
|
|
use App\Site; |
10
|
|
|
use App\Location; |
11
|
|
|
use Carbon\Carbon; |
12
|
|
|
|
13
|
|
|
class DashboardController extends Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Create a new controller instance. |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->middleware('auth'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Show the application dashboard. |
26
|
|
|
* |
27
|
|
|
* @param Request $request |
28
|
|
|
* @return \Illuminate\Http\Response||\Illuminate\Http\JsonResponse |
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
public function index(Request $request) |
31
|
|
|
{ |
32
|
|
View Code Duplication |
if (Gate::denies('index-dashboard')) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
if (\Request::ajax()) { |
35
|
|
|
return response()->json("Please contact [email protected] to request access.", 403); |
36
|
|
|
} else { |
37
|
|
|
return redirect()->route('home') |
38
|
|
|
->with('failure', 'Please contact [email protected] to request access.'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$request->validate([ |
43
|
|
|
'site_id' => 'sometimes|required|integer|digits_between:1,10', |
44
|
|
|
'location_id' => 'sometimes|required|integer|digits_between:1,10', |
45
|
|
|
'device_id' => 'sometimes|required|integer|digits_between:1,10', |
46
|
|
|
'page' => 'sometimes|required|integer|digits_between:1,10', |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
if (count($request->all()) == 0) |
50
|
|
|
{ |
51
|
|
|
//Get the device, location, and site ids based on the user's preferred device |
52
|
|
|
$device = Auth::user()->preferredDevice ?? null; |
53
|
|
|
$location = $device->location ?? null; |
54
|
|
|
|
55
|
|
|
//If the location exists then set the pagination page to be where the device is located |
56
|
|
|
if ($location != null) |
57
|
|
|
$request->merge([ 'page' => $device->dashPageNum(4) ]); |
|
|
|
|
58
|
|
|
$site_id = $location->site_id ?? 0; |
59
|
|
|
$location_id = $location->id ?? 0; |
60
|
|
|
$device_id = $device->id ?? 0; |
61
|
|
|
} |
62
|
|
|
else |
63
|
|
|
{ |
64
|
|
|
//Get the active site, location, and device ids |
65
|
|
|
$site_id = $request->site_id ?? 0; |
66
|
|
|
$location_id = $request->location_id ?? 0; |
67
|
|
|
$device_id = $request->device_id ?? 0; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
//Get all sites with the selected site first |
71
|
|
|
$sites = Site::select('id', 'name') |
72
|
|
|
->orderByRaw("id = ? DESC", $site_id) |
|
|
|
|
73
|
|
|
->orderBy('name', 'ASC') |
74
|
|
|
->get(); |
75
|
|
|
//Get all locations for the selected site with the selected location first |
76
|
|
|
$locations = Location::select('id', 'name', 'site_id') |
77
|
|
|
->where('site_id', '=', $sites[ 0 ]->id ?? 0) |
78
|
|
|
->orderByRaw("id = ? DESC", $location_id) |
79
|
|
|
->orderBy('name', 'ASC') |
80
|
|
|
->get(); |
81
|
|
|
//Get all devices for the selected location |
82
|
|
|
$devices = Device::publicDashData() |
83
|
|
|
->leftJoin('deviceimages as image', 'devices.id', '=', 'image.device_id') |
84
|
|
|
->where('location_id', '=', $locations[ 0 ]->id ?? 0) |
85
|
|
|
->orderBy('name', 'ASC') |
86
|
|
|
->paginate(4); |
87
|
|
|
|
88
|
|
|
//Set the active device to the device defined by the given device id |
89
|
|
|
$active_device = $devices->where('id', $device_id)->first(); |
90
|
|
|
|
91
|
|
|
//Check if there were devices found |
92
|
|
|
if (!$devices->isEmpty()) |
93
|
|
|
{ |
94
|
|
|
//If the original device with the given device id was not found |
95
|
|
|
//Then assign the the first device in the device list as the active device |
96
|
|
|
if ($active_device == null) |
97
|
|
|
$active_device = $devices[ 0 ]; |
98
|
|
|
|
99
|
|
|
//Add an attribute to each device defining if it is stale |
100
|
|
|
$devices->transform(function($item) |
101
|
|
|
{ |
102
|
|
|
//Mark the device as stale if the device has missed three updates plus a minute |
103
|
|
|
$deviceStaleMins = ceil(($item->update_rate * 3) / 60) + 1; |
104
|
|
|
$item[ 'isDeviceStale' ] = ($item->last_network_update_at <= Carbon::now()->subMinute($deviceStaleMins)) ? true : false; |
|
|
|
|
105
|
|
|
|
106
|
|
|
//Mark the device image as stale if the device has missed three image updates plus a minute |
107
|
|
|
if ($item->image_updated_at != null) |
108
|
|
|
{ |
109
|
|
|
$imageStaleMins = ceil(($item->image_rate * 3) / 60) + 1; |
110
|
|
|
$item[ 'isImageStale' ] = ($item->image_updated_at <= Carbon::now()->subMinute($imageStaleMins)) ? true : false; |
111
|
|
|
} |
112
|
|
|
else |
113
|
|
|
$item[ 'isImageStale' ] = false; |
114
|
|
|
|
115
|
|
|
return $item; |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
//Store the active site, location, and device in a collection |
120
|
|
|
$active_data = collect([ 'device' => $active_device, 'location' => $locations[ 0 ] ?? null, 'site' => $sites[ 0 ] ?? null ]); |
121
|
|
|
|
122
|
|
|
//Use the device_list.blade.php to generate the device table html |
123
|
|
|
$html_device_table = view('dashboard.device_list', [ 'devices' => $devices, 'active_data' => $active_data ])->render(); |
124
|
|
|
|
125
|
|
|
if (\Request::ajax()) { |
126
|
|
|
return response()->json([ 'active_data' => $active_data, 'devices' => $devices, 'locations' => $locations, |
127
|
|
|
'sites' => $sites, 'html_device_table' => $html_device_table ]); |
128
|
|
|
} else { |
129
|
|
|
return view('dashboard.index', [ 'active_data' => $active_data, 'devices' => $devices, |
130
|
|
|
'locations' => $locations, 'sites' => $sites ]); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Show the development layouts for dashboard. |
136
|
|
|
* |
137
|
|
|
* @return \Illuminate\Http\Response |
138
|
|
|
*/ |
139
|
|
|
public function dev_layout() |
140
|
|
|
{ |
141
|
|
|
return view('dashboard.dev_layout'); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|