下發指令(SDK)


1. SDK使用說明

通過線下SDK開發的方式,將指令下發給設備

  • 添加依賴
  • 建立連接

https://www.ctwing.cn/yykf/124#/callback

2. 指令下發相關文檔

  • 指令下發

https://www.ctwing.cn/channel-help-api.htm?api=99

3.環境准備

3.1 下載sdk相關jar包

應用管理-----服務管理-----SDK下載

AEP應用管理:https://application.ctwing.cn/#/guide

3.2 添加本地依賴

  • 將下載的兩個jar包添加到本地Maven倉庫
  • 將jar包版本替換為自己下載的版本
# 在jar包所在位置打開命令窗口,分別執行下面兩條指令
mvn install:install-file -Dfile=ctg-ag-sdk-core-2.5.0-SNAPSHOT.jar -DpomFile=ctg-ag-sdk-core-2.5.0-SNAPSHOT.pom.xml

mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file -Dfile=ag-sdk-biz-71823.tar.gz-20211108.154321-SNAPSHOT.jar -DpomFile=ag-sdk-biz-71823.tar.gz-20211108.154321-SNAPSHOT.pom.xml

3.3 新建一個SpringBoot項目

  • 將這兩個jar包添加依賴
<!--下發指令需要的兩個jar包-->
<dependency>
    <groupId>com.ctg.ag</groupId>
    <artifactId>ctg-ag-sdk-core</artifactId>
    <version>2.5.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>com.ctg.ag</groupId>
    <artifactId>ag-sdk-biz-71823.tar.gz</artifactId>
    <version>20211108.154321-SNAPSHOT</version>
</dependency>

4. 業務代碼編寫

Controller層

  • 創建一個接口用來接收請求
@Controller
@RequestMapping("/Pdu")
public class PduAepWdController  {
    @Autowired
    private PduWdService pduWdService;

    @PostMapping("/sendCommand")
    @ResponseBody
    public JSONObject sendCommand(@RequestBody String req){
        JSONObject response = pduWdService.sendCommand(req);
        return response;
    }
}

Service層

  • 對請求進行處理
  • 建立一個發送請求的客戶端
@Autowired
private  AepUtils aepUtils;

@Override
public JSONObject sendCommand(String requestStr) {
    JSONObject response = aepUtils.sendCommand(requestStr);
    System.out.println(response);
    return response;
}

配置文件application.properties

aep.appKey=kIbJudSj
aep.secret=8kvo8wa
aep.masterkey=d16aad3dc3237ca0b

工具類AepUtils

@Component
public class AepUtils {
    @Value("${aep.appKey}")
    private  String appKey;
    @Value("${aep.secret}")
    private  String secret;
    @Value("${aep.masterkey}")
    private  String masterKey;

    public JSONObject sendCommand(String requestStr) {
        //創建一個下發命令客戶端
        AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(appKey).appSecret(secret).build();
        //設備級命令
        CreateCommandRequest request = new CreateCommandRequest();
        request.setParamMasterKey(masterKey);

        //將請求體轉換為JSON,再轉化為Byte數組
        JSONObject jsonObject = JSON.parseObject(requestStr);
        byte[] bytes = JSON.toJSONBytes(jsonObject);
        request.setBody(bytes);

        CreateCommandResponse response=null;
        try {
            response = client.CreateCommand(request);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String resp= new String(response.getBody());
        client.shutdown();  //關閉連接
        return JSONObject.parseObject(resp);
    }
}

5. Postman測試請求

url: 127.0.0.1:9001/Pdu/sendCommand

請求體:

  • 請求參數MasterKey在工具類中已經傳入。只需要傳入請求體即可。
{
  "deviceId": "5e7b154faf93211b4efececa",
  "operator": "string",
  "productId": 15091111,
  "ttl": 7200,
  "level": 1,
  "content":{
      "dataType": 1,
      "payload": "04443442"
    }
}

響應結果:

{
    "msg": "ok",
    "result": {
        "createBy": "string",
        "productId": 150911161,
        "commandStatus": "指令已保存",
        "createTime": 1636508451076,
        "imei": "862592111118144",
        "commandId": "20",
        "deviceId": "5e7b154f7b3211b4efececa",
        "ttl": 7200,
        "command": "04443442"
    },
    "code": 0
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM