jinpengyong
2023-08-25 f9f8f90ac63d6ce3274410d3721b173f40db6e41
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.moral.api.kafka.consumer;
 
import com.alibaba.fastjson.JSONObject;
import com.moral.api.service.*;
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.ObjectUtils;
 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
import com.alibaba.fastjson.JSON;
import com.moral.constant.KafkaConstants;
import com.moral.constant.RedisConstants;
 
 
 
@Component
@Slf4j
public class DeviceConsumer {
 
    @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);
                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 {
            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 = deviceService.adjustDeviceData(data,"0");
            //存入redis
            data.put("DataTime", time);
            redisTemplate.opsForHash().put(RedisConstants.DATA_SECOND, mac, data);
            //判断并修改设备状态
            data.put("mac", mac);
            deviceService.judgeDeviceState(data);
            ack.acknowledge();
        } catch (Exception e) {
            log.error("param{}" + msg);
        }
    }
 
    //无人机秒数据
    @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);
        }
    }
}