設備 心跳封包 資料驗証多加入 branch_name與room.type,room.name 用於資料正規

20250522
This commit is contained in:
allen.yan 2025-05-22 15:25:12 +08:00
parent ab3db35352
commit 9b8499f655
5 changed files with 56 additions and 20 deletions

View File

@ -8,6 +8,7 @@ use App\Http\Requests\ReceiveRoomStatusDataRequest;
use App\Services\TcpSocketClient; use App\Services\TcpSocketClient;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use App\Models\Branch;
use App\Models\Room; use App\Models\Room;
use App\Models\MachineStatus; use App\Models\MachineStatus;
use App\Enums\RoomStatus; use App\Enums\RoomStatus;
@ -91,9 +92,18 @@ class RoomControlController extends Controller
$validated = $request->validated(); // branch_id, room_name, room_ip $validated = $request->validated(); // branch_id, room_name, room_ip
// 5. 找出對應包廂 // 5. 找出對應包廂
$room = Room::where('branch_id', $validated['branch_id']) $roomType = null;
->where('name', $validated['room_name']) $roomName = null;
->first(); // 從 room_name例如 PC101, SVR01中擷取 type 與 name
if (preg_match('/^([A-Za-z]+)(\d+)$/', $validated['room_name'], $matches)) {
$roomType = strtolower($matches[1]); // 'PC' → 'pc'
$roomName = $matches[2]; // '101'
}
$branch=Branch::where('name',$validated['branch_name'])->first();
$room = Room::where('branch_id', $branch->id)
->where('name', $roomName)
->where('type', $roomType)
->first();
if (!$room) { if (!$room) {
return ApiResponse::error('找不到對應包廂'); return ApiResponse::error('找不到對應包廂');
@ -162,8 +172,33 @@ class RoomControlController extends Controller
*/ */
public function StatusReport(ReceiveRoomStatusDataRequest $request) public function StatusReport(ReceiveRoomStatusDataRequest $request)
{ {
$validated = $request->validated();
$roomType = null;
$roomName = null;
// 從 room_name例如 PC101, SVR01中擷取 type 與 name
if (preg_match('/^([A-Za-z]+)(\d+)$/', $validated['hostname'], $matches)) {
$roomType = strtolower($matches[1]); // 'PC' → 'pc'
$roomName = $matches[2]; // '101'
}
$branch=Branch::where('name',$validated['branch_name'])->first();
$room = Room::where('branch_id', $branch->id)
->where('name', $roomName)
->where('type', $roomType)
->first();
// 決定 status 欄位值
$validated['status']= 'error';
if($room){
$validated['status']= 'online';
if($room->internal_ip != $validated['ip']){
$room->internal_ip = $validated['ip'];
$validated['status']='error';
}
$room->is_online=1;
$room->save();
}
return ApiResponse::success([ return ApiResponse::success([
'data' => MachineStatus::create($request->validated()), 'data' => MachineStatus::create($validated),
]); ]);
} }
/** /**

View File

@ -25,7 +25,7 @@ class ReceiveRoomRegisterRequest extends ApiRequest
public function rules(): array public function rules(): array
{ {
return [ return [
'branch_id' => 'required|integer|exists:branches,id', 'branch_name' => 'required|string|exists:branches,name',
'room_name' => 'required|string', 'room_name' => 'required|string',
'room_ip' => 'required|string', 'room_ip' => 'required|string',
'email' => 'required|email', 'email' => 'required|email',

View File

@ -8,12 +8,11 @@ use Illuminate\Foundation\Http\FormRequest;
* @OA\Schema( * @OA\Schema(
* schema="ReceiveRoomStatusDataRequest", * schema="ReceiveRoomStatusDataRequest",
* required={"hostname", "ip", "status"}, * required={"hostname", "ip", "status"},
* @OA\Property(property="hostname", type="string", example=""), * @OA\Property(property="hostname", type="string", example="PC101"),
* @OA\Property(property="ip", type="string", example=""), * @OA\Property(property="ip", type="string", example="192.168.XX.XX"),
* @OA\Property(property="cpu", type="numeric", example=""), * @OA\Property(property="cpu", type="numeric", example="0.00"),
* @OA\Property(property="memory", type="numeric", example=""), * @OA\Property(property="memory", type="numeric", example="25603"),
* @OA\Property(property="disk", type="numeric", example=""), * @OA\Property(property="disk", type="numeric", example="158266.49"),
* @OA\Property(property="status", type="string", example=""),
* ) * )
*/ */
class ReceiveRoomStatusDataRequest extends ApiRequest class ReceiveRoomStatusDataRequest extends ApiRequest
@ -31,7 +30,6 @@ class ReceiveRoomStatusDataRequest extends ApiRequest
'cpu' => 'nullable|numeric', 'cpu' => 'nullable|numeric',
'memory' => 'nullable|numeric', 'memory' => 'nullable|numeric',
'disk' => 'nullable|numeric', 'disk' => 'nullable|numeric',
'status' => 'required|string',
]; ];
} }
} }

View File

@ -8,17 +8,19 @@ use Illuminate\Database\Eloquent\Model;
* @OA\Schema( * @OA\Schema(
* schema="MachineStatus", * schema="MachineStatus",
* type="object", * type="object",
* @OA\Property(property="hostname", type="string", example=""), * @OA\Property(property="branch_name", type="string", example="測試"),
* @OA\Property(property="ip", type="string", example=""), * @OA\Property(property="hostname", type="string", example="PC101"),
* @OA\Property(property="cpu", type="string"), * @OA\Property(property="ip", type="string", example="192.168.XX.XX"),
* @OA\Property(property="memory", type="string", example=""), * @OA\Property(property="cpu", type="numeric", example="0.00"),
* @OA\Property(property="disk", type="string", example=""), * @OA\Property(property="memory", type="numeric", example="25603"),
* @OA\Property(property="status", type="string", example=""), * @OA\Property(property="disk", type="numeric", example="158266.49"),
* @OA\Property(property="status", type="string", example="online,error"),
* ) * )
*/ */
class MachineStatus extends Model class MachineStatus extends Model
{ {
protected $fillable = [ protected $fillable = [
'branch_name',
'hostname', 'hostname',
'ip', 'ip',
'cpu', 'cpu',

View File

@ -13,11 +13,12 @@ return new class extends Migration
{ {
Schema::create('machine_statuses', function (Blueprint $table) { Schema::create('machine_statuses', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('branch_name');
$table->string('hostname'); $table->string('hostname');
$table->string('ip')->nullable(); $table->string('ip')->nullable();
$table->decimal('cpu', 5, 2)->nullable(); $table->decimal('cpu', 5, 2)->nullable();
$table->decimal('memory', 5, 2)->nullable(); $table->unsignedInteger('memory')->nullable();
$table->decimal('disk', 5, 2)->nullable(); $table->decimal('disk', 10, 2)->nullable();
$table->string('status'); $table->string('status');
$table->timestamps(); $table->timestamps();
}); });