用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

解析SNMP的dateAndTime类型

2013-04-23 作者: 神马举报

[java]代码库

import java.util.Vector;

import org.apache.log4j.Logger;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.PDU;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;

import com.asb.mediation.snmp.SNMPManager;
import com.asb.mediation.util.INMSLog;


public class SNMPAdapter  extends SNMPManager {
    private Logger adapterlog = INMSLog.getInstance().getAdapterLog();
    private Logger recvlog = INMSLog.getInstance().getReceiveLog();

    public SNMPAdapter (String snmpIP, String snmpPort) {
        super (snmpIP, snmpPort);
    }

    @Override
    protected void notifyMsg (CommandResponderEvent event) {
        adapterlog.info ("Mapper::notifyMsg>>    =====NOTIFY_ALARM=====");

        PDU pdu = event.getPDU();
        Vector vec = pdu.getVariableBindings();
        adapterlog.info ("YYWifiMapper::process>>    oid size = " + vec.size() );

        for (int i = 0; i < vec.size(); i++) {
            String oidNameValue = "";
            VariableBinding vb = (VariableBinding) vec.get (i);
            if (vb.getVariable() instanceof OctetString) {
                String value = new String ( ( (OctetString) vb.getVariable() ).getValue() );

                if (vb.getOid().toString().trim().equals ("1.3.6.1.4.1.88888.2") ) {
                    oidNameValue = "OID:" + vb.getOid() + "\t" + "value:" + parseDateAndTime (vb.getVariable() );
                } else {
                    oidNameValue = "OID:" + vb.getOid() + "\t" + "value:" + value.trim();
                }
            } else {
                //略
            }
        }

    }

    public String parseDateAndTime (Variable v) {
        adapterlog.info ("YYWifiMapper::parseDateAndTime>>    v=" + v);
        //        adapterlog.info("YYWifiMapper::parseDateAndTime>>    v string="+v.toString());

        OctetString oct = (OctetString) v;
        //        adapterlog.info("YYWifiMapper::parseDateAndTime>>    v hex="+ oct.toHexString());

        byte[] bts = oct.getValue();

        byte[] format_str = new byte[128];   //保存格式化过后的时间字符串
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;
        int msecond;

        //        for(byte b:bts){
        //            adapterlog.info("YYWifiMapper::parseDateAndTime>>    bts:"+b);
        //        }
        year = bts[0] * 256 + 256 + bts[1]; //adapterlog.info("YYWifiMapper::parseDateAndTime>>    year:"+year);

        month = bts[2];
        day = bts[3];
        hour = bts[4];
        minute = bts[5];
        second = bts[6];
        msecond = bts[7];
        //以下为格式化字符串
        int index = 3;
        int temp = year;
        for (; index >= 0; index--) {
            format_str[index] = (byte) (48 + (temp - temp / 10 * 10) );
            temp /= 10;
        }

        format_str[4] = '-';
        index = 6;
        temp = month;
        for (; index >= 5; index--) {
            format_str[index] = (byte) (48 + (temp - temp / 10 * 10) );
            temp /= 10;
        }

        format_str[7] = '-';
        index = 9;
        temp = day;
        for (; index >= 8; index--) {
            format_str[index] = (byte) (48 + (temp - temp / 10 * 10) );
            temp /= 10;
        }

        format_str[10] = ' ';
        index = 12;
        temp = hour;
        for (; index >= 11; index--) {
            format_str[index] = (byte) (48 + (temp - temp / 10 * 10) );
            temp /= 10;
        }

        format_str[13] = ':';
        index = 15;
        temp = minute;
        for (; index >= 14; index--) {
            format_str[index] = (byte) (48 + (temp - temp / 10 * 10) );
            temp /= 10;
        }

        format_str[16] = ':';
        index = 18;
        temp = second;
        for (; index >= 17; index--) {
            format_str[index] = (byte) (48 + (temp - temp / 10 * 10) );
            temp /= 10;
        }


        //        format_str[19]='.';
        //        index=21;
        //        temp=msecond;
        //        for(; index>=20; index--){
        //            format_str[index]=(byte)(48+(temp-temp/10*10));
        //            temp/=10;
        //        }
        //
        //        format_str[22]=0;
        //        adapterlog.info("YYWifiMapper::parseDateAndTime>>    format_str = " + new String(format_str));

        return new String (format_str);
    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...