diff --git a/app/api/controller/DataCollectController.php b/app/api/controller/DataCollectController.php index bcbfe81e..8d60188c 100644 --- a/app/api/controller/DataCollectController.php +++ b/app/api/controller/DataCollectController.php @@ -30,8 +30,6 @@ class DataCollectController extends BaseApiController if ($parmas['event'] == 'message.publish') { $payload= json_decode($parmas['payload'], true); - // 如果该设备之前离线,现在重新上线则删除之前的设备离线记录 - DeviceOffline::where('land_id', $land[3])->delete(); $device = explode('_', $parmas['topic']); // 命名规则:topic_deviceid deviceid为设备主键id $data = [ 'land_id' => $land[3], @@ -188,6 +186,19 @@ class DataCollectController extends BaseApiController } 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) { return $this->fail($e->getMessage()); } diff --git a/app/api/controller/dataview/DeviceController.php b/app/api/controller/dataview/DeviceController.php index d6924924..6e2d14f8 100644 --- a/app/api/controller/dataview/DeviceController.php +++ b/app/api/controller/dataview/DeviceController.php @@ -5,6 +5,7 @@ namespace app\api\controller\dataview; use app\api\controller\BaseApiController; use app\common\model\device\Device; use app\common\model\device\MonitorAlarm; +use app\common\model\land\LandPlant; use think\App; use think\Exception; use think\exception\ErrorException; @@ -13,7 +14,7 @@ use think\exception\ValidateException; class DeviceController extends BaseApiController { - public array $notNeedLogin = ['deviceAlarmCount']; + public array $notNeedLogin = ['deviceAlarmCount', 'deviceCount']; public $areaCode; public $streetCode; @@ -28,6 +29,7 @@ class DeviceController extends BaseApiController } } + // 设备告警统计 public function deviceAlarmCount() { $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') ->where(function ($query) { if ($this->streetCode != '') { - $query->where('l.street_code', $this->streetCode); + $query->where('l.town_code', $this->streetCode); } else { - $query->where('l.area_code', $this->areaCode); + $query->where('l.county_code', $this->areaCode); } }) ->whereWeek('ma.create_time') @@ -48,4 +50,47 @@ class DeviceController extends BaseApiController 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')); + + } + } \ No newline at end of file diff --git a/app/api/controller/dataview/LandController.php b/app/api/controller/dataview/LandController.php new file mode 100644 index 00000000..ace9150b --- /dev/null +++ b/app/api/controller/dataview/LandController.php @@ -0,0 +1,58 @@ +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')); + } +} \ No newline at end of file