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
}
