package com.casic.util.aep; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.casic.util.SpringContextUtil; import com.ctg.ag.sdk.biz.AepDeviceCommandClient; import com.ctg.ag.sdk.biz.aep_device_command.CreateCommandRequest; import com.ctg.ag.sdk.biz.aep_device_command.CreateCommandResponse; import lombok.extern.slf4j.Slf4j; import java.nio.charset.StandardCharsets; /** * @author cz * @date 2024-04-24 */ @Slf4j public class AepCommandSend { private String deviceId; private String productId; private String masterApiKey; public AepCommandSend() { } public AepCommandSend(String deviceId, String productId, String masterApiKey) { this.deviceId = deviceId; this.productId = productId; this.masterApiKey = masterApiKey; } /** * 处理电信平台的报文解析,并组装回复报文,发送电信平台 * * @param frameStr */ public int handleAndReply(String frameStr) throws Exception { AepConfig aepConfig = SpringContextUtil.getBean(AepConfig.class); AepDeviceCommandClient client = getAepClient(aepConfig); CreateCommandRequest request = new CreateCommandRequest(); request.setParamMasterKey(masterApiKey); AepCommandBody body = new AepCommandBody(); body.setDeviceId(deviceId); body.setProductId(productId); body.setTtl(aepConfig.getTtl()); body.setOperator(aepConfig.getOperator()); body.setLevel(aepConfig.getLevel()); body.setPayloadString(frameStr); request.setBody(JSONObject.toJSONString(body).getBytes()); log.info("向AEP平台发送指令: {}", JSON.toJSONString(body)); JSONObject retObj = new JSONObject(); try { CreateCommandResponse msgResponse = client.CreateCommand(request); retObj = JSON.parseObject(new String(msgResponse.getBody(), StandardCharsets.UTF_8)); log.info("AEP平台返回消息: {}", retObj.toJSONString()); } catch (Exception ex) { log.error("AEP平台发送失败,异常信息{}", ex.getMessage()); } finally { client.shutdown(); } return (int) retObj.get("code"); } /** * 获取电信平台客户端 * * @param aepConfig * @return */ private AepDeviceCommandClient getAepClient(AepConfig aepConfig) { String aepKey = aepConfig.getKey(); String aepSecret = aepConfig.getSecret(); return AepDeviceCommandClient.newClient().appKey(aepKey).appSecret(aepSecret).build(); } }