From 0eef056851e548798ad495f579691ab9e4b9222a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Fri, 28 Apr 2023 16:56:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Device.php | 10 +++++ app/Models/DeviceLog.php | 16 ++++++++ ..._04_28_113344_create_device_logs_table.php | 37 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 app/Models/DeviceLog.php create mode 100644 database/migrations/2023_04_28_113344_create_device_logs_table.php diff --git a/app/Models/Device.php b/app/Models/Device.php index aa5f9fe..81706f8 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use EloquentFilter\Filterable; +use Illuminate\Database\Eloquent\Relations\HasMany; class Device extends Model { @@ -16,6 +17,10 @@ class Device extends Model public const TYPE_WATER_QUALITY = 3; //水质设备 public const TYPE_METEOROLOGICAL = 4; //气象设备 + public const STATE_DISABLED = 0; + public const STATE_ONLINE = 1; + public const STATE_OFFLINE = 2; + public const STATE_FAULT = 3; protected $fillable = [ 'name', 'sn', 'powered_by', 'type', 'model_sn', 'state', 'extends', @@ -26,6 +31,11 @@ class Device extends Model return $date->format('Y-m-d H:i:s'); } + public function logs(): HasMany + { + return $this->hasMany(DeviceLog::class); + } + public static function typeMap() { return [ diff --git a/app/Models/DeviceLog.php b/app/Models/DeviceLog.php new file mode 100644 index 0000000..fe2bfd6 --- /dev/null +++ b/app/Models/DeviceLog.php @@ -0,0 +1,16 @@ + 'json', + 'reported_at' => 'datetime', + ]; +} diff --git a/database/migrations/2023_04_28_113344_create_device_logs_table.php b/database/migrations/2023_04_28_113344_create_device_logs_table.php new file mode 100644 index 0000000..fd9332c --- /dev/null +++ b/database/migrations/2023_04_28_113344_create_device_logs_table.php @@ -0,0 +1,37 @@ +id(); + $table->unsignedBigInteger('device_id'); + $table->json('data')->nullable(); + $table->timestamp('reported_at'); + $table->timestamps(); + + $table->index(['device_id', 'reported_at']); + $table->index('reported_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('device_logs'); + } +};