| package com.moral.api.kafka.consumer; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.moral.api.service.*; | 
| import com.moral.constant.KafkaConstants; | 
| import com.moral.constant.RedisConstants; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.kafka.clients.consumer.CommitFailedException; | 
| import org.apache.kafka.clients.consumer.ConsumerRecord; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.data.redis.core.RedisTemplate; | 
| import org.springframework.kafka.annotation.KafkaListener; | 
| import org.springframework.kafka.support.Acknowledgment; | 
| import org.springframework.stereotype.Component; | 
| import org.springframework.util.ObjectUtils; | 
|   | 
| import java.util.*; | 
|   | 
|   | 
| @Component | 
| @Slf4j | 
| public class DeviceConsumer { | 
|   | 
|     @Value("${result.device.list}") | 
|     private String deviceList; | 
|     @Autowired | 
|     private HistoryMinutelyService historyMinutelyService; | 
|   | 
|     @Autowired | 
|     private HistoryHourlyService historyHourlyService; | 
|   | 
|     @Autowired | 
|     private DeviceService deviceService; | 
|   | 
|     @Autowired | 
|     private RedisTemplate redisTemplate; | 
|   | 
|     @Autowired | 
|     private HistorySecondCruiserService historySecondCruiserService; | 
|   | 
|     @Autowired | 
|     private HistorySecondUavService historySecondUavService; | 
|   | 
|     //分钟数据 | 
|     @KafkaListener(topics = KafkaConstants.TOPIC_MINUTE, containerFactory = "insertListenerContainerFactory") | 
|     public void listenMinute(ConsumerRecord<String, String> record, Acknowledgment ack) { | 
|         String msg = record.value(); | 
|         try { | 
|             Map<String, Object> data = JSON.parseObject(msg, Map.class); | 
|             Object mac = data.get("mac"); | 
|             Object time = data.get("DataTime"); | 
|             if (ObjectUtils.isEmpty(time) || ObjectUtils.isEmpty(mac)) { | 
|                 log.warn("some properties is null, param{}", msg); | 
|                 ack.acknowledge(); | 
|                 return; | 
|             } | 
|   | 
|             //数据过滤 | 
| //            data.remove("time"); | 
|             data.remove("entryTime"); | 
|             Iterator<Map.Entry<String, Object>> iterator = data.entrySet().iterator(); | 
|             Map<String, Object> newMap = new HashMap<>(); | 
|             Map.Entry<String, Object> next; | 
|             while (iterator.hasNext()) { | 
|                 next = iterator.next(); | 
|                 String key = next.getKey(); | 
|                 Object value = next.getValue(); | 
|                 if (key.contains("-Avg")) { | 
|                     newMap.put(key.replaceAll("-Avg", ""), Double.parseDouble(value.toString())); | 
|                 } else { | 
|                     newMap.put(key, value); | 
|                 } | 
|                 iterator.remove(); | 
|             } | 
|             //存入数据库 | 
|             historyMinutelyService.insertHistoryMinutely(newMap); | 
|             ack.acknowledge(); | 
|         } catch (Exception e) { | 
|             log.error("param{}" + msg); | 
|         } | 
|     } | 
|   | 
|     //小时数据 | 
|     @KafkaListener(topics = KafkaConstants.TOPIC_HOUR, containerFactory = "insertListenerContainerFactory") | 
|     public void listenHour(ConsumerRecord<String, String> record, Acknowledgment ack) { | 
|         String msg = record.value(); | 
|         try { | 
|             Map<String, Object> data = JSON.parseObject(msg, Map.class); | 
|             Object mac = data.get("mac"); | 
|             Object time = data.get("DataTime"); | 
|             if (ObjectUtils.isEmpty(time) || ObjectUtils.isEmpty(mac)) { | 
|                 log.warn("some properties is null, param{}", msg); | 
|                 ack.acknowledge(); | 
|                 return; | 
|             } | 
|   | 
|             //数据过滤 | 
| //            data.remove("time"); | 
|             data.remove("entryTime"); | 
|             Map<String, Object> deviceByMac = deviceService.getDeviceByMac(mac.toString()); | 
|             HashMap<String, Object> map = (HashMap<String, Object>) deviceByMac.get("organization"); | 
|             String id = map.get("id").toString(); | 
|             if (id.equals("71")){ | 
|                 log.warn(id, msg); | 
|                 ack.acknowledge(); | 
|                 return; | 
|             } | 
| //            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
| //            String format = dateFormat.format(new Date().getTime()); | 
| //            //获取年份 | 
| //            String substring = format.substring(0, 4); | 
| // | 
| //            String substring1 = time.toString().substring(0, 4); | 
| // | 
| //            String replace = time.toString().replace(substring1, substring); | 
| // | 
| //            data.put("DataTime",replace); | 
|   | 
|             Iterator<Map.Entry<String, Object>> iterator = data.entrySet().iterator(); | 
|             Map<String, Object> newMap = new HashMap<>(); | 
|             Map.Entry<String, Object> next; | 
|             while (iterator.hasNext()) { | 
|                 next = iterator.next(); | 
|                 String key = next.getKey(); | 
|                 Object value = next.getValue(); | 
|                 if (key.contains("-Avg")) { | 
|                     newMap.put(key.replaceAll("-Avg", ""), Double.parseDouble(value.toString())); | 
|                 } else { | 
|                     newMap.put(key, value); | 
|                 } | 
|                 iterator.remove(); | 
|             } | 
|             //存入数据库 | 
|             historyHourlyService.insertHistoryHourly(newMap); | 
|             ack.acknowledge(); | 
|         } catch (Exception e) { | 
|             log.error("param{}" + msg); | 
|         } | 
|     } | 
|   | 
|     //秒数据,修改设备状态,缓存最新秒数据 | 
|     @KafkaListener(topics = KafkaConstants.TOPIC_SECOND, containerFactory = "stateListenerContainerFactory") | 
|     public void listenSecond(ConsumerRecord<String, String> record, Acknowledgment ack) { | 
|         String msg = record.value(); | 
|         try { | 
|             ack.acknowledge(); | 
|             Map<String, Object> data = JSON.parseObject(msg, Map.class); | 
|             Object mac = data.get("mac"); | 
|             Object time = data.get("DataTime"); | 
|             if (ObjectUtils.isEmpty(time) || ObjectUtils.isEmpty(mac)) { | 
|                 log.warn("some properties is null, param{}", msg); | 
|                 return; | 
|             } | 
|             //数据过滤 | 
|             data.remove("time"); | 
|             data.remove("entryTime"); | 
|             data.put("DataTime", time); | 
|             List<String> listDictDevice = Arrays.asList(deviceList.split(",")); | 
|             if(listDictDevice.contains(mac.toString())){ | 
|                 //判断并修改设备状态 | 
|                 data.put("mac", mac); | 
|                 deviceService.judgeDeviceState(data); | 
|             } | 
|             //数据校准 | 
|             //data = deviceService.adjustDeviceData(data,"0"); | 
|             //存入redis | 
|             //redisTemplate.opsForHash().put(RedisConstants.DATA_SECOND, mac, data); | 
|         }catch (CommitFailedException e){ | 
|             log.error("param{}" + msg); | 
|         } catch (Exception e) { | 
|             log.error("param{}" + msg); | 
|             ack.acknowledge(); | 
|         } | 
|     } | 
|   | 
|     //无人机秒数据 | 
|     @KafkaListener(topics = KafkaConstants.UAV_TOPIC_SECOND, groupId = KafkaConstants.GROUP_INSERT, containerFactory = "insertListenerContainerFactory") | 
|     public void listenSecondSpecial(ConsumerRecord<String, String> record, Acknowledgment ack) { | 
|         String msg = record.value(); | 
|         try { | 
|             Map<String, Object> data = JSON.parseObject(msg, Map.class); | 
|             Object mac = data.get("mac"); | 
|             Object time = data.get("DataTime"); | 
|             if (ObjectUtils.isEmpty(time) || ObjectUtils.isEmpty(mac)) { | 
|                 log.warn("some properties is null, param{}", msg); | 
|                 ack.acknowledge(); | 
|                 return; | 
|             } | 
|   | 
|             //数据过滤 | 
|             data.remove("time"); | 
|             data.remove("entryTime"); | 
|   | 
|             historySecondUavService.insertHistorySecond(data); | 
|             ack.acknowledge(); | 
|         } catch (Exception e) { | 
|             log.error("param{}" + msg); | 
|         } | 
|     } | 
|   | 
|     //走航车秒数据 | 
|     @KafkaListener(topics = KafkaConstants.CRUISER_TOPIC_SECOND, containerFactory = "insertListenerContainerFactory") | 
|     public void listenSecondCruiser(ConsumerRecord<String, String> record, Acknowledgment ack) { | 
|         String msg = record.value(); | 
|         try { | 
|             Map<String, Object> data = JSON.parseObject(msg, Map.class); | 
|             Object mac = data.get("mac"); | 
|             Object time = data.get("DataTime"); | 
|             if (ObjectUtils.isEmpty(time) || ObjectUtils.isEmpty(mac)) { | 
|                 log.warn("some properties is null, param{}", msg); | 
|                 ack.acknowledge(); | 
|                 return; | 
|             } | 
|   | 
|             //数据过滤 | 
|             data.remove("time"); | 
|             data.remove("entryTime"); | 
|   | 
|             historySecondCruiserService.insertHistorySecond(data); | 
|             ack.acknowledge(); | 
|         } catch (Exception e) { | 
|             log.error("param{}" + e); | 
|         } | 
|     } | 
| } |