Newer
Older
sink / src / main / java / org / flume / alarm / restful / DeviceNetUtilDTO.java
zhout on 2 Mar 2022 5 KB first commit
package org.flume.alarm.restful;


import net.sf.json.JSONObject;
import org.flume.alarm.base.DeviceTypeEnum;
import org.flume.alarm.core.util.StringUtils;
import org.flume.alarm.domain.Device;
import org.flume.alarm.mq.RocketMQProducer;

import java.util.*;

public class DeviceNetUtilDTO {


    private String deviceName;
    private String deviceNo;
    private String deviceInnerNo;
    private String deviceType;
    private String specialNo;
    private String remark;
    private String ext;
    private String supplier;
    private String latitude;
    private String longitude;
    private String position;
    private String productKey;

    public static Map<String, String> devTypeCodeMap = new HashMap<String, String>();

    static {//设备类型编码
        devTypeCodeMap.put(DeviceTypeEnum.FireHydrant.toString(), "DT1132");
        devTypeCodeMap.put(DeviceTypeEnum.Liquid.toString(), "DT1127");
        devTypeCodeMap.put(DeviceTypeEnum.Noise.toString(), "DT1130");
        devTypeCodeMap.put(DeviceTypeEnum.MultiLeak.toString(), "DT1129");
        devTypeCodeMap.put(DeviceTypeEnum.Methane.toString(), "DT1131");
        devTypeCodeMap.put(DeviceTypeEnum.Well.toString(), "DT1126");
        devTypeCodeMap.put(DeviceTypeEnum.WasteGas.toString(), "DT1128");
    }

    public DeviceNetUtilDTO() {

    }


    public DeviceNetUtilDTO(Device device) {
        this.deviceName = device.getDevName();
        this.deviceNo = device.getInstallPosition();
        this.deviceInnerNo = device.getDevCode();
        this.deviceType = devTypeCodeMap.get(device.getDeviceType().getTypeName());
        this.specialNo = "A001";
        this.remark = "";
        this.ext = "";
        this.supplier = "SP0000";
        this.latitude = device.getLatitude();
        this.longitude = device.getLongtitude();
        this.position = device.getFactory();
        this.productKey = "";
    }

    public static void sendDevices(RocketMQProducer rocketMQProducer, List<Device> deviceList, String tags) {

        List<List<Device>> deviceGroupList = groupListByQuantity(deviceList, 100);
        for (List<Device> list : deviceGroupList) {
            JSONObject json = new JSONObject();
            json.put("requestId", getUUID());
            json.put("version", "1.0");
            json.put("timestamp", System.currentTimeMillis());
            List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
            for (Device device : list) {
                if (StringUtils.isBlank(device.getInstallPosition())) continue;
                DeviceNetUtilDTO deviceNetUtilDTO = new DeviceNetUtilDTO(device);
                JSONObject jsonObject = JSONObject.fromObject(deviceNetUtilDTO);
                jsonObjects.add(jsonObject);
            }
            json.put("params", jsonObjects);
            rocketMQProducer.sendMsg(json.toString(), tags);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * List按个数分组
     *
     * @param list
     * @param quantity
     * @return
     */
    public static List<List<Device>> groupListByQuantity(List<Device> list, int quantity) {
        List<List<org.flume.alarm.domain.Device>> wrapList = new ArrayList<>();
        if (list == null || list.size() == 0) {
            return wrapList;
        }
        int count = 0;
        while (count < list.size()) {
            wrapList.add(list.subList(count, (count + quantity) > list.size() ? list.size() : (count + quantity)));
            count += quantity;
        }
        return wrapList;

    }

    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        String randomString = uuid.toString().replace("-", "").toUpperCase();
        return randomString;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getDeviceNo() {
        return deviceNo;
    }

    public void setDeviceNo(String deviceNo) {
        this.deviceNo = deviceNo;
    }

    public String getDeviceInnerNo() {
        return deviceInnerNo;
    }

    public void setDeviceInnerNo(String deviceInnerNo) {
        this.deviceInnerNo = deviceInnerNo;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }

    public String getSpecialNo() {
        return specialNo;
    }

    public void setSpecialNo(String specialNo) {
        this.specialNo = specialNo;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public String getSupplier() {
        return supplier;
    }

    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getProductKey() {
        return productKey;
    }

    public void setProductKey(String productKey) {
        this.productKey = productKey;
    }
}