Your Name
2023-04-17 da3b5c72fa45bc26a9868c2b26e3efda14845179
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
package com.java110.common.charge.factory.kehang;
 
 
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.chargeMachine.NotifyChargeOrderDto;
import com.java110.dto.chargeMachine.NotifyChargePortDto;
import com.java110.dto.chargeMachine.ChargeMachinePortDto;
import com.java110.vo.ResultVo;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * 科航充电桩
 */
@Service("keHangChargeMachineFactory")
public class KeHangChargeMachineFactoryAdapt implements IChargeFactoryAdapt {
    @Override
    public ResultVo startCharge(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto, String chargeType, double duration, String orderId) {
        JSONObject body = new JSONObject();
        body.put("version", "2.0");
        body.put("time", duration * 60);
        body.put("equipCd", chargeMachineDto.getMachineCode());
        body.put("port", chargeMachinePortDto.getPortCode());
        body.put("chargeId", orderId);
        String paramOut = null;
        try {
            paramOut = KeHangChargeUtils.execute("net.equip.charge.slow.run", body.toJSONString(), HttpMethod.GET);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut);
        if (paramObj.getIntValue("code") != 1) {
            throw new IllegalArgumentException(paramObj.getString("msg"));
        }
 
        return new ResultVo(ResultVo.CODE_OK, paramObj.getString("msg"));
    }
 
    @Override
    public ResultVo stopCharge(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto) {
        JSONObject body = new JSONObject();
        body.put("equipCd", chargeMachineDto.getMachineCode());
        body.put("port", chargeMachinePortDto.getPortCode());
        String paramOut = null;
        try {
            paramOut = KeHangChargeUtils.execute("net.equip.charge.slow.run.stop", body.toJSONString(), HttpMethod.GET);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut);
        if (paramObj.getIntValue("code") != 1) {
            throw new IllegalArgumentException(paramObj.getString("msg"));
        }
 
        return new ResultVo(ResultVo.CODE_OK, paramObj.getString("msg"));
    }
 
    @Override
    public ChargeMachinePortDto getChargePortState(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto) {
        JSONObject body = new JSONObject();
        body.put("equipCd", chargeMachineDto.getMachineCode());
        String paramOut = null;
        try {
            paramOut = KeHangChargeUtils.execute("net.equip.charge.slow.port.query", body.toJSONString(), HttpMethod.GET);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut);
        if (paramObj.getIntValue("code") != 1) {
            throw new IllegalArgumentException(paramObj.getString("msg"));
        }
 
        JSONArray ports = paramObj.getJSONArray("ports");
        String state = ChargeMachinePortDto.STATE_BREAKDOWN;
        JSONObject port = null;
        for(int portIndex = 0;portIndex < ports.size() ;portIndex++){
            port = ports.getJSONObject(portIndex);
            if(chargeMachinePortDto.getPortCode().equals(port.getString("port"))){
                if("1".equals(port.getString("status"))){
                    state = ChargeMachinePortDto.STATE_FREE;
                } else if ("2".equals(port.getString("status"))) {
                    state = ChargeMachinePortDto.STATE_WORKING;
                }else{
                    state = ChargeMachinePortDto.STATE_BREAKDOWN;
                }
            }
        }
 
        chargeMachinePortDto.setState(state);
        return chargeMachinePortDto;
    }
 
    @Override
    public void queryChargeMachineState(ChargeMachineDto chargeMachineDto) {
        JSONObject body = new JSONObject();
        body.put("equipCd", chargeMachineDto.getMachineCode());
        String paramOut = null;
        try {
            paramOut = KeHangChargeUtils.execute("net.equip.online.query", body.toJSONString(), HttpMethod.GET);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut);
        if (paramObj.getIntValue("code") != 1) {
            throw new IllegalArgumentException(paramObj.getString("msg"));
        }
 
        JSONObject data = paramObj.getJSONObject("data");
 
        if(data.getBoolean("online")){
            chargeMachineDto.setState(ChargeMachineDto.STATE_ONLINE);
            chargeMachineDto.setStateName("在线");
            return ;
        }
 
        chargeMachineDto.setState(ChargeMachineDto.STATE_OFFLINE);
        chargeMachineDto.setStateName("离线");
    }
 
    @Override
    public void workHeartbeat(ChargeMachineDto chargeMachineDto, String bodyParam) {
 
    }
 
    @Override
    public List<NotifyChargePortDto> getChargeHeartBeatParam(NotifyChargeOrderDto notifyChargeOrderDto) {
        return null;
    }
}