whyclxw
2024-09-03 ac892b6a175b752374986810f4d8aa7fdc30c0c8
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
package com.whyc.service;
 
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.whyc.dto.A200ResDto;
import com.whyc.dto.Response;
import com.whyc.dto.ResultA200Dto;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.mapper.DevA200TestparamMapper;
import com.whyc.mapper.DevInfMapper;
import com.whyc.pojo.db_lithium_ram_db.DevA200Testparam;
import com.whyc.pojo.db_lithium_ram_db.DevInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.TestparamHttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
@Service
public class DevA200TestparamService {
    @Autowired(required = false)
    private DevA200TestparamMapper mapper;
 
    @Autowired(required = false)
    private DevInfMapper dinfMapper;
 
    @Resource
    private RestTemplate restTemplate;
 
    //读取a200一体机参数
    public Object getA200Param(int devId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("dev_id",devId);
        wrapper.last("limit 1");
        DevInf dinf=dinfMapper.selectOne(wrapper);
        String url="http://"+dinf.getDevIp()+":8080/webService/getTestParameter";
        String results = restTemplate.postForObject(url, null, String.class);
        ResultA200Dto dto= ActionUtil.getGson().fromJson(results, ResultA200Dto.class);
        if(dto.getCode().equals("200")){
            DevA200Testparam a200Param=new DevA200Testparam();
            copyA200DTO(dto.getData(),a200Param);
            a200Param.setDevId(devId);
            a200Param.setReturnCode(Integer.parseInt(dto.getCode()));
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("dev_id",devId);
            mapper.update(a200Param,updateWrapper);
        }
        return dto;
    }
 
 
    //设置a200均衡仪参数
    public Object setA200Param(A200ResDto param) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("dev_id",param.getDevId());
        wrapper.last("limit 1");
        DevInf dinf=dinfMapper.selectOne(wrapper);
        String url="http://"+dinf.getDevIp()+":8080/webService/setTestParameter";
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        String paramJson=JSONObject.toJSONString(param);
        paramMap.add("testParameter",paramJson);
        ResultA200Dto dto= (ResultA200Dto) TestparamHttpUtil.postforform_dataA200(restTemplate,url,paramMap);
        return dto;
    }
 
    //批量设置a200均衡仪参数
    public Object setA200ParamPl(A200ResDto param) {
        Map<Integer,Object> map=new HashMap<>();
        int size=param.getDevIds().size();
        try {
            ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
            CountDownLatch latch = new CountDownLatch(size);
            for (Integer devId:param.getDevIds()) {
                poolExecutor.execute(() -> {
                    param.setDevId(devId);
                    ResultA200Dto dto= (ResultA200Dto) setA200Param(param);
                    map.put(param.getDevId(),dto);
                    latch.countDown();
                });
            }
            latch.await(10, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Response().setII(1,true,map,"批量设置a200均衡仪参数");
    }
 
    //启动a200均衡仪放电/充电
    public Object startA200Param(int devId,int type) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("dev_id",devId);
        wrapper.last("limit 1");
        DevInf dinf=dinfMapper.selectOne(wrapper);
        String url="http://"+dinf.getDevIp()+":8080/webService/startTest";
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("testType",type);
        ResultA200Dto dto= (ResultA200Dto) TestparamHttpUtil.postforform_dataA200(restTemplate,url,paramMap);
        return dto;
    }
    //停止a200均衡仪放电/充电
    public Object stopA200Param(int devId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("dev_id",devId);
        wrapper.last("limit 1");
        DevInf dinf=dinfMapper.selectOne(wrapper);
        String url="http://"+dinf.getDevIp()+":8080/webService/stopTest";
        String results = restTemplate.postForObject(url, null, String.class);
        ResultA200Dto dto= ActionUtil.getGson().fromJson(results, ResultA200Dto.class);
        return dto;
    }
 
    //批量启动a200均衡仪
    public Response startA200ParamPl(List<Integer> devIds,int type) {
        Map<Integer,Object> map=new HashMap<>();
        try {
            ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
            CountDownLatch latch = new CountDownLatch(devIds.size());
            for (int devId:devIds) {
                poolExecutor.execute(() -> {
                    ResultA200Dto dto= (ResultA200Dto) startA200Param(devId,type);
                    map.put(devId,dto);
                    latch.countDown();
                });
            }
            latch.await(10, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Response().set(1,true,"批量控制a200均衡仪");
    }
 
    //批量启动a200均衡仪
    public Response stopA200ParamPl(List<Integer> devIds) {
        Map<Integer,Object> map=new HashMap<>();
        try {
            ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
            CountDownLatch latch = new CountDownLatch(devIds.size());
            for (int devId:devIds) {
                poolExecutor.execute(() -> {
                    ResultA200Dto dto= (ResultA200Dto) stopA200Param(devId);
                    map.put(devId,dto);
                    latch.countDown();
                });
            }
            latch.await(10, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Response().set(1,true,"批量控制a200均衡仪");
    }
 
    //将dto拷贝至param
    private void copyA200DTO(Object data, DevA200Testparam a200Param) {
        A200ResDto dto= (A200ResDto) data;
        a200Param.setBatteryName(dto.getBatteryName());
        a200Param.setNominalCapacity(dto.getNominalCapacity());
        a200Param.setBatteryType(dto.getBatteryType());
        a200Param.setBatteryNumber(dto.getBatteryNumber());
        a200Param.setDiffBatteryVoltage(dto.getDiffBatteryVoltage());
        a200Param.setDischargeCurrent(dto.getDischargeCurrent());
        a200Param.setDischargeVoltage(dto.getDischargeVoltage());
        a200Param.setDischargeTime(dto.getDischargeTime());
        a200Param.setDischargeCapacity(dto.getDischargeCapacity());
        if(dto.getIsDischargeCapacityEnable()){
            a200Param.setIsDischargeCapacityEnable(1);
        }else{
            a200Param.setIsDischargeCapacityEnable(0);
        }
        a200Param.setMinBatteryVoltage(dto.getMinBatteryVoltage());
        if(dto.getIsMinBatteryVoltageEnable()){
            a200Param.setIsMinBatteryVoltageEnable(1);
        }else{
            a200Param.setIsMinBatteryVoltageEnable(0);
        }
        a200Param.setChargeVoltage(dto.getChargeVoltage());
        a200Param.setChargeProtectVoltage(dto.getChargeProtectVoltage());
        a200Param.setChargeCurrent(dto.getChargeCurrent());
        a200Param.setChargeStopCurrent(dto.getChargeStopCurrent());
        a200Param.setChargeTime(dto.getChargeTime());
        a200Param.setChargeCapacity(dto.getChargeCapacity());
        if(dto.getIsChargeCapacityEnable()){
            a200Param.setIsChargeCapacityEnable(1);
        }else{
            a200Param.setIsChargeCapacityEnable(0);
        }
 
        a200Param.setMaxBatteryVoltage(dto.getMaxBatteryVoltage());
        if(dto.getIsMaxBatteryVoltageEnable()){
            a200Param.setIsMaxBatteryVoltageEnable(1);
        }else{
            a200Param.setIsMaxBatteryVoltageEnable(0);
        }
        a200Param.setChargePower(dto.getChargePower());
        a200Param.setBatteryTemperatureProtect(dto.getBatteryTemperatureProtect());
    }
 
 
}