Migrate to general jlibarduino
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.github.boukefalos.arduino.implementation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import base.work.Listen;
|
||||
|
||||
import com.github.boukefalos.arduino.AbstractArduino;
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
import com.github.boukefalos.arduino.port.Port;
|
||||
|
||||
public class Local extends AbstractArduino {
|
||||
protected Port arduino;
|
||||
protected OutputStream outputStream;
|
||||
|
||||
public Local() throws Exception {
|
||||
this(Port.getInstance());
|
||||
}
|
||||
|
||||
public Local(Port arduino) throws ArduinoException {
|
||||
this.arduino = arduino;
|
||||
outputStream = arduino.getOutputStream();
|
||||
}
|
||||
|
||||
public void register(Listen<Object> listen) {
|
||||
arduino.register(listen);
|
||||
}
|
||||
|
||||
public void remove(Listen<Object> listen) {
|
||||
arduino.remove(listen);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
arduino.close();
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws ArduinoException {
|
||||
try {
|
||||
outputStream.write(buffer);
|
||||
} catch (IOException e) {
|
||||
throw new ArduinoException("Failed to write to arduino");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.github.boukefalos.arduino.implementation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.work.Listen;
|
||||
|
||||
import com.github.boukefalos.arduino.AbstractArduino;
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
|
||||
public class Remote extends AbstractArduino implements Receiver {
|
||||
protected Duplex duplex;
|
||||
|
||||
public Remote(Duplex duplex) {
|
||||
this.duplex = duplex;
|
||||
listenList = new ArrayList<Listen<Object>>();
|
||||
duplex.register(this); // Server > [receive()]
|
||||
}
|
||||
|
||||
public void start() {
|
||||
duplex.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
duplex.stop();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
super.stop();
|
||||
duplex.exit();
|
||||
}
|
||||
|
||||
public void register(Listen<Object> listen) {
|
||||
listenList.add(listen);
|
||||
}
|
||||
|
||||
public void remove(Listen<Object> listen) {
|
||||
listenList.remove(listen);
|
||||
}
|
||||
|
||||
public void receive(byte[] buffer) {
|
||||
// Arduino > Server > [Client]
|
||||
// Should decode here?
|
||||
for (Listen<Object> listen : listenList) {
|
||||
listen.add(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws ArduinoException {
|
||||
// [Client] > Server > Arduino
|
||||
try {
|
||||
duplex.send(buffer);
|
||||
} catch (IOException e) {
|
||||
throw new ArduinoException("Failed to send");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user