summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/Device.java
blob: dcd70239bc45e0837ba804757f3c5b2660704a6d (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.reprap;

import java.io.IOException;

import org.reprap.comms.Address;
import org.reprap.comms.Communicator;
import org.reprap.comms.IncomingContext;
import org.reprap.comms.OutgoingMessage;
import org.reprap.comms.IncomingMessage.InvalidPayloadException;
import org.reprap.comms.messages.VersionRequestMessage;
import org.reprap.comms.messages.VersionResponseMessage;

/**
 * Class implements an abstract device containing the basic properties and methods.
 * An "implemented" device refers to for example a UCB/Stepper motor combination, 
 * extruder or other. 
 */
public abstract class Device {

	/**
	 * Adress of the device. Identifier returned by the firmware in the device
	 */
	private Address address;
	
	/**
	 * Communicator
	 * 
	 */
	private Communicator communicator;

	/**
	 * Basic constructor for a device.
	 * @param communicator communicator used by the device
	 * @param address address of the device
	 */
	public Device(Communicator communicator, Address address) {
		this.communicator = communicator;
		this.address = address;
	}

	/**
	 * @return the adress of the device
	 */
	public Address getAddress() {
		return address;
	}

	/**
	 * @return the communicator
	 */
	public Communicator getCommunicator() {
		return communicator;
	}

	/**
	 * @return Version ID of the firmware the device is running  
	 * @throws IOException
	 * @throws InvalidPayloadException
	 */
	public int getVersion() throws IOException, InvalidPayloadException {
		VersionRequestMessage request = new VersionRequestMessage();
		IncomingContext replyContext = sendMessage(request);
		VersionResponseMessage reply = new VersionResponseMessage(replyContext);
		return reply.getVersion(); 
	}
	
	/**
	 * @param message 
	 * @return incoming context
	 * @throws IOException
	 */
	public IncomingContext sendMessage(OutgoingMessage message) throws IOException {
		return communicator.sendMessage(this, message);
	}
	
	/**
	 * Method to lock communication to this device. 
	 * <p>TODO: when called?</P> 
	 */
	protected void lock() {
		communicator.lock();
	}
	
	/**
	 * Method to unlock communication to this device
	 * <p>TODO: when called?</P> 
	 *  
	 */
	protected void unlock() {
		communicator.unlock();
	}
	
}