From ab061be50b7653531cbe134416c3ebdd876e4791 Mon Sep 17 00:00:00 2001 From: cjl <276999030@qq.com> Date: Thu, 19 Oct 2023 13:56:17 +0800 Subject: [PATCH] fix:风场数据范围提交 --- screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java | 638 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 599 insertions(+), 39 deletions(-) diff --git a/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java index dd63413..f0cb4d7 100644 --- a/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java +++ b/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java @@ -1,33 +1,48 @@ package com.moral.api.service.impl; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.moral.api.config.mybatis.MybatisPlusConfig; +import com.moral.api.config.rest.Crc16Utils; +import com.moral.api.entity.Device; import com.moral.api.entity.HistoryHourly; import com.moral.api.entity.Sensor; +import com.moral.api.mapper.DeviceMapper; import com.moral.api.mapper.HistoryHourlyMapper; import com.moral.api.mapper.HistoryMinutelyMapper; import com.moral.api.service.HistoryHourlyService; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.service.SensorService; import com.moral.constant.Constants; import com.moral.constant.RedisConstants; +import com.moral.constant.SeparateTableType; import com.moral.util.AmendUtils; import com.moral.util.DateUtils; +import com.moral.util.MybatisPLUSUtils; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.PostMethod; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.OptionalDouble; -import java.util.Set; +import java.io.*; +import java.math.BigDecimal; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.DoubleStream; +import java.util.stream.Stream; /** * <p> @@ -38,10 +53,16 @@ * @since 2021-06-28 */ @Service -public class HistoryHourlyServiceImpl extends ServiceImpl<HistoryHourlyMapper, HistoryHourly> implements HistoryHourlyService { +@Slf4j +public class HistoryHourlyServiceImpl implements HistoryHourlyService { + @Value("${result.date.changshu}") + private String dateChangShu; @Autowired private HistoryHourlyMapper historyHourlyMapper; + + @Autowired + private DeviceMapper deviceMapper; @Autowired private RedisTemplate redisTemplate; @@ -51,6 +72,16 @@ @Autowired private SensorService sensorService; + + @Override + public void createTable(String timeUnits) { + historyHourlyMapper.createTable(timeUnits); + } + + @Override + public void createTableComplete(String timeUnits) { + historyHourlyMapper.createTableComplete(timeUnits); + } @Override public void insertHistoryHourly() { @@ -63,21 +94,21 @@ Date now = new Date(); String time = DateUtils.dateToDateString(now, format) + ":00:00"; - QueryWrapper<HistoryHourly> queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("time", time); //������������������������������������������������macs������������������������������ - Integer count = historyHourlyMapper.selectCount(queryWrapper); - if (macs.size() > count) { + Map<String, Object> prop = new HashMap<>(); + prop.put("timeUnits", DateUtils.getDateStringOfMon(0, DateUtils.yyyyMM_EN)); + prop.put("time", time); + Integer count = historyHourlyMapper.selectCountByTime(prop); + + if (macs.size() <= count) { + return; + } else { macs.removeIf(mac -> { - queryWrapper.clear(); - queryWrapper.eq("time", time); - queryWrapper.eq("mac", mac); - Integer num = historyHourlyMapper.selectCount(queryWrapper); + prop.put("mac", mac); + Integer num = historyHourlyMapper.selectCountByTime(prop); return num != 0; }); } - - Map<String, Object> params = new HashMap<>(); //������������ String dateString = DateUtils.getDateStringOfHour(-1, format); @@ -91,9 +122,11 @@ //������ QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); - sensorQueryWrapper.select("code").eq("is_delete", Constants.NOT_DELETE); - List<Object> sensorCodes = sensorService.listObjs(sensorQueryWrapper); + sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE); + List<Sensor> sensors = sensorService.list(sensorQueryWrapper); + //������������������������������������������ + Map<String, Object> params = new HashMap<>(); params.put("timeUnits", timeUnits); params.put("start", start); params.put("end", end); @@ -109,47 +142,574 @@ .collect(Collectors.groupingBy(o -> (String) o.get("mac"))); //��������������������������� - List<HistoryHourly> insertData = new ArrayList<>(); + List<Map<String, Object>> insertData = new ArrayList<>(); data.forEach((key, value) -> { - HistoryHourly historyHourly = new HistoryHourly(); - historyHourly.setMac(key); - historyHourly.setTime(end); + Map<String, Object> historyHourly = new HashMap<>(); + historyHourly.put("mac", key); + historyHourly.put("time", end); + Map<String, Object> jsonMap = new HashMap<>(); + Map<String, Object> map = new HashMap<>(); + map.put("data", value); + map.put("type", "hour"); + + for (Sensor sensor : sensors) { + String sensorCode = sensor.getCode(); + + //��������������� + if (sensorCode.equals(Constants.SENSOR_CODE_WIND_DIR)) { + if (sensor.getUpper() != null) { + map.put("windDirUpper", sensor.getUpper()); + } + if (sensor.getLower() != null) { + map.put("windDirLower", sensor.getLower()); + } + } + + //��������������� + if (sensorCode.equals(Constants.SENSOR_CODE_WIND_SPEED)) { + if (sensor.getUpper() != null) { + map.put("windSpeedUpper", sensor.getUpper()); + } + if (sensor.getLower() != null) { + map.put("windSpeedLower", sensor.getLower()); + } + } + } //��������������������������� - Object windDirAvg = AmendUtils.getWindDirAvg(value); - if (windDirAvg != null) { - jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg); + Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(map); + if (!ObjectUtils.isEmpty(windDirAvg)) { + jsonMap.putAll(windDirAvg); } //������������������������������������ - sensorCodes.forEach(sensorCode -> { - OptionalDouble optionalDouble = value.parallelStream() + sensors.forEach(sensor -> { + String sensorCode = sensor.getCode(); + Double upper = sensor.getUpper(); + Double lower = sensor.getLower(); + AtomicInteger size = new AtomicInteger(); + DoubleStream optionalDouble = value.parallelStream() .flatMapToDouble(v -> { Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); - Object sensorValue = dataValue.get(sensorCode.toString()); + Object sensorValue = dataValue.get(sensorCode); + //������������������������ + Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY); + if (!Constants.MARKER_BIT_TRUE.equals(flag)) { + return null; + } + if (ObjectUtils.isEmpty(sensorValue)) { return null; } - //������������������ + + //������������������ if (Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) { return null; } - return DoubleStream.of(Double.parseDouble(sensorValue.toString())); - }).average(); - if (optionalDouble.isPresent()) { + + //������������������������������������ + double aDouble = Double.parseDouble(sensorValue.toString()); + if (!ObjectUtils.isEmpty(upper)) { + if (aDouble < upper) { + return null; + } + } + if (!ObjectUtils.isEmpty(lower)) { + if (aDouble > lower) { + return null; + } + } + size.getAndIncrement(); + return DoubleStream.of(aDouble); + }); + OptionalDouble average = optionalDouble.average(); + if (average.isPresent()) { //��������������������� - double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4); - jsonMap.put(sensorCode.toString(), sciCal); + double sciCal = AmendUtils.sciCal(average.getAsDouble(), 4); + jsonMap.put(sensorCode, sciCal); + //��������� + if (size.get() >= 45) { + jsonMap.put(sensorCode + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_TRUE); + } else { + jsonMap.put(sensorCode + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_FALSE); + } } }); - historyHourly.setValue(JSONObject.toJSONString(jsonMap)); - historyHourly.setVersion((Integer) value.get(0).get("version")); + historyHourly.put("version", value.get(0).get("version")); + historyHourly.put("value", JSONObject.toJSONString(jsonMap)); insertData.add(historyHourly); }); //��������������� historyHourlyMapper.insertHistoryHourly(insertData); + } + + @Override + public List<Map<String, Object>> selectDailyData(Map<String, Object> params) { + return historyHourlyMapper.selectDailyData(params); + } + + /** + * @Description: ���������������������������mac��������� + * @Param: [mac, startDate, endDate] + * @return: java.util.List<com.moral.api.entity.HistoryHourly> + * @Author: ��������� + * @Date: 2021/9/23 + */ + @Override + public List<HistoryHourly> getValueByMacAndTime(String mac, Date startDate, Date endDate) { + QueryWrapper<HistoryHourly> wrapper = new QueryWrapper<>(); + wrapper.eq("mac", mac); + wrapper.between("time", startDate, endDate); + List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH); + List<HistoryHourly> datas = multiTableQuery(wrapper, tableNames); + return datas; + } + + @Override + public void insertHistoryHourlyComplete() { + //������������������yyyy-MM-dd HH:mm + String format = DateUtils.yyyy_MM_dd_HH_EN; + Date now = new Date(); + + //������������������������������ + Map<String, Object> params = new HashMap<>(); + //������������ + Date start = DateUtils.dataToTimeStampTime(DateUtils.addHours(now, -1), format); + //������������ + Date end = DateUtils.dataToTimeStampTime(now, format); + params.put("start", start); + params.put("end", end); + //������������������������������ + String timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); + params.put("timeUnits", timeUnits); + + //������ + QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); + sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE); + List<Sensor> sensors = sensorService.list(sensorQueryWrapper); + + //������������������������������������������ + List<Map<String, Object>> hourlyData = historyMinutelyMapper.getHourlyData(params); + if (ObjectUtils.isEmpty(hourlyData)) { + return; + } + + //���mac������ + Map<String, List<Map<String, Object>>> data = hourlyData.parallelStream() + .collect(Collectors.groupingBy(o -> (String) o.get("mac"))); + + //��������������������������� + List<Map<String, Object>> insertData = new ArrayList<>(); + + data.forEach((key, value) -> { + Map<String, Object> dataMap = new HashMap<>(); + dataMap.put("mac", key); + dataMap.put("time", start); + Map<String, Object> jsonMap = new HashMap<>(); + + + Map<String, Object> map = new HashMap<>(); + map.put("data", value); + map.put("type", "hourlyComplete"); + for (Sensor sensor : sensors) { + String sensorCode = sensor.getCode(); + + //��������������� + if (sensorCode.equals(Constants.SENSOR_CODE_WIND_DIR)) { + if (sensor.getUpper() != null) { + map.put("windDirUpper", sensor.getUpper()); + } + if (sensor.getLower() != null) { + map.put("windDirLower", sensor.getLower()); + } + } + + //��������������� + if (sensorCode.equals(Constants.SENSOR_CODE_WIND_SPEED)) { + if (sensor.getUpper() != null) { + map.put("windSpeedUpper", sensor.getUpper()); + } + if (sensor.getLower() != null) { + map.put("windSpeedLower", sensor.getLower()); + } + } + } + Supplier<Stream<Map<String, Object>>> streamSupplier = () -> value.stream(); + //������������������������������������ + sensors.forEach(sensor -> { + Double avg = calculatedValue(streamSupplier, sensor, "avg"); + if (Constants.SENSOR_CODE_WIND_DIR.equals(sensor.getCode())) { + //��������������������������� + Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(map); + if (!ObjectUtils.isEmpty(windDirAvg)) { + avg = (Double) windDirAvg.get(Constants.SENSOR_CODE_WIND_DIR); + } + } + Double min = calculatedValue(streamSupplier, sensor, "min"); + Double max = calculatedValue(streamSupplier, sensor, "max"); + List<Double> doubles = new ArrayList<>(3); + if (avg != null && min != null && max != null) { + doubles.add(avg); + doubles.add(min); + doubles.add(max); + jsonMap.put(sensor.getCode(), doubles); + } + }); + dataMap.put("value", JSONObject.toJSONString(jsonMap)); + insertData.add(dataMap); + }); + + //������ + String insertTimeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); + //��������������� + historyHourlyMapper.insertHistoryHourlyComplete(insertData, insertTimeUnits); + } + + /** + * @Description: ������������������������������������������������wrapper��������������� + * @Param: [wrapper, tableNames] + * @return: java.util.List<com.moral.api.entity.HistoryHourly> + * @Author: ��������� + * @Date: 2021/9/23 + */ + private List<HistoryHourly> multiTableQuery(QueryWrapper<HistoryHourly> wrapper, List<String> tableNames) { + List<HistoryHourly> result = new ArrayList<>(); + for (String tableName : tableNames) { + MybatisPlusConfig.tableName.set(tableName); + List<HistoryHourly> datas = historyHourlyMapper.selectList(wrapper); + result.addAll(datas); + } + MybatisPlusConfig.tableName.remove(); + return result; + } + + //��������������������������������������� + private Double calculatedValue(Supplier<Stream<Map<String, Object>>> supplier, Sensor sensor, String type) { + String sensorCode = sensor.getCode(); + Double upper = sensor.getUpper(); + Double lower = sensor.getLower(); + DoubleStream doubleStream = supplier.get() + .flatMapToDouble(v -> { + Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); + Object sensorValue = dataValue.get(sensorCode); + //������������������������ + Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY); + if (!Constants.MARKER_BIT_TRUE.equals(flag)) { + return null; + } + + if (ObjectUtils.isEmpty(sensorValue)) { + return null; + } + //������������������ + if ("avg".equals(type) && Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) { + return null; + } + //������������������������������������ + double aDouble = Double.parseDouble(sensorValue.toString()); + if (!ObjectUtils.isEmpty(upper)) { + if (aDouble > upper) { + return null; + } + } + if (!ObjectUtils.isEmpty(lower)) { + if (aDouble < lower) { + return null; + } + } + return DoubleStream.of(aDouble); + }); + OptionalDouble average = null; + if ("avg".equals(type)) { + average = doubleStream.average(); + } else if ("min".equals(type)) { + average = doubleStream.min(); + } else if ("max".equals(type)) { + average = doubleStream.max(); + } + + if (average.isPresent()) { + //��������������������� + return AmendUtils.sciCal(average.getAsDouble(), 4); + } + return null; + } + + @Override + public void dateToChangShu(String time) { + //������������������������������ + Date end = new Date(); + List<String> stringList = Arrays.asList(dateChangShu.split(",")); + String startTime = DateUtils.dateToDateString(DateUtils.addHours(end,-1),DateUtils.yyyy_MM_dd_HH_EN)+":00:00"; + Map<String, Object> prop = new HashMap<>(); + String timeUnits = DateUtils.dateToDateString(end, DateUtils.yyyyMM_EN); + prop.put("timeUnits", timeUnits); + prop.put("start", startTime); + prop.put("end", DateUtils.dateToDateString(end,DateUtils.yyyy_MM_dd_HH_EN)+":00:00"); + prop.put("list", stringList); + List<Map<String, Object>> dailyData = this.selectDailyData(prop); + String startTimeStr = DateUtils.dateToDateString(DateUtils.addHours(end,-1),"yyyyMMddHH")+"0000"; + List<String> list = new ArrayList<>(); + + for(Map<String, Object> m : dailyData){ + String result = strList(startTimeStr,m.get("mac").toString(),m.get("value").toString()); + list.add(result); + redisTemplate.opsForHash().delete(RedisConstants.DATE_CHANG_SHU,m.get("mac").toString()); + redisTemplate.opsForHash().put(RedisConstants.DATE_CHANG_SHU,m.get("mac").toString(),result); + } + for(String s : list){ + sendSocket("222.92.166.238",15031,s); + } + log.info("������������"); + } + + /** + * ������������������������������ + */ + @Override + public void dateInsertHistoryHourlyAvg() { + QueryWrapper<Device> wrapper = new QueryWrapper<>(); + wrapper.select("mac"); + wrapper.eq("organization_id",71); + wrapper.eq("is_delete",Constants.NOT_DELETE); + List<Device> devices = deviceMapper.selectList(wrapper); + ArrayList<String> macs = new ArrayList<>(); + for (Device device : devices) { + macs.add(device.getMac()); + } + //������������������yyyy-MM-dd HH:mm + String format = DateUtils.yyyy_MM_dd_HH_EN; + Date now = new Date(); + + //������������������������������ + Map<String, Object> params = new HashMap<>(); + //������������ + Date start = DateUtils.dataToTimeStampTime(DateUtils.addHours(now, -1), format); + //������������ + Date end = DateUtils.dataToTimeStampTime(now, format); +// String start1= "2023-08-18 03:00:00"; +// String end1 = "2023-08-18 04:00:00"; +// Date date1 = DateUtils.getDate(start1, DateUtils.yyyy_MM_dd_HH_mm_ss_EN); +// Date date2 = DateUtils.getDate(end1, DateUtils.yyyy_MM_dd_HH_mm_ss_EN); + + params.put("start", start); + params.put("end", end); + params.put("macs",macs); + //������������������������������ + String timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); + params.put("timeUnits", timeUnits); + + //������ + QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); + sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE); + List<Sensor> sensors = sensorService.list(sensorQueryWrapper); + // ������������������������������������������������ + List<Map<String, Object>> hourlyData = historyMinutelyMapper.getHistoryMinutelyData(params); + if (ObjectUtils.isEmpty(hourlyData)) { + return; + } + //���mac������ + Map<String, List<Map<String, Object>>> data = hourlyData.parallelStream() + .collect(Collectors.groupingBy(o -> (String) o.get("mac"))); + //��������������������������� + List<Map<String, Object>> insertData = new ArrayList<>(); + data.forEach((key, value) -> { + Map<String, Object> historyHourly = new HashMap<>(); + historyHourly.put("mac", key); + historyHourly.put("time",start); + + Map<String, Object> jsonMap = new HashMap<>(); + Map<String, Object> map = new HashMap<>(); + map.put("data", value); + map.put("type", "hour"); + + for (Sensor sensor : sensors) { + String sensorCode = sensor.getCode(); + + //��������������� + if (sensorCode.equals(Constants.SENSOR_CODE_WIND_DIR)) { + if (sensor.getUpper() != null) { + map.put("windDirUpper", sensor.getUpper()); + } + if (sensor.getLower() != null) { + map.put("windDirLower", sensor.getLower()); + } + } + + //��������������� + if (sensorCode.equals(Constants.SENSOR_CODE_WIND_SPEED)) { + if (sensor.getUpper() != null) { + map.put("windSpeedUpper", sensor.getUpper()); + } + if (sensor.getLower() != null) { + map.put("windSpeedLower", sensor.getLower()); + } + } + } + + //��������������������������� + Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(map); + if (!ObjectUtils.isEmpty(windDirAvg)) { + jsonMap.putAll(windDirAvg); + } + + //������������������������������������ + sensors.forEach(sensor -> { + String sensorCode = sensor.getCode(); + Double upper = sensor.getUpper(); + Double lower = sensor.getLower(); + AtomicInteger size = new AtomicInteger(); + DoubleStream optionalDouble = value.parallelStream() + .flatMapToDouble(v -> { + Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); + Object sensorValue = dataValue.get(sensorCode); + //������������������������ + Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY); + if (!Constants.MARKER_BIT_TRUE.equals(flag)) { + return null; + } + + if (ObjectUtils.isEmpty(sensorValue)) { + return null; + } + + //������������������ + if (Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) { + return null; + } + + //������������������������������������ + double aDouble = Double.parseDouble(sensorValue.toString()); + if (!ObjectUtils.isEmpty(upper)) { + if (aDouble < upper) { + return null; + } + } + if (!ObjectUtils.isEmpty(lower)) { + if (aDouble > lower) { + return null; + } + } + size.getAndIncrement(); + return DoubleStream.of(aDouble); + }); + OptionalDouble average = optionalDouble.average(); + if (average.isPresent()) { + //��������������������� + double sciCal = AmendUtils.sciCal(average.getAsDouble(), 4); + jsonMap.put(sensorCode, sciCal); + //��������� + if (size.get() >= 45) { + jsonMap.put(sensorCode + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_TRUE); + } else { + jsonMap.put(sensorCode + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_FALSE); + } + } + }); + historyHourly.put("version", value.get(0).get("version")); + historyHourly.put("value", JSONObject.toJSONString(jsonMap)); + insertData.add(historyHourly); + }); + //��������������� + historyHourlyMapper.insertHistoryHourlyAvg(insertData,timeUnits); + + } + + private String strList(String startTime,String mn,String msg){ + String qn = "QN="+ startTime + "001;ST=22;CN=2061;PW=123456;MN="+mn.toUpperCase()+";CP=&&DataTime="+startTime+";"; + Map<String, Object> data = JSON.parseObject(msg, Map.class); + StringBuffer stringBuffer = new StringBuffer(); + if(data.size()>0){ + data.remove("mac"); + data.remove("time"); + for(Map.Entry<String, Object> entry : data.entrySet()){ + String mapKey = entry.getKey(); + Object mapValue = entry.getValue(); + if(mapKey.contains("e")){ + if(mapKey.contains("a00e12")||mapKey.contains("a00e13")){ + + }else { + continue; + } + + } + if(!mapKey.contains("-Flag")){ + if(mapKey.contains("a34002")||mapKey.contains("a34004")||mapKey.contains("a21026")||mapKey.contains("a21004")||mapKey.contains("a05024")){ + BigDecimal d = Objects.nonNull(mapValue)? BigDecimal.valueOf(Double.valueOf(mapValue.toString())).divide(BigDecimal.valueOf(1000)):BigDecimal.ZERO; + mapValue = d.toString(); + } + if(mapKey.contains("a00e13")){ + mapKey = "a90085"; + } + stringBuffer.append(mapKey+"-Avg=").append(mapValue+",").append(mapKey+"-Flag=N;"); + } + + } + } + stringBuffer.deleteCharAt(stringBuffer.length()-1); + stringBuffer.append("&&"); + //String result = "QN=20230726160000001;ST=22;CN=2061;PW=123456;MN=P5DND7A0391978;CP=&&DataTime=20230726160000;a21005-Avg=0.03072,a21005-Flag=N;a21004-Avg=20.6,a21004-Flag=N;a21026-Avg=140.43,a21026-Flag=N;a21028-Avg=0.00778,a21028-Flag=N;a21001-Avg=0.13132,a21001-Flag=N;a01001-Avg=38.68,a01001-Flag=N;a05024-Avg=116.27,a05024-Flag=N;a01002-Avg=57.47,a01002-Flag=N;a01007-Avg=0.864,a01007-Flag=N;a01006-Avg=811.918,a01006-Flag=N;a01008-Avg=156.66,a01008-Flag=N;a34002-Avg=9.18,a34002-Flag=N;a34004-Avg=8.1,a34004-Flag=N;a99054-Avg=0.02283,a99054-Flag=N;a31001-Avg=0,a31001-Flag=N&&"; + + String result = qn+stringBuffer.toString(); + + String s = Crc16Utils.padLeftTest(String.valueOf(result.length()),4,null); + String s1 = Crc16Utils.crc16_2017(result, result.length()); + return "##"+s+result+s1+"\r\n"; + } + + public static void main(String[] args) { + String result = "QN=20231019000100001;ST=31;CN=2051;PW=123456;MN=P5DND7A0392082;Flag=4;CP=&&DataTime=20231019000100;a01001-Avg=25.0,a01001-Flag=N;a01002-Avg=61.0,a01002-Flag=N;a34004-Avg=19.3,a34004-Flag=N;a34002-Avg=38.5,a34002-Flag=N;a34001-Avg=41.2,a34001-Flag=N;a01007-Avg=0.00,a01007-Flag=N;a01008-Avg=137.53,a01008-Flag=N;a01006-Avg=4255.774,a01006-Flag=T;a00e13-Avg=39.2,a00e13-Flag=N&&"; + + String s = Crc16Utils.padLeftTest(String.valueOf(result.length()),4,null); + String s1 = Crc16Utils.crc16_2017(result, result.length()); + System.out.println("##"+s+result+s1+"\r\n"); + int i = 0; + } + + private static String sendSocket(String host, Integer port, String message) { + log.debug("���������������{}������������{}������������{}", host, port, message); + Socket socket = null; + OutputStream outputStream = null; + InputStream inputStream = null; + BufferedReader bufferedReader = null; + try { + socket = new Socket(host, port); + socket.setSoTimeout(3000); + // ������������������������������ + outputStream = socket.getOutputStream(); + outputStream.write(message.getBytes()); + outputStream.flush(); + socket.shutdownOutput(); + inputStream = socket.getInputStream(); + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String readLen=bufferedReader.readLine(); + log.debug("���������������{}", readLen); + return readLen; + } catch (IOException e) { + log.error("socket���������������{}", e.toString()); + } finally { + try { + if(bufferedReader != null){ + bufferedReader.close(); + } + if (inputStream != null) { + inputStream.close(); + } + if (outputStream != null) { + outputStream.close(); + } + if (socket != null) { + socket.close(); + } + } catch (IOException e) { + log.error("socket���������������{}", e.toString()); + } + } + return null; + } + } -- Gitblit v1.8.0