| | |
| | | import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame; |
| | | import com.sandu.ximon.admin.manager.iot.rrpc.topic.IBaseTopic; |
| | | import com.sandu.ximon.admin.manager.iot.rrpc.topic.ICustomizeTopic; |
| | | import com.sandu.ximon.admin.manager.iot.rrpc.util.FrameUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @author chenjiantian |
| | | * @date 2021/12/2 18:21 |
| | | */ |
| | | public abstract class BaseInvokeSyncService implements IInvoke, IBaseTopic, ICustomizeTopic { |
| | | |
| | | /** |
| | | * 获取产品吗 |
| | | * |
| | | * @return 产品吗 |
| | | */ |
| | | public abstract String getProductKey(); |
| | | |
| | | /** |
| | | * 发送rrpc 请求 |
| | | * |
| | | * @param deviceName 设备名 |
| | | * @param iRequestFrame 编码帧 |
| | | * @return 返回的解码帧实体类 |
| | | */ |
| | | public abstract CommonFrame sendRRPC(String deviceName, IRequestFrame iRequestFrame); |
| | | |
| | | /** |
| | | * 发送rrpc 请求 |
| | |
| | | * @param clz 内部帧响应实体类 如果不想自己解析 可带上 |
| | | * @return 返回的解码帧实体类 同时把内部帧解码之后也携带上 |
| | | */ |
| | | public abstract <T extends BaseResponseInnerFrame<T>> WrapResponseCommonFrame<T> sendRRPC(String deviceName, IRequestFrame iRequestFrame, Class<T> clz); |
| | | public <T extends BaseResponseInnerFrame<T>> WrapResponseCommonFrame<T> sendRRPC(String deviceName, IRequestFrame iRequestFrame, Class<T> clz){ |
| | | CommonFrame commonFrame = sendRRPC(deviceName, iRequestFrame); |
| | | if (commonFrame == null) { |
| | | return null; |
| | | } |
| | | WrapResponseCommonFrame<T> responseCommonFrame = new WrapResponseCommonFrame<>(); |
| | | BeanUtils.copyProperties(commonFrame, responseCommonFrame); |
| | | try { |
| | | responseCommonFrame.setResponseInnerFrame(clz.newInstance().transformFrame(responseCommonFrame.getPayload())); |
| | | } catch (InstantiationException | IllegalAccessException e) { |
| | | throw new BusinessException(String.format("找不到%s的构造函数", clz.getName())); |
| | | } |
| | | return responseCommonFrame; |
| | | } |
| | | |
| | | @Override |
| | | public CommonFrame sendRRPC(String deviceName, IRequestFrame iRequestFrame) { |
| | | InvokeParam param = new InvokeParam(); |
| | | param.setOperate("1001"); |
| | | param.setFrame(iRequestFrame.getEncodeFrame()); |
| | | InvokeThingServiceResponse.Data data = invokeThing(deviceName, param); |
| | | if (data == null) { |
| | | return null; |
| | | } |
| | | String result = data.getResult(); |
| | | result = result.replace("\\", ""); |
| | | Map map = JSON.parseObject(result, Map.class); |
| | | result = (String) map.get("msg"); |
| | | return FrameUtils.transformMessageToFrame(result); |
| | | } |
| | | |
| | | /** |
| | | * 基础通信同步调用 |
| | |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public InvokeThingServiceResponse.Data invokeThing(String deviceName, InvokeParam param) { |
| | | /** |
| | | * 调用自定义topic |
| | | * https://help.aliyun.com/document_detail/69584.htm?spm=a2c4g.11186623.0.0.25b33982bSQSom#reference-snk-mrz-wdb |
| | | * |
| | | * @param deviceName 设备名 |
| | | * @param param 参数 |
| | | * @return 设备返回的结果 |
| | | */ |
| | | protected InvokeThingServiceResponse.Data invokeThing(String deviceName, InvokeParam param) { |
| | | // 填充服务调用的参数 |
| | | InvokeThingServiceRequest request = new InvokeThingServiceRequest(); |
| | | // 设备证书之productKey |