DeviceAddressProducer.java 3.72 KB
package cn.com.glsx.smart.component;


import cn.com.glsx.smart.common.utils.GCJ2WGS;

import cn.com.glsx.smart.config.KafkaProperties;
import cn.com.glsx.smart.modules.model.pb.InstanceFence.Fence;
import com.alibaba.fastjson.JSON;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @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 Producer producer;

    @Autowired
    private KafkaProperties kafkaConfig;

    /**
     * 推送设备经纬度数据
     * @param deviceId
     * @param lngAndLat
     * @param value  1新增 2删除 4更新
     */
    public void pushUserDeviceData(String deviceId, String lngAndLat, Integer value) {
        //kafka 发送消息数据封装
        HashMap<String, Double> hm;
        if(value == Fence.OperationType.DEL_VALUE
            || value == Fence.OperationType.GET_VALUE){  //删除和获取不需要经纬度数据
            hm = new HashMap<>(2);
            hm.put("lat", Double.valueOf(0));
            hm.put("lon", Double.valueOf(0));
        } else{
            GCJ2WGS wg = new GCJ2WGS();
            String[] lngLat = lngAndLat.split(":");
            hm = wg.delta(Double.valueOf(lngLat[1]), Double.valueOf(lngLat[0]));
        }
        Fence.GpsLoc gpsLoc = this.buildGpsLoc(hm.get("lon"), hm.get("lat"));
        Fence.FenceDeviceFamilyInfo deviceFamilyInfo = this.buildFenceDevice(deviceId, gpsLoc);
        Fence.OptFamilyFenceInfo familyFenceInfo = this.buildFamilyFence(value, deviceFamilyInfo);
        try {
            String topic = kafkaConfig.getDeviceTopic();
            log.info("推送kafka设备地址:{} {} {}", deviceId, lngAndLat, value);
            log.info("推送kafka topic【{}】的数据:{} {} {}", topic, deviceId, JSON.toJSONString(hm), value);
            KeyedMessage<byte[], byte[]> message = new KeyedMessage<>(topic, deviceId.getBytes(), familyFenceInfo.toByteArray());
            producer.send(message);
        }catch (Exception e){
            log.error("消息推送kafka失败:{}", e);
        }
    }

    /**
     * 围栏设备经纬度数据封装
     * @param lng
     * @param lat
     * @return
     */
    private Fence.GpsLoc buildGpsLoc(double lng, double lat){
        Fence.GpsLoc.Builder gpsLocBuilder = Fence.GpsLoc.newBuilder();
        gpsLocBuilder.setLng((int)(lng * 1000000));
        gpsLocBuilder.setLat((int)(lat * 1000000));
        return gpsLocBuilder.build();
    }

    /**
     * 围栏设备数据封装
     * @param deviceId
     * @param gpsLoc
     * @return
     */
    private Fence.FenceDeviceFamilyInfo buildFenceDevice(String deviceId, Fence.GpsLoc gpsLoc){
        Fence.FenceDeviceFamilyInfo.Builder fenceDeviceBuilder = Fence.FenceDeviceFamilyInfo.newBuilder();
        fenceDeviceBuilder.setSn(deviceId);
        fenceDeviceBuilder.addGpslocs(gpsLoc);
        return fenceDeviceBuilder.build();
    }


    /**
     * 围栏整个数据封装
     * @param value
     * @param familyInfo
     * @return
     */
    private Fence.OptFamilyFenceInfo buildFamilyFence(Integer value, Fence.FenceDeviceFamilyInfo familyInfo){
        Fence.OptFamilyFenceInfo.Builder familyFenceBuilder = Fence.OptFamilyFenceInfo.newBuilder();
        familyFenceBuilder.addFamilyfenceInfos(familyInfo);
        familyFenceBuilder.setOperationType(Fence.OperationType.valueOf(value));
        return familyFenceBuilder.build();
    }

}