最近在做物联网的项目,正好对接电信aep的物联网开放平台,这里就跟大家分享一下我的遇到的问题和问题解决方式, 希望可以帮到大家
配置文件
我的后台用的springboot,所以直接在application.yml文件进行配置即可
jar包引入
需要导入两个jar包,可以在电信aep平台,找到下载地址
<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-84419.tar.gz</artifactId>
<version>20220430.105806-SNAPSHOT</version>
</dependency>
工具类
@Component
public class CtWingUtil {
//绑定配置文件中的参数,以后进行修改不用修改代码
private static String APP_KEY;
private static String APP_SECRET;
@Value("${ctWing.APP_KEY}")
public void setAppKey(String appkey) {
APP_KEY = appkey;
}
@Value("${ctWing.APP_SECRET}")
public void setAppSecret(String appSecret) {
APP_SECRET = appSecret;
}
//增加设备 后面的参数是API文档中的请求参数
public String createDevice(CreateDeviceDto dto, String masterKey){
//创建一个客户端连接
AepDeviceManagementClient client = AepDeviceManagementClient
.newClient()
.appKey(APP_KEY)
.appSecret(APP_SECRET).build();
CreateDeviceRequest request = new CreateDeviceRequest();
request.setParamMasterKey(masterKey);
//将对象转换为一个byte数组作为body传递
byte[] body = JSON.toJSONBytes(dto);
request.setBody(body);
CreateDeviceResponse response=null; //为了方便在try外使用该参数
//客户端调用方法
try {
response = client.CreateDevice(request);
} catch (Exception e) {
e.printStackTrace();
}
String string = new String(response.getBody());
client.shutdown(); //关闭连接返回响应结果
return string;
}
//增加设备 后面的参数是API文档中的请求参数
public String deleteDevice(DelDeviceDto dto, String masterKey){
//创建一个客户端连接
AepDeviceManagementClient client = AepDeviceManagementClient
.newClient()
.appKey(APP_KEY)
.appSecret(APP_SECRET).build();
DeleteDeviceRequest request = new DeleteDeviceRequest();
request.setParamMasterKey(masterKey);
request.setParamDeviceIds(dto.getDeviceIds());
request.setParamProductId(dto.getProductId());
//将对象转换为一个byte数组作为body传递
// byte[] body = JSON.toJSONBytes(dto);
// request.setBody(body);
DeleteDeviceResponse response=null; //为了方便在try外使用该参数
//客户端调用方法
try {
response = client.DeleteDevice(request);
} catch (Exception e) {
e.printStackTrace();
}
String string = new String(response.getBody());
client.shutdown(); //关闭连接返回响应结果
return string;
}
//查询设备状态
public int getDeviceStatus( String masterKey,String deviceId){
//创建一个客户端连接
AepDeviceManagementClient client = AepDeviceManagementClient
.newClient()
.appKey(APP_KEY)
.appSecret(APP_SECRET).build();
QueryDeviceRequest request=new QueryDeviceRequest();
request.setParamMasterKey(masterKey);
request.setParamDeviceId(deviceId);
request.setParamProductId(xxxxx);
QueryDeviceResponse response=null;
try {
response=client.QueryDevice(request);
} catch (Exception e) {
e.printStackTrace();
}
JSONObject body = JSONObject.parseObject(new String(response.getBody(), StandardCharsets.UTF_8));
if(body.getJSONObject("result")!=null)
{
return body.getJSONObject("result").getIntValue("netStatus");
}
client.shutdown();
return 0;
}
//下发指令
public JSONObject sendCommand(String requestStr,String masterKey) {
//创建一个下发命令客户端
AepDeviceCommandLwmProfileClient client = AepDeviceCommandLwmProfileClient.newClient().appKey(APP_KEY).appSecret(APP_SECRET).build();
//设备级命令
CreateCommandLwm2mProfileRequest request = new CreateCommandLwm2mProfileRequest ();
// 将请求体转换为JSON,再转化为Byte数组
System.out.println(requestStr);
JSONObject jsonObject = JSON.parseObject(requestStr);
byte[] bytes = JSON.toJSONBytes(jsonObject);
request.setBody(bytes);
request.setParamMasterKey(masterKey);
CreateCommandLwm2mProfileResponse response=null;
try {
response = client.CreateCommandLwm2mProfile(request);
} catch (Exception e) {
e.printStackTrace();
}
String resp= new String(response.getBody());
System.out.println(resp);
client.shutdown(); //关闭连接
return JSONObject.parseObject(resp);
}
遇到的问题
下发指令的格式 因为我这边使用的是LWM2M,在下发指令的时候,需要拼接好指令的格式,如下
/**
* 下发指令
*/
@RequestMapping(value = "/sendCommd",method = RequestMethod.POST)
public Result sendCommd(@RequestBody Devices devices){
Devices devices1=devicesService.getById(devices.getId());
String json="{" +
" "deviceId": ""+devices1.getCode()+""," +
" "operator": "string"," +
" "productId": xxxx," +
" "ttl": 7200," +
" "level": 1," +
" "command":{" +
" "serviceId":"DownCmdToDeviceLong","+
" "method":"DownCmdLong","+
" "paras": {"+
" "CmdId": "02"," +
" "TmpWidth": ""+devices.getTmpWidth()+""," +
" "TimePeriod": ""+devices.getTimePeriod()+""," +
" "MinCntSet": ""+devices.getMinCntSet()+""," +
" "Extend1": ""+devices.getExtend1()+""," +
" "Extend2": ""+devices.getExtend2()+""" +
" } }" +
"}";
ctWingUtil.sendCommand(json,"xxxxxxxxxxxxxxx");
return Result.success();
}
大家有问题,可以关注公众号[泉城IT圈子]