From 6892f7c06ddd3eab28cdf6934d14143a5d9ddf9f Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Fri, 28 Oct 2022 15:00:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AD=A6=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/DeviceWarningController.php | 71 +++++++++++++++++ app/Http/Resources/DeviceWarningResource.php | 24 ++++++ app/ModelFilters/DeviceWarningFilter.php | 17 +++++ app/Models/DeviceWarning.php | 17 +++++ ...27_145020_create_device_warnings_table.php | 41 ++++++++++ database/seeders/KeywordsTableSeeder.php | 2 +- database/seeders/SettingTableSeeder.php | 76 +++++++++++++++++++ routes/api.php | 6 +- 8 files changed, 252 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/DeviceWarningController.php create mode 100644 app/Http/Resources/DeviceWarningResource.php create mode 100644 app/ModelFilters/DeviceWarningFilter.php create mode 100644 app/Models/DeviceWarning.php create mode 100644 database/migrations/2022_10_27_145020_create_device_warnings_table.php diff --git a/app/Http/Controllers/DeviceWarningController.php b/app/Http/Controllers/DeviceWarningController.php new file mode 100644 index 0000000..cfe33b3 --- /dev/null +++ b/app/Http/Controllers/DeviceWarningController.php @@ -0,0 +1,71 @@ +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)); + } + +} diff --git a/app/Http/Resources/DeviceWarningResource.php b/app/Http/Resources/DeviceWarningResource.php new file mode 100644 index 0000000..2cde4a6 --- /dev/null +++ b/app/Http/Resources/DeviceWarningResource.php @@ -0,0 +1,24 @@ + $this->id, + 'lv' => $this->lv, + 'content' => $this->content, + 'created_at' => strtotime($this->created_at) ?? 0, //录入时间 + ]; + } +} diff --git a/app/ModelFilters/DeviceWarningFilter.php b/app/ModelFilters/DeviceWarningFilter.php new file mode 100644 index 0000000..7f57d98 --- /dev/null +++ b/app/ModelFilters/DeviceWarningFilter.php @@ -0,0 +1,17 @@ +where('base_id', $base); + } + + public function status($status){ + return $this->where('status', $status); + } +} diff --git a/app/Models/DeviceWarning.php b/app/Models/DeviceWarning.php new file mode 100644 index 0000000..b4ea6ba --- /dev/null +++ b/app/Models/DeviceWarning.php @@ -0,0 +1,17 @@ +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'); + } +}; diff --git a/database/seeders/KeywordsTableSeeder.php b/database/seeders/KeywordsTableSeeder.php index 0e50545..a09e156 100644 --- a/database/seeders/KeywordsTableSeeder.php +++ b/database/seeders/KeywordsTableSeeder.php @@ -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' => ''], ]], ]; diff --git a/database/seeders/SettingTableSeeder.php b/database/seeders/SettingTableSeeder.php index 20d0795..b1c5e2e 100644 --- a/database/seeders/SettingTableSeeder.php +++ b/database/seeders/SettingTableSeeder.php @@ -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); } diff --git a/routes/api.php b/routes/api.php index 3e9a138..5c433d2 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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']);//季度统计