All checks were successful
star-cloud-deploy-demo / deploy-demo (push) Successful in 36s
40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Jobs\Machine\ProcessMachineLog;
|
|
use App\Models\Machine\Machine;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class MachineController extends ApiController
|
|
{
|
|
/**
|
|
* 接收機台回傳的日誌 (IoT Endpoint)
|
|
* 採用異步處理 (Queue)
|
|
*/
|
|
public function storeLog(Request $request, int $id): JsonResponse
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'level' => 'required|string|in:info,warning,error',
|
|
'message' => 'required|string',
|
|
'context' => 'nullable|array',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return $this->errorResponse('Validation error', 422, $validator->errors());
|
|
}
|
|
|
|
// 檢查機台是否存在
|
|
if (!Machine::where('id', $id)->exists()) {
|
|
return $this->errorResponse('Machine not found', 404);
|
|
}
|
|
|
|
// 丟入隊列進行異步處理,回傳 202 Accepted
|
|
ProcessMachineLog::dispatch($id, $request->only(['level', 'message', 'context']));
|
|
|
|
return $this->successResponse([], 'Log accepted. Processing asynchronously.', 202);
|
|
}
|
|
}
|