2021与蓝度共同重构项目,服务端
liuhaonan
2022-02-14 37041ac6ae6baa2996f447e495f6f5280113c4a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
    }
 
}