DeviceAddressProducer.java 1.24 KB
package com.glsx.smart.config;

import com.alibaba.fastjson.JSON;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.pojo.DeviceMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author think.wang
 * @version 1.0.0
 * @Title DeviceAddressProducer
 * @Package com.glsx.smart.config
 * @Description 设备围栏数据kafka推送
 * @date 2021/4/11
 */
@Slf4j
@Component
public class DeviceAddressProducer {

    @Resource
    private KafkaTemplate kafkaTemplate;

    /**
     * 推送设备经纬度数据
     * @param deviceId
     * @param lngAndLat
     * @param type  1新增 2更新 3删除
     */
    public void pushUserDeviceData(String deviceId, String lngAndLat, Integer type) {
        //kafka 发送消息数据封装
        DeviceMessage message = new DeviceMessage();
        message.setDeviceId(deviceId);
        message.setLngAndLat(lngAndLat);
        message.setType(type);
        log.info("推送kafka topic【{}】的数据:{}", Constants.DEVICE_DATA_TOPIC, message.toString());
        //推送kafka
        kafkaTemplate.send(Constants.DEVICE_DATA_TOPIC, JSON.toJSONString(message));
    }

}