IMEI device API: telemetry, commands, receipts + migrations and routing

This commit is contained in:
2025-09-02 18:29:27 +10:00
parent e0bb2ea67c
commit 0868b04298
9 changed files with 204 additions and 0 deletions

21
app/Models/Command.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Command extends Model
{
protected $table = 'command_queue';
protected $fillable = [
'device_id','command_type','payload','priority','status',
'not_before_at','expires_at','created_by_user_id',
];
protected $casts = [
'payload' => 'array',
'not_before_at' => 'datetime',
'expires_at' => 'datetime',
];
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CommandReceipt extends Model
{
protected $fillable = [
'command_id','device_id','acked_at','executed_at','result','result_detail',
];
protected $casts = [
'acked_at' => 'datetime',
'executed_at' => 'datetime',
];
}

18
app/Models/Device.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Device extends Model
{
protected $fillable = [
'user_id','name','imei','is_active','last_seen_at','last_ip',
'firmware_version','hardware_version',
];
protected $casts = [
'is_active' => 'boolean',
'last_seen_at' => 'datetime',
];
}

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('devices', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->string('name')->nullable();
$table->string('imei', 32)->unique();
$table->boolean('is_active')->default(true);
$table->timestamp('last_seen_at')->nullable()->index();
$table->string('last_ip')->nullable();
$table->string('firmware_version')->nullable();
$table->string('hardware_version')->nullable();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('devices');
}
};

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('device_status', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('device_id')->unique();
$table->boolean('is_car_on')->default(false);
$table->unsignedTinyInteger('battery_percent')->nullable();
$table->boolean('external_power')->default(false);
$table->unsignedInteger('sleep_interval_sec')->default(60);
$table->timestamp('last_gps_fix_at')->nullable();
$table->timestamp('reported_at')->nullable()->index();
$table->timestamps();
$table->foreign('device_id')->references('id')->on('devices')->onDelete('cascade');
});
}
public function down(): void
{
Schema::dropIfExists('device_status');
}
};

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('device_telemetry', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('device_id')->index();
$table->timestamp('recorded_at')->index();
$table->decimal('lat', 10, 7);
$table->decimal('lng', 10, 7);
$table->decimal('altitude_m', 8, 2)->nullable();
$table->decimal('speed_kmh', 6, 2)->nullable();
$table->decimal('heading_deg', 6, 2)->nullable();
$table->decimal('accuracy_m', 8, 2)->nullable();
$table->json('raw')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->foreign('device_id')->references('id')->on('devices')->onDelete('cascade');
});
}
public function down(): void
{
Schema::dropIfExists('device_telemetry');
}
};

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('command_queue', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('device_id')->index();
$table->string('command_type', 64);
$table->json('payload')->nullable();
$table->unsignedTinyInteger('priority')->default(5);
$table->enum('status', ['queued','sent','acked','failed','expired'])->default('queued')->index();
$table->timestamp('not_before_at')->nullable()->index();
$table->timestamp('expires_at')->nullable()->index();
$table->unsignedBigInteger('created_by_user_id')->nullable();
$table->timestamps();
$table->foreign('device_id')->references('id')->on('devices')->onDelete('cascade');
});
}
public function down(): void
{
Schema::dropIfExists('command_queue');
}
};

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('command_receipts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('command_id')->index();
$table->unsignedBigInteger('device_id')->index();
$table->timestamp('acked_at')->nullable();
$table->timestamp('executed_at')->nullable();
$table->enum('result', ['ok','error'])->nullable();
$table->text('result_detail')->nullable();
$table->timestamps();
$table->foreign('command_id')->references('id')->on('command_queue')->onDelete('cascade');
$table->foreign('device_id')->references('id')->on('devices')->onDelete('cascade');
});
}
public function down(): void
{
Schema::dropIfExists('command_receipts');
}
};

View File

@ -2,6 +2,7 @@
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use App\Http\Controllers\GpsController; use App\Http\Controllers\GpsController;
use App\Http\Controllers\DeviceApiController;
Route::get('/health', fn () => response()->json(['ok' => true])); Route::get('/health', fn () => response()->json(['ok' => true]));