package com.sandu.ximon.admin.utils;
|
|
import com.alibaba.fastjson.JSON;
|
import com.aliyun.oss.ClientException;
|
import com.aliyuncs.exceptions.ServerException;
|
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceRequest;
|
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceResponse;
|
import com.sandu.ximon.admin.entity.RRPCParams;
|
|
/**
|
* 同步通信
|
*/
|
public class RRPCUtils {
|
|
public static boolean RRPC_FLAG = true;
|
private static final Integer READ_TIMEOUT = 5000;
|
// 读取超时时间
|
// private static final Integer READ_TIMEOUT = 8000;
|
|
public static boolean sendRRPC (String productKey, String mac, RRPCParams rrpcParams) {
|
|
// 填充服务调用的参数
|
InvokeThingServiceRequest request = getReq(productKey,mac,rrpcParams);
|
|
request.setReadTimeout(READ_TIMEOUT);
|
// 获得服务调用响应
|
InvokeThingServiceResponse response = null;
|
try {
|
if (RRPC_FLAG) {
|
LogUtils.error("send MQTT msg: " + JSON.toJSONString(rrpcParams));
|
long start = System.currentTimeMillis();
|
response = ClientUtils.getBean().getClient().getAcsResponse(request);
|
LogUtils.error("RRPC cost time :" + (System.currentTimeMillis() - start) + "ms");
|
}
|
} catch (ClientException e) {
|
// e.printStackTrace();
|
LogUtils.error("MQTT连接服务器生成失败");
|
} catch (ServerException e) {
|
e.printStackTrace();
|
} catch (com.aliyuncs.exceptions.ClientException e) {
|
e.printStackTrace();
|
}
|
|
if (response == null) {
|
LogUtils.print("MQTT RRPC调用服务异常");
|
return false;
|
}
|
|
if (response != null && response.getSuccess()) {
|
return true;
|
} else {
|
LogUtils.error("MQTT RRPC服务调用失败");
|
return false;
|
}
|
}
|
|
|
public static String sendRRPCwithReturn (String productKey, String mac, RRPCParams rrpcParams) {
|
// 填充服务调用的参数
|
InvokeThingServiceRequest request = getReq(productKey, mac, rrpcParams);
|
|
request.setReadTimeout(READ_TIMEOUT);
|
// 获得服务调用响应
|
InvokeThingServiceResponse response = null;
|
try {
|
if (RRPC_FLAG) {
|
LogUtils.error("send MQTT msg: " + JSON.toJSONString(rrpcParams));
|
long start = System.currentTimeMillis();
|
response = ClientUtils.getBean().getClient().getAcsResponse(request);
|
LogUtils.error("RRPC cost time :" + (System.currentTimeMillis() - start) + "ms");
|
}
|
} catch (ClientException e) {
|
LogUtils.error("MQTT连接服务器生成失败");
|
} catch (ServerException e) {
|
e.printStackTrace();
|
} catch (com.aliyuncs.exceptions.ClientException e) {
|
e.printStackTrace();
|
}
|
if (response == null) {
|
LogUtils.print("MQTT RRPC调用服务异常");
|
return null;
|
}
|
|
if (response != null && response.getSuccess()) {
|
return response.getData().getResult();
|
} else {
|
LogUtils.error("MQTT RRPC服务调用失败");
|
return null;
|
}
|
}
|
|
private static InvokeThingServiceRequest getReq(String productKey,
|
String mac,
|
RRPCParams rrpcParams) {
|
InvokeThingServiceRequest request = new InvokeThingServiceRequest();
|
request.setProductKey(productKey);
|
request.setDeviceName(mac);
|
request.setIdentifier("rrpc");
|
request.setArgs(JSON.toJSONString(rrpcParams));
|
return request;
|
}
|
|
}
|