[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);
}
}