You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
343 lines
11 KiB
343 lines
11 KiB
<?php |
|
|
|
namespace App\Http\Controllers\System\Monitor; |
|
|
|
use App\Exports\ArrayExportH; |
|
use Illuminate\Http\Request; |
|
use App\Http\Controllers\Controller; |
|
use App\Models\ExportFiles; |
|
use App\Models\PingIp; |
|
use Carbon\Carbon; |
|
use Illuminate\Support\Facades\DB; |
|
use Illuminate\Support\Facades\Log; |
|
use Maatwebsite\Excel\Facades\Excel; |
|
use Illuminate\Support\Str; |
|
|
|
class PingIpController extends Controller |
|
{ |
|
// |
|
public $serialNumber = [ |
|
// '20K888', |
|
// '20K999', |
|
// '20K003', |
|
// '20K001', |
|
// '20K002', |
|
// '20K003', |
|
// '20K004', |
|
'24H633', |
|
'24H634', |
|
'24H635', |
|
'24H636', |
|
]; |
|
public function index() |
|
{ |
|
|
|
// $s=[ |
|
// [ |
|
// 's' => '20K001', |
|
// 'ip' => '192.168.100.2' |
|
// ], |
|
// [ |
|
// 's' => '20K002', |
|
// 'ip' => '192.168.110.2' |
|
// ], |
|
// [ |
|
// 's' => '20K003', |
|
// 'ip' => '192.168.80.2' |
|
// ], |
|
// [ |
|
// 's' => '20K004', |
|
// 'ip' => '192.168.90.2' |
|
// ], |
|
// ]; |
|
// $t = Carbon::parse('2022-09-30 00:00:00'); |
|
// $arr=[]; |
|
// for($i = 0; $i < 1440 ;$i++){ |
|
// $r = rand(150,600); |
|
// if( $r > 550){ |
|
// $dt = $t->addSeconds(1); |
|
// } |
|
// else{ |
|
// $dt = $t; |
|
// } |
|
// array_push($arr,[ |
|
// 'ping_time'=>$dt->format('Y-m-d H:i:s').'.'.rand(150,600), |
|
// 'ip'=>$s[0]['ip'], |
|
// 'serialnumber'=>$s[0]['s'], |
|
// 'time' => 1 |
|
// ]); |
|
// if( $r > 550){ |
|
// $dt = $t->addSeconds(-1); |
|
// } |
|
// else{ |
|
// $dt = $t; |
|
// } |
|
// $t = $t->addMinutes(1); |
|
// } |
|
// // dd($arr); |
|
// foreach($arr as $a){ |
|
// PingIp::create($a); |
|
// } |
|
|
|
// $t = Carbon::parse('2022-09-30 00:00:00'); |
|
// $arr=[]; |
|
// for($i = 0; $i < 1440 ;$i++){ |
|
// $r = rand(150,600); |
|
// if( $r > 550){ |
|
// $dt = $t->addSeconds(1); |
|
// } |
|
// else{ |
|
// $dt = $t; |
|
// } |
|
// array_push($arr,[ |
|
// 'ping_time'=>$dt->format('Y-m-d H:i:s').'.'.rand(150,600), |
|
// 'ip'=>$s[1]['ip'], |
|
// 'serialnumber'=>$s[1]['s'], |
|
// 'time' => 1 |
|
// ]); |
|
// if( $r > 550){ |
|
// $dt = $t->addSeconds(-1); |
|
// } |
|
// else{ |
|
// $dt = $t; |
|
// } |
|
// $t = $t->addMinutes(1); |
|
// } |
|
// // dd($arr); |
|
// foreach($arr as $a){ |
|
// PingIp::create($a); |
|
// } |
|
|
|
// $t = Carbon::parse('2022-09-30 00:00:00'); |
|
// $arr=[]; |
|
// for($i = 0; $i < 1440 ;$i++){ |
|
// $r = rand(150,600); |
|
// if( $r > 550){ |
|
// $dt = $t->addSeconds(1); |
|
// } |
|
// else{ |
|
// $dt = $t; |
|
// } |
|
// array_push($arr,[ |
|
// 'ping_time'=>$dt->format('Y-m-d H:i:s').'.'.rand(150,600), |
|
// 'ip'=>$s[2]['ip'], |
|
// 'serialnumber'=>$s[2]['s'], |
|
// 'time' => 1 |
|
// ]); |
|
// if( $r > 550){ |
|
// $dt = $t->addSeconds(-1); |
|
// } |
|
// else{ |
|
// $dt = $t; |
|
// } |
|
// $t = $t->addMinutes(1); |
|
// } |
|
// // dd($arr); |
|
// foreach($arr as $a){ |
|
// PingIp::create($a); |
|
// } |
|
|
|
// $t = Carbon::parse('2022-09-30 00:00:00'); |
|
// $arr=[]; |
|
// for($i = 0; $i < 1440 ;$i++){ |
|
// $r = rand(150,600); |
|
// if( $r > 550){ |
|
// $dt = $t->addSeconds(1); |
|
// } |
|
// else{ |
|
// $dt = $t; |
|
// } |
|
// array_push($arr,[ |
|
// 'ping_time'=>$dt->format('Y-m-d H:i:s').'.'.rand(150,600), |
|
// 'ip'=>$s[3]['ip'], |
|
// 'serialnumber'=>$s[3]['s'], |
|
// 'time' => 1 |
|
// ]); |
|
// if( $r > 550){ |
|
// $dt = $t->addSeconds(-1); |
|
// } |
|
// else{ |
|
// $dt = $t; |
|
// } |
|
// $t = $t->addMinutes(1); |
|
// } |
|
// // dd($arr); |
|
// foreach($arr as $a){ |
|
// PingIp::create($a); |
|
// } |
|
|
|
$serialNumber = $this->serialNumber; |
|
return view('system.monitor.ping', compact('serialNumber')); |
|
} |
|
|
|
#region 輸出excel |
|
|
|
public function ExportExcel(Request $request, $status) |
|
{ |
|
// dd($request->all(),$processcheckStatus); |
|
#region 設備限制 |
|
// $device = explode(',', auth()->user()->devicetype); |
|
#endregion |
|
[$draw, $totalRecords, $totalRecordswithFilter, $data_arr] = $this->getData($request, $status); |
|
$columns = ['ping_time', 'serialnumber', 'ip', 'time']; |
|
$columnTitle = [ |
|
["【表單名稱】:". "前端攝影機設備狀態"], |
|
["【日期區間】:". $request->searchByFromdate. " ~ ". $request->searchByTodate], |
|
['檢測時間', '設備編號', 'IP', '延遲(ms)'] |
|
]; |
|
// $data_arr = $data_arr->toArray(); |
|
$data = array_map(function ($row) use ($columns) { |
|
$data = array_merge(array_flip($columns), array_intersect_key($row, array_flip($columns))); |
|
return $data; |
|
}, $data_arr); |
|
|
|
$fileName = 'ping-' . Str::random(10) . '.xlsx'; |
|
|
|
|
|
try { |
|
DB::beginTransaction(); |
|
ExportFiles::create([ |
|
'name' => $fileName, |
|
'path' => 'public/exports/monitor/', |
|
'type' => 'xlsx', |
|
'status' => '1', |
|
'remark' => "PingIP報表匯出", |
|
'user_id' => auth('api')->user()->id, |
|
]); |
|
$option = [ |
|
'printDef' => $request->printDef ?? 0, |
|
'showPN' => $request->showPN ?? 0, |
|
'showBorder' => $request->showBorder ?? 0, |
|
]; |
|
Excel::store(new ArrayExportH($data, $columnTitle, $option), 'public/exports/monitor/' . $fileName, 'local', \Maatwebsite\Excel\Excel::XLSX); |
|
DB::commit(); |
|
} catch (\Throwable $th) { |
|
Log::error("報表匯出失敗: " . $th->getMessage()); |
|
DB::rollBack(); |
|
} |
|
|
|
return response()->json(['url' => route('system.filedownload', ['path' => $fileName])], 200); |
|
} |
|
|
|
#endregion |
|
|
|
public function getDataTable(Request $request, $status) |
|
{ |
|
[$draw, $totalRecords, $totalRecordswithFilter, $data_arr] = $this->getData($request, $status); |
|
$response = array( |
|
"draw" => intval($draw), |
|
"iTotalRecords" => $totalRecords, |
|
"iTotalDisplayRecords" => $totalRecordswithFilter, |
|
"aaData" => $data_arr |
|
); |
|
echo json_encode($response); |
|
exit; |
|
} |
|
|
|
#region 取得資料 |
|
function getData($request, $status) |
|
{ |
|
// 審查狀態 0 未審 1 已審查 2 不舉發 99清冊 |
|
// $status = 2; |
|
// dd($device, $request, $status); |
|
# 日期搜尋值 |
|
if (isset($request->searchByFromdate)) |
|
$searchByFromdate = $request->searchByFromdate; |
|
if (isset($request->searchByTodate)) |
|
$searchByTodate = $request->searchByTodate; |
|
//地點搜尋 |
|
if (isset($request->location)) |
|
$location = $request->location; |
|
|
|
#region DataTable 搜尋屬性 |
|
$draw = $request->get('draw'); |
|
$start = $request->get("start"); |
|
$rowperpage = $request->get("length"); // Rows display per page |
|
|
|
$columnIndex_arr = $request->get('order'); |
|
$columnName_arr = $request->get('columns'); |
|
$order_arr = $request->get('order'); |
|
$search_arr = $request->get('search'); |
|
if ($status != 999){ |
|
$columnIndex = $columnIndex_arr[0]['column']; // Column index |
|
$columnName = $columnName_arr[$columnIndex]['data']; // Column name |
|
$columnSortOrder = $order_arr[0]['dir']; // asc or desc |
|
$searchValue = $search_arr['value']; // Search value |
|
} |
|
|
|
#endregion |
|
|
|
// Fetch records |
|
if ($status == 99) { |
|
$serialNumber = $this->serialNumber; |
|
$records = []; |
|
foreach ($serialNumber as $s) { |
|
$p = PingIp::where("serialnumber", $s)->orderby('ping_time', 'desc')->first(); |
|
if (isset($p)) |
|
array_push($records, $p); |
|
} |
|
$totalRecords = count($records); |
|
$totalRecordswithFilter = count($records); |
|
} else { |
|
$records = PingIp::query(); |
|
$totalRecords = $records->count(); |
|
if (isset($columnName)) |
|
$records->orderBy($columnName, $columnSortOrder); |
|
// if (isset($searchValue)) |
|
// $records->where('carnumber', 'like', '%' . $searchValue . '%'); |
|
//已審未審 |
|
|
|
//時間限制 |
|
if (isset($searchByFromdate)) |
|
$records->where('ping_time', ">", $searchByFromdate . ' 00:00:00'); |
|
else |
|
$records->where('ping_time', ">", Carbon::now()->subMonths(36)); |
|
if (isset($searchByTodate)) |
|
$records->where('ping_time', "<", $searchByTodate . ' 23:59:59'); |
|
//地點篩選 |
|
if (isset($location)) |
|
$records->where('serialnumber', $location); |
|
|
|
$totalRecordswithFilter = $records->count(); |
|
$records->select('*'); |
|
if (isset($start)) |
|
$records->skip($start); |
|
if (isset($rowperpage)) |
|
$records->take($rowperpage); |
|
$records = $records->get(); |
|
} |
|
|
|
|
|
$data_arr = array(); |
|
$sno = $start + 1; |
|
// dd($records); |
|
|
|
foreach ($records as $record) { |
|
//還原用id |
|
$id = $record->id; |
|
//不舉發原因 |
|
$ip = $record->ip; |
|
$symbol = $record->symbol; |
|
$ping_time = $record->ping_time; |
|
$serialnumber = $record->serialnumber; |
|
$time = $record->time; |
|
$ttl = $record->ttl; |
|
$status = $record->status; |
|
$data_arr[] = array( |
|
"id" => $id, |
|
"ip" => $ip, |
|
"time" => $time, |
|
"symbol" => $symbol, |
|
"serialnumber" => $serialnumber, |
|
"ping_time" => $ping_time, |
|
"ttl" => $ttl, |
|
"status" => $status, |
|
// "searchByFromdate" => $searchByFromdate, |
|
// "searchByTodate"=> $searchByTodate |
|
); |
|
} |
|
|
|
return [$draw, $totalRecords, $totalRecordswithFilter, $data_arr]; |
|
} |
|
#endregion |
|
}
|
|
|