java110
2023-03-17 d01347c33be03a99526fc35c68dbc022e13c886e
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
package com.java110.common.charge.factory.dingding;
 
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.common.charge.IChargeFactoryAdapt;
import com.java110.dto.chargeMachine.ChargeMachineDto;
import com.java110.dto.chargeMachineOrder.NotifyChargeOrderDto;
import com.java110.dto.chargeMachineOrder.NotifyChargePortDto;
import com.java110.dto.chargeMachinePort.ChargeMachinePortDto;
import com.java110.vo.ResultVo;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 叮叮充电桩 充电接口类
 * <p>
 * 叮叮 充电桩 通知处理类名为 NotifyDingDingChargeController.java
 */
@Service("dingdingChargeMachineFactory")
public class DingdingChargeMachineFactoryAdapt implements IChargeFactoryAdapt {
 
    private static final String QUERY_PORT_URL = DingdingChargeUtils.URL + "/equipments/ID/PORT";
 
    private static final String QUERY_CHARGE_STATE_URL = DingdingChargeUtils.URL + "/equipments/ID";
 
 
    //开始充电
    private static final String START_CHARGE_URL = DingdingChargeUtils.URL + "/equipments/ID/PORT/open";
 
    //关闭充电
    private static final String STOP_CHARGE_URL = DingdingChargeUtils.URL + "/equipments/ID/PORT/close";
 
    @Override
    public ResultVo startCharge(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto, String chargeType, double duration, String orderId) {
        String url = START_CHARGE_URL.replace("ID", chargeMachineDto.getMachineCode()).replace("PORT", chargeMachinePortDto.getPortCode());
 
        JSONObject body = new JSONObject();
        body.put("mode", 1);
        body.put("duration", duration * 60);
        body.put("energy", 0);
        body.put("chargeType", "order");
        body.put("chargeId", orderId);
        String paramOut = null;
        try {
            paramOut = DingdingChargeUtils.execute(url, body.toJSONString(), HttpMethod.POST);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut);
        if (paramObj.getIntValue("code") != 200) {
            throw new IllegalArgumentException(paramObj.getString("msg"));
        }
 
        return new ResultVo(ResultVo.CODE_OK, paramObj.getString("msg"));
    }
 
    @Override
    public ResultVo stopCharge(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto) {
        String url = STOP_CHARGE_URL.replace("ID", chargeMachineDto.getMachineCode()).replace("PORT", chargeMachinePortDto.getPortCode());
 
        JSONObject body = new JSONObject();
        String paramOut = null;
        try {
            paramOut = DingdingChargeUtils.execute(url, body.toJSONString(), HttpMethod.POST);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut);
        if (paramObj.getIntValue("code") != 200) {
            throw new IllegalArgumentException(paramObj.getString("msg"));
        }
 
        return new ResultVo(ResultVo.CODE_OK, paramObj.getString("msg"));
    }
 
    @Override
    public ChargeMachinePortDto getChargePortState(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto) {
 
        String url = QUERY_PORT_URL.replace("ID", chargeMachineDto.getMachineCode()).replace("PORT", chargeMachinePortDto.getPortCode());
 
        String paramOut = null;
        try {
            paramOut = DingdingChargeUtils.execute(url, "", HttpMethod.GET);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut);
        if (paramObj.getIntValue("code") != 200) {
            throw new IllegalArgumentException(paramObj.getString("msg"));
        }
 
        chargeMachinePortDto.setState(paramObj.getJSONObject("data").getString("status"));
        return chargeMachinePortDto;
    }
 
    @Override
    public List<NotifyChargePortDto> getChargeHeartBeatParam(NotifyChargeOrderDto notifyChargeOrderDto) {
 
        JSONArray jsonArray = JSONArray.parseArray(notifyChargeOrderDto.getBodyParam());
        List<NotifyChargePortDto> ports = new ArrayList<>();
 
        if (jsonArray == null || jsonArray.size() < 1) {
            return ports;
        }
        NotifyChargePortDto port = null;
        for (int portIndex = 0; portIndex < jsonArray.size(); portIndex++) {
            port = new NotifyChargePortDto();
            port.setPortCode(jsonArray.getJSONObject(portIndex).getString("port"));
            port.setMachineCode(notifyChargeOrderDto.getMachineCode());
            port.setOrderId(jsonArray.getJSONObject(portIndex).getString("chargeId"));
            port.setEnergy(jsonArray.getJSONObject(portIndex).getString("energy"));
            long time = jsonArray.getJSONObject(portIndex).getLongValue("powerTime");
            port.setPowerTime(new Date(time));
            ports.add(port);
        }
 
        return ports;
    }
 
    @Override
    public void queryChargeMachineState(ChargeMachineDto chargeMachineDto) {
        String url = QUERY_CHARGE_STATE_URL.replace("ID", chargeMachineDto.getMachineCode());
 
        String paramOut = null;
        try {
            paramOut = DingdingChargeUtils.execute(url, "", HttpMethod.GET);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut);
        if (paramObj.getIntValue("code") != 200) {
            throw new IllegalArgumentException(paramObj.getString("msg"));
        }
 
        Boolean online = paramObj.getJSONObject("data").getBoolean("online");
        if (online) {
            chargeMachineDto.setState(ChargeMachineDto.STATE_ONLINE);
            chargeMachineDto.setStateName("在线");
            return;
        }
 
        chargeMachineDto.setState(ChargeMachineDto.STATE_OFFLINE);
        chargeMachineDto.setStateName("离线");
    }
}