1
0
Fork 0
internet-everythings-agricu.../app/Models/Device.php

96 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Device extends Model
{
use Filterable;
public const TYPE_MONITOR = 1; //监控设备
public const TYPE_SOIL = 2; //土壤设备
public const TYPE_WATER_QUALITY = 3; //水质设备
public const TYPE_METEOROLOGICAL = 4; //气象设备
public const TYPE_AIR = 5; //通风设备
public const TYPE_ATOMIZING = 6; //喷雾设备
public const TYPE_INSECT = 7; //虫情监测
public const STATE_DISABLED = 0;
public const STATE_ONLINE = 1;
public const STATE_OFFLINE = 2;
public const STATE_FAULT = 3;
protected $casts = [
'extends' => 'json'
];
protected $fillable = [
'name', 'sn', 'powered_by', 'type', 'model_sn', 'state', 'extends'
];
public function scopePoweredBy(Builder $query, string $factory): void
{
$query->whereHas('factory', fn ($query) => $query->where('key', $factory));
}
protected function serializeDate(\DateTimeInterface $date){
return $date->format('Y-m-d H:i:s');
}
public static function typeMap()
{
return [
self::TYPE_MONITOR => '监控设备',
self::TYPE_SOIL => '土壤设备',
// self::TYPE_WATER_QUALITY => '水质设备',
self::TYPE_METEOROLOGICAL => '气象设备',
self::TYPE_AIR => '通风设备',
self::TYPE_ATOMIZING => '喷雾设备',
self::TYPE_INSECT => '虫情监测',
];
}
public static function stateMap(){
return [
self::STATE_DISABLED => '禁用',
self::STATE_ONLINE => '在线',
self::STATE_OFFLINE => '离线',
self::STATE_FAULT => '故障',
];
}
public function modes(){
return $this->belongsToMany(MonitorMode::class, MonitorDevice::class, 'device_id', 'monitor_id')->withPivot('fields');
}
public function logs(): HasMany
{
return $this->hasMany(DeviceLog::class);
}
public function factory()
{
return $this->belongsTo(Keyword::class, 'powered_by');
}
public function isTypeSoil(): bool
{
return $this->type === static::TYPE_SOIL;
}
public function isTypeMeteorological(): bool
{
return $this->type === static::TYPE_METEOROLOGICAL;
}
public function isTypeAir(): bool
{
return $this->type === static::TYPE_AIR;
}
}