| | |
| | | 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.HistoryHourly; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.mapper.HistoryHourlyMapper; |
| | |
| | | |
| | | 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; |
| | |
| | | * @since 2021-06-28 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class HistoryHourlyServiceImpl implements HistoryHourlyService { |
| | | |
| | | @Value("${result.date.changshu}") |
| | | private String dateChangShu; |
| | | @Autowired |
| | | private HistoryHourlyMapper historyHourlyMapper; |
| | | |
| | |
| | | } |
| | | 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("发送完成"); |
| | | } |
| | | |
| | | 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"; |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | } |