summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/machines/MachineFactory.java
blob: 53d4abb8695c84e00e4e9332765ce3b86723092f (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
package org.reprap.machines;

import java.net.URL;
import java.util.Properties;

import org.reprap.Printer;
import org.reprap.ReprapException;
import org.reprap.comms.port.Port;
import org.reprap.comms.port.SerialPort;

/**
 * 
 * Returns an appropriate Printer object based on the current properties
 * 
 */
public class MachineFactory {

	private static final int defaultBaudRate = 19200; ///< Default baud rate if none is specified
	
	private MachineFactory() {
	}

	static public Printer create(Properties props, Port port) throws Exception {
		// Currently this just always assumes we're building
		// a 3-axis cartesian printer.  It should build an
		// appropriate type based on the local configuration.
		
		String geometry = props.getProperty("Geometry");
		if (geometry.compareToIgnoreCase("cartesian") == 0)
		  	return new Reprap(props, port);
		else if (geometry.compareToIgnoreCase("nullcartesian") == 0)
		    return new NullCartesianMachine(props);		
		else
			throw new ReprapException("Invalid geometry in properties file");
		
	}
	
	static public Printer create(Properties props) throws Exception {
		String commPortName = props.getProperty("Port");
		String geometry = props.getProperty("Geometry");

		Port port = null;
		
		// Create a port, provided this isn't a null machine
		if (geometry.compareToIgnoreCase("nullcartesian") != 0) {		
			int baudRate = defaultBaudRate;
			if (props.containsKey("BaudRate"))
				baudRate = Integer.parseInt(props.getProperty("BaudRate"));
			port = new SerialPort(commPortName, baudRate);
		}
		
		return create(props, port);
	}
	
	static public Printer create() throws Exception {
		Properties props = new Properties();
		URL url = ClassLoader.getSystemResource("reprap.properties");
		props.load(url.openStream());

		return create(props);
	}
	
}