This commit is contained in:
yaooo 2023-12-14 16:47:09 +08:00
commit 8da1cae193
3 changed files with 119 additions and 5 deletions

View File

@ -30,8 +30,6 @@ class DataCollectController extends BaseApiController
if ($parmas['event'] == 'message.publish') { if ($parmas['event'] == 'message.publish') {
$payload= json_decode($parmas['payload'], true); $payload= json_decode($parmas['payload'], true);
// 如果该设备之前离线,现在重新上线则删除之前的设备离线记录
DeviceOffline::where('land_id', $land[3])->delete();
$device = explode('_', $parmas['topic']); // 命名规则topic_deviceid deviceid为设备主键id $device = explode('_', $parmas['topic']); // 命名规则topic_deviceid deviceid为设备主键id
$data = [ $data = [
'land_id' => $land[3], 'land_id' => $land[3],
@ -188,6 +186,19 @@ class DataCollectController extends BaseApiController
} }
return $this->success('接收成功'); return $this->success('接收成功');
} }
// 连接链接事件 设备已上线
if ($parmas['event'] == 'client.connected') {
// 查询该土地关联的设备
$landProduct = LandProduct::where('land_id', $land[3])->find();
$productDevice = ProductDevice::where('product_id', $landProduct['product_id'])->select();
// 如果该设备之前离线,现在重新上线则删除之前的设备离线记录
DeviceOffline::where('land_id', $land[3])->delete();
foreach ($productDevice as $item) {
Device::where('id', $item['device_id'])->update(['is_online' => 1]);
}
return $this->success('接收成功');
}
} catch (Exception $e) { } catch (Exception $e) {
return $this->fail($e->getMessage()); return $this->fail($e->getMessage());
} }

View File

@ -5,6 +5,7 @@ namespace app\api\controller\dataview;
use app\api\controller\BaseApiController; use app\api\controller\BaseApiController;
use app\common\model\device\Device; use app\common\model\device\Device;
use app\common\model\device\MonitorAlarm; use app\common\model\device\MonitorAlarm;
use app\common\model\land\LandPlant;
use think\App; use think\App;
use think\Exception; use think\Exception;
use think\exception\ErrorException; use think\exception\ErrorException;
@ -13,7 +14,7 @@ use think\exception\ValidateException;
class DeviceController extends BaseApiController class DeviceController extends BaseApiController
{ {
public array $notNeedLogin = ['deviceAlarmCount']; public array $notNeedLogin = ['deviceAlarmCount', 'deviceCount'];
public $areaCode; public $areaCode;
public $streetCode; public $streetCode;
@ -28,6 +29,7 @@ class DeviceController extends BaseApiController
} }
} }
// 设备告警统计
public function deviceAlarmCount() public function deviceAlarmCount()
{ {
$list = MonitorAlarm::alias('ma')->field('ma.*,d.code AS device_code,d.name AS device_name') $list = MonitorAlarm::alias('ma')->field('ma.*,d.code AS device_code,d.name AS device_name')
@ -37,9 +39,9 @@ class DeviceController extends BaseApiController
->join('land l', 'l.id=lp.land_id') ->join('land l', 'l.id=lp.land_id')
->where(function ($query) { ->where(function ($query) {
if ($this->streetCode != '') { if ($this->streetCode != '') {
$query->where('l.street_code', $this->streetCode); $query->where('l.town_code', $this->streetCode);
} else { } else {
$query->where('l.area_code', $this->areaCode); $query->where('l.county_code', $this->areaCode);
} }
}) })
->whereWeek('ma.create_time') ->whereWeek('ma.create_time')
@ -48,4 +50,47 @@ class DeviceController extends BaseApiController
return $this->success('成功', compact('list')); return $this->success('成功', compact('list'));
} }
// 检测设备数量统计
public function deviceCount()
{
$total = Device::alias('d')
->join('product_device pd', 'd.id=pd.device_id')
->join('land_product lp', 'pd.product_id=lp.product_id')
->join('land l', 'l.id=lp.land_id')
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
})
->count();
$online = Device::alias('d')
->join('product_device pd', 'd.id=pd.device_id')
->join('land_product lp', 'pd.product_id=lp.product_id')
->join('land l', 'l.id=lp.land_id')
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
})->where('d.is_online', 1)->count();
$offline = Device::alias('d')
->join('product_device pd', 'd.id=pd.device_id')
->join('land_product lp', 'pd.product_id=lp.product_id')
->join('land l', 'l.id=lp.land_id')
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
})->where('d.is_online', 0)->count();
return $this->success('成功', compact('total', 'online', 'offline'));
}
} }

View File

@ -0,0 +1,58 @@
<?php
namespace app\api\controller\dataview;
use app\api\controller\BaseApiController;
use app\common\model\land\LandPlant;
use app\common\model\LandCollection;
use think\exception\ValidateException;
class LandController extends BaseApiController
{
public array $notNeedLogin = ['plantProductCount', 'landCollectionList'];
public $areaCode;
public $streetCode;
public function initialize()
{
parent::initialize();
$this->areaCode = $this->request->param('areaCode', '');
$this->streetCode = $this->request->param('streetCode', '');
if ($this->areaCode == '') {
throw new ValidateException('未获取到位置信息');
}
}
// 溯源产品统计
public function plantProductCount()
{
$list = LandPlant::alias('lp')->join('land l', 'l.id=lp.land_id')->field('lp.kind,lp.qr_code')
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
})->limit(30)->select();
return $this->success('成功', compact('list'));
}
// 智能预警数据
public function landCollectionList()
{
$list = LandCollection::alias('lc')
->field('lc.*,l.title')
->join('land l', 'l.id=lc.land_id')
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
})->limit(30)->select();
return $this->success('成功', compact('list'));
}
}