KafkaBatteryConsumer.java
2.12 KB
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
package cn.com.glsx.kafka.queue.consumer;
import cn.com.glsx.kafka.model.BatteryDTO;
import cn.com.glsx.kafka.protocol.KafkaJourney;
import cn.com.glsx.kafka.queue.base.MessageReceiver;
import cn.com.glsx.stdinterface.api.BatteryClient;
import cn.com.glsx.stdinterface.dto.DevicePowerDTO;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.nio.charset.Charset;
import java.util.Optional;
/**
* 消费电量数据
*
* @author xiangyanlin
* @date 2022/4/12
*/
@Slf4j
public class KafkaBatteryConsumer extends MessageReceiver {
@Autowired
private BatteryClient batteryClient;
@Override
public void processMessage(String topic, byte[] message) throws Exception {
try {
String devicePowerJson = new String(message, Charset.defaultCharset());
log.info("{}", "电量消息:" + devicePowerJson);
if (StringUtils.isEmpty(devicePowerJson)) {
return;
}
BatteryDTO devicePower = JSONObject.parseObject(devicePowerJson, BatteryDTO.class);
//电量信息保存入库
DevicePowerDTO devicePowerDTO = convert(devicePower);
batteryClient.saveBatteryInfo(devicePowerDTO);
} catch (Exception e) {
log.error("KafkaBatteryConsumer--消费异常:" + e.getMessage());
}
}
public DevicePowerDTO convert(BatteryDTO batteryDTO) {
DevicePowerDTO devicePowerDTO = new DevicePowerDTO();
Optional.ofNullable(batteryDTO.getId())
.map(Integer::longValue)
.ifPresent(devicePowerDTO::setId);
devicePowerDTO.setSn(batteryDTO.getSn());
devicePowerDTO.setAlarmTime(DateUtils.parse(batteryDTO.getAlarmTime()));
devicePowerDTO.setSoc(batteryDTO.getSOC());
devicePowerDTO.setCreatedDate(DateUtils.parse(batteryDTO.getCreateTime()));
devicePowerDTO.setUpdatedDate(DateUtils.parse(batteryDTO.getUpdateTime()));
return devicePowerDTO;
}
}