I'm implementing my DIY IoT. I have a central node (server) which receives the commands from different sources and executes them.
Input format:
<DEVICE_NAME>_<COMMAND> <DEVICE_NAME>_<COMMAND> <DEVICE_NAME>_<COMMAND>
<DEVICE_NAME>_<COMMAND> <DEVICE_NAME>_<COMMAND>
<DEVICE_NAME>_<COMMAND> <DEVICE_NAME>_<COMMAND> <DEVICE_NAME>_<COMMAND>
Each line may contain multiple commands.
I have implemented a command executor server which takes the commands from the session as an InputStream. Then I split the data and process it:
private Device c0 = // Device constructor
private Device c1 = // Device constructor
private Device c2 = // Device constructor
private Device c3 = // Device constructor
private ExecutorService executor = Executors.newFixedThreadPool(3);
public void onConnection(InputStream in)
InputStreamReader isr = new InputStreamReader(in);
LineNumberReader reader = new LineNumberReader(isr);
String line = null;
while ((line = reader.readLine()) != null) {
String[] strings = line.split(",");
for (String raw : strings) {
String command = raw.substring(0, 3);
if (raw.startsWith("C0_")) {
executor.submit(() -> c0.execute(command));
} else if (raw.startsWith("C1_")) {
executor.submit(() -> c1.execute(command));
} else if (raw.startsWith("C2_")) {
executor.submit(() -> c2.execute(command));
} else if (raw.startsWith("C3_")) {
executor.submit(() -> c3.execute(command));
}
}
}
}
I understand the code looks ugly. Do you have any improvement ideas? Maybe I could use Steam API?
Any hints/advices are appreciated.
UPDATE
I've tried to clean the code a bit by submitting the task only once, but the compailer sais the device must be final or effectively final so this does not work:
String command = raw.substring(0, 3);
Device device;
if (raw.startsWith("C0_")) {
device = c0;
} else if (raw.startsWith("C1_")) {
device = c1;
} else if (raw.startsWith("C2_")) {
device = c2;
} else if (raw.startsWith("C3_")) {
device = c3;
}
executor.submit(() -> device.execute(command));