summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/comms/messages/IncomingIntMessage.java
blob: 341a13ff93417168b4fef78652128b65f2ca289c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.reprap.comms.messages;

import java.io.IOException;

import org.reprap.Device;
import org.reprap.comms.IncomingContext;
import org.reprap.comms.IncomingMessage;
import org.reprap.comms.OutgoingMessage;

/**
 *
 */
public abstract class IncomingIntMessage extends IncomingMessage {

	/**
	 * @param incomingContext
	 * @throws IOException
	 */
	public IncomingIntMessage(IncomingContext incomingContext)
			throws IOException {
		super(incomingContext);
	}

	/**
	 * @param device
	 * @param message
	 * @param timeout
	 * @throws IOException
	 */
	public IncomingIntMessage(Device device, OutgoingMessage message, long timeout)
		throws IOException
	{
		super(device, message, timeout);
	}
	
	/**
	 * @param b1
	 * @param b2
	 * @return integer
	 */
	public static int ConvertBytesToInt(byte b1, byte b2) {
		int low = b1;
		int high = b2;
	    if (low < 0) low += 256;
	    if (high < 0) high += 256;
	    return low + (high << 8);
	}
	
	/**
	 * @return value
	 * @throws InvalidPayloadException
	 */
	public int getValue() throws InvalidPayloadException {
	    byte [] reply = getPayload();
	    if (reply == null || reply.length != 3)
	    	throw new InvalidPayloadException();
	    return ConvertBytesToInt(reply[1], reply[2]);
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.comms.IncomingMessage#isExpectedPacketType(byte)
	 */
	abstract protected boolean isExpectedPacketType(byte packetType);

}