Pu Zhibing
2025-02-05 19b7530c7feb8eca1dfecd6502feaf61057fdab8
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.ruoyi.integration.drainage;
 
import cn.hutool.http.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.integration.drainage.kuaidian.model.NotificationStationChangeResult;
import com.ruoyi.integration.drainage.kuaidian.model.StationChange;
import com.ruoyi.integration.drainage.model.*;
import com.ruoyi.integration.drainage.model.enu.InterfaceUrlEnum;
import com.ruoyi.other.api.domain.Operator;
import com.ruoyi.other.api.feignClient.OperatorClient;
import lombok.extern.slf4j.Slf4j;
 
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 中电联TCEC标准
 * @author zhibing.pu
 * @Date 2025/1/21 11:48
 */
@Slf4j
public class TCECUtil {
    
//    private static OperatorClient operatorClient = SpringUtils.getBean(OperatorClient.class);
    
    public static Map<Long, Integer> map = new HashMap<>();
    
    
    /**
     * 获取token
     */
    public static String queryToken(Operator operator){
        HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.QUERY_TOKEN.getUrl());
        post.contentType("application/json;charset=utf-8");
        JSONObject body = new JSONObject();
        body.put("OperatorID", operator.getOurOperatorId());
        body.put("OperatorSecret", operator.getOurOperatorSecret());
        post.body(body.toJSONString());
        HttpResponse execute = post.execute();
        if(200 != execute.getStatus()){
            log.error("获取三方token失败:" + execute.body());
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(execute.body());
        Integer succStat = jsonObject.getInteger("SuccStat");
        if(0 != succStat){
            log.error("获取三方token失败:" + jsonObject.getString("FailReason"));
            return null;
        }
        String token = jsonObject.getString("AccessToken");
        Long tokenAvailableTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) + jsonObject.getLong("TokenAvailableTime");
        LocalDateTime localDateTime = Instant.ofEpochSecond(tokenAvailableTime).atOffset(ZoneOffset.UTC).toLocalDateTime();
        operator.setAccessToken(token);
        operator.setTokenAvailableTime(localDateTime);
//        operatorClient.editOperator(operator);
        return token;
    }
    
    
    /**
     * 获取token
     * @return
     */
    public static String getToken(Operator operator){
        if(null != operator.getTokenAvailableTime() && operator.getTokenAvailableTime().isBefore(LocalDateTime.now())){
            return operator.getAccessToken();
        }else{
            return queryToken(operator);
        }
    }
    
    
    
    /**
     * 设备状态变化推送
     * @param info
     */
    public static NotificationStationStatusResult notificationStationStatus(Operator operator, ConnectorStatusInfo info) {
        HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_STATION_STATUS.getUrl());
        buildBody(post, info, operator);
        HttpResponse execute = post.execute();
        if(200 != execute.getStatus()){
            log.error("推送三方设备状态变化失败:" + execute.body());
            return null;
        }
        return JSON.parseObject(execute.body(), NotificationStationStatusResult.class);
    }
    
    
    
    
    
    
    /**
     * 推送启动充电结果
     * @param info
     * @return
     */
    public static NotificationStartChargeResult notificationStartChargeResult(Operator operator, NotificationStartCharge info){
        HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_START_CHARGE_RESULT.getUrl());
        buildBody(post, info, operator);
        HttpResponse execute = post.execute();
        if(200 != execute.getStatus()){
            log.error("推送三方启动充电结果失败:" + execute.body());
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(execute.body());
        Integer ret = jsonObject.getInteger("Ret");
        if(0 != ret){
            log.error("推送三方启动充电结果失败:" + execute.body());
            return null;
        }
        return jsonObject.getObject("Data", NotificationStartChargeResult.class);
    }
    
    
    
    
    
    
    
    /**
     * 推动充电状态
     * @param info
     * @return
     */
    public static NotificationEquipChargeStatusResult notificationEquipChargeStatus(Operator operator, QueryEquipChargeStatusResult info){
        HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_EQUIP_CHARGE_STATUS.getUrl());
        buildBody(post, info, operator);
        HttpResponse execute = post.execute();
        if(200 != execute.getStatus()){
            log.error("推送三方充电状态失败:" + execute.body());
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(execute.body());
        Integer ret = jsonObject.getInteger("Ret");
        if(0 != ret){
            log.error("推送三方充电状态失败:" + execute.body());
            return null;
        }
        return jsonObject.getObject("Data", NotificationEquipChargeStatusResult.class);
    }
    
    
    /**
     * 推送停止充电结果
     * @param info
     * @return
     */
    public static NotificationStopCharge notificationStopChargeResult(Operator operator, NotificationStopChargeResult info){
        HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_STOP_CHARGE_RESULT.getUrl());
        buildBody(post, info, operator);
        HttpResponse execute = post.execute();
        if(200 != execute.getStatus()){
            log.error("推送三方停止充电结果失败:" + execute.body());
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(execute.body());
        Integer ret = jsonObject.getInteger("Ret");
        if(0 != ret){
            log.error("推送三方停止充电结果失败:" + execute.body());
            return null;
        }
        return jsonObject.getObject("Data", NotificationStopCharge.class);
    }
    
    
    
    
    /**
     * 推送充电订单信息
     * @param info
     * @return
     */
    public static NotificationChargeOrderInfoResult notificationChargeOrderInfo(Operator operator, NotificationChargeOrderInfo info){
        HttpRequest post = HttpUtil.createPost(operator.getTestUrl() + InterfaceUrlEnum.NOTIFICATION_CHARGE_ORDER_INFO.getUrl());
        buildBody(post, info, operator);
        HttpResponse execute = post.execute();
        if(200 != execute.getStatus()){
            log.error("推送三方充电订单信息失败:" + execute.body());
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(execute.body());
        Integer ret = jsonObject.getInteger("Ret");
        if(0 != ret){
            log.error("推送三方充电订单信息失败:" + execute.body());
            return null;
        }
        return jsonObject.getObject("Data", NotificationChargeOrderInfoResult.class);
    }
    
    
    /**
     * 构建请求参数和消息头
     * @param post
     * @param o
     */
    public static void buildBody(HttpRequest post, Object o, Operator operator){
        Long timeStamp = Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        Integer integer = map.get(timeStamp);
        if(null == integer){
            integer = 1;
        }else{
            integer++;
        }
        map.put(timeStamp, integer);
        post.contentType("application/json;charset=utf-8");
        post.header("Authorization", "Bearer " + getToken(operator));
        BaseRequest baseRequest = new BaseRequest();
        baseRequest.setOperatorID(operator.getOurOperatorId());
        baseRequest.setTimeStamp(timeStamp);
        baseRequest.setSeq(String.format("%04d", integer));
        String jsonString = JSON.toJSONString(o);
        String encrypt = AESUtil.encrypt(jsonString, operator.getOurDataSecret(), operator.getOurDataSecretIv());
        baseRequest.setData(encrypt);
        baseRequest.setOperator(operator);
        baseRequest.setSig(ourBuildSign(baseRequest));
        post.body(JSON.toJSONString(baseRequest));
        //清空小于当前时间的map中的无效数据
        for (Long k : map.keySet()) {
            if(k <= (timeStamp - 10)){
                map.remove(k);
            }
        }
    }
    
    
    /**
     * 构建签名字符串
     * @param model
     * @return
     */
    public static String buildSign(BaseModel model){
        Operator operator = model.getOperator();
        //签名秘钥SigSecret
        String key = operator.getSigSecret();
        String sign = "";
        switch (operator.getName()){
            case "XinDianTu":
                //进行字符串拼接、计算
                String m = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
                byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes());
                // 打印计算得到的签名Sig
                sign = SignUtil.bytesToHexString(hmacMd5);
                break;
            case "KuaiDian":
                sign = SignUtil.hmacSign(model.getData(), key);
                break;
        }
        return sign;
    }
    
    
    
    /**
     * 构建签名字符串
     * @param model
     * @return
     */
    public static String ourBuildSign(BaseModel model){
        Operator operator = model.getOperator();
        //签名秘钥SigSecret
        String key = operator.getOurSigSecret();
        String sign = "";
        switch (operator.getName()){
            case "XinDianTu":
                //进行字符串拼接、计算
                String m = new StringBuilder(operator.getOurOperatorId()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
                byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes());
                // 打印计算得到的签名Sig
                sign = SignUtil.bytesToHexString(hmacMd5);
                break;
            case "KuaiDian":
                sign = SignUtil.hmacSign(model.getData(), key);
                break;
        }
        return sign;
    }
    
    
    public static void main(String[] args) {
        BaseModel model = new BaseModel();
        model.setOperatorID("MA25CNM38");
        model.setData("lkQUXmphG3h5Wf0UIxSfzNoCdqDLYkmDGceCbofJuWFqKBwCYulTUHSn9bJpVMVFNji/4xtSq3o1U5jSZZwIhA==");
        model.setTimeStamp(20250205120800L);
        model.setSeq("0001");
        Operator operator = new Operator();
        operator.setName("XinDianTu");
        operator.setSigSecret("S94xUpTpOIlLJBk8");
        model.setOperator(operator);
        String key = operator.getSigSecret();
        
        String m = new StringBuilder(model.getOperatorID()).append(model.getData()).append(model.getTimeStamp()).append(model.getSeq()).toString();
        byte[] hmacMd5 = SignUtil.getHMacMD5Bytes(key.getBytes(), m.getBytes());
        // 打印计算得到的签名Sig
        String s = SignUtil.bytesToHexString(hmacMd5);
        System.err.println(s);
    }
}