Compare commits
4 Commits
ba69244e2d
...
0e10085b6e
| Author | SHA1 | Date |
|---|---|---|
|
|
0e10085b6e | |
|
|
4816a77d7f | |
|
|
cb40ea6383 | |
|
|
3cd7f6c206 |
|
|
@ -206,7 +206,7 @@ class DeviceController extends AdminController
|
||||||
$monitorMode = $request->monitor_mode ?? 0;
|
$monitorMode = $request->monitor_mode ?? 0;
|
||||||
$monitorMode = MonitorMode::find($monitorMode);
|
$monitorMode = MonitorMode::find($monitorMode);
|
||||||
|
|
||||||
$data = $monitorMode ? $this->service->getMonitorModeDeviceChartConfig($monitorMode, $startTime, $endTime, 2):[];
|
$data = $monitorMode ? $this->service->getMonitorModeDeviceChartConfig($monitorMode, $startTime.' 00:00:00', $endTime.' 23:59:59', 2):[];
|
||||||
|
|
||||||
return $this->response()->success($data);
|
return $this->response()->success($data);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filters;
|
||||||
|
|
||||||
|
use EloquentFilter\ModelFilter;
|
||||||
|
use App\Models\Device;
|
||||||
|
|
||||||
|
class MonitorModeFilter extends ModelFilter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 类型
|
||||||
|
*/
|
||||||
|
public function type($type){
|
||||||
|
return $this->where('type', $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类型
|
||||||
|
*/
|
||||||
|
public function typeName($typeName){
|
||||||
|
$type = 0;
|
||||||
|
foreach(Device::typeMap() as $key => $item){
|
||||||
|
if($item == $typeName){
|
||||||
|
$type = $key;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return $this->where('type', $type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filters;
|
||||||
|
|
||||||
|
use EloquentFilter\ModelFilter;
|
||||||
|
|
||||||
|
class WarningNoticeFilter extends ModelFilter
|
||||||
|
{
|
||||||
|
public function lv($lv){
|
||||||
|
return $this->where('lv', $lv);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function status($status)
|
||||||
|
{
|
||||||
|
return $this->where('status', $status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Helpers;
|
||||||
|
|
||||||
|
class Paginator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 解析每页显示的条数
|
||||||
|
*
|
||||||
|
* @param string $perPageName
|
||||||
|
* @param int $default
|
||||||
|
* @param int|null $max
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public static function resolvePerPage(string $perPageName = 'per_page', int $default = 20, ?int $max = null): int
|
||||||
|
{
|
||||||
|
$perPage = (int) request()->input($perPageName);
|
||||||
|
|
||||||
|
if ($perPage >= 1) {
|
||||||
|
if ($max !== null && $max >= 1 && $perPage >= $max) {
|
||||||
|
return $max;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $perPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,15 +10,19 @@ use DB;
|
||||||
class DeviceController extends Controller
|
class DeviceController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 统计某个基地下所有设备状态数量
|
* 统计某个实验田下所有设备状态数量
|
||||||
*/
|
*/
|
||||||
public function typeStateNum(Request $request)
|
public function typeStateNum(Request $request)
|
||||||
{
|
{
|
||||||
$regionId = $request->input('region_id', 0);
|
$regionId = $request->input('region_id', 0);
|
||||||
|
$monitorId = $request->input('monitor_id', 0);
|
||||||
|
|
||||||
$query = Device::query();
|
$query = Device::query();
|
||||||
|
|
||||||
if($regionId){
|
if($monitorId){
|
||||||
|
$deviceIds = MonitorDevice::where('monitor_id', $monitorId)->pluck('id')->toArray();
|
||||||
|
$query->whereIn('id', $deviceIds);
|
||||||
|
}elseif($regionId){
|
||||||
$monitorIds = RegionMonitor::where('region_id', $regionId)->pluck('id')->toArray();
|
$monitorIds = RegionMonitor::where('region_id', $regionId)->pluck('id')->toArray();
|
||||||
|
|
||||||
if(count($monitorIds) > 0){
|
if(count($monitorIds) > 0){
|
||||||
|
|
@ -45,4 +49,11 @@ class DeviceController extends Controller
|
||||||
|
|
||||||
return $this->json($data);
|
return $this->json($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 农机短信提醒-todo
|
||||||
|
*/
|
||||||
|
public function deviceNotice(){
|
||||||
|
return $this->success('操作成功');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Filters\MonitorModeFilter;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Resources\DeviceResource;
|
||||||
|
use App\Http\Resources\MonitorModeResource;
|
||||||
|
use App\Models\MonitorMode;
|
||||||
|
use App\Services\Admin\DeviceService;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class MonitorModeController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 获取监控点列表;
|
||||||
|
*/
|
||||||
|
public function getMonitorMode(Request $request)
|
||||||
|
{
|
||||||
|
$monitors = MonitorMode::filter($request->all(), MonitorModeFilter::class)->get();//有推荐,排序字段,不过目前没开放;
|
||||||
|
return $this->json(MonitorModeResource::collection($monitors));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取监控点位数据-可指定字段
|
||||||
|
*/
|
||||||
|
public function getMonitorDeviceData(Request $request)
|
||||||
|
{
|
||||||
|
$monitorId = $request->input('monitor_id');
|
||||||
|
$column = $request->input('column'); //指定字段
|
||||||
|
//监控类型(气象,土壤为折线图数据;虫情数据待定;监控设备rtsp流;其他类型无数据)
|
||||||
|
$data = [];
|
||||||
|
$monitor = MonitorMode::find($monitorId);
|
||||||
|
if($monitor){
|
||||||
|
switch($monitor->type){
|
||||||
|
case MonitorMode::TYPE_MONITOR://监控视频
|
||||||
|
$data = DeviceResource::collection($monitor->devices);
|
||||||
|
break;
|
||||||
|
case MonitorMode::TYPE_SOIL://土壤设备--只拿最近6小时数据
|
||||||
|
case MonitorMode::TYPE_METEOROLOGICAL://气象设备--只拿最近6小时数据
|
||||||
|
$startTime = now()->subHours(6)->format('Y-m-d H:i:s');
|
||||||
|
$endTime = now()->format('Y-m-d H:i:s');
|
||||||
|
$data = (new DeviceService())->getMonitorModeDeviceData($monitor, $startTime, $endTime);
|
||||||
|
if($column){
|
||||||
|
$data = $data[$column] ?? [];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MonitorMode::TYPE_INSECT://虫情设备-todo
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->json($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class SettingController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 基础数据信息-最多返回6项-todo
|
||||||
|
*/
|
||||||
|
public function staticBaseData(){
|
||||||
|
$data = [
|
||||||
|
[
|
||||||
|
'name' => '稻田种植面积',
|
||||||
|
'value' => '6,120',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => '稻田种植面积',
|
||||||
|
'value' => '6,120',
|
||||||
|
],[
|
||||||
|
'name' => '稻田种植面积',
|
||||||
|
'value' => '6,120',
|
||||||
|
],[
|
||||||
|
'name' => '稻田种植面积',
|
||||||
|
'value' => '6,120',
|
||||||
|
],[
|
||||||
|
'name' => '稻田种植面积',
|
||||||
|
'value' => '6,120',
|
||||||
|
],[
|
||||||
|
'name' => '稻田种植面积',
|
||||||
|
'value' => '6,120',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
return $this->json($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Helpers\Paginator;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\WarningNotice;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Filters\WarningNoticeFilter;
|
||||||
|
use App\Http\Resources\WarningNoticeResource;
|
||||||
|
use DB;
|
||||||
|
|
||||||
|
class WarningNoticeController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 获取警报数量
|
||||||
|
*/
|
||||||
|
public function warningLogNum(Request $request){
|
||||||
|
$list = WarningNotice::filter($request->input(), WarningNoticeFilter::class)
|
||||||
|
->select(DB::raw('lv, count(1) as num'))
|
||||||
|
->groupBy('lv')
|
||||||
|
->get()
|
||||||
|
->pluck('num', 'lv')->toArray();
|
||||||
|
for($i = 1; $i <= 4; $i++) {
|
||||||
|
$data[$i] = $list[$i] ?? 0;
|
||||||
|
}
|
||||||
|
return $this->json($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取警报记录
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function warningLog(Request $request)
|
||||||
|
{
|
||||||
|
$query = WarningNotice::with(['device.modes'])->filter($request->input(), WarningNoticeFilter::class)->orderBy('created_at', 'desc');
|
||||||
|
$list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
|
||||||
|
|
||||||
|
return $this->json(WarningNoticeResource::collection($list));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use App\Models\Device;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class DeviceResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'type' => $this->type,
|
||||||
|
'type_name' => Device::typeMap()[$this->type] ?? '未知',
|
||||||
|
'state' => $this->state,
|
||||||
|
'state_name' => Device::stateMap()[$this->state] ?? '未知',
|
||||||
|
'extends' => $this->extends ?? [],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class MonitorModeResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'name' => $this->name
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class WarningNoticeResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'lv' => $this->lv,
|
||||||
|
'monitor_modes' => MonitorModeResource::collection($this->whenLoaded('device')->modes),
|
||||||
|
'status' => $this->status,
|
||||||
|
'content' => $this->content,
|
||||||
|
'created_at' => strtotime($this->created_at) ?? 0, //录入时间
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,12 +2,12 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use EloquentFilter\Filterable;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class WarningNotice extends Model
|
class WarningNotice extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use Filterable;
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'device_id', 'lv', 'content', 'status', 'remarks',
|
'device_id', 'lv', 'content', 'status', 'remarks',
|
||||||
|
|
|
||||||
|
|
@ -56,12 +56,13 @@ class DeviceService extends BaseService
|
||||||
$diffDays = 0;
|
$diffDays = 0;
|
||||||
$day = date('Y-m-d');
|
$day = date('Y-m-d');
|
||||||
$xKeys = [];
|
$xKeys = [];
|
||||||
|
|
||||||
if($startTime && $endTime){
|
if($startTime && $endTime){
|
||||||
if($startTime == $endTime){//查询某一天
|
$startDay = Carbon::parse($startTime);
|
||||||
$day = $startTime;
|
$endDay = Carbon::parse($endTime);
|
||||||
|
if($startDay->format('Y-m-d') == $endDay->format('Y-m-d')){//判断是否同一天
|
||||||
|
$day = $startDay->format('Y-m-d');
|
||||||
}else{
|
}else{
|
||||||
$startDay = Carbon::parse($startTime);
|
|
||||||
$endDay = Carbon::parse($endTime);
|
|
||||||
$diffDays = $startDay->diffInDays($endDay, false);
|
$diffDays = $startDay->diffInDays($endDay, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,11 +74,15 @@ class DeviceService extends BaseService
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
//调整截至到当前小时
|
//调整截至到当前小时
|
||||||
$h = 23;
|
|
||||||
|
$th = $startDay->format('H');
|
||||||
|
$eh = $endDay->format('H');;
|
||||||
if($day == date('Y-m-d')){
|
if($day == date('Y-m-d')){
|
||||||
$h = date('H');
|
if($eh > date('H')){
|
||||||
|
$eh = date('H');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for ($i = 0; $i < ($h+1); $i++) {
|
for ($i = $th; $i < ($eh+1); $i++) {
|
||||||
$xKeys[] = str_pad($i, 2, '0', STR_PAD_LEFT).':00';
|
$xKeys[] = str_pad($i, 2, '0', STR_PAD_LEFT).':00';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -213,4 +218,58 @@ class DeviceService extends BaseService
|
||||||
|
|
||||||
return $configData;
|
return $configData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getMonitorModeDeviceData($monitorMode, $startTime, $endTime)
|
||||||
|
{
|
||||||
|
list($day, $diffDays, $xKeys) = $this->makeChartXkeys($startTime, $endTime);
|
||||||
|
switch($monitorMode->type){
|
||||||
|
case MonitorMode::TYPE_METEOROLOGICAL:
|
||||||
|
$dayliyReportQuery = MeteorologicalDailyReport::query();
|
||||||
|
$reportQuery = MeteorologicalReport::query();
|
||||||
|
$fieldNameMap = MonitorMode::fieldMap(MonitorMode::TYPE_METEOROLOGICAL);
|
||||||
|
$fieldUnitMap = MonitorMode::fieldUnitMap(MonitorMode::TYPE_METEOROLOGICAL);
|
||||||
|
break;
|
||||||
|
case MonitorMode::TYPE_SOIL:
|
||||||
|
$dayliyReportQuery = SoilDailyReport::query();
|
||||||
|
$reportQuery = SoilReport::query();
|
||||||
|
$fieldNameMap = MonitorMode::fieldMap(MonitorMode::TYPE_SOIL);
|
||||||
|
$fieldUnitMap = MonitorMode::fieldUnitMap(MonitorMode::TYPE_SOIL);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$monitorMode->load('devices');
|
||||||
|
$fieldMap = [];
|
||||||
|
|
||||||
|
foreach($monitorMode->devices as $device){
|
||||||
|
$_fields = explode(',', $device->pivot->fields);
|
||||||
|
|
||||||
|
if($diffDays) {
|
||||||
|
$modelQuery = $dayliyReportQuery->whereBetween('reported_at', [$startTime, $endTime]);
|
||||||
|
}else{
|
||||||
|
$modelQuery = $reportQuery->whereDate('reported_at', $day);
|
||||||
|
}
|
||||||
|
if($modelQuery){
|
||||||
|
$datalist = $modelQuery->where('device_id', $device->id)->get()->keyBy('reported_at')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
//组装数据;
|
||||||
|
foreach($_fields as $field){
|
||||||
|
$_data = [];
|
||||||
|
foreach($xKeys as $key){
|
||||||
|
if(!$diffDays) {
|
||||||
|
$key = date('Y-m-d').' '. $key.':00';
|
||||||
|
}else{
|
||||||
|
$key .= ' 00:00:00';
|
||||||
|
}
|
||||||
|
$_data[] = $datalist[$key][$field] ?? 0;
|
||||||
|
}
|
||||||
|
$fieldMap[$field] = [
|
||||||
|
'name' => $fieldNameMap[$field],
|
||||||
|
'unit' => $fieldUnitMap[$field],
|
||||||
|
'data' => $_data,
|
||||||
|
'xkeys'=> $xKeys
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $fieldMap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,15 @@ Route::post('auth/login', [AuthController::class, 'login']);
|
||||||
|
|
||||||
Route::group(['middleware' => 'auth:sanctum'], function () {
|
Route::group(['middleware' => 'auth:sanctum'], function () {
|
||||||
|
|
||||||
Route::get('devices-num', [DeviceController::class, 'typeStateNum'])->name('device.type_state_num');
|
Route::get('devices-num', [DeviceController::class, 'typeStateNum'])->name('devices.type_state_num');
|
||||||
|
Route::get('devices-notice', [DeviceController::class, 'deviceNotice'])->name('devices.notice');
|
||||||
|
Route::get('monitor-modes', [MonitorModeController::class, 'getMonitorMode'])->name('monitor_modes.list');
|
||||||
|
Route::get('monitor-modes/device-data', [MonitorModeController::class, 'getMonitorDeviceData'])->name('monitor_modes.device_data');
|
||||||
|
Route::get('static/base-data', [SettingController::class, 'staticBaseData'])->name('static.base_data');
|
||||||
|
|
||||||
|
Route::get('warning-notices/nums', [WarningNoticeController::class, 'warningLogNum']);
|
||||||
|
Route::get('warning-notices/logs', [WarningNoticeController::class, 'warningLog']);
|
||||||
|
|
||||||
Route::prefix('users')->group(function () {
|
Route::prefix('users')->group(function () {
|
||||||
Route::delete('logout', [UserController::class, 'logout']);
|
Route::delete('logout', [UserController::class, 'logout']);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue