| | |
| | | package com.moral.api.kafka.consumer; |
| | | |
| | | import com.moral.api.constant.TopicConstants; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.kafka.clients.consumer.ConsumerRecord; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | 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.StringUtils; |
| | | |
| | | import java.util.Random; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Component |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.moral.api.service.DeviceService; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.api.service.HistoryMinutelyService; |
| | | import com.moral.constant.KafkaConstants; |
| | | import com.moral.constant.RedisConstants; |
| | | |
| | | //@Component |
| | | @Slf4j |
| | | public class KafkaConsumer { |
| | | |
| | | /* *//** |
| | | * 这是手动提交的消费方式 |
| | | * @param record |
| | | * @param ack |
| | | * @throws Exception |
| | | *//* |
| | | @KafkaListener(topics = TopicConstants.TEST_TOPIC_MESSAGE,groupId = "test") |
| | | public void listenTest(ConsumerRecord<String, String> record , Acknowledgment ack) throws Exception { |
| | | @Autowired |
| | | private HistoryMinutelyService historyMinutelyService; |
| | | |
| | | @Autowired |
| | | private HistoryHourlyService historyHourlyService; |
| | | |
| | | @Autowired |
| | | private DeviceService deviceService; |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | //分钟数据 |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_MINUTE, groupId = KafkaConstants.GROUP_ID_INSERT, containerFactory = "kafkaListenerContainerFactory") |
| | | public void listenMinute(ConsumerRecord<String, String> record, Acknowledgment ack) { |
| | | String msg = record.value(); |
| | | System.out.println(msg); |
| | | if (new Random().nextInt(100)<50){ |
| | | log.info(String.format("kafka 消费消息成功---------------- listen1 topic = %s, offset = %d, value = %s ", record.topic(), record.offset(), record.value())); |
| | | try { |
| | | Map<String, Object> data = JSON.parseObject(msg, HashMap.class); |
| | | Object mac = data.get("mac"); |
| | | Object time = data.get("DataTime"); |
| | | Object ver = data.get("ver"); |
| | | if (StringUtils.isEmpty(ver) || StringUtils.isEmpty(time) || StringUtils.isEmpty(mac)) { |
| | | log.warn("some properties is null, param{}", msg); |
| | | ack.acknowledge(); |
| | | return; |
| | | } |
| | | |
| | | //数据过滤 |
| | | data = data.entrySet().stream() |
| | | .filter(map -> { |
| | | String key = map.getKey(); |
| | | return !(key.contains("Min") || key.contains("Max") || key.contains("Cou")); |
| | | }).collect(Collectors.toMap(m -> m.getKey().replaceAll("-Avg", ""), Map.Entry::getValue)); |
| | | data.remove("time"); |
| | | //存入数据库 |
| | | historyMinutelyService.insertHistoryMinutely(data); |
| | | ack.acknowledge(); |
| | | } catch (Exception e) { |
| | | //log.error("param{}" + msg); |
| | | } |
| | | } |
| | | |
| | | }*/ |
| | | //小时数据 |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_HOUR, groupId = KafkaConstants.GROUP_ID_INSERT, containerFactory = "kafkaListenerContainerFactory") |
| | | public void listenHour(ConsumerRecord<String, String> record, Acknowledgment ack) { |
| | | String msg = record.value(); |
| | | try { |
| | | Map<String, Object> data = JSON.parseObject(msg, HashMap.class); |
| | | Object mac = data.get("mac"); |
| | | Object time = data.get("DataTime"); |
| | | Object ver = data.get("ver"); |
| | | if (StringUtils.isEmpty(ver) || StringUtils.isEmpty(time) || StringUtils.isEmpty(mac)) { |
| | | log.warn("some properties is null, param{}", msg); |
| | | ack.acknowledge(); |
| | | return; |
| | | } |
| | | |
| | | //数据过滤 |
| | | data = data.entrySet().stream() |
| | | .filter(map -> { |
| | | String key = map.getKey(); |
| | | return !(key.contains("Min") || key.contains("Max") || key.contains("Cou")); |
| | | }).collect(Collectors.toMap(m -> m.getKey().replaceAll("-Avg", ""), Map.Entry::getValue)); |
| | | data.remove("time"); |
| | | //存入数据库 |
| | | historyHourlyService.insertHistoryHourly(data); |
| | | ack.acknowledge(); |
| | | } catch (Exception e) { |
| | | //log.error("param{}" + msg); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | //秒数据,修改设备状态,缓存最新秒数据 |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_SECOND, groupId = KafkaConstants.GROUP_ID_STATE, containerFactory = "kafkaListenerContainerFactory") |
| | | public void listenSecond(ConsumerRecord<String, String> record, Acknowledgment ack) { |
| | | String msg = record.value(); |
| | | //System.out.println(record.offset() + "===>" + msg); |
| | | try { |
| | | Map<String, Object> data = JSON.parseObject(msg, HashMap.class); |
| | | Object mac = data.get("mac"); |
| | | Object time = data.get("DataTime"); |
| | | Object ver = data.get("ver"); |
| | | if (StringUtils.isEmpty(ver) || StringUtils.isEmpty(time) || StringUtils.isEmpty(mac)) { |
| | | log.warn("some properties is null, param{}", msg); |
| | | ack.acknowledge(); |
| | | return; |
| | | } |
| | | //数据校准 |
| | | data = deviceService.adjustDeviceData(data); |
| | | //存入redis |
| | | redisTemplate.opsForValue().set(RedisConstants.DEVICE_DATA + mac, data); |
| | | //判断并修改设备状态 |
| | | deviceService.judgeDeviceState(data); |
| | | ack.acknowledge(); |
| | | } catch (Exception e) { |
| | | //log.error("param{}" + msg); |
| | | } |
| | | } |
| | | } |