kaiyu
2021-06-10 ddebb2ea352012c10ec31a9d9774b0320af4caac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.moral.api.kafka.consumer;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
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.HashMap;
import java.util.Map;
 
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;
 
/*@Slf4j
@Component
public class KafkaReceiver {
 
    @Autowired
    private HistoryMinutelyService historyMinutelyService;
 
    @Autowired
    private HistoryHourlyService historyHourlyService;
 
    @Autowired
    private DeviceService deviceService;
 
    //分钟数据
    @KafkaListener(topics = KafkaConstants.TOPIC_MINUTE, groupId = KafkaConstants.GROUP_ID, 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);
            System.out.println(data);
            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[0] message:" + msg);
                return;
            }
            Map<String, Object> deviceInfo = deviceService.getDeviceByMac(mac.toString());
            if (deviceInfo == null) {
                String deviceRealState = "null or deleted";
                log.warn("device record is " + deviceRealState + ", param[0] message:" + msg);
                return;
            }
            //清除毫秒,四舍五入
            data.put("DataTime", Math.round(new Double((Long) time) / 1000) * 1000);
 
            //存入数据库
            historyMinutelyService.insertHistoryMinutely(data);
 
 
            ack.acknowledge();
        } catch (Exception e) {
            log.error("param[0] message:" + msg);
        }
    }
 
    //小时数据
    @KafkaListener(topics = KafkaConstants.TOPIC_HOUR, groupId = KafkaConstants.GROUP_ID, 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);
            System.out.println(data);
            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[0] message:" + msg);
                return;
            }
            Map<String, Object> deviceInfo = deviceService.getDeviceByMac(mac.toString());
            if (deviceInfo == null) {
                String deviceRealState = "null or deleted";
                log.warn("device record is " + deviceRealState + ", param[0] message:" + msg);
                return;
            }
            //清除毫秒,四舍五入
            data.put("DataTime", Math.round(new Double((Long) time) / 1000) * 1000);
 
            //存入数据库
            historyHourlyService.insertHistoryHourly(data);
 
            ack.acknowledge();
        } catch (Exception e) {
            log.error("param[0] message:" + msg);
        }
    }
}*/