READERNAME CommandFormatter.java

From RifidiWiki

Jump to: navigation, search

This class should implement CommandHandler. This page goes through each of the four methods in CommandHandler and describes in detail what each one does and how it should be implemented. The first two methods are important, and the last two methods are not as important.

decode

This method needs to decode an incoming command into a command to be handled in the command formatter. It should put the command into a commandObject and put the commandObject in the first slot in an arraylist and return the arraylist. Normally the command object is a string that Rifidi uses to find the the appropriate command handler. The rest of the slots in the ArrayList may be used to pass arguments to the command handler, i.e. a coherent object that contains the the information already parsed and ready for consumption. For an idea on how to do that, look up the ThingMagic source code.

public ArrayList<Object> decode(byte[] arg);

Arguments

  1. arg - The incoming command as an array of bytes.

Return Value

An array of objects where the first object is a command in the correct format to be processed by the command handler

Reference Implementation

This is the decode method in the alien reader

	public ArrayList<Object> decode(byte[] arg) {
		if (arg.length == 0) {
			ArrayList<Object> argArray = new ArrayList<Object>();
			argArray.add("");
			console = false;
			return argArray;
		}
		String newString = new String(arg);

		logger.debug("Formatter arg: " + newString);

		ArrayList<Object> retVal = this.parseCommand(newString);

		return retVal;
	}

encode

After a command has been processed by the command handler, the response it returns must be formatted to be sent back out to the client.

public ArrayList<Object> encode(ArrayList<Object> arg);

Arguments

  1. arg - The return message(s) before they has been processed is in the array of objects

Return Value

An array of objects where each object is a formatted response message ready to be sent back to the client. It must be formatted in a way that READERNAME_Protocol.java::addProtocol() can understand.

Reference Implementation

This is the encode method for the symbol reader. Because some commands in the symbol reader need to have more than one response message (e.g. a data message and a final message), more than one raw message can be in the array list in arg. For each message, the CRC must be calculated and appended.

	public ArrayList<Object> encode(ArrayList<Object> arg) {

		ArrayList<Object> retVal = new ArrayList<Object>();

		for (Object o : arg) {
			byte[] command = (byte[]) o;

			/*
			 * Create outgoing array list that is 3 slots bigger than incoming
			 * one: one for start of frame and two for crc bytes
			 */
			byte[] outgoingCommand = new byte[command.length + 3];
			for (int i = 0; i < command.length; i++) {
				outgoingCommand[i + 1] = command[i];
			}

			/* Add Start of Frame Byte */
			outgoingCommand[0] = 0x01;

			// Calculate CRC
			int crc = CRC16.calculateCRC(command, 0xBEEF,
					CRC16.XR400_CRC_TABLE, true);
			byte[] crcBytes = ByteAndHexConvertingUtility
					.intToByteArray(crc, 2);

			// put crc in outgoingCommand, LSB first
			outgoingCommand[outgoingCommand.length - 2] = crcBytes[1];
			outgoingCommand[outgoingCommand.length - 1] = crcBytes[0];
			
			logger.debug(ByteAndHexConvertingUtility.toHexString(outgoingCommand));
			
			
			retVal.add(outgoingCommand);

		}

		return retVal;
	}


promptSuppress

This method is mainly used by the alien reader to determine if the reader should supress the prompt when sending back a response. Other readers may need similar functionality.

public boolean promptSuppress();

Arguments

Return Value

true if the prompt should be surpressed

getActualCommand

This method is mainly used by the alien reader to return a string that a user actually typed in. Other readers may need similar functionality

public String getActualCommand(byte[] arg);

Arguments

  1. arg - The incoming command

Return Value

A string that is the command the user typed in

Personal tools