添加警报

dev
vine_liutk 2022-10-28 15:00:22 +08:00
parent c62573d69e
commit 6892f7c06d
8 changed files with 252 additions and 2 deletions

View File

@ -0,0 +1,71 @@
<?php
namespace App\Http\Controllers;
use App\Helpers\Paginator;
use Illuminate\Http\Request;
use App\Models\DeviceWarning;
use Peidikeji\Setting\Models\Setting;
use App\Http\Resources\DeviceWarningResource;
class DeviceWarningController extends Controller
{
/**
* 预计规则
*
* @return void
*/
public function rules()
{
$settings = Setting::where('slug', 'like', 'device_warning_rule%')->get()->toArray();
return $this->json(array_map(function ($item) {
$_value = json_decode($item['value'], true);
return [
'name' => $item['name'],
'slug' => $item['slug'],
'value' => $_value,
];
}, $settings));
}
public function updateRule(Request $request)
{
$slug = $request->input(['slug']);
$ruleValue = $request->input(['value'], []);
$rule = Setting::where('slug', $slug)->first();
//获取提交的规则
if($rule){
$oldValue = json_decode($rule->value, true);
foreach($oldValue as $key => $value){
foreach($value as $lv =>$item){
if(isset($ruleValue[$key][$lv])){
$oldValue[$key][$lv] = $ruleValue[$key][$lv];
}
}
}
$rule->update([
'value' => json_encode($oldValue)
]);
}
return $this->success('更新成功');
}
/**
* 获取警报记录
*
* @return void
*/
public function warningLog(Request $request)
{
$query = DeviceWarning::filter($request->input())->orderBy('created_at', 'desc');
$list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
return $this->json(DeviceWarningResource::collection($list));
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class DeviceWarningResource 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,
'content' => $this->content,
'created_at' => strtotime($this->created_at) ?? 0, //录入时间
];
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\ModelFilters;
use EloquentFilter\ModelFilter;
class DeviceWarningFilter extends ModelFilter
{
public function base($base)
{
return $this->where('base_id', $base);
}
public function status($status){
return $this->where('status', $status);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Models;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class DeviceWarning extends Model
{
use HasFactory, Filterable;
protected $fillable = [
'device_id', 'base_id', 'lv', 'content', 'status', 'remarks',
'linkos_device_id', 'linkos_reported_at'
];
}

View File

@ -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('device_warnings', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('base_id')->comment('基地ID');
$table->unsignedBigInteger('device_id')->comment('设备ID');
$table->unsignedTinyInteger('lv')->default(0)->comment('警报等级');
$table->string('content')->comment('提醒内容:【基地名称-监控点-设备名称】【几级警报】XX达到N值');
$table->unsignedTinyInteger('status')->default(0)->comment('状态0未处理1已处理2已忽略');
$table->string('remarks')->nullable()->comment('备注');
$table->string('linkos_device_id');
$table->timestamp('linkos_reported_at');
$table->unsignedBigInteger('updated_by')->nullable()->comment('操作人');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('device_warnings');
}
};

View File

@ -30,7 +30,7 @@ class KeywordsTableSeeder extends Seeder
['key' => 'crops-cate-yuye', 'name' => '渔业', 'type_key' => 'crops-category', 'value' => '',],
['key' => 'crops-cate-xumuye', 'name' => '畜牧业', 'type_key' => 'crops-category', 'value' => ''],
['key' => 'crops-cate-lingye', 'name' => '林业', 'type_key' => 'crops-category', 'value' => ''],
['key' => 'crops-cate-activity', 'name' => '农林牧渔活动', 'type_key' => 'crops-category', 'value' => ''],
['key' => 'crops-cate-activity', 'name' => '其他', 'type_key' => 'crops-category', 'value' => ''],
]],
];

View File

@ -32,6 +32,82 @@ class SettingTableSeeder extends Seeder
['name' => '全市数据-人口', 'slug' => 'city_data_population', 'value' => '{"value":"0", "unit":"万人"}'],
['name' => '全市数据-耕地总面积', 'slug' => 'city_data_cultivated_area', 'value' => '{"value":"0", "unit":"万亩"}'],
['name' => '全市数据-生猪年出栏', 'slug' => 'city_data_pig_output', 'value' => '{"value":"0", "unit":"万头"}'],
['name' => '警报规则-土壤设备', 'slug' => 'device_warning_rule_soil', 'value' =>'{
"temperature":{
"1":[{"min":null,"max":null},{"min":null,"max":null}],
"2":[{"min":null,"max":null},{"min":null,"max":null}],
"3":[{"min":null,"max":null},{"min":null,"max":null}],
"4":[{"min":null,"max":null},{"min":null,"max":null}]
},
"conductivity":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
},
"humidity":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
},
"n":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
},
"p":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
},
"k":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
}
}'],
['name' => '警报规则-水质设备', 'slug' => 'device_warning_rule_waterquality', 'value' =>'{
"temperature":{
"1":[{"min":null,"max":null},{"min":null,"max":null}],
"2":[{"min":null,"max":null},{"min":null,"max":null}],
"3":[{"min":null,"max":null},{"min":null,"max":null}],
"4":[{"min":null,"max":null},{"min":null,"max":null}]
},
"conductivity":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
},
"oxygen":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
},
"turbidity":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
},
"chlorine":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
},
"ph":{
"1":[{"min":null,"max":null}],
"2":[{"min":null,"max":null}],
"3":[{"min":null,"max":null}],
"4":[{"min":null,"max":null}]
}
}']
];
Setting::insert($list);
}

View File

@ -28,7 +28,7 @@ Route::group(['middleware' => 'auth:sanctum'], function () {
Route::get('citydata-statistics', [CityDataController::class, 'statistics'])->name('citydata_statistics.index');
//基地数据
Route::apiResource('agricultural-basic', AgriculturalBaseController::class)->names('agricultural_basic');
Route::get('agricultural-device-basic', [DeviceBasicController::class, 'deviceBase'])->name('agricultural_basic.device_bases');//通过设备类型查询基地
Route::get('agricultural-device-basic', [AgriculturalBaseController::class, 'deviceBase'])->name('agricultural_basic.device_bases');//通过设备类型查询基地
//农作物
Route::apiResource('crops', CropController::class)->names('crops');
@ -41,6 +41,10 @@ Route::group(['middleware' => 'auth:sanctum'], function () {
Route::apiResource('crop-flows', CropFlowController::class)->names('crops_flow');
//设备管理
Route::apiResource('devices', DeviceController::class)->names('device');
//设备警报配置
Route::get('device-warning-rules', [DeviceWarningController::class, 'rules']); //预警规则
Route::put('device-warning-rules', [DeviceWarningController::class, 'updateRule']); //预警规则
Route::get('device-warning-logs', [DeviceWarningController::class, 'warningLog']);
/**统计 **/
Route::get('crop-yield-quarter-statics', [CropYieldController::class, 'quarterStaticsChart']);//季度统计