Merge branch 'develop' of https://gitea.hmily.club/liutk/internet-everythings-agricultural into develop
commit
2ed2ce6968
|
|
@ -2,17 +2,12 @@
|
|||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use Slowlyo\OwlAdmin\Renderers\Page;
|
||||
use Slowlyo\OwlAdmin\Renderers\Form;
|
||||
use Slowlyo\OwlAdmin\Renderers\TableColumn;
|
||||
use Slowlyo\OwlAdmin\Renderers\TextControl;
|
||||
use Slowlyo\OwlAdmin\Renderers\CRUDTable;
|
||||
use Slowlyo\OwlAdmin\Renderers\{Button, Form, Page, TableColumn, TextControl, Component, CRUDTable, Card, Video, DateRangeControl};
|
||||
use Slowlyo\OwlAdmin\Controllers\AdminController;
|
||||
use App\Services\Admin\DeviceService;
|
||||
use App\Models\Device;
|
||||
use App\Models\Keyword;
|
||||
use App\Admin\Components;
|
||||
use Slowlyo\OwlAdmin\Renderers\Button;
|
||||
|
||||
class DeviceController extends AdminController
|
||||
{
|
||||
|
|
@ -68,18 +63,12 @@ class DeviceController extends AdminController
|
|||
\amisMake()->SelectControl()->name('powered_by')->label('厂家')->options(Keyword::getByParentKey('device-factory')->pluck('name', 'id')->toArray())->required(true),
|
||||
TextControl::make()->name('model_sn')->label('型号'),
|
||||
\amisMake()->RadiosControl()->name('type')->label('类型')->options(Device::typeMap())->required(true),
|
||||
//监控设备-额外参数
|
||||
\amisMake()->TextControl()->name('extends.ip')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label('设备IP'),
|
||||
\amisMake()->TextControl()->name('extends.username')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label('设备用户名'),
|
||||
\amisMake()->TextControl()->name('extends.password')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label('设备密码'),
|
||||
\amisMake()->GroupControl()->body([
|
||||
\amisMake()->TextControl()->name('extends.live_port')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label('实时端口'),
|
||||
\amisMake()->TextControl()->name('extends.live_channel')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label('实时通道'),
|
||||
]),
|
||||
\amisMake()->GroupControl()->body([
|
||||
\amisMake()->TextControl()->name('extends.back_port')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label('回放端口'),
|
||||
\amisMake()->TextControl()->name('extends.back_channel')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label('回放通道'),
|
||||
]),
|
||||
// 监控设备-额外参数
|
||||
// rtsp://admin:lcdx12345@172.16.40.2:554/Streaming/Channels/5201
|
||||
TextControl::make()->name('extends.rtsp_url')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label(__('device.rtsp_url')),
|
||||
// rtsp://admin:lcdx12345@172.16.40.2:554/Streaming/tracks/5201
|
||||
TextControl::make()->name('extends.rtsp_history')->hiddenOn('data.type != '.Device::TYPE_MONITOR)->label(__('device.rtsp_history')),
|
||||
|
||||
Components::make()->keywordsTagControl('group_tags', '分组', 'device-group'),
|
||||
Components::make()->sortControl('sort', __('admin.order')),
|
||||
TextControl::make()->name('is_enable')->type('switch')->default(1)->label('显示'),
|
||||
|
|
@ -101,64 +90,60 @@ class DeviceController extends AdminController
|
|||
/**
|
||||
* 监控设备列表
|
||||
*/
|
||||
public function monitorList(){
|
||||
return CRUDTable::make()->mode('cards')->columnsCount(3)
|
||||
->data([
|
||||
'items' => [
|
||||
[
|
||||
'id'=>1,
|
||||
'src'=>''
|
||||
// ws://183.221.204.29:8100/rtsp?url=cnRzcDovL2FkbWluOjEyMzQ1Njc4OXhAMTE3LjE3NC4xODQuMTE4OjkwMDkvY2FtL3JlYWxtb25pdG9yP2NoYW5uZWw9MSZzdWJ0eXBlPTA=
|
||||
],
|
||||
]
|
||||
])
|
||||
->filter([
|
||||
'title' => '搜索条件',
|
||||
'body' => [
|
||||
\amisMake()->TextControl()->name('name')->label('点位名称')->size('sm'),
|
||||
amis('submit')->label(__('admin.search'))->level('primary'),
|
||||
]
|
||||
])
|
||||
public function monitorList()
|
||||
{
|
||||
if ($this->actionOfGetData()) {
|
||||
return $this->response()->success($this->service->list());
|
||||
}
|
||||
return CRUDTable::make()
|
||||
->mode('cards')
|
||||
->hideCheckToggler()
|
||||
->columnsCount(3)
|
||||
->perPage(6)
|
||||
->affixHeader(false)
|
||||
->filterTogglable(true)
|
||||
->set('primaryField', $this->service->primaryKey())
|
||||
->api(admin_url($this->queryPath . '?_action=getData&_type=' . Device::TYPE_MONITOR))
|
||||
->footerToolbar(['statistics', 'pagination'])
|
||||
->headerToolbar([])
|
||||
->filter($this->baseFilter()->actions([])->body([
|
||||
TextControl::make()->name('name')->label('点位名称')->size('md'),
|
||||
Button::make()->label(__('admin.reset'))->actionType('clear-and-submit'),
|
||||
Component::make()->setType('submit')->label(__('admin.search'))->level('primary'),
|
||||
]))
|
||||
->actions([])
|
||||
->itemClassName('col-sm-4')
|
||||
->card([
|
||||
'header' => [],
|
||||
'body' => amisMake()->Video()
|
||||
// ->isLive(true)->videoType('video/x-flv')->muted(true)->autoPlay(true)
|
||||
->src('${src}')
|
||||
]);
|
||||
->card(Card::make()->header(['title' => '$name'])->body(Video::make()->videoType('video/x-flv')->muted(true)->autoPlay(true)->src('${src}')));
|
||||
}
|
||||
|
||||
/**
|
||||
* 监控历史视频
|
||||
*/
|
||||
public function monitorVideoList(){
|
||||
return CRUDTable::make()->mode('cards')->columnsCount(3)
|
||||
->data([
|
||||
'items' => [
|
||||
[
|
||||
'id'=>1,
|
||||
'src'=>''
|
||||
// ws://183.221.204.29:8100/rtsp?url=cnRzcDovL2FkbWluOjEyMzQ1Njc4OXhAMTE3LjE3NC4xODQuMTE4OjkwMDkvY2FtL3JlYWxtb25pdG9yP2NoYW5uZWw9MSZzdWJ0eXBlPTA=
|
||||
],
|
||||
]
|
||||
])
|
||||
->filter([
|
||||
'title' => '搜索条件',
|
||||
'body' => [
|
||||
\amisMake()->TextControl()->name('name')->label('点位名称')->size('sm'),
|
||||
\amisMake()->DateRangeControl()->label('时间范围'),
|
||||
amis('submit')->label(__('admin.search'))->level('primary'),
|
||||
]
|
||||
])
|
||||
public function monitorVideoList()
|
||||
{
|
||||
if ($this->actionOfGetData()) {
|
||||
return $this->response()->success($this->service->list());
|
||||
}
|
||||
return CRUDTable::make()
|
||||
->mode('cards')
|
||||
->hideCheckToggler()
|
||||
->columnsCount(3)
|
||||
->perPage(6)
|
||||
->affixHeader(false)
|
||||
->filterTogglable(true)
|
||||
->set('primaryField', $this->service->primaryKey())
|
||||
->api(admin_url($this->queryPath . '?_action=getData&_type=' . Device::TYPE_MONITOR . '&_mode=history'))
|
||||
->footerToolbar(['statistics', 'pagination'])
|
||||
->headerToolbar([])
|
||||
->filter($this->baseFilter()->actions([])->body([
|
||||
TextControl::make()->name('name')->label('点位名称')->size('md'),
|
||||
DateRangeControl::make()->name('date')->label('日期')->maxDate('now')->size('md'),
|
||||
Button::make()->label(__('admin.reset'))->actionType('clear-and-submit'),
|
||||
Component::make()->setType('submit')->label(__('admin.search'))->level('primary'),
|
||||
]))
|
||||
->actions([])
|
||||
->itemClassName('col-sm-4')
|
||||
->card([
|
||||
'header' => [],
|
||||
'body' => amisMake()->Video()
|
||||
// ->isLive(true)->videoType('video/x-flv')->muted(true)->autoPlay(true)
|
||||
->src('${src}')
|
||||
]);
|
||||
->card(Card::make()->header(['title' => '$name'])->body(Video::make()->videoType('video/x-flv')->muted(true)->autoPlay(true)->src('${src}')));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -18,10 +18,7 @@ class SettingController extends AdminController
|
|||
|
||||
public function index()
|
||||
{
|
||||
$page = $this->basePage()->body([
|
||||
Alert::make()->showIcon(true)->body("此处内容仅供演示, 设置项无实际意义,实际开发中请根据实际情况进行修改。"),
|
||||
$this->form(),
|
||||
]);
|
||||
$page = $this->basePage()->body($this->form());
|
||||
|
||||
return $this->response()->success($page);
|
||||
}
|
||||
|
|
@ -37,13 +34,8 @@ class SettingController extends AdminController
|
|||
->body(
|
||||
Tabs::make()->tabs([
|
||||
Tab::make()->title('基本设置')->body([
|
||||
TextControl::make()->label('网站名称')->name('site_name'),
|
||||
InputKV::make()->label('附加配置')->name('addition_config'),
|
||||
]),
|
||||
Tab::make()->title('上传设置')->body([
|
||||
TextControl::make()->label('上传域名')->name('upload_domain'),
|
||||
TextControl::make()->label('上传路径')->name('upload_path'),
|
||||
]),
|
||||
TextControl::make()->label('RTSP 转流服务')->name('rtsp_url'),
|
||||
])
|
||||
])
|
||||
);
|
||||
}
|
||||
|
|
@ -51,10 +43,7 @@ class SettingController extends AdminController
|
|||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->only([
|
||||
'site_name',
|
||||
'addition_config',
|
||||
'upload_domain',
|
||||
'upload_path',
|
||||
'rtsp_url'
|
||||
]);
|
||||
|
||||
return settings()->adminSetMany($data);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,197 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Device;
|
||||
use App\Models\MeteorologicalMonitoringReport;
|
||||
use App\Models\SoilMonitoringReport;
|
||||
use App\Models\WaterQualityMonitoringReport;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class DeviceLogReportCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'device-log:report
|
||||
{factory}
|
||||
{--sleep=300 : 监控报告生成后的休眠时间(秒)}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '按厂家生成设备监控报告';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$factory = $this->argument('factory');
|
||||
|
||||
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 300, $this->option('sleep'));
|
||||
|
||||
while (true) {
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$devices = Device::with(['factory'])->poweredBy($factory)->get();
|
||||
|
||||
foreach ($devices as $device) {
|
||||
$this->createMonitoringReport($device);
|
||||
}
|
||||
|
||||
sleep($sleep);
|
||||
};
|
||||
}
|
||||
|
||||
protected function createMonitoringReport(Device $device)
|
||||
{
|
||||
switch ($device->factory?->key) {
|
||||
case 'link-os':
|
||||
$this->createLinkosDeviceMonitoringReport($device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected function createLinkosDeviceMonitoringReport(Device $device)
|
||||
{
|
||||
switch ($device->type) {
|
||||
case Device::TYPE_SOIL:
|
||||
$lastMonitoringReport = SoilMonitoringReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->first();
|
||||
|
||||
$lastReportedAt = $lastMonitoringReport?->reported_at;
|
||||
|
||||
$device->logs()
|
||||
->when($lastReportedAt, fn ($query, $lastReportedAt) => $query->where('reported_at', '>=', $lastReportedAt->reported_at))
|
||||
->oldest('reported_at')
|
||||
->chunkById(500, function ($deviceLogs) use ($device, &$lastMonitoringReport) {
|
||||
/** @var \App\Models\DeviceLog */
|
||||
foreach ($deviceLogs as $deviceLog) {
|
||||
$monitoringReport = SoilMonitoringReport::firstOrCreate(
|
||||
[
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $deviceLog->reported_at->startOfHour(),
|
||||
],
|
||||
Arr::except($lastMonitoringReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at'])
|
||||
);
|
||||
|
||||
foreach ([
|
||||
'conductivity' => 'conductivity',
|
||||
'soil_humidity' => 'humidity',
|
||||
'soil_temperature' => 'temperature',
|
||||
'nitrogen_content' => 'n',
|
||||
'potassium_content' => 'k',
|
||||
'phosphorus_content' => 'p',
|
||||
] as $key => $attribute) {
|
||||
if (! is_array($deviceLog->data) || ! array_key_exists($key, $deviceLog->data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$monitoringReport->{$attribute} = $deviceLog->data[$key];
|
||||
}
|
||||
|
||||
$lastMonitoringReport = tap($monitoringReport)->save();
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case Device::TYPE_WATER_QUALITY:
|
||||
$lastMonitoringReport = WaterQualityMonitoringReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->first();
|
||||
|
||||
$lastReportedAt = $lastMonitoringReport?->reported_at;
|
||||
|
||||
$device->logs()
|
||||
->when($lastReportedAt, fn ($query, $lastReportedAt) => $query->where('reported_at', '>=', $lastReportedAt->reported_at))
|
||||
->oldest('reported_at')
|
||||
->chunkById(500, function ($deviceLogs) use ($device, &$lastMonitoringReport) {
|
||||
/** @var \App\Models\DeviceLog */
|
||||
foreach ($deviceLogs as $deviceLog) {
|
||||
$monitoringReport = WaterQualityMonitoringReport::firstOrCreate(
|
||||
[
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $deviceLog->reported_at->startOfHour(),
|
||||
],
|
||||
Arr::except($lastMonitoringReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at'])
|
||||
);
|
||||
|
||||
foreach ([
|
||||
'chlorine' => 'chlorine',
|
||||
'conductivity' => 'conductivity',
|
||||
'oxygen' => 'oxygen',
|
||||
'ph' => 'ph',
|
||||
'temp' => 'temperature',
|
||||
'turbidity' => 'turbidity',
|
||||
] as $key => $attribute) {
|
||||
if (! is_array($deviceLog->data) || ! array_key_exists($key, $deviceLog->data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$monitoringReport->{$attribute} = $deviceLog->data[$key];
|
||||
}
|
||||
|
||||
$lastMonitoringReport = tap($monitoringReport)->save();
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case Device::TYPE_METEOROLOGICAL:
|
||||
$lastMonitoringReport = MeteorologicalMonitoringReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->first();
|
||||
|
||||
$lastReportedAt = $lastMonitoringReport?->reported_at;
|
||||
|
||||
$device->logs()
|
||||
->when($lastReportedAt, fn ($query, $lastReportedAt) => $query->where('reported_at', '>=', $lastReportedAt->reported_at))
|
||||
->oldest('reported_at')
|
||||
->chunkById(500, function ($deviceLogs) use ($device, &$lastMonitoringReport) {
|
||||
/** @var \App\Models\DeviceLog */
|
||||
foreach ($deviceLogs as $deviceLog) {
|
||||
$monitoringReport = MeteorologicalMonitoringReport::firstOrCreate(
|
||||
[
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $deviceLog->reported_at->startOfHour(),
|
||||
],
|
||||
Arr::except($lastMonitoringReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at'])
|
||||
);
|
||||
|
||||
foreach ([
|
||||
'current_rainfall' => 'today_rainfall',
|
||||
'day_rainfall' => 'yesterday_rainfall',
|
||||
'accumulate_rainfall' => 'accumulate_rainfall',
|
||||
'moment_rainfall' => 'moment_rainfall',
|
||||
'pm10_concentration' => 'pm10',
|
||||
'pm25_concentration' => 'pm25',
|
||||
'box_illumination' => 'box_illumination',
|
||||
'box_pressure' => 'box_pressure',
|
||||
'box_carbon' => 'box_co2',
|
||||
'box_temperature' => 'box_temperature',
|
||||
'box_humidity' => 'box_humidity',
|
||||
'box_noise' => 'box_noise',
|
||||
'wind_degree' => 'wind_degree',
|
||||
'wind_direction' => 'wind_direction',
|
||||
'wind_power' => 'wind_power',
|
||||
'wind_speed' => 'wind_speed',
|
||||
] as $key => $attribute) {
|
||||
if (! is_array($deviceLog->data) || ! array_key_exists($key, $deviceLog->data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$monitoringReport->{$attribute} = $deviceLog->data[$key];
|
||||
}
|
||||
|
||||
$lastMonitoringReport = tap($monitoringReport)->save();
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Device;
|
||||
use App\Services\DeviceLogService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Throwable;
|
||||
|
||||
class DeviceLogSyncCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'device-log:sync
|
||||
{factory}
|
||||
{--sleep=60 : 设备数据同步完成后的休眠时间(秒)}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '按设备厂商同步数据';
|
||||
|
||||
/**
|
||||
* @var \App\Services\DeviceLogService
|
||||
*/
|
||||
protected $deviceLogService;
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(DeviceLogService $deviceLogService)
|
||||
{
|
||||
$this->deviceLogService = $deviceLogService;
|
||||
|
||||
$factory = $this->argument('factory');
|
||||
|
||||
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 60, $this->option('sleep'));
|
||||
|
||||
while (true) {
|
||||
$this->runSync($factory);
|
||||
|
||||
sleep($sleep);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行同步
|
||||
*/
|
||||
protected function runSync(string $factory): void
|
||||
{
|
||||
$end = now();
|
||||
$start = $end->copy()->subHours(1);
|
||||
|
||||
$this->info('------------------------------------------');
|
||||
$this->info('开始时间: '. $start);
|
||||
$this->info('结束时间: '. $end);
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$devices = Device::with(['factory'])->poweredBy($factory)->get();
|
||||
|
||||
foreach ($devices as $device) {
|
||||
$this->info('==========================================');
|
||||
$this->info('设备编号: ' . $device->sn);
|
||||
$this->info('设备名称: ' . $device->name);
|
||||
$this->info('设备类型: ' . match ($device->type) {
|
||||
Device::TYPE_SOIL => '土壤设备',
|
||||
Device::TYPE_WATER_QUALITY => '水质设备',
|
||||
Device::TYPE_METEOROLOGICAL => '气象设备',
|
||||
Device::TYPE_AIR => '通风设备',
|
||||
default => '其它',
|
||||
});
|
||||
|
||||
try {
|
||||
$this->deviceLogService->sync($device, $start, $end);
|
||||
|
||||
$this->info('同步成功!');
|
||||
} catch (Throwable $e) {
|
||||
report($e);
|
||||
|
||||
$this->error('同步失败: '. $e->getMessage());
|
||||
}
|
||||
|
||||
$this->info('==========================================');
|
||||
}
|
||||
|
||||
$this->info('------------------------------------------');
|
||||
$this->newLine();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,14 +2,13 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use EloquentFilter\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Device extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use Filterable;
|
||||
|
||||
public const TYPE_MONITOR = 1; //监控设备
|
||||
|
|
@ -24,12 +23,22 @@ class Device extends Model
|
|||
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',
|
||||
'is_enable', 'sort', 'is_recommend',
|
||||
'group_tags'
|
||||
];
|
||||
|
||||
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');
|
||||
}
|
||||
|
|
@ -51,7 +60,13 @@ class Device extends Model
|
|||
return $this->hasMany(DeviceLog::class);
|
||||
}
|
||||
|
||||
public function factory(){
|
||||
public function factory()
|
||||
{
|
||||
return $this->belongsTo(Keyword::class, 'powered_by');
|
||||
}
|
||||
|
||||
public function isTypeSoil(): bool
|
||||
{
|
||||
return $this->type === static::TYPE_SOIL;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MeteorologicalMonitoringReport extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $casts = [
|
||||
'reported_at' => 'datetime'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'device_id',
|
||||
'today_rainfall',
|
||||
'yesterday_rainfall',
|
||||
'accumulate_rainfall',
|
||||
'moment_rainfall',
|
||||
'pm10',
|
||||
'pm25',
|
||||
'box_illumination',
|
||||
'box_pressure',
|
||||
'box_co2',
|
||||
'box_temperature',
|
||||
'box_humidity',
|
||||
'box_noise',
|
||||
'wind_degree',
|
||||
'wind_direction',
|
||||
'wind_power',
|
||||
'wind_speed',
|
||||
'reported_at',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SoilMonitoringReport extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $casts = [
|
||||
'reported_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'device_id',
|
||||
'temperature',
|
||||
'humidity',
|
||||
'n',
|
||||
'p',
|
||||
'k',
|
||||
'conductivity',
|
||||
'reported_at',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class WaterQualityMonitoringReport extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $casts = [
|
||||
'reported_at' => 'datetime'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'device_id',
|
||||
'chlorine',
|
||||
'conductivity',
|
||||
'oxygen',
|
||||
'ph',
|
||||
'temperature',
|
||||
'turbidity',
|
||||
'reported_at',
|
||||
];
|
||||
}
|
||||
|
|
@ -16,4 +16,33 @@ class DeviceService extends BaseService
|
|||
protected array $withRelationships = ['factory'];
|
||||
|
||||
protected string $modelFilterName = DeviceFilter::class;
|
||||
|
||||
public function list()
|
||||
{
|
||||
$query = $this->listQuery();
|
||||
|
||||
$items = (clone $query)->paginate(request()->input('perPage', 20))->items();
|
||||
$base = settings()->get('rtsp_url');
|
||||
if (request('_type') == Device::TYPE_MONITOR) {
|
||||
$history = request('_mode') === 'history';
|
||||
$date = explode(',', request('date'));
|
||||
$start = data_get($date, 0);
|
||||
$end = data_get($date, 1);
|
||||
foreach ($items as &$item) {
|
||||
$url = data_get($item->extends, $history ? 'rtsp_history' : 'rtsp_url');
|
||||
$src = null;
|
||||
if ($base && $url) {
|
||||
// 查看历史监控
|
||||
if ($history && $start && $end) {
|
||||
$url .= '?start=' . date('Y-m-d', $start) . '&end=' . data('Y-m-d', $end);
|
||||
}
|
||||
$src = $base . base64_encode($url);
|
||||
}
|
||||
$item->src = $src;
|
||||
}
|
||||
}
|
||||
$total = (clone $query)->count();
|
||||
|
||||
return compact('items', 'total');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Iot\Linkos\HttpClient as LinkosHttpClient;
|
||||
use App\Models\Device;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class DeviceLogService
|
||||
{
|
||||
/**
|
||||
* 同步设备历史流水
|
||||
*/
|
||||
public function sync(Device $device, Carbon $start, Carbon $end): void
|
||||
{
|
||||
switch ($device->factory?->key) {
|
||||
case 'link-os':
|
||||
$this->syncLinkosDeviceLogs($device, $start, $end);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步 Linkos 设备历史流水
|
||||
*/
|
||||
protected function syncLinkosDeviceLogs(Device $device, Carbon $start, Carbon $end): void
|
||||
{
|
||||
/** @var \App\Iot\Linkos\HttpClient */
|
||||
$httpClient = app(LinkosHttpClient::class);
|
||||
|
||||
$page = 1;
|
||||
|
||||
$perPage = 50;
|
||||
|
||||
do {
|
||||
$data = $httpClient->getDeviceFlowList(
|
||||
$device->sn, $start, $end, $page, $perPage
|
||||
);
|
||||
|
||||
$countResults = count($data['content']);
|
||||
|
||||
if ($countResults === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($data['content'] as $item) {
|
||||
$isSoilMonitoring = Arr::hasAny($item['data'], [
|
||||
'soil_humidity',
|
||||
'soil_temperature',
|
||||
'nitrogen_content',
|
||||
'potassium_content',
|
||||
'phosphorus_content',
|
||||
]);
|
||||
|
||||
if (($device->isTypeSoil() && ! $isSoilMonitoring) || (! $device->isTypeSoil() && $isSoilMonitoring)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$device->logs()->firstOrCreate([
|
||||
'reported_at' => $item['createTime'],
|
||||
], [
|
||||
'data' => empty($item['data']) ? (new \stdClass) : $item['data'],
|
||||
]);
|
||||
}
|
||||
|
||||
unset($data);
|
||||
|
||||
$page++;
|
||||
} while ($countResults === $perPage);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use App\Models\Device;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Device>
|
||||
*/
|
||||
class DeviceFactory extends Factory
|
||||
{
|
||||
protected $model = Device::class;
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
'name' => $this->faker->name,
|
||||
'sn' => $this->faker->isbn10(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('soil_monitoring_reports', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('device_id')->comment('设备ID');
|
||||
$table->decimal('temperature', 8, 2)->nullable()->comment('温度(单位: ℃)');
|
||||
$table->decimal('humidity', 8, 2)->nullable()->comment('湿度(单位: %RH)');
|
||||
$table->integer('n')->nullable()->comment('氮 (单位: mg/kg)');
|
||||
$table->integer('p')->nullable()->comment('磷 (单位: mg/kg)');
|
||||
$table->integer('k')->nullable()->comment('钾 (单位: mg/kg)');
|
||||
$table->decimal('conductivity', 8, 2)->nullable()->comment('电导率(单位: us/cm)');
|
||||
$table->timestamp('reported_at')->comment('报告时间(小时),示例: 2023-05-01 01:00:00');
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['device_id', 'reported_at']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('soil_monitoring_reports');
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('water_quality_monitoring_reports', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('device_id')->comment('设备ID');
|
||||
$table->decimal('chlorine', 8, 2)->nullable()->comment('余氯(单位: mg/L)');
|
||||
$table->decimal('conductivity', 8, 2)->nullable()->comment('电导率(单位: us/cm)');
|
||||
$table->decimal('oxygen', 8, 2)->nullable()->comment('溶解氧(单位: mg/L)');
|
||||
$table->decimal('ph', 8, 2)->nullable()->comment('PH');
|
||||
$table->decimal('temperature', 8, 2)->nullable()->comment('温度(单位: ℃)');
|
||||
$table->decimal('turbidity', 8, 2)->nullable()->comment('浊度(单位: NTU)');
|
||||
$table->timestamp('reported_at')->comment('报告时间(小时),示例: 2023-05-01 01:00:00');
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['device_id', 'reported_at']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('water_quality_monitoring_reports');
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('meteorological_monitoring_reports', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('device_id');
|
||||
$table->decimal('today_rainfall', 8, 2)->nullable()->comment('今日积雨量 (单位: mm)');
|
||||
$table->decimal('yesterday_rainfall', 8, 2)->nullable()->comment('昨日积雨量 (单位: mm)');
|
||||
$table->decimal('accumulate_rainfall', 8, 2)->nullable()->comment('累积雨量 (单位: mm)');
|
||||
$table->decimal('moment_rainfall', 8, 2)->nullable()->comment('瞬时雨量 (单位: mm)');
|
||||
$table->decimal('pm10', 8, 2)->nullable()->comment('PM10浓度 (单位: ug/m3)');
|
||||
$table->decimal('pm25', 8, 2)->nullable()->comment('PM2.5浓度 (单位: ug/m3)');
|
||||
$table->decimal('box_illumination', 10, 2)->nullable()->comment('百叶箱光照度 (单位: Lux)');
|
||||
$table->decimal('box_pressure', 8, 2)->nullable()->comment('百叶箱大气压力 (单位: Kpa)');
|
||||
$table->decimal('box_co2', 8, 2)->nullable()->comment('百叶箱CO2浓度 (单位: ppm)');
|
||||
$table->decimal('box_temperature', 8, 2)->nullable()->comment('百叶箱温度 (单位: ℃)');
|
||||
$table->decimal('box_humidity', 8, 2)->nullable()->comment('百叶箱湿度 (单位: %RH)');
|
||||
$table->decimal('box_noise', 8, 2)->nullable()->comment('百叶箱噪声 (单位: db)');
|
||||
$table->integer('wind_degree')->nullable()->comment('风向度数');
|
||||
$table->tinyInteger('wind_direction')->nullable()->comment('风向: 0 北风, 1 东北风, 2 东风, 3 东南风, 4 南风, 5 西南风, 6 西风, 7 西北风');
|
||||
$table->integer('wind_power')->nullable()->comment('风力 (单位: 级)');
|
||||
$table->decimal('wind_speed', 8, 2)->nullable()->comment('风速 (单位: m/s)');
|
||||
$table->timestamp('reported_at')->comment('报告时间(小时),示例: 2023-05-01 01:00:00');
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['device_id', 'reported_at']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('meteorological_monitoring_reports');
|
||||
}
|
||||
};
|
||||
|
|
@ -18,6 +18,7 @@ class BannerSeeder extends Seeder
|
|||
{
|
||||
$places = [
|
||||
['key' => 'h5-home-banner', 'name' => '首页广告位'],
|
||||
['key' => 'h5-home-special', 'name' => '首页园区要点'],
|
||||
];
|
||||
|
||||
Banner::truncate();
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@
|
|||
|
||||
namespace Database\Seeders;
|
||||
|
||||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
{
|
||||
use WithoutModelEvents;
|
||||
/**
|
||||
* Seed the application's database.
|
||||
*
|
||||
|
|
@ -16,5 +17,8 @@ class DatabaseSeeder extends Seeder
|
|||
{
|
||||
$this->call(AdminMenuSeeder::class);
|
||||
$this->call(AdminSeeder::class);
|
||||
$this->call(KeywordSeeder::class);
|
||||
$this->call(BannerSeeder::class);
|
||||
$this->call(SettingSeeder::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Device;
|
||||
use Database\Factories\DeviceFactory;
|
||||
|
||||
class DeviceSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
Device::truncate();
|
||||
(new DeviceFactory)->count(100)->create([
|
||||
'type' => Device::TYPE_MONITOR,
|
||||
// 'extends' => [
|
||||
// 'rtsp_url' => 'rtsp://admin:lcdx12345@172.16.40.2:554/Streaming/Channels/5201',
|
||||
// 'rtsp_history' => 'rtsp://admin:lcdx12345@172.16.40.2:554/Streaming/tracks/5201',
|
||||
// ]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Slowlyo\OwlAdmin\Models\AdminSetting;
|
||||
|
||||
class SettingSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$settings = [
|
||||
['key' => 'rtsp_url', 'values' => json_encode('ws://117.176.117.148:8100/rtsp?url=')]
|
||||
];
|
||||
AdminSetting::truncate();
|
||||
AdminSetting::insert($settings);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'rtsp_url' => 'RTSP 直播流',
|
||||
'rtsp_history' => 'RTSP 回放流',
|
||||
];
|
||||
Loading…
Reference in New Issue