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.
180 lines
7.0 KiB
180 lines
7.0 KiB
<?php |
|
|
|
namespace App\Console; |
|
|
|
use App\Class\Entry; |
|
use App\Class\ItlEntry; |
|
use App\Class\LineNotify; |
|
use App\Models\ExportFiles; |
|
use Illuminate\Console\Scheduling\Schedule; |
|
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; |
|
use Illuminate\Support\Str; |
|
use App\Exports\ArrayExport; |
|
use App\Models\Clientlog; |
|
use App\Models\Clientlogsecond; |
|
use App\Models\Serverlog; |
|
use App\Models\UserLog; |
|
use Carbon\Carbon; |
|
use DateInterval; |
|
use DateTime; |
|
use Maatwebsite\Excel\Facades\Excel; |
|
|
|
class Kernel extends ConsoleKernel |
|
{ |
|
/** |
|
* Define the application's command schedule. |
|
*/ |
|
protected function schedule(Schedule $schedule): void |
|
{ |
|
ini_set('memory_limit', '2048M'); |
|
// $schedule->command('inspire')->hourly(); |
|
// schedule function everyday 15:00 output |
|
$schedule->call(function () { |
|
// Entry::multisys_entry(14); |
|
// Entry::overspeedred_entry(14); |
|
|
|
// $ts_data = Entry::get_multisys_entry_data(1); |
|
// if (count($ts_data) > 0) { |
|
// Entry::multisys_entry_local($ts_data); |
|
// Entry::multisys_entry_post($ts_data); |
|
// } |
|
|
|
// $ovo_data = Entry::get_overspeedred_entry_data(1); |
|
// if (count($ovo_data) > 0) { |
|
// Entry::overspeedred_entry_local($ovo_data); |
|
// Entry::overspeedred_entry_post($ovo_data); |
|
// } |
|
|
|
// Entry::multisys_entry_post_img(); |
|
// Entry::overspeedred_entry_post_img(); |
|
}); |
|
$schedule->call(function () { |
|
$data = []; |
|
$message = []; |
|
// message 增加 系統發信時間 |
|
$message[] = "系統發信時間: " . date('Y-m-d H:i:s'); |
|
$message[] = "校時異常 or 斷線!!!請檢查設備!!!"; |
|
$server_log = Serverlog::orderBy('this_time', 'desc')->first(); |
|
$nowTime = new DateTime(); |
|
$server_time = DateTime::createFromFormat('Y-m-d H:i:s.u', Carbon::parse($server_log->this_time)->format('Y-m-d H:i:s.u')); |
|
|
|
// 如果Server log 的 diffsecond > 200 毫秒 或 this_time 與 現在的 的時間 差距大於或小於 |2.5分鐘| |
|
if ($server_log-> diffsecond > 200 || abs(strtotime($server_log->this_time) - strtotime($nowTime->format('Y-m-d H:i:s'))) > 150) { |
|
$message[] = "Server : " . $server_time . " " . $server_log->diffsecond . "ms"; |
|
} |
|
// 取得 clientLog 的最新時間紀錄 |
|
|
|
$distinct_name = Clientlog::cachedDistinctName(); |
|
// dd($distinct_name); |
|
foreach ($distinct_name as $name) { |
|
$client_log = Clientlog::where('name', $name->name)->orderBy('this_time', 'desc')->first(); |
|
if ($client_log) { |
|
$data[] = $client_log->toArray(); |
|
} |
|
} |
|
|
|
$distinct_name = Clientlogsecond::cachedDistinctName(); |
|
foreach ($distinct_name as $name) { |
|
$client_log = Clientlogsecond::where('name', $name->name)->orderBy('this_time', 'desc')->first(); |
|
if ($client_log) { |
|
$data[] = $client_log->toArray(); |
|
} |
|
} |
|
|
|
foreach ($data as $key => $value) { |
|
// 如果 $date->thistime 與 $server_log->this_time 的時間 差距大於 150秒 |
|
$thisTime = DateTime::createFromFormat('Y-m-d H:i:s.u', $value['this_time']); |
|
if ($thisTime !== false && abs(strtotime($value['this_time']) - strtotime($server_log->this_time)) > 150) { |
|
$message[] = "大於2.5分鐘: " . $value['name'] . " " . $value['this_time'] . " "; |
|
} |
|
//如果diffsecond > 200毫秒 (絕對值 diffsecond*1000 > 200) |
|
else if (abs($value['diffsecond']) > 200) { |
|
$message[] = "大於200毫秒: " . $value['name'] . " " . $value['this_time'] . " " . $value['diffsecond'] . "ms"; |
|
} |
|
} |
|
// dd($message); |
|
// if (count($message) > 2) { |
|
// $this->send("\n" . implode("\n", $message)); |
|
// } |
|
})->everyMinute(); |
|
$schedule->call(function () { |
|
$this->exportLog(); |
|
Entry::multisys_entry(14); |
|
Entry::overspeedred_entry(14); |
|
ItlEntry::interval_entry(14); |
|
}) |
|
// ->everyMinute(); |
|
->timezone('Asia/Taipei')->dailyAt('00:00'); |
|
|
|
$schedule |
|
->call(function () { |
|
// 步驟2 -撈出所有的建檔本地端儲存 |
|
$ts_data = Entry::get_multisys_entry_data(1); |
|
if (count($ts_data) > 0) { |
|
Entry::multisys_entry_local($ts_data); |
|
Entry::multisys_entry_post($ts_data); |
|
} |
|
|
|
$ovo_data = Entry::get_overspeedred_entry_data(1); |
|
if (count($ovo_data) > 0) { |
|
Entry::overspeedred_entry_local($ovo_data); |
|
Entry::overspeedred_entry_post($ovo_data); |
|
} |
|
$itl_data = ItlEntry::get_interval_entry_data(10); |
|
// Log::channel('entry')->notice($itl_data); |
|
if(count($itl_data)> 0){ |
|
ItlEntry::interval_entry_local($itl_data); |
|
ItlEntry::interval_entry_post($itl_data); |
|
} |
|
// dd(count($ts_data),count($vpk_data)); |
|
}) |
|
->everyMinute(); |
|
// ->timezone('Asia/Taipei')->dailyAt('17:24'); |
|
$schedule |
|
->call(function () { |
|
// 步驟3-送出圖片 |
|
Entry::multisys_entry_post_img(); |
|
Entry::overspeedred_entry_post_img(); |
|
ItlEntry::interval_entry_post_img(); |
|
}) |
|
->everyMinute(); |
|
// ->timezone('Asia/Taipei')->dailyAt('00:30'); |
|
} |
|
|
|
protected function exportLog() |
|
{ |
|
$data_arr = UserLog::OrderBy('id', 'desc')->get()->toArray(); |
|
$columns = ['user_name', 'ip', 'action_detail', 'created_at', 'remark']; |
|
$columnTitle = [ |
|
['使用者名稱', 'IP位址', '動作', '時間', '備註'] |
|
]; |
|
$data = array_map(function ($row) use ($columns) { |
|
return array_merge(array_flip($columns), array_intersect_key($row, array_flip($columns))); |
|
}, $data_arr); |
|
|
|
$fileName = 'userlog-' . Str::random(10) . '.xlsx'; |
|
ExportFiles::create([ |
|
'name' => $fileName, |
|
'path' => 'public/exports_log', |
|
'type' => 'xlsx', |
|
'status' => '1', |
|
'remark' => '操作紀錄匯出(自動排程)', |
|
'user_id' => 1, |
|
]); |
|
Excel::store(new ArrayExport($data, $columnTitle), 'public/exports_log/' . $fileName, 'local', \Maatwebsite\Excel\Excel::XLSX); |
|
} |
|
/** |
|
* Register the commands for the application. |
|
*/ |
|
protected function commands(): void |
|
{ |
|
$this->load(__DIR__ . '/Commands'); |
|
|
|
require base_path('routes/console.php'); |
|
} |
|
|
|
protected function send($message) |
|
{ |
|
LineNotify::send($message); |
|
} |
|
}
|
|
|