All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.foxtech.device.protocol.v1.snmp.template.JDefaultTemplate Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package cn.foxtech.device.protocol.v1.snmp.template;

import cn.foxtech.device.protocol.v1.core.context.ApplicationContext;
import cn.foxtech.device.protocol.v1.core.exception.ProtocolException;
import cn.foxtech.device.protocol.v1.core.template.ITemplate;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 默认格式的模板
 * 列名称格式:value_name	oid	value_type	remark
 */
@Data
public class JDefaultTemplate implements ITemplate {
    public static final String FORMAT_NAME = "default";

    private JDecoderParam decoderParam = new JDecoderParam();

    public String getSysTemplateName() {
        return FORMAT_NAME;
    }

    public void loadJsnModel(String modelName) {
        // 从进程的上下文中,获得设备模型信息
        Map deviceTemplateEntity = ApplicationContext.getDeviceModels(modelName);

        // 检测:上下文侧的时间戳和当前模型的时间戳是否一致
        Object updateTime = deviceTemplateEntity.getOrDefault("updateTime", 0L);
        if (this.decoderParam.updateTime.equals(updateTime)) {
            return;
        }

        // 取出JSON模型的数据列表
        Map modelParam = (Map) deviceTemplateEntity.getOrDefault("modelParam", new HashMap<>());
        List> rows = (List>) modelParam.getOrDefault("list", new ArrayList<>());

        // 将文件记录组织到map中
        Map nameMap = new HashMap<>();
        Map oidMap = new HashMap<>();
        for (Map row : rows) {
            JDecoderValueParam jDecoderValueParam = new JDecoderValueParam();
            jDecoderValueParam.setValue_name((String) row.get("value_name"));
            jDecoderValueParam.setOid((String) row.get("oid"));
            jDecoderValueParam.setValue_type((String) row.get("value_type"));

            nameMap.put(jDecoderValueParam.getValue_name(), jDecoderValueParam);
            oidMap.put(jDecoderValueParam.getOid(), jDecoderValueParam);
        }

        this.decoderParam.nameMap = nameMap;
        this.decoderParam.oidMap = oidMap;
        this.decoderParam.table = modelName;
        this.decoderParam.updateTime = updateTime;
    }


    public List encodeOIDList(List objectNameList) {
        List oidList = new ArrayList<>();
        for (String objectName : objectNameList) {
            JDecoderValueParam jDecoderValueParam = this.decoderParam.nameMap.get(objectName);
            if (jDecoderValueParam == null) {
                throw new ProtocolException("csv中未定义该对象的信息:" + objectName);
            }

            oidList.add(jDecoderValueParam.oid);
        }


        return oidList;
    }

    public Map decodeValue(Map oid2value) {
        Map result = new HashMap<>();

        for (String oid : oid2value.keySet()) {
            String value = (String) oid2value.get(oid);

            JDecoderValueParam jDecoderValueParam = this.decoderParam.oidMap.get(oid);
            if (jDecoderValueParam.oid == null) {
                throw new ProtocolException("csv中未定义该对象的信息:" + oid);
            }

            try {
                if ("String".equals(jDecoderValueParam.value_type)) {
                    result.put(jDecoderValueParam.getValue_name(), value);
                    continue;
                }
                if ("Integer".equals(jDecoderValueParam.value_type)) {
                    result.put(jDecoderValueParam.getValue_name(), Integer.valueOf(value));
                    continue;
                }
                if ("Long".equals(jDecoderValueParam.value_type)) {
                    result.put(jDecoderValueParam.getValue_name(), Long.valueOf(value));
                    continue;
                }
                if ("Float".equals(jDecoderValueParam.value_type)) {
                    result.put(jDecoderValueParam.getValue_name(), Float.valueOf(value));
                    continue;
                }
                if ("Double".equals(jDecoderValueParam.value_type)) {
                    result.put(jDecoderValueParam.getValue_name(), Double.valueOf(value));
                    continue;
                }
            } catch (Exception e) {
                throw new ProtocolException("数据转换错误:" + jDecoderValueParam.value_name + " 文本为:" + value);
            }
        }

        return result;
    }

    @Data
    static public class JEncoderParam implements Serializable {
        private String name;
    }

    /**
     * 代表一页记录
     */
    @Data
    static public class JDecoderParam implements Serializable {
        private String table;
        private Object updateTime = 0L;
        private Map nameMap = new HashMap<>();
        private Map oidMap = new HashMap<>();
    }


    /**
     * 代表一行记录
     */
    @Data
    static public class JDecoderValueParam implements Serializable {
        /**
         * 对象的名称
         */
        private String value_name;
        /**
         * 对象的oid
         */
        private String oid;
        /**
         * 数据类型
         * Hex-STRING:十六进制格式的字符串
         * Opaque:浮点数
         * INTEGER:整数
         * STRING:字符串格式
         */
        private String value_type;

        private String remark;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy