Commit d20f54c6 by levphon

设备状态消费

1 parent 87f946eb
package cn.com.glsx.kafka.constant; package cn.com.glsx.kafka.common.constant;
/** /**
* 常量类 * 常量类
......
package cn.com.glsx.kafka.constant; package cn.com.glsx.kafka.common.constant;
/** /**
* 全局变量 * 全局变量
......
package cn.com.glsx.kafka.common.enums;
import lombok.Getter;
/**
* 设备上点相关topic枚举类
*/
@Getter
public enum TopicsEnum {
/**
* gps轨迹
*/
GPS_GPSDATA(1, "gps_gpsdata"),
/**
* gps基站
*/
GPS_BASESTATION(2, "gps_basestation"),
/**
* GPS和基站
*/
GPS_GPSANDBS(3, "gps_gpsandbs"),
/**
* 设备状态
*/
DEVICE_STATE(4, "device_state");
private Integer id;
private String topic;
TopicsEnum(Integer id, String topic) {
this.id = id;
this.topic = topic;
}
public static TopicsEnum getTopic(String topic) {
for (TopicsEnum entity : TopicsEnum.values()) {
if (entity.getTopic().equals(topic)) {
return entity;
}
}
return null;
}
}
package cn.com.glsx.kafka.config; package cn.com.glsx.kafka.config;
import cn.com.glsx.kafka.constant.KafkaConstant; import cn.com.glsx.kafka.common.constant.KafkaConstant;
import kafka.consumer.ConsumerConfig; import kafka.consumer.ConsumerConfig;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.beans.factory.config.PropertiesFactoryBean;
......
package cn.com.glsx.kafka.config; package cn.com.glsx.kafka.config;
import cn.com.glsx.kafka.queue.consumer.BaseStationConsumer; import cn.com.glsx.kafka.queue.consumer.BaseStationConsumer;
import cn.com.glsx.kafka.queue.consumer.DeviceStateConsumer;
import cn.com.glsx.kafka.queue.consumer.GpsAndBsConsumer; import cn.com.glsx.kafka.queue.consumer.GpsAndBsConsumer;
import cn.com.glsx.kafka.queue.consumer.GpsConsumer; import cn.com.glsx.kafka.queue.consumer.GpsConsumer;
import kafka.consumer.ConsumerConfig; import kafka.consumer.ConsumerConfig;
...@@ -28,6 +29,15 @@ public class KafkaConsumerConfig { ...@@ -28,6 +29,15 @@ public class KafkaConsumerConfig {
private Integer nThreads; private Integer nThreads;
@Bean(initMethod = "init", destroyMethod = "destroy") @Bean(initMethod = "init", destroyMethod = "destroy")
GpsConsumer gpsConsumer(@Value("${mq.kafka.device.gps.topic}") String topic) {
GpsConsumer gpsConsumer = new GpsConsumer();
gpsConsumer.setnThreads(nThreads);
gpsConsumer.setConsumerConfig(gpsConsumerConfig);
gpsConsumer.setTopic(topic);
return gpsConsumer;
}
@Bean(initMethod = "init", destroyMethod = "destroy")
BaseStationConsumer baseStationConsumer(@Value("${mq.kafka.device.bs.topic}") String topic) { BaseStationConsumer baseStationConsumer(@Value("${mq.kafka.device.bs.topic}") String topic) {
BaseStationConsumer baseStationConsumer = new BaseStationConsumer(); BaseStationConsumer baseStationConsumer = new BaseStationConsumer();
baseStationConsumer.setnThreads(nThreads); baseStationConsumer.setnThreads(nThreads);
...@@ -46,11 +56,12 @@ public class KafkaConsumerConfig { ...@@ -46,11 +56,12 @@ public class KafkaConsumerConfig {
} }
@Bean(initMethod = "init", destroyMethod = "destroy") @Bean(initMethod = "init", destroyMethod = "destroy")
GpsConsumer channelRiskConsumer(@Value("${mq.kafka.device.gps.topic}") String topic) { DeviceStateConsumer stateConsumer(@Value("${mq.kafka.device.state.topic}") String topic) {
GpsConsumer gpsConsumer = new GpsConsumer(); DeviceStateConsumer stateConsumer = new DeviceStateConsumer();
gpsConsumer.setnThreads(nThreads); stateConsumer.setnThreads(nThreads);
gpsConsumer.setConsumerConfig(gpsConsumerConfig); stateConsumer.setConsumerConfig(gpsConsumerConfig);
gpsConsumer.setTopic(topic); stateConsumer.setTopic(topic);
return gpsConsumer; return stateConsumer;
} }
} }
package cn.com.glsx.kafka.queue.consumer; package cn.com.glsx.kafka.queue.consumer;
import cn.com.glsx.kafka.common.enums.TopicsEnum;
import cn.com.glsx.kafka.protocol.kafkaSvr; import cn.com.glsx.kafka.protocol.kafkaSvr;
import cn.com.glsx.kafka.queue.base.MessageReceiver; import cn.com.glsx.kafka.queue.base.MessageReceiver;
import cn.com.glsx.kafka.service.BloomFilterService; import cn.com.glsx.kafka.service.BloomFilterService;
import cn.com.glsx.kafka.service.TransitHandleService; import cn.com.glsx.kafka.service.TransitHandleService;
import cn.com.glsx.stdinterface.dto.DeviceLocationDto; import cn.com.glsx.stdinterface.dto.DeviceLocationDto;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.glsx.plat.redis.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -24,9 +24,6 @@ import java.util.Date; ...@@ -24,9 +24,6 @@ import java.util.Date;
public class BaseStationConsumer extends MessageReceiver { public class BaseStationConsumer extends MessageReceiver {
@Autowired @Autowired
private RedisUtils redisUtils;
@Autowired
private BloomFilterService bloomFilterService; private BloomFilterService bloomFilterService;
@Autowired @Autowired
...@@ -48,10 +45,9 @@ public class BaseStationConsumer extends MessageReceiver { ...@@ -48,10 +45,9 @@ public class BaseStationConsumer extends MessageReceiver {
return; return;
} }
// 写入redis 并且比较 gps和基站时间的先后 // 写入redis 并且比较 gps和基站时间的先后
transitHandleService.handleGpsAndBsData(deviceLocationDto); transitHandleService.handleGpsAndBsData(deviceLocationDto, TopicsEnum.GPS_BASESTATION.getId());
} catch (Exception e) { } catch (Exception e) {
log.error("基站数据消费异常===topic:{},{}", topic, e.getMessage()); log.error("基站数据消费异常===topic:{},{}", topic, e.getMessage());
e.printStackTrace();
} }
} }
......
package cn.com.glsx.kafka.queue.consumer;
import cn.com.glsx.kafka.common.enums.TopicsEnum;
import cn.com.glsx.kafka.protocol.KafkaDeviceState;
import cn.com.glsx.kafka.queue.base.MessageReceiver;
import cn.com.glsx.kafka.service.BloomFilterService;
import cn.com.glsx.kafka.service.TransitHandleService;
import cn.com.glsx.stdinterface.dto.DeviceLocationDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
/**
* 设备状态数据消费
* topic:device_state
*/
@Slf4j
public class DeviceStateConsumer extends MessageReceiver {
@Autowired
private BloomFilterService bloomFilterService;
@Autowired
private TransitHandleService transitHandleService;
@Override
public void processMessage(String topic, byte[] message) {
try {
KafkaDeviceState.DeviceStateReq deviceStateReq = KafkaDeviceState.DeviceStateReq.parseFrom(message);
String sn = deviceStateReq.getDevicesn();
// 判断是否是新E盾的设备号 如果不是则过滤掉
if (!bloomFilterService.isExits(sn)) {
return;
}
DeviceLocationDto deviceLocationDto = setDeviceStateData(deviceStateReq);
transitHandleService.handleGpsAndBsData(deviceLocationDto, TopicsEnum.DEVICE_STATE.getId());
} catch (Exception e) {
log.error("设备状态数据消费异常===topic:{},{}", topic, e.getMessage());
}
}
private DeviceLocationDto setDeviceStateData(KafkaDeviceState.DeviceStateReq deviceStateReq) {
DeviceLocationDto locationDto = new DeviceLocationDto();
locationDto.setSn(deviceStateReq.getDevicesn());
locationDto.setStayStatus(deviceStateReq.getState());
Date updateTime = new Date(deviceStateReq.getStatebegintime() * 1000L);
locationDto.setStayStatusUpdatetime(updateTime);
return locationDto;
}
}
package cn.com.glsx.kafka.queue.consumer; package cn.com.glsx.kafka.queue.consumer;
import cn.com.glsx.kafka.constant.Constants; import cn.com.glsx.kafka.common.constant.Constants;
import cn.com.glsx.kafka.common.enums.TopicsEnum;
import cn.com.glsx.kafka.protocol.kafkaSvr; import cn.com.glsx.kafka.protocol.kafkaSvr;
import cn.com.glsx.kafka.queue.base.MessageReceiver; import cn.com.glsx.kafka.queue.base.MessageReceiver;
import cn.com.glsx.kafka.service.BloomFilterService; import cn.com.glsx.kafka.service.BloomFilterService;
...@@ -50,20 +51,16 @@ public class GpsAndBsConsumer extends MessageReceiver { ...@@ -50,20 +51,16 @@ public class GpsAndBsConsumer extends MessageReceiver {
return; return;
} }
// 写入redis 并且比较 gps和基站时间的先后 // 写入redis 并且比较 gps和基站时间的先后
transitHandleService.handleGpsAndBsData(deviceLocationDto); transitHandleService.handleGpsAndBsData(deviceLocationDto, TopicsEnum.GPS_GPSANDBS.getId());
} catch (Exception e) { } catch (Exception e) {
log.error("Gps基站数据消费异常===topic:{},{}", topic, e.getMessage()); log.error("Gps基站数据消费异常===topic:{},{}", topic, e.getMessage());
e.printStackTrace();
} }
} }
private DeviceLocationDto setGpsAndBsData(kafkaSvr.GPSAndBSReq gpsAndBsReq) { private DeviceLocationDto setGpsAndBsData(kafkaSvr.GPSAndBSReq gpsAndBsReq) {
kafkaSvr.GPSDATA gpsData = gpsAndBsReq.getGpsdata(); kafkaSvr.GPSDATA gpsData = gpsAndBsReq.getGpsdata();
if (null == gpsData) {
return null;
}
log.info("设备号:{},速度:{}", gpsAndBsReq.getDevicesn(), gpsAndBsReq.getGpsdata().getSpeed());
Date time = new Date(gpsData.getTime() * 1000L); Date time = new Date(gpsData.getTime() * 1000L);
//当前时间如果大于今日,过滤 //当前时间如果大于今日,过滤
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
...@@ -74,6 +71,7 @@ public class GpsAndBsConsumer extends MessageReceiver { ...@@ -74,6 +71,7 @@ public class GpsAndBsConsumer extends MessageReceiver {
DeviceLocationDto deviceLocationDto = new DeviceLocationDto(); DeviceLocationDto deviceLocationDto = new DeviceLocationDto();
deviceLocationDto.setSn(gpsAndBsReq.getDevicesn()); deviceLocationDto.setSn(gpsAndBsReq.getDevicesn());
deviceLocationDto.setSpeed(gpsData.getSpeed());
if (gpsData.getValid().equals(Constants.GPS_IS)) { if (gpsData.getValid().equals(Constants.GPS_IS)) {
//gps有效定位 封装Gps数据 //gps有效定位 封装Gps数据
deviceLocationDto.setLastGpsTime(time); deviceLocationDto.setLastGpsTime(time);
......
package cn.com.glsx.kafka.queue.consumer; package cn.com.glsx.kafka.queue.consumer;
import cn.com.glsx.kafka.constant.Constants; import cn.com.glsx.kafka.common.constant.Constants;
import cn.com.glsx.kafka.common.enums.TopicsEnum;
import cn.com.glsx.kafka.protocol.kafkaSvr; import cn.com.glsx.kafka.protocol.kafkaSvr;
import cn.com.glsx.kafka.queue.base.MessageReceiver; import cn.com.glsx.kafka.queue.base.MessageReceiver;
import cn.com.glsx.kafka.service.BloomFilterService; import cn.com.glsx.kafka.service.BloomFilterService;
import cn.com.glsx.kafka.service.TransitHandleService; import cn.com.glsx.kafka.service.TransitHandleService;
import cn.com.glsx.stdinterface.dto.DeviceLocationDto; import cn.com.glsx.stdinterface.dto.DeviceLocationDto;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.glsx.plat.redis.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -24,9 +24,6 @@ import java.util.List; ...@@ -24,9 +24,6 @@ import java.util.List;
public class GpsConsumer extends MessageReceiver { public class GpsConsumer extends MessageReceiver {
@Autowired @Autowired
private RedisUtils redisUtils;
@Autowired
private BloomFilterService bloomFilterService; private BloomFilterService bloomFilterService;
@Autowired @Autowired
...@@ -48,10 +45,9 @@ public class GpsConsumer extends MessageReceiver { ...@@ -48,10 +45,9 @@ public class GpsConsumer extends MessageReceiver {
return; return;
} }
// 写入redis 并且比较 gps和基站时间的先后 // 写入redis 并且比较 gps和基站时间的先后
transitHandleService.handleGpsAndBsData(deviceLocationDto); transitHandleService.handleGpsAndBsData(deviceLocationDto, TopicsEnum.GPS_GPSDATA.getId());
} catch (Exception e) { } catch (Exception e) {
log.error("Gps数据消费异常===topic:{},{}", topic, e.getMessage()); log.error("Gps数据消费异常===topic:{},{}", topic, e.getMessage());
e.printStackTrace();
} }
} }
...@@ -73,6 +69,7 @@ public class GpsConsumer extends MessageReceiver { ...@@ -73,6 +69,7 @@ public class GpsConsumer extends MessageReceiver {
DeviceLocationDto deviceLocationDto = new DeviceLocationDto(); DeviceLocationDto deviceLocationDto = new DeviceLocationDto();
deviceLocationDto.setSn(trackDataReq.getDevicesn()); deviceLocationDto.setSn(trackDataReq.getDevicesn());
deviceLocationDto.setLastGpsTime(gpsTime); deviceLocationDto.setLastGpsTime(gpsTime);
deviceLocationDto.setSpeed(gpsData.getSpeed());
return deviceLocationDto; return deviceLocationDto;
} }
} }
......
package cn.com.glsx.kafka.service; package cn.com.glsx.kafka.service;
import cn.com.glsx.kafka.common.enums.TopicsEnum;
import cn.com.glsx.kafka.queue.remote.RemoteDeviceLocationService; import cn.com.glsx.kafka.queue.remote.RemoteDeviceLocationService;
import cn.com.glsx.stdinterface.dto.DeviceLocationDto; import cn.com.glsx.stdinterface.dto.DeviceLocationDto;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
...@@ -48,12 +49,11 @@ public class TransitHandleService { ...@@ -48,12 +49,11 @@ public class TransitHandleService {
* *
* @param deviceLocationDto Gps和基站的数据 * @param deviceLocationDto Gps和基站的数据
*/ */
public void handleGpsAndBsData(DeviceLocationDto deviceLocationDto) { public void handleGpsAndBsData(DeviceLocationDto deviceLocationDto, Integer topicId) {
DeviceLocationDto deviceLocationCache = (DeviceLocationDto) redisUtils.get(setKey(deviceLocationDto.getSn())); DeviceLocationDto deviceLocationCache = (DeviceLocationDto) redisUtils.get(setKey(deviceLocationDto.getSn()));
if (ObjectUtil.isNull(deviceLocationCache)) { if (ObjectUtil.isNull(deviceLocationCache)) {
// 缓存中没有 就是第一次插入数据 设置创建时间同步更新时间 // 缓存中没有 就是第一次插入数据 设置创建时间同步更新时间
deviceLocationDto.setCreatedTime(new Date()); deviceLocationDto.setCreatedTime(new Date());
deviceLocationDto.setUpdatedTime(new Date());
log.info("报警消费数据===sn:" + deviceLocationDto.getSn() + "Gps时间:" + DateUtil.formatDateTime(deviceLocationDto.getLastGpsTime()) + "基站时间:" + DateUtil.formatDateTime(deviceLocationDto.getLastBsTime())); log.info("报警消费数据===sn:" + deviceLocationDto.getSn() + "Gps时间:" + DateUtil.formatDateTime(deviceLocationDto.getLastGpsTime()) + "基站时间:" + DateUtil.formatDateTime(deviceLocationDto.getLastBsTime()));
redisUtils.set(setKey(deviceLocationDto.getSn()), deviceLocationDto); redisUtils.set(setKey(deviceLocationDto.getSn()), deviceLocationDto);
deviceLocationMap.put(deviceLocationDto.getSn(), deviceLocationDto); deviceLocationMap.put(deviceLocationDto.getSn(), deviceLocationDto);
...@@ -75,10 +75,14 @@ public class TransitHandleService { ...@@ -75,10 +75,14 @@ public class TransitHandleService {
flag = true; flag = true;
} }
if (TopicsEnum.DEVICE_STATE.getId().equals(topicId)) {
deviceLocationCache.setStayStatus(deviceLocationDto.getStayStatus());
deviceLocationCache.setStayStatusUpdatetime(deviceLocationDto.getStayStatusUpdatetime());
}
// 判断是否有数据更新 有数据更新重新设置回缓存 // 判断是否有数据更新 有数据更新重新设置回缓存
if (flag) { if (flag) {
// 同步更新时间 // 同步更新时间
deviceLocationCache.setUpdatedTime(new Date());
log.info("报警消费数据===sn:" + deviceLocationCache.getSn() + "Gps时间:" + DateUtil.formatDateTime(deviceLocationCache.getLastGpsTime()) + "基站时间:" + DateUtil.formatDateTime(deviceLocationCache.getLastBsTime())); log.info("报警消费数据===sn:" + deviceLocationCache.getSn() + "Gps时间:" + DateUtil.formatDateTime(deviceLocationCache.getLastGpsTime()) + "基站时间:" + DateUtil.formatDateTime(deviceLocationCache.getLastBsTime()));
redisUtils.set(setKey(deviceLocationCache.getSn()), deviceLocationCache); redisUtils.set(setKey(deviceLocationCache.getSn()), deviceLocationCache);
deviceLocationMap.put(deviceLocationCache.getSn(), deviceLocationCache); deviceLocationMap.put(deviceLocationCache.getSn(), deviceLocationCache);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!