From 08269d6fa33d22037a1c5aba4516b9ad5906511b Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Wed, 29 Nov 2023 19:12:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9F=A5=E8=AF=A2=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E4=BF=A1=E6=81=AF=E4=BF=9D=E5=AD=98=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Base.php | 70 +++++++++++++++++++- application/api/controller/Device.php | 89 ++++++++++++++++++++++++++ application/api/controller/Product.php | 52 ++++++--------- 3 files changed, 178 insertions(+), 33 deletions(-) create mode 100644 application/api/controller/Device.php diff --git a/application/api/controller/Base.php b/application/api/controller/Base.php index 86244bb..c4b117a 100644 --- a/application/api/controller/Base.php +++ b/application/api/controller/Base.php @@ -3,6 +3,12 @@ namespace app\api\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 { @@ -19,7 +25,7 @@ class Base extends Controller return json_encode($arr); } - public function jsonSuccess($msg = '成功',$data = []) + protected function jsonSuccess($msg = '成功',$data = []) { $arr = [ 'code' => $this->SUCCESS_CODE, @@ -29,4 +35,66 @@ class Base extends Controller 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); + } + } } \ No newline at end of file diff --git a/application/api/controller/Device.php b/application/api/controller/Device.php new file mode 100644 index 0000000..f24ca20 --- /dev/null +++ b/application/api/controller/Device.php @@ -0,0 +1,89 @@ +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(); + } + +} \ No newline at end of file diff --git a/application/api/controller/Product.php b/application/api/controller/Product.php index 27c873d..b69d555 100644 --- a/application/api/controller/Product.php +++ b/application/api/controller/Product.php @@ -35,68 +35,56 @@ class Product extends Base $list = $jsonDataDe['result']['list']; + $idArr = []; foreach ($list as $item) { $productId = $item['productId']; - $query = Db::table($tableName)->where('productId',$productId)->find(); + $query = Db::table($tableName)->where('productId', $productId)->find(); $data = [ 'productId' => $productId, // 产品ID 'productName' => $item['productName'], // 产品名称 'tenantId' => $item['tenantId'], // 租户ID 'productDesc' => $item['productDesc'], // 产品描述 -// 'productType' => $item['productType'], // Integer,产品类别 -// 'secondaryType' => $item['secondaryType'], // Integer 二级分类名称 -// 'thirdType' => $item['thirdType'], // Integer,三级分类 + // 'productType' => $item['productType'], // Integer,产品类别 + // 'secondaryType' => $item['secondaryType'], // 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.南向云 -// 'authType' => $item['authType'], // Integer,认证方式 1:特征串认证,2:sm9认证,3:dtls双向认证,4:IMEI认证,5:SIMID认证,6:sm2认证 -// 'payloadFormat' => $item['payloadFormat'], // Integer,消息类型 1:json,2:紧凑二进制 + // 'authType' => $item['authType'], // Integer,认证方式 1:特征串认证,2:sm9认证,3:dtls双向认证,4:IMEI认证,5:SIMID认证,6:sm2认证 + // 'payloadFormat' => $item['payloadFormat'], // Integer,消息类型 1:json,2:紧凑二进制 'createTime' => $item['createTime'], // 创建时间 'updateTime' => $item['updateTime'], // 更新时间 'networkType' => $item['networkType'], // Integer,网络类型:1.wifi2.移动蜂窝数据3.NB-IoT4.以太网 -// 'endpointFormat' => $item['endpointFormat'], // Integer,Endpoint格式:1.IMEI 2.URN:IMEI:############### 3.URN:IMEI-IMSI: ###############-############### -// 'powerModel' => $item['powerModel'], // Integer,省电模式:1.PSM 2.DRX 3.eDRX + // 'endpointFormat' => $item['endpointFormat'], // Integer,Endpoint格式:1.IMEI 2.URN:IMEI:############### 3.URN:IMEI-IMSI: ###############-############### + // 'powerModel' => $item['powerModel'], // Integer,省电模式:1.PSM 2.DRX 3.eDRX 'apiKey' => $item['apiKey'], // MasterKey 'deviceCount' => $item['deviceCount'], // Integer,设备总数 'productTypeValue' => $item['productTypeValue'], // 产品分类名称 'secondaryTypeValue' => $item['secondaryTypeValue'], // 二级分类名称 'thirdTypeValue' => $item['thirdTypeValue'], // 三级分类名称 'encryptionType' => $item['encryptionType'], // 加密认证方式 0:一机一密,1:一型一密 -// 'rootCert' => $item['rootCert'], // T-Link证书认证根证书 + // 'rootCert' => $item['rootCert'], // T-Link证书认证根证书 'createBy' => $item['createBy'], // 创建者 'updateBy' => $item['updateBy'], // 更新者 -// 'tupDeviceModel' => $item['tupDeviceModel'], // tup设备类型 -// 'tupDeviceType' => $item['tupDeviceType'], // tup设备型号 + // 'tupDeviceModel' => $item['tupDeviceModel'], // tup设备类型 + // 'tupDeviceType' => $item['tupDeviceType'], // tup设备型号 'accessType' => $item['accessType'], // 接入类型: 1.设备直连,2.网关接入,3.南向云接入,4.视图云接入 'nodeType' => $item['nodeType'], // 节点类型:1设备 ,2.网关 'tupIsThrough' => $item['tupIsThrough'], // 是否透传:0.透传,1不透传 'dataEncryption' => $item['dataEncryption'], // 数据加密方式 1:sm1,2:sm2,3:sm4,4:dtls,5:明文 -// 'lwm2mEdrxTime' => $item['lwm2mEdrxTime'], // eDRX模式时间窗 -// 'categoryId' => $item['categoryId'], // 社区协议设备分类 -// 'categoryName' => $item['categoryName'], // 社区协议设备分类名称 -// 'msgParserMode' => $item['msgParserMode'], // 脚本解析方式:1.js脚本解析 + // 'lwm2mEdrxTime' => $item['lwm2mEdrxTime'], // eDRX模式时间窗 + // 'categoryId' => $item['categoryId'], // 社区协议设备分类 + // 'categoryName' => $item['categoryName'], // 社区协议设备分类名称 + // 'msgParserMode' => $item['msgParserMode'], // 脚本解析方式:1.js脚本解析 ]; - if ($query) { - - $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); - } + $idArr = $this->queryUpdate($tableName, $query, $data, $idArr); } + # 修改状态为停用 + $this->updateStatus($tableName, $idArr); + return $this->jsonError($jsonDataDe['msg']); } else { return $this->jsonError($jsonDataDe['msg'] ?: '数据获取失败');