Skip to content

Commit ff1be2b

Browse files
author
Daniel Türk
committed
add test device
1 parent 30a1758 commit ff1be2b

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

api/src/main/java/net/wbz/selectrix4java/api/device/AbstractDevice.java

-5
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ public Void call() throws Exception {
141141
*/
142142
abstract public void doDisconnect() throws DeviceAccessException;
143143

144-
// abstract public boolean isConnected();
145-
146144
/**
147145
* Get {@link net.wbz.selectrix4java.api.bus.BusAddress} to read the data value or send new values.
148146
* <p/>
@@ -203,7 +201,6 @@ public synchronized BlockModule getBlockModule(byte... addresses) throws DeviceA
203201
return blockModules.get(busAddress);
204202
}
205203

206-
207204
public synchronized FeedbackBlockModule getFeedbackBlockModule(byte address, byte feedbackAddress, byte... additionalAddresses) throws DeviceAccessException {
208205
BusAddress busAddress = getBusAddress(1, address);
209206
if (!blockModules.containsKey(busAddress)) {
@@ -239,10 +236,8 @@ public boolean getRailVoltage() throws DeviceAccessException {
239236
public void setRailVoltage(boolean state) throws DeviceAccessException {
240237
BusAddress busAddress = getBusAddress(1, (byte) 127);
241238
if (state) {
242-
// busAddress.setBit(6);
243239
busAddress.setBit(8);
244240
} else {
245-
// busAddress.clearBit(6);
246241
busAddress.clearBit(8);
247242
}
248243
busAddress.send();

api/src/main/java/net/wbz/selectrix4java/api/device/Device.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
*/
1313
public interface Device extends Serializable {
1414

15-
void addDeviceConnectionListener(DeviceConnectionListener listener);
15+
public void addDeviceConnectionListener(DeviceConnectionListener listener);
1616

17-
void removeDeviceConnectionListener(DeviceConnectionListener listener);
17+
public void removeDeviceConnectionListener(DeviceConnectionListener listener);
1818

1919
/**
2020
* Create connection of the device.
@@ -33,7 +33,7 @@ public interface Device extends Serializable {
3333
*/
3434
public boolean isConnected();
3535

36-
BlockModule getBlockModule(byte... addresses) throws DeviceAccessException;
36+
public BlockModule getBlockModule(byte... addresses) throws DeviceAccessException;
3737

3838
public boolean getRailVoltage() throws DeviceAccessException;
3939

manager/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
<artifactId>selectrix4java-serial</artifactId>
2626
<version>${project.version}</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>net.wbz.selectrix4java</groupId>
30+
<artifactId>selectrix4java-test</artifactId>
31+
<version>${project.version}</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.reflections</groupId>
36+
<artifactId>reflections</artifactId>
37+
<version>0.9.9-RC1</version>
38+
</dependency>
2839
</dependencies>
2940

3041
<build>

manager/src/main/java/net/wbz/selectrix4java/manager/DeviceManager.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,42 @@
66
import net.wbz.selectrix4java.api.device.Device;
77
import net.wbz.selectrix4java.api.device.DeviceAccessException;
88
import net.wbz.selectrix4java.api.device.DeviceConnectionListener;
9+
import net.wbz.selectrix4java.test.TestDevice;
10+
import org.reflections.Reflections;
911

12+
import java.lang.reflect.Modifier;
1013
import java.util.List;
1114
import java.util.Map;
15+
import java.util.Set;
1216

1317
/**
14-
* TODO: refactor
18+
* TODO: refactor for multiple device types
1519
*
1620
* @author Daniel Tuerk (daniel.tuerk@w-b-z.com)
1721
*/
1822
public class DeviceManager {
1923

2024
private final Map<String, Device> devices = Maps.newHashMap();
2125

22-
public enum DEVICE_TYPE {COM1}
26+
private final List<DeviceConnectionListener> listeners = Lists.newArrayList();
27+
28+
public enum DEVICE_TYPE {SERIAL, TEST}
29+
30+
31+
32+
// public DeviceManager() {
33+
//
34+
// Reflections reflections = new Reflections("net.wbz.selectrix4java");
35+
// Set<Class<? extends Device>> subTypes = reflections.getSubTypesOf(Device.class);
36+
//
37+
// for (Class<? extends Device> deviceClazz : subTypes) {
38+
// if(!Modifier.isAbstract(deviceClazz.getModifiers())) {
39+
//
40+
//
41+
// }
42+
// }
43+
//
44+
// }
2345

2446
public Device registerDevice(DEVICE_TYPE type, String deviceId, int baudRate) {
2547
if (!devices.containsKey(deviceId)) {
@@ -34,8 +56,10 @@ public Device registerDevice(DEVICE_TYPE type, String deviceId, int baudRate) {
3456

3557
private Device createDevice(DEVICE_TYPE type, String deviceId, int baudRate) {
3658
switch (type) {
37-
case COM1:
59+
case SERIAL:
3860
return new SerialDevice(deviceId, baudRate);
61+
case TEST:
62+
return new TestDevice();
3963
default:
4064
throw new RuntimeException("no device found for type " + type.name());
4165
}
@@ -83,7 +107,6 @@ public void removeDevice(Device device) {
83107
throw new RuntimeException("no device found to delete");
84108
}
85109

86-
private final List<DeviceConnectionListener> listeners = Lists.newArrayList();
87110

88111
public void addDeviceConnectionListener(DeviceConnectionListener listener) {
89112
listeners.add(listener);

pom.xml

+25
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@
4040
</configuration>
4141
</plugin>
4242

43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-source-plugin</artifactId>
46+
<executions>
47+
<execution>
48+
<id>attach-sources</id>
49+
<goals>
50+
<goal>jar</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-javadoc-plugin</artifactId>
59+
<executions>
60+
<execution>
61+
<id>attach-javadocs</id>
62+
<goals>
63+
<goal>jar</goal>
64+
</goals>
65+
</execution>
66+
</executions>
67+
</plugin>
4368
</plugins>
4469
</build>
4570

serial/src/main/java/net/wbz/selectrix4java/SerialDevice.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import gnu.io.CommPortIdentifier;
44
import gnu.io.SerialPort;
5-
import net.wbz.selectrix4java.api.bus.AllBusDataConsumer;
65
import net.wbz.selectrix4java.api.bus.BusDataDispatcher;
7-
import net.wbz.selectrix4java.api.data.BusData;
86
import net.wbz.selectrix4java.api.data.BusDataChannel;
97
import net.wbz.selectrix4java.api.device.AbstractDevice;
108
import net.wbz.selectrix4java.api.device.DeviceAccessException;
@@ -124,7 +122,11 @@ public boolean isConnected() {
124122
return outputStream != null && inputStream != null;
125123
}
126124

127-
125+
/**
126+
* Test main method to send commands by console and print the output.
127+
*
128+
* @param args ignored
129+
*/
128130
public static void main(String[] args) {
129131
SerialDevice serialDevice = new SerialDevice("/dev/tty.usbserial-145", SerialDevice.DEFAULT_BAUD_RATE_FCC);
130132
try {
@@ -157,9 +159,5 @@ public static void main(String[] args) {
157159
} catch (DeviceAccessException e) {
158160
e.printStackTrace();
159161
}
160-
161-
162-
163-
164162
}
165163
}

0 commit comments

Comments
 (0)