jiazx0107@163.com
2023-11-08 0a8ef890592a9c0389c42daeebb9a3f0974c37e6
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.fzzy.gateway.hx2023.service;
 
import com.alibaba.fastjson2.JSONObject;
import com.fzzy.api.data.PushProtocol;
import com.fzzy.api.utils.MyMD5Util;
import com.fzzy.api.utils.RSAUtils;
import com.fzzy.api.utils.RedisConst;
import com.fzzy.api.utils.RedisUtil;
import com.fzzy.api.view.repository.ApiLogRep;
import com.fzzy.gateway.service.GatewayConfService;
import com.fzzy.gateway.util.GatewayHttpUtil;
import com.fzzy.gateway.api.GatewayRemoteService;
import com.fzzy.gateway.entity.GatewayConf;
import com.fzzy.gateway.hx2023.ScConstant;
import com.fzzy.gateway.hx2023.data.CloudResp;
import com.fzzy.gateway.util.GatewayRSAUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
 
 
@Slf4j
@Data
@Component
public class HxGatewayRemoteServiceImpl implements GatewayRemoteService {
 
 
    @Resource
    private ApiLogRep apiLogRep;
 
    @Resource
    private GatewayConfService gatewayConfService;
 
 
    @Override
    public String getProtocol() {
        return PushProtocol.GATEWAY_SC_2023.getCode();
    }
 
    @Override
    public void init(GatewayConf gatewayConf) {
        try {
 
            if (StringUtils.isEmpty(gatewayConf.getApiUrl())) {
                return;
            }
 
            //获取公私钥接口
            Map<String, String> params = new HashMap<>();
            params.put("appId", gatewayConf.getGatewayId());
 
            String url = gatewayConf.getApiUrl() + "reserver/api/key/apply";
            String jsonStr = GatewayHttpUtil.doGet(url, params);
 
            log.info("---获取公私钥接口-返回---{}", jsonStr);
 
            CloudResp respKey = JSONObject.parseObject(jsonStr, CloudResp.class);
 
            if (ScConstant.CODE_200 == respKey.getCode()) {
                JSONObject object = respKey.getData();
                String pubKey = (String) object.get("pubKey");
                String priKey = (String) object.get("priKey");
 
                gatewayConf.setPublicKey(pubKey);
                gatewayConf.setPrivateKey(priKey);
            }
 
 
            //获取 AccessToken 接口
            String sign = getSign(params, gatewayConf.getPrivateKey());
            params.put("sign", sign);
            url = gatewayConf.getApiUrl() + "reserver/api/token/apply";
            jsonStr = GatewayHttpUtil.doGet(url, params);
            log.info("---获取AccessToken接口-返回---{}", jsonStr);
            CloudResp respToken = JSONObject.parseObject(jsonStr, CloudResp.class);
 
            if (ScConstant.CODE_200 == respToken.getCode()) {
                JSONObject object = respKey.getData();
                if (null != object) {
                    String token = (String) object.get("token");
                    gatewayConf.setAccessToken(token);
                }
            }
 
            //更新缓存
            updateAuthToken(gatewayConf);
 
        } catch (Exception e) {
            log.error("------初始化失败-----{}", e);
        }
    }
 
    @Override
    public void heartbeat(GatewayConf gatewayConf) {
        try {
 
            if (StringUtils.isEmpty(gatewayConf.getApiUrl())) {
                return;
            }
 
            gatewayConf = getCacheConf(gatewayConf.getKqdm());
            //网关心跳接口
            Map<String, String> params = new HashMap<>();
            params.put("token", gatewayConf.getAccessToken());
            params.put("gatewayId", gatewayConf.getGatewayId());
            params.put("gatewayIp", gatewayConf.getGatewayIp());
            params.put("gatewayMac", gatewayConf.getGatewayMac());
            params.put("heartbeat", "1");
            params.put("timestamp", System.currentTimeMillis() + "");
 
            String sign = getSign(params, gatewayConf.getPrivateKey());
            params.put("sign", sign);
 
            String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat";
 
            String jsonStr = GatewayHttpUtil.doGet(url, params);
            log.info("---网关心跳接口-返回---{}", jsonStr);
 
        } catch (Exception e) {
            log.error("------网关心跳接口--执行失败-----{}", e);
        }
 
    }
 
    @Override
    public void pushInfo(GatewayConf gatewayConf) {
        try {
 
            if (StringUtils.isEmpty(gatewayConf.getApiUrl())) {
                return;
            }
 
            gatewayConf = getCacheConf(gatewayConf.getKqdm());
            //网关心跳接口
            Map<String, String> params = new HashMap<>();
            params.put("token", gatewayConf.getAccessToken());
            params.put("gatewayId", gatewayConf.getGatewayId());
            params.put("gatewayIp", gatewayConf.getGatewayIp());
            params.put("gatewayMac", gatewayConf.getGatewayMac());
            params.put("gatewayCPU", gatewayConf.getGatewayCPU());
            params.put("gatewayMem", gatewayConf.getGatewayMem());
            params.put("gatewayHardDisk", gatewayConf.getGatewayHardDisk());
            params.put("timestamp", System.currentTimeMillis() + "");
 
            String sign = getSign(params, gatewayConf.getPrivateKey());
            params.put("sign", sign);
 
            String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat";
 
            String jsonStr = GatewayHttpUtil.doGet(url, params);
            log.info("---推送网关信息-返回---{}", jsonStr);
 
        } catch (Exception e) {
            log.error("------推送网关信息--执行失败-----{}", e);
        }
    }
 
 
    public String getSign(Map<String, String> parames, String priKey) {
        //参数拼接
        String msg = "";
        for (Map.Entry<String, String> param : parames.entrySet()) {
            msg += param.getKey() + "=" + param.getValue() + "&";
        }
        msg = msg.substring(0, msg.length() - 1);
 
        log.debug("------待加密信息-----{}", msg);
 
        //MD5加密
        String md5sign = MyMD5Util.getMD5(msg);
        log.debug("------md5加密-----{}", md5sign);
 
        //RSA加密
        String result = GatewayRSAUtils.encryptByPrivate(md5sign, priKey);
        log.debug("------RSA加密-----{}", result);
 
        return result;
    }
 
 
    /**
     * @param kqdm
     * @return
     */
    public GatewayConf getCacheConf(String kqdm) {
        return gatewayConfService.getCacheConf(kqdm);
    }
 
    private void updateAuthToken(GatewayConf conf) {
 
        gatewayConfService.updateCache(conf);
 
    }
 
 
}