From 7a842805abf9fcc56c27aa031e6934f33840248c Mon Sep 17 00:00:00 2001 From: Sam Rolfe Date: Wed, 10 Sep 2025 12:31:35 +1000 Subject: [PATCH] Add commands for posting and reboot --- app/Http/Controllers/Api/DeviceController.php | 11 +++++++++ app/Http/Controllers/DeviceApiController.php | 24 +++++++++++++++++++ routes/api.php | 1 + 3 files changed, 36 insertions(+) create mode 100644 app/Http/Controllers/Api/DeviceController.php diff --git a/app/Http/Controllers/Api/DeviceController.php b/app/Http/Controllers/Api/DeviceController.php new file mode 100644 index 0000000..e5d9ee1 --- /dev/null +++ b/app/Http/Controllers/Api/DeviceController.php @@ -0,0 +1,11 @@ +json(['ok' => true], 200); } + public function storeCommand(Request $request, $imei) + { + // 1. Validate the incoming data from the T3 dashboard + $validated = $request->validate([ + 'type' => 'required|string|in:wifi,sleep,telemetry_sec,poll_sec,ota,ring_fence,lights,camera,reboot', + 'payload' => 'present|nullable|array', + ]); + + // 2. Find the device by its IMEI, or create it if it's the first time we've seen it + $device = Device::firstOrCreate(['imei' => $imei]); + + // 3. Create the command and associate it with the device + $command = $device->commands()->create([ + 'type' => $validated['type'], + 'payload' => $validated['payload'], + 'status' => 'queued', // Set a default status + ]); + + // 4. Return a success response with the new command's ID + return response()->json([ + 'ok' => true, + 'command_id' => $command->id, + ], 201); // HTTP 201 Created + } } diff --git a/routes/api.php b/routes/api.php index 3d4f74e..fb2fa82 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,4 +13,5 @@ Route::get('/gps/latest-any', [GpsController::class, 'latestAny']); Route::get('/gps/recent', [GpsController::class, 'recent']); Route::post('/device/{imei}/telemetry', [DeviceApiController::class, 'telemetry']); Route::get('/device/{imei}/commands', [DeviceApiController::class, 'commands']); +Route::post('/device/{imei}/commands', [DeviceApiController::class, 'storeCommand']); Route::post('/device/{imei}/command-receipts', [DeviceApiController::class, 'commandReceipts']);