suyuan/app/api/controller/DataCollectController.php

54 lines
2.4 KiB
PHP
Raw Normal View History

2023-12-12 16:20:07 +08:00
<?php
namespace app\api\controller;
use app\common\model\LandCollection;
use Exception;
2023-12-13 10:31:57 +08:00
use think\facade\Log;
2023-12-12 16:20:07 +08:00
2023-12-12 16:20:38 +08:00
class DataCollectController extends BaseApiController
2023-12-12 16:20:07 +08:00
{
2023-12-12 16:23:28 +08:00
public array $notNeedLogin = ['collect'];
2023-12-12 16:20:07 +08:00
// 种植数据采集
public function collect()
{
try {
2023-12-13 10:31:57 +08:00
$parmas = $this->request->post();
2023-12-13 11:54:28 +08:00
Log::info(json_encode($parmas));
2023-12-12 16:20:07 +08:00
if(!$parmas || !isset($parmas['username']) || $parmas['username']==''){
return $this->fail('参数错误');
}
$payload= json_decode($parmas['payload'], true);
2023-12-13 11:24:38 +08:00
$land = explode('_', $parmas['username']); // 命名规则land_id id土地表主键id
$device = explode('_', $parmas['topic']); // 命名规则topic_deviceid deviceid为设备主键id
2023-12-12 16:20:07 +08:00
$data = [
2023-12-13 13:40:25 +08:00
'land_id' => $land[3],
2023-12-13 11:24:38 +08:00
'topic'=>$device[1],
2023-12-12 16:20:07 +08:00
'qos'=>$parmas['qos'],
'wind_speed' => $payload['wind_speed'],
'wind_direction' => $payload['wind_direction'],
'ambient_temperature' => $payload['ambient_temperature'],
'ambient_humidity' => $payload['ambient_humidity'],
'carbon_dioxide' => $payload['carbon_dioxide'],
'ambient_air_pressure' => $payload['ambient_air_pressure'],
'rainfall' => $payload['rainfall'],
'ambient_lighting' => $payload['ambient_lighting'],
'soil_temperature' => $payload['soil_temperature'],
'soil_moisture' => $payload['soil_moisture'],
'soil_PH' => $payload['soil_PH'],
'soil_potassium_phosphate_nitrogen' => $payload['soil_potassium_phosphate_nitrogen'],
'soil_potassium_phosphate_phosphorus' => $payload['soil_potassium_phosphate_phosphorus'],
'soil_potassium_phosphate_potassium' => $payload['soil_potassium_phosphate_potassium'],
'clientid'=>$parmas['clientid'],
'create_time'=>date('Y-m-d H:i:s'),
'update_time'=>date('Y-m-d H:i:s'),
2023-12-13 11:24:38 +08:00
];
2023-12-12 16:20:07 +08:00
LandCollection::create($data);
return $this->success('接收成功', ['user_name'=>$parmas['username'], 'topic'=>$parmas['topic']]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
}
}