X Tutup
Skip to content

Commit bba052d

Browse files
committed
Refactor fake sensors - lower code duplication
1 parent 71f7b96 commit bba052d

File tree

8 files changed

+43
-245
lines changed

8 files changed

+43
-245
lines changed

src/test/java/fake_ev3dev/BaseElement.java

Lines changed: 18 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public abstract class BaseElement {
1515

1616
private static final String JAVA_IO_TEMPDIR = System.getProperty("java.io.tmpdir");
17-
public static final String EV3DEV_FAKE_SYSTEM_PATH = JAVA_IO_TEMPDIR + "/" + "ev3dev_fake_system";
17+
public static final String EV3DEV_FAKE_SYSTEM_PATH = Paths.get(JAVA_IO_TEMPDIR, "ev3dev_fake_system").toString();
1818

1919
protected static final String LEGO_PORT_PATH = "lego-port";
2020
protected static final String PORT = "port";
@@ -34,14 +34,9 @@ public abstract class BaseElement {
3434
protected static final String SENSOR_ADDRESS = "address";
3535
protected static final String SENSOR_MODE = "mode";
3636
protected static final String SENSOR_COMMAND = "command";
37-
protected static final String VALUE0 = "value0";
38-
protected static final String VALUE1 = "value1";
39-
protected static final String VALUE2 = "value2";
40-
protected static final String VALUE3 = "value3";
41-
protected static final String VALUE4 = "value4";
42-
protected static final String VALUE5 = "value5";
43-
protected static final String VALUE6 = "value6";
44-
protected static final String VALUE7 = "value7";
37+
38+
protected static final String SENSOR1_BASE = Paths.get(EV3DEV_FAKE_SYSTEM_PATH, LEGO_SENSOR_PATH, SENSOR1).toString();
39+
protected static final String PORT1_BASE = Paths.get(EV3DEV_FAKE_SYSTEM_PATH, LEGO_SENSOR_PATH, PORT1).toString();
4540

4641
/**
4742
*
@@ -65,91 +60,32 @@ protected void createDirectories(final Path path) throws IOException {
6560
System.out.println(Files.exists(path));
6661
}
6762

63+
protected void createDirectoriesDirect(final String first, final String... more) throws IOException {
64+
createDirectories(Paths.get(first, more));
65+
}
66+
6867
protected void createFile(final Path path) throws IOException {
69-
if(!Files.exists(path)) {
68+
if (!Files.exists(path)) {
7069
Files.createFile(path);
7170
}
7271
}
7372

73+
protected void writeFileDirect(final String contents,
74+
final String first,
75+
final String... more) throws IOException {
76+
createFile(Paths.get(first, more), contents);
77+
}
78+
7479
protected void createFile(final Path path, final String value) throws IOException {
7580
this.createFile(path);
7681
Files.write(path, value.getBytes());
7782
}
7883

7984
protected void populateValues(final List<Integer> values) throws IOException {
80-
81-
if(values.size() >= 1) {
82-
Path value0 = Paths.get(
83-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
84-
LEGO_SENSOR_PATH + "/" +
85-
SENSOR1 + "/" +
86-
VALUE0);
87-
createFile(value0, String.valueOf(values.get(0)));
85+
for (int i = 0; i < values.size(); i++) {
86+
Path value = Paths.get(SENSOR1_BASE, "value" + i);
87+
createFile(value, String.valueOf(values.get(i)));
8888
}
89-
90-
if(values.size() >= 2) {
91-
Path value1 = Paths.get(
92-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
93-
LEGO_SENSOR_PATH + "/" +
94-
SENSOR1 + "/" +
95-
VALUE1);
96-
createFile(value1, String.valueOf(values.get(1)));
97-
}
98-
99-
if(values.size() >= 3) {
100-
Path value2 = Paths.get(
101-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
102-
LEGO_SENSOR_PATH + "/" +
103-
SENSOR1 + "/" +
104-
VALUE2);
105-
createFile(value2, String.valueOf(values.get(2)));
106-
}
107-
108-
if(values.size() >= 4) {
109-
Path value3 = Paths.get(
110-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
111-
LEGO_SENSOR_PATH + "/" +
112-
SENSOR1 + "/" +
113-
VALUE3);
114-
createFile(value3, String.valueOf(values.get(3)));
115-
}
116-
117-
if(values.size() >= 5) {
118-
Path value4 = Paths.get(
119-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
120-
LEGO_SENSOR_PATH + "/" +
121-
SENSOR1 + "/" +
122-
VALUE4);
123-
createFile(value4, String.valueOf(values.get(4)));
124-
}
125-
126-
if(values.size() >= 6) {
127-
Path value5 = Paths.get(
128-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
129-
LEGO_SENSOR_PATH + "/" +
130-
SENSOR1 + "/" +
131-
VALUE5);
132-
createFile(value5, String.valueOf(values.get(5)));
133-
}
134-
135-
if(values.size() >= 7) {
136-
Path value6 = Paths.get(
137-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
138-
LEGO_SENSOR_PATH + "/" +
139-
SENSOR1 + "/" +
140-
VALUE6);
141-
createFile(value6, String.valueOf(values.get(6)));
142-
}
143-
144-
if(values.size() >= 8) {
145-
Path value7 = Paths.get(
146-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
147-
LEGO_SENSOR_PATH + "/" +
148-
SENSOR1 + "/" +
149-
VALUE7);
150-
createFile(value7, String.valueOf(values.get(7)));
151-
}
152-
15389
}
15490

15591
}
Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fake_ev3dev.ev3dev.sensors;
22

33
import ev3dev.hardware.EV3DevPlatform;
4+
import ev3dev.utils.Sysfs;
45
import fake_ev3dev.BaseElement;
56

67
import java.io.IOException;
@@ -12,84 +13,46 @@ public class FakeLegoSensor extends BaseElement {
1213

1314
public FakeLegoSensor(final EV3DevPlatform ev3DevPlatform) throws IOException {
1415

15-
Path batteryPath = Paths.get(
16-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
17-
LEGO_SENSOR_PATH);
16+
Path batteryPath = Paths.get(EV3DEV_FAKE_SYSTEM_PATH, LEGO_SENSOR_PATH);
1817
this.createDirectories(batteryPath);
1918

20-
2119
if(ev3DevPlatform.equals(EV3DevPlatform.EV3BRICK)) {
22-
2320
createStructureSensor1("ev3-ports:in1");
2421

2522
} else {
26-
2723
//BrickPi3
2824
createStructurePort1("spi0.1:S1");
29-
3025
createStructureSensor1("spi0.1:S1");
31-
3226
}
3327
}
3428

3529
private void createStructureSensor1(final String inputPort) throws IOException {
30+
// create sensor1
31+
this.createDirectoriesDirect(SENSOR1_BASE);
3632

37-
Path sensor1 = Paths.get(
38-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
39-
LEGO_SENSOR_PATH + "/" +
40-
SENSOR1
41-
);
42-
this.createDirectories(sensor1);
43-
44-
Path addressPath = Paths.get(
45-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
46-
LEGO_SENSOR_PATH + "/" +
47-
SENSOR1 + "/" +
48-
SENSOR_ADDRESS);
49-
this.createFile(addressPath, inputPort);
33+
// set sensor1 address
34+
this.writeFileDirect(inputPort, SENSOR1_BASE, SENSOR_ADDRESS);
35+
36+
// set sensor1 mode
37+
this.writeFileDirect("BOGUS-MODE", SENSOR1_BASE, SENSOR_MODE);
38+
39+
// set sensor1 command to BOGUS-CMD
40+
this.writeFileDirect("BOGUS-CMD", SENSOR1_BASE, SENSOR_COMMAND);
5041
}
5142

5243
private void createStructurePort1(final String inputPort) throws IOException {
44+
// create lego port1
45+
this.createDirectoriesDirect(PORT1_BASE);
46+
47+
// set port1 address to <inputPort>
48+
this.writeFileDirect(inputPort, PORT1_BASE, PORT_ADDRESS);
49+
50+
// set port1 mode to ev3-bogus
51+
this.writeFileDirect("ev3-bogus", PORT1_BASE, PORT_MODE);
52+
}
5353

54-
Path portPath = Paths.get(
55-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
56-
LEGO_PORT_PATH);
57-
this.createDirectories(portPath);
58-
59-
Path sensorPath = Paths.get(
60-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
61-
LEGO_PORT_PATH + "/" +
62-
PORT1
63-
);
64-
this.createDirectories(sensorPath);
65-
66-
Path addressPath = Paths.get(
67-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
68-
LEGO_PORT_PATH + "/" +
69-
PORT1 + "/" +
70-
PORT_ADDRESS);
71-
this.createFile(addressPath, inputPort);
72-
73-
Path modePath = Paths.get(
74-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
75-
LEGO_PORT_PATH + "/" +
76-
PORT1 + "/" +
77-
PORT_MODE);
78-
this.createFile(modePath);
79-
80-
Path mode = Paths.get(
81-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
82-
LEGO_SENSOR_PATH + "/" +
83-
SENSOR1 + "/" +
84-
SENSOR_MODE);
85-
createDirectories(mode);
86-
87-
Path command = Paths.get(
88-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
89-
LEGO_SENSOR_PATH + "/" +
90-
SENSOR1 + "/" +
91-
SENSOR_COMMAND);
92-
createFile(command);
54+
public String getCurrentMode() {
55+
return Sysfs.readString(Paths.get(SENSOR1_BASE, SENSOR_MODE).toString());
9356
}
9457

9558
}

src/test/java/fake_ev3dev/ev3dev/sensors/ev3/FakeEV3ColorSensor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import fake_ev3dev.ev3dev.sensors.FakeLegoSensor;
55

66
import java.io.IOException;
7-
import java.nio.file.Path;
8-
import java.nio.file.Paths;
97
import java.util.Arrays;
108

119
public class FakeEV3ColorSensor extends FakeLegoSensor {
@@ -14,13 +12,5 @@ public FakeEV3ColorSensor(EV3DevPlatform ev3DevPlatform) throws IOException {
1412
super(ev3DevPlatform);
1513

1614
populateValues(Arrays.asList(2, 100, 200));
17-
18-
Path mode = Paths.get(
19-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
20-
LEGO_SENSOR_PATH + "/" +
21-
SENSOR1 + "/" +
22-
SENSOR_MODE);
23-
24-
createFile(mode);
2515
}
2616
}
Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package fake_ev3dev.ev3dev.sensors.ev3;
22

33
import ev3dev.hardware.EV3DevPlatform;
4-
import ev3dev.utils.Sysfs;
54
import fake_ev3dev.ev3dev.sensors.FakeLegoSensor;
65

76
import java.io.IOException;
8-
import java.nio.file.Path;
9-
import java.nio.file.Paths;
107
import java.util.Arrays;
118

129
public class FakeEV3GyroSensor extends FakeLegoSensor {
@@ -15,20 +12,5 @@ public FakeEV3GyroSensor(EV3DevPlatform ev3DevPlatform) throws IOException {
1512
super(ev3DevPlatform);
1613

1714
populateValues(Arrays.asList(10, 10));
18-
19-
Path mode = Paths.get(
20-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
21-
LEGO_SENSOR_PATH + "/" +
22-
SENSOR1 + "/" +
23-
SENSOR_MODE);
24-
25-
createFile(mode);
26-
}
27-
28-
public String getCurrentMode() {
29-
return Sysfs.readString(EV3DEV_FAKE_SYSTEM_PATH + "/" +
30-
LEGO_SENSOR_PATH + "/" +
31-
SENSOR1 + "/" +
32-
SENSOR_MODE);
3315
}
3416
}

src/test/java/fake_ev3dev/ev3dev/sensors/ev3/FakeEV3IRSensor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import fake_ev3dev.ev3dev.sensors.FakeLegoSensor;
55

66
import java.io.IOException;
7-
import java.nio.file.Path;
8-
import java.nio.file.Paths;
97
import java.util.Arrays;
108

119
public class FakeEV3IRSensor extends FakeLegoSensor {
@@ -14,13 +12,5 @@ public FakeEV3IRSensor(EV3DevPlatform ev3DevPlatform) throws IOException {
1412
super(ev3DevPlatform);
1513

1614
populateValues(Arrays.asList(10, 10, 10, 10, 10, 10, 10, 10));
17-
18-
Path mode = Paths.get(
19-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
20-
LEGO_SENSOR_PATH + "/" +
21-
SENSOR1 + "/" +
22-
SENSOR_MODE);
23-
createFile(mode);
24-
2515
}
2616
}
Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package fake_ev3dev.ev3dev.sensors.ev3;
22

33
import ev3dev.hardware.EV3DevPlatform;
4-
import ev3dev.utils.Sysfs;
54
import fake_ev3dev.ev3dev.sensors.FakeLegoSensor;
65

76
import java.io.IOException;
8-
import java.nio.file.Path;
9-
import java.nio.file.Paths;
107
import java.util.Arrays;
118

129
public class FakeEV3UltrasonicSensor extends FakeLegoSensor {
@@ -15,31 +12,9 @@ public FakeEV3UltrasonicSensor(EV3DevPlatform ev3DevPlatform) throws IOException
1512
super(ev3DevPlatform);
1613

1714
populateValues(Arrays.asList(20));
18-
19-
Path mode = Paths.get(
20-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
21-
LEGO_SENSOR_PATH + "/" +
22-
SENSOR1 + "/" +
23-
SENSOR_MODE);
24-
25-
createFile(mode);
2615
}
2716

2817
public void setListenMode() throws IOException {
29-
30-
Path value0 = Paths.get(
31-
EV3DEV_FAKE_SYSTEM_PATH + "/" +
32-
LEGO_SENSOR_PATH + "/" +
33-
SENSOR1 + "/" +
34-
VALUE0);
35-
createFile(value0, String.valueOf(0));
36-
37-
}
38-
39-
public String getCurrentMode() {
40-
return Sysfs.readString(EV3DEV_FAKE_SYSTEM_PATH + "/" +
41-
LEGO_SENSOR_PATH + "/" +
42-
SENSOR1 + "/" +
43-
SENSOR_MODE);
18+
this.writeFileDirect(String.valueOf(0), SENSOR1_BASE, "value0");
4419
}
4520
}

0 commit comments

Comments
 (0)
X Tutup