Browse Source

批量查询设备信息保存更新

master
wanghongjun 2 years ago
parent
commit
08269d6fa3
  1. 70
      application/api/controller/Base.php
  2. 89
      application/api/controller/Device.php
  3. 52
      application/api/controller/Product.php

70
application/api/controller/Base.php

@ -3,6 +3,12 @@
namespace app\api\controller; namespace app\api\controller;
use think\Controller; use think\Controller;
use think\Db;
use think\Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
class Base extends Controller class Base extends Controller
{ {
@ -19,7 +25,7 @@ class Base extends Controller
return json_encode($arr); return json_encode($arr);
} }
public function jsonSuccess($msg = '成功',$data = []) protected function jsonSuccess($msg = '成功',$data = [])
{ {
$arr = [ $arr = [
'code' => $this->SUCCESS_CODE, 'code' => $this->SUCCESS_CODE,
@ -29,4 +35,66 @@ class Base extends Controller
return json_encode($arr); return json_encode($arr);
} }
/**
*
* @param $tableName
* @param $queryData
* @param $data
* @param $idArr
* @return array
* @throws Exception
* @throws PDOException
*/
protected function queryUpdate($tableName,$queryData,$data,$idArr)
{
$idArr = [];
if ($queryData) {
$id = $queryData['id'];
$update = [];
foreach ($queryData as $key => $row) {
if (isset($data[$key]) && $data[$key] != $row) {
$update[$key] = $row;
}
}
if ($update) {
Db::table($tableName)->where('id',$id)->update($update);
}
$idArr[] = $id;
} else {
Db::table($tableName)->insert($data);
$idArr[] = Db::table($tableName)->getLastInsID();
}
return $idArr;
}
/**
*
* @param $tableName
* @param $idArr
* @param $updateStatus
* @throws DataNotFoundException
* @throws DbException
* @throws Exception
* @throws ModelNotFoundException
* @throws PDOException
*/
protected function updateStatus($tableName,$idArr,$updateStatus = ['status' => 0])
{
if ($idArr) {
# 查询不存在的id
$notList = Db::table($tableName)->where('id', 'not in', $idArr)->field('id')->select();
$notIdArr = [];
foreach ($notList as $notListRow) $notList[] = $notListRow['id'];
if ($notIdArr) {
Db::table($tableName)->where('id','in',$notIdArr)->update($updateStatus);
}
} else {
Db::table($tableName)->update($updateStatus);
}
}
} }

89
application/api/controller/Device.php

@ -0,0 +1,89 @@
<?php
namespace app\api\controller;
use app\common\AepSdk\Apis\Aep_device_management;
use think\Db;
use think\Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
class Device extends Base
{
/**
* 获取设备信息列表
* @return false|string
* @throws DataNotFoundException
* @throws DbException
* @throws Exception
* @throws ModelNotFoundException
* @throws PDOException
*/
public function queryDeviceList()
{
$app_key = config('iot.app_key');
$app_secret = config('iot.app_secret');
$tableName = 'bs_product_device';
$productList = Db::table('bs_product')->where('status', 1)->field('productId,apiKey')->select();
$updateStatus = ['status' => 0];
foreach ($productList as $productListRow) {
$apiKey = $productListRow['apiKey'];
$productId = $productListRow['productId'];
$idArr = [];
$result = Aep_device_management::QueryDeviceList($app_key, $app_secret, $apiKey, $productId);
$jsonDataDe = json_decode($result, true);
if ($jsonDataDe['code'] !== 0) continue;
$list = $jsonDataDe['result']['list'];
if (!$list) {
Db::table($tableName)->where('productId', $productId)->update($updateStatus);
continue;
}
foreach ($list as $item) {
$deviceId = $item['deviceId'];
$queryRes = Db::table($tableName)->where('deviceId', $deviceId)->find();
$data = [
'deviceId' => $deviceId, // 设备id
'deviceName' => $item['deviceName'], // 终端名称
'tenantId' => $item['tenantId'], // 租户id
'productId' => $item['productId'], // 产品id
'firmwareVersion' => $item['firmwareVersion'], // String,固件版本
'deviceStatus' => $item['deviceStatus'], //Integer 设备状态 0:已注册,1:已激活,2:已注销
'createTime' => $item['createTime'], // 创建时间
'updateTime' => $item['updateTime'], // 修改时间
'netStatus' => $item['netStatus'], // 设备在线状态
'onlineAt' => $item['onlineAt'], // 设备最后上线时间
'offlineAt' => $item['offlineAt'], // 设备最后下线时间
];
if (isset($item['imei'])) $data['imei'] = $item['imei'];// String,IMEI号,全局唯一,根据产品的Endpoint必填,创建时可相同,则删除原产品新建产品
if (isset($item['imsi'])) $data['imsi'] = $item['imsi'];// String,IMSI号,根据产品的Endpoint选填
if (isset($item['autoObserver'])) $data['autoObserver'] = $item['autoObserver'];// 是否订阅,0.订阅1.不订阅
if (isset($item['createBy'])) $data['createBy'] = $item['createBy'];// 创建者
if (isset($item['updateBy'])) $data['updateBy'] = $item['updateBy'];// 修改者
$idArr = $this->queryUpdate($tableName, $queryRes, $data, $idArr);
}
$this->updateStatus($tableName, $idArr);
}
return $this->jsonSuccess();
}
}

52
application/api/controller/Product.php

@ -35,68 +35,56 @@ class Product extends Base
$list = $jsonDataDe['result']['list']; $list = $jsonDataDe['result']['list'];
$idArr = [];
foreach ($list as $item) { foreach ($list as $item) {
$productId = $item['productId']; $productId = $item['productId'];
$query = Db::table($tableName)->where('productId',$productId)->find(); $query = Db::table($tableName)->where('productId', $productId)->find();
$data = [ $data = [
'productId' => $productId, // 产品ID 'productId' => $productId, // 产品ID
'productName' => $item['productName'], // 产品名称 'productName' => $item['productName'], // 产品名称
'tenantId' => $item['tenantId'], // 租户ID 'tenantId' => $item['tenantId'], // 租户ID
'productDesc' => $item['productDesc'], // 产品描述 'productDesc' => $item['productDesc'], // 产品描述
// 'productType' => $item['productType'], // Integer,产品类别 // 'productType' => $item['productType'], // Integer,产品类别
// 'secondaryType' => $item['secondaryType'], // Integer 二级分类名称 // 'secondaryType' => $item['secondaryType'], // Integer 二级分类名称
// 'thirdType' => $item['thirdType'], // Integer,三级分类 // 'thirdType' => $item['thirdType'], // Integer,三级分类
'productProtocol' => $item['productProtocol'], // Integer,产品协议:1.T-LINK协议 2.MQTT协议 3.LWM2M协议 4.TUP协议 5.HTTP协议 6.JT/T808 7.TCP协议 8.私有TCP(网关子设备协议) 9.私有UDP(网关子设备协议) 10.网关产品MQTT(网关产品协议) 11.南向云 'productProtocol' => $item['productProtocol'], // Integer,产品协议:1.T-LINK协议 2.MQTT协议 3.LWM2M协议 4.TUP协议 5.HTTP协议 6.JT/T808 7.TCP协议 8.私有TCP(网关子设备协议) 9.私有UDP(网关子设备协议) 10.网关产品MQTT(网关产品协议) 11.南向云
// 'authType' => $item['authType'], // Integer,认证方式 1:特征串认证,2:sm9认证,3:dtls双向认证,4:IMEI认证,5:SIMID认证,6:sm2认证 // 'authType' => $item['authType'], // Integer,认证方式 1:特征串认证,2:sm9认证,3:dtls双向认证,4:IMEI认证,5:SIMID认证,6:sm2认证
// 'payloadFormat' => $item['payloadFormat'], // Integer,消息类型 1:json,2:紧凑二进制 // 'payloadFormat' => $item['payloadFormat'], // Integer,消息类型 1:json,2:紧凑二进制
'createTime' => $item['createTime'], // 创建时间 'createTime' => $item['createTime'], // 创建时间
'updateTime' => $item['updateTime'], // 更新时间 'updateTime' => $item['updateTime'], // 更新时间
'networkType' => $item['networkType'], // Integer,网络类型:1.wifi2.移动蜂窝数据3.NB-IoT4.以太网 'networkType' => $item['networkType'], // Integer,网络类型:1.wifi2.移动蜂窝数据3.NB-IoT4.以太网
// 'endpointFormat' => $item['endpointFormat'], // Integer,Endpoint格式:1.IMEI 2.URN:IMEI:############### 3.URN:IMEI-IMSI: ###############-############### // 'endpointFormat' => $item['endpointFormat'], // Integer,Endpoint格式:1.IMEI 2.URN:IMEI:############### 3.URN:IMEI-IMSI: ###############-###############
// 'powerModel' => $item['powerModel'], // Integer,省电模式:1.PSM 2.DRX 3.eDRX // 'powerModel' => $item['powerModel'], // Integer,省电模式:1.PSM 2.DRX 3.eDRX
'apiKey' => $item['apiKey'], // MasterKey 'apiKey' => $item['apiKey'], // MasterKey
'deviceCount' => $item['deviceCount'], // Integer,设备总数 'deviceCount' => $item['deviceCount'], // Integer,设备总数
'productTypeValue' => $item['productTypeValue'], // 产品分类名称 'productTypeValue' => $item['productTypeValue'], // 产品分类名称
'secondaryTypeValue' => $item['secondaryTypeValue'], // 二级分类名称 'secondaryTypeValue' => $item['secondaryTypeValue'], // 二级分类名称
'thirdTypeValue' => $item['thirdTypeValue'], // 三级分类名称 'thirdTypeValue' => $item['thirdTypeValue'], // 三级分类名称
'encryptionType' => $item['encryptionType'], // 加密认证方式 0:一机一密,1:一型一密 'encryptionType' => $item['encryptionType'], // 加密认证方式 0:一机一密,1:一型一密
// 'rootCert' => $item['rootCert'], // T-Link证书认证根证书 // 'rootCert' => $item['rootCert'], // T-Link证书认证根证书
'createBy' => $item['createBy'], // 创建者 'createBy' => $item['createBy'], // 创建者
'updateBy' => $item['updateBy'], // 更新者 'updateBy' => $item['updateBy'], // 更新者
// 'tupDeviceModel' => $item['tupDeviceModel'], // tup设备类型 // 'tupDeviceModel' => $item['tupDeviceModel'], // tup设备类型
// 'tupDeviceType' => $item['tupDeviceType'], // tup设备型号 // 'tupDeviceType' => $item['tupDeviceType'], // tup设备型号
'accessType' => $item['accessType'], // 接入类型: 1.设备直连,2.网关接入,3.南向云接入,4.视图云接入 'accessType' => $item['accessType'], // 接入类型: 1.设备直连,2.网关接入,3.南向云接入,4.视图云接入
'nodeType' => $item['nodeType'], // 节点类型:1设备 ,2.网关 'nodeType' => $item['nodeType'], // 节点类型:1设备 ,2.网关
'tupIsThrough' => $item['tupIsThrough'], // 是否透传:0.透传,1不透传 'tupIsThrough' => $item['tupIsThrough'], // 是否透传:0.透传,1不透传
'dataEncryption' => $item['dataEncryption'], // 数据加密方式 1:sm1,2:sm2,3:sm4,4:dtls,5:明文 'dataEncryption' => $item['dataEncryption'], // 数据加密方式 1:sm1,2:sm2,3:sm4,4:dtls,5:明文
// 'lwm2mEdrxTime' => $item['lwm2mEdrxTime'], // eDRX模式时间窗 // 'lwm2mEdrxTime' => $item['lwm2mEdrxTime'], // eDRX模式时间窗
// 'categoryId' => $item['categoryId'], // 社区协议设备分类 // 'categoryId' => $item['categoryId'], // 社区协议设备分类
// 'categoryName' => $item['categoryName'], // 社区协议设备分类名称 // 'categoryName' => $item['categoryName'], // 社区协议设备分类名称
// 'msgParserMode' => $item['msgParserMode'], // 脚本解析方式:1.js脚本解析 // 'msgParserMode' => $item['msgParserMode'], // 脚本解析方式:1.js脚本解析
]; ];
if ($query) { $idArr = $this->queryUpdate($tableName, $query, $data, $idArr);
$update = [];
foreach ($query as $key => $row) {
if (isset($data[$key]) && $data[$key] != $row) {
$update[$key] = $row;
}
}
if ($update) {
Db::table($tableName)->where('id',$query['id'])->update($update);
}
} else {
Db::table($tableName)->insert($data);
}
} }
# 修改状态为停用
$this->updateStatus($tableName, $idArr);
return $this->jsonError($jsonDataDe['msg']); return $this->jsonError($jsonDataDe['msg']);
} else { } else {
return $this->jsonError($jsonDataDe['msg'] ?: '数据获取失败'); return $this->jsonError($jsonDataDe['msg'] ?: '数据获取失败');

Loading…
Cancel
Save