⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 epcgnotificationchanneltablerow.java

📁 关于 RFID 读写器的相关内容
💻 JAVA
字号:
/*
 * Copyright (C) 2007 ETH Zurich
 *
 * This file is part of Fosstrak (www.fosstrak.org).
 *
 * Fosstrak is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * Fosstrak is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Fosstrak; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table;

import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

import org.fosstrak.reader.rprm.core.NotificationChannel;
import org.fosstrak.reader.rprm.core.ReaderProtocolException;
import org.fosstrak.reader.rprm.core.mgmt.AdministrativeStatus;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.mib.EpcglobalReaderMib;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.SnmpTable.TableTypeEnum;
import org.fosstrak.reader.rprm.core.mgmt.alarm.AlarmLevel;
import org.fosstrak.reader.rprm.core.mgmt.util.SnmpUtil;
import org.apache.log4j.Logger;
import org.snmp4j.smi.Counter32;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UnsignedInteger32;
import org.snmp4j.smi.Variable;

/**
 * Forms a row of the epcgNotificationChannelTable.
 */
public class EpcgNotificationChannelTableRow extends SnmpTableRow {
	
	private static long refreshTimeInMs = 100; // default value of the refresh time
	
	public static final int numOfColumns = EpcglobalReaderMib.getInstance().getEpcgNotificationChannelEntry().getColumnCount();
	private static Logger log = Logger.getLogger(EpcgNotificationChannelTableRow.class);
	private long[] lastRefreshTimes = new long[EpcgNotificationChannelTableRow.numOfColumns];
	private NotificationChannel notifChan;
	
	/**
	 * Protected Constructor. Use <code>getSnmpTableRow()</code> to
	 * instantiate.
	 * 
	 * @param cont
	 *            Row object container which corresponds to this row
	 * @throws ReaderProtocolException
	 *             Thrown if the <code>ReaderDevice</code> instance cannot be
	 *             obtained.
	 */
	protected EpcgNotificationChannelTableRow(RowObjectContainer cont)
			throws ReaderProtocolException {
		super(EpcgNotificationChannelTableRow.computeIndex(cont), new Variable[numOfColumns], cont);
		notifChan = (NotificationChannel)cont.getRowObjects()[0];
	}
	
	/**
	 * Computes the index for this row
	 * 
	 * @param cont
	 *            Row object container of this row
	 * @return Index
	 */
	private static OID computeIndex(RowObjectContainer cont) {
		SnmpTable table = (SnmpTable)SnmpUtil.getSnmpTable(TableTypeEnum.EPCG_NOTIFICATION_CHANNEL_TABLE);
		Vector<OID> indices = table.getSortedIndices();
		
		Enumeration<OID> elements = indices.elements();
		while (elements.hasMoreElements()) {
			OID tempIndex = elements.nextElement().nextPeer();
			if (!indices.contains(tempIndex)) return tempIndex;
		}
		return new OID("1");
	}
	
	/**
	 * Gets the value at the specified column index.
	 * 
	 * @param column
	 *            The (zero-based) column index
	 * @return The value at the specified index
	 */
	@Override
	public Variable getValue(int column) {
		boolean refresh = ((System.currentTimeMillis() - lastRefreshTimes[column]) > refreshTimeInMs);
		if (refresh) {
			log.debug("refreshing row " + index.toString());
			lastRefreshTimes[column] = System.currentTimeMillis();
		} else {
			log.debug("no need to refresh row " + index.toString());
		}
		
		switch (column) {
			case EpcglobalReaderMib.idxEpcgNotifChanName:
				if (refresh) setValue(column, new OctetString(notifChan.getName()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanAddressType:
				// TODO: find out the address type
				if (refresh) setValue(column, new Integer32(1));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanAddress:
				if (refresh) setValue(column, new OctetString(notifChan.getAddress()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanLastAttempt:
				if (refresh) {
					Date lastAttempt = notifChan.getLastNotificationAttempt();
					if (lastAttempt != null)
						setValue(column, SnmpUtil.dateToOctetString(lastAttempt));
				}
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanLastSuccess:
				if (refresh) {
					Date lastSuccess = notifChan.getLastSuccessfulNotification();
					if (lastSuccess != null)
						setValue(column, SnmpUtil.dateToOctetString(lastSuccess));
				}
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanAdminStatus:
				if (refresh) setValue(column, new Integer32(notifChan.getAdminStatus().toInt()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperStatus:
				if (refresh) setValue(column, new Integer32(notifChan.getOperStatus().toInt()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperNotifEnable:
				if (refresh) setValue(column, new Integer32(notifChan.getOperStatusAlarmControl().getEnabled() ? 1 : 2));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperNotifLevel:
				if (refresh) setValue(column, new Integer32(notifChan.getOperStatusAlarmControl().getLevel().toInt()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperNotifFromState:
				if (refresh) setValue(column, SnmpUtil.operStateToBITS(notifChan.getOperStatusAlarmControl().getTriggerFromState()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperNotifToState:
				if (refresh) setValue(column, SnmpUtil.operStateToBITS(notifChan.getOperStatusAlarmControl().getTriggerToState()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperStateSuppressInterval:
				if (refresh) setValue(column, new UnsignedInteger32(notifChan.getOperStatusAlarmControl().getSuppressInterval()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperStateSuppressions:
				if (refresh) setValue(column, new Counter32(notifChan.getOperStateSuppressions()));
				break;
		}
		return super.getValue(column);
	}
	
	/**
	 * Sets the value of a column of this row.
	 * 
	 * @param column
	 *            The (zero-based) column index
	 * @param value
	 *            The new value for the specified column
	 */
	@Override
	public void setValue(int column, Variable value) {
		switch (column) {
			case EpcglobalReaderMib.idxEpcgNotifChanAdminStatus:
				notifChan.setAdminStatus(AdministrativeStatus
					.intToEnum(((Integer32) value).toInt()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperNotifEnable:
				notifChan.getOperStatusAlarmControl().setEnabled(
					((Integer32) value).toInt() == 1 ? true : false);
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperNotifLevel:
				notifChan.getOperStatusAlarmControl().setLevel(
					AlarmLevel.intToEnum(((Integer32) value).toInt()));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperNotifFromState:
				notifChan.getOperStatusAlarmControl().setTriggerFromState(SnmpUtil.bitsToOperState((OctetString)value));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperNotifToState:
				notifChan.getOperStatusAlarmControl().setTriggerToState(SnmpUtil.bitsToOperState((OctetString)value));
				break;
			case EpcglobalReaderMib.idxEpcgNotifChanOperStateSuppressInterval:
				notifChan.getOperStatusAlarmControl().setSuppressInterval(((UnsignedInteger32) value).toInt());
				break;
		}
		super.setValue(column, value);
	}

	/**
	 * Forces a refresh of all values.
	 */
	public void forceRefresh() {
		for (int i = 0; i < lastRefreshTimes.length; i++) {
			lastRefreshTimes[i] = 0;
		}
	}
	
	/**
	 * Forces a refresh of the value of a given column.
	 * 
	 * @param column
	 *            Column
	 */
	public void forceRefresh(int column) {
		lastRefreshTimes[column] = 0;
	}
	
	/**
	 * Sets the refresh time in ms.
	 * 
	 * @param refreshTimeInMs
	 *            Refresh time in ms
	 */
	public void setRefreshTime(long refreshTimeInMs) {
		EpcgNotificationChannelTableRow.refreshTimeInMs = refreshTimeInMs;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -