| | |
| | | import org.springframework.kafka.annotation.KafkaListener; |
| | | import org.springframework.kafka.support.Acknowledgment; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | 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.api.util.AdjustDataUtils; |
| | | import com.moral.api.service.HistorySecondSpecialService; |
| | | import com.moral.constant.KafkaConstants; |
| | | import com.moral.constant.RedisConstants; |
| | | |
| | | //@Component |
| | | /* |
| | | * 设备数据接入 |
| | | * */ |
| | | @Component |
| | | @Slf4j |
| | | public class KafkaConsumer { |
| | | |
| | |
| | | private DeviceService deviceService; |
| | | |
| | | @Autowired |
| | | private AdjustDataUtils adjustDataUtils; |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | private HistorySecondSpecialService historySecondSpecialService; |
| | | |
| | | //分钟数据 |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_MINUTE, groupId = KafkaConstants.GROUP_ID_INSERT, containerFactory = "kafkaListenerContainerFactory") |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_MINUTE, groupId = KafkaConstants.GROUP_INSERT, containerFactory = "kafkaListenerContainerFactory") |
| | | public void listenMinute(ConsumerRecord<String, String> record, Acknowledgment ack) { |
| | | String msg = record.value(); |
| | | try { |
| | | Map<String, Object> data = JSON.parseObject(msg, HashMap.class); |
| | | Map<String, Object> data = JSON.parseObject(msg, Map.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)) { |
| | | if (ObjectUtils.isEmpty(time) || ObjectUtils.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"); |
| | | 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(data); |
| | | historyMinutelyService.insertHistoryMinutely(newMap); |
| | | ack.acknowledge(); |
| | | } catch (Exception e) { |
| | | //log.error("param{}" + msg); |
| | | log.error("param{}" + msg); |
| | | } |
| | | } |
| | | |
| | | //小时数据 |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_HOUR, groupId = KafkaConstants.GROUP_ID_INSERT, containerFactory = "kafkaListenerContainerFactory") |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_HOUR, groupId = KafkaConstants.GROUP_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); |
| | | Map<String, Object> data = JSON.parseObject(msg, Map.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)) { |
| | | if (ObjectUtils.isEmpty(time) || ObjectUtils.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"); |
| | | 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(); |
| | | } |
| | | //存入数据库 |
| | | historyHourlyService.insertHistoryHourly(data); |
| | | historyHourlyService.insertHistoryHourly(newMap); |
| | | ack.acknowledge(); |
| | | } catch (Exception e) { |
| | | //log.error("param{}" + msg); |
| | | 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) { |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_SECOND, groupId = KafkaConstants.GROUP_STATE, containerFactory = "kafkaListenerContainerFactory") |
| | | public void listenSecond(ConsumerRecord<String, String> record) { |
| | | String msg = record.value(); |
| | | try { |
| | | Map<String, Object> data = JSON.parseObject(msg, HashMap.class); |
| | | Map<String, Object> data = JSON.parseObject(msg, Map.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)) { |
| | | if (ObjectUtils.isEmpty(time) || ObjectUtils.isEmpty(mac)) { |
| | | log.warn("some properties is null, param{}", msg); |
| | | return; |
| | | } |
| | | //数据过滤 |
| | | data.remove("time"); |
| | | data.remove("entryTime"); |
| | | |
| | | //数据校准 |
| | | data = deviceService.adjustDeviceData(data); |
| | | //存入redis |
| | | redisTemplate.opsForHash().put(RedisConstants.DATA_SECOND, mac, data); |
| | | //判断并修改设备状态 |
| | | deviceService.judgeDeviceState(data); |
| | | } catch (Exception e) { |
| | | log.error("param{}" + msg); |
| | | } |
| | | } |
| | | |
| | | //特殊设备秒数据 |
| | | @KafkaListener(topics = KafkaConstants.TOPIC_SECOND_SPECIAL, groupId = KafkaConstants.GROUP_INSERT, containerFactory = "kafkaListenerContainerFactory") |
| | | 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 = adjustDataUtils.adjust(data); |
| | | //存入redis |
| | | redisTemplate.opsForValue().set(RedisConstants.DEVICE_DATA + "_" + mac, data); |
| | | //判断并修改设备状态 |
| | | deviceService.judgeDeviceState(data); |
| | | |
| | | //数据过滤 |
| | | data.remove("time"); |
| | | data.remove("entryTime"); |
| | | |
| | | historySecondSpecialService.insertHistorySecond(data); |
| | | ack.acknowledge(); |
| | | } catch (Exception e) { |
| | | //log.error("param{}" + msg); |
| | | log.error("param{}" + msg); |
| | | } |
| | | } |
| | | } |