Snapshot, replace tabs with spaces
This commit is contained in:
@@ -9,19 +9,28 @@ import base.work.Listen;
|
|||||||
import base.worker.Worker;
|
import base.worker.Worker;
|
||||||
|
|
||||||
public abstract class AbstractArduino extends Listen<Object> implements Arduino {
|
public abstract class AbstractArduino extends Listen<Object> implements Arduino {
|
||||||
public static final int BUFFER_SIZE = 1024;
|
public static final int BUFFER_SIZE = 1024;
|
||||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
protected ArrayList<Listen<Object>> listenList;
|
protected ArrayList<Listen<Object>> listenList;
|
||||||
|
|
||||||
public AbstractArduino() {
|
public AbstractArduino() {
|
||||||
super(Worker.Type.DIRECT);
|
super(Worker.Type.DIRECT);
|
||||||
}
|
listenList = new ArrayList<Listen<Object>>();
|
||||||
|
}
|
||||||
|
|
||||||
public void start() {}
|
public void register(Listen<Object> listen) {
|
||||||
|
listenList.add(listen);
|
||||||
|
}
|
||||||
|
|
||||||
public void stop() {}
|
public void remove(Listen<Object> listen) {
|
||||||
|
listenList.remove(listen);
|
||||||
|
}
|
||||||
|
|
||||||
public void exit() {
|
public void start() {}
|
||||||
stop();
|
|
||||||
}
|
public void stop() {}
|
||||||
|
|
||||||
|
public void exit() {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import base.work.Listen;
|
|||||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||||
|
|
||||||
public interface Arduino extends Control {
|
public interface Arduino extends Control {
|
||||||
public void register(Listen<Object> listen);
|
public void register(Listen<Object> listen);
|
||||||
public void remove(Listen<Object> listen);
|
public void remove(Listen<Object> listen);
|
||||||
|
|
||||||
public void send(byte[] buffer) throws ArduinoException;
|
public void send(byte[] buffer) throws ArduinoException;
|
||||||
}
|
}
|
||||||
@@ -18,64 +18,64 @@ public class Loader extends AbstractLoader<Loader> {
|
|||||||
protected static final String PROPERTIES_FILE = "arduino.properties";
|
protected static final String PROPERTIES_FILE = "arduino.properties";
|
||||||
|
|
||||||
public Loader(Properties properties) throws LoaderException {
|
public Loader(Properties properties) throws LoaderException {
|
||||||
this(Local.class, Remote.class, Server.class, properties);
|
this(Local.class, Remote.class, Server.class, properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Loader(Class<?> localClass, Class<?> remoteClass, Class<?> serverClass, Properties properties) throws LoaderException {
|
public Loader(Class<?> localClass, Class<?> remoteClass, Class<?> serverClass, Properties properties) throws LoaderException {
|
||||||
/* Add implementation */
|
/* Add implementation */
|
||||||
switch (properties.getProperty("implementation")) {
|
switch (properties.getProperty("implementation")) {
|
||||||
case "local":
|
case "local":
|
||||||
pico.addComponent(localClass);
|
pico.addComponent(localClass);
|
||||||
break;
|
break;
|
||||||
case "remote":
|
case "remote":
|
||||||
pico.addComponent(remoteClass);
|
pico.addComponent(remoteClass);
|
||||||
|
|
||||||
/* Add remote duplex implementation */
|
/* Add remote duplex implementation */
|
||||||
try {
|
try {
|
||||||
String protocol = properties.getOrDefault("protocol", "tcp").toString();
|
String protocol = properties.getOrDefault("protocol", "tcp").toString();
|
||||||
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
|
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
|
||||||
String host = properties.getProperty("remote.host");
|
String host = properties.getProperty("remote.host");
|
||||||
int port = Integer.valueOf(properties.getProperty("remote.port"));
|
int port = Integer.valueOf(properties.getProperty("remote.port"));
|
||||||
addClientDuplex(protocol, implementation, host, port);
|
addClientDuplex(protocol, implementation, host, port);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new LoaderException("Failed to parse remote.port");
|
throw new LoaderException("Failed to parse remote.port");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add server */
|
/* Add server */
|
||||||
if (properties.getProperty("server") != null) {
|
if (properties.getProperty("server") != null) {
|
||||||
boolean direct = Boolean.parseBoolean(properties.getOrDefault("server.direct", Server.DIRECT).toString());
|
boolean direct = Boolean.parseBoolean(properties.getOrDefault("server.direct", Server.DIRECT).toString());
|
||||||
pico.addComponent(serverClass, serverClass, new Parameter[] {
|
pico.addComponent(serverClass, serverClass, new Parameter[] {
|
||||||
new ComponentParameter(),
|
new ComponentParameter(),
|
||||||
new ComponentParameter(),
|
new ComponentParameter(),
|
||||||
new ConstantParameter(direct)});
|
new ConstantParameter(direct)});
|
||||||
|
|
||||||
/* Add server forwarder implementation */
|
/* Add server forwarder implementation */
|
||||||
try {
|
try {
|
||||||
String protocol = properties.getOrDefault("server.protocol", "tcp").toString();
|
String protocol = properties.getOrDefault("server.protocol", "tcp").toString();
|
||||||
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
|
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
|
||||||
int port = Integer.valueOf(properties.getProperty("server.port"));
|
int port = Integer.valueOf(properties.getProperty("server.port"));
|
||||||
addServerDuplex(protocol, implementation, port);
|
addServerDuplex(protocol, implementation, port);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new LoaderException("Failed to parse server.port");
|
throw new LoaderException("Failed to parse server.port");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Arduino getArduino() throws ArduinoException {
|
public Arduino getArduino() throws ArduinoException {
|
||||||
try {
|
try {
|
||||||
return (Arduino) pico.getComponent(Arduino.class);
|
return (Arduino) pico.getComponent(Arduino.class);
|
||||||
} catch (PicoCompositionException e) {
|
} catch (PicoCompositionException e) {
|
||||||
throw new ArduinoException("Failed to load");
|
throw new ArduinoException("Failed to load");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Server getServer() throws ArduinoException {
|
public Server getServer() throws ArduinoException {
|
||||||
try {
|
try {
|
||||||
return pico.getComponent(Server.class);
|
return pico.getComponent(Server.class);
|
||||||
} catch (PicoCompositionException e) {
|
} catch (PicoCompositionException e) {
|
||||||
throw new ArduinoException("Failed to load");
|
throw new ArduinoException("Failed to load");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,53 +12,53 @@ import base.work.Listen;
|
|||||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||||
|
|
||||||
public class Server extends Listen<Object> implements Control, Receiver {
|
public class Server extends Listen<Object> implements Control, Receiver {
|
||||||
protected static final boolean DIRECT = false;
|
protected static final boolean DIRECT = false;
|
||||||
|
|
||||||
protected Arduino arduino;
|
protected Arduino arduino;
|
||||||
protected Duplex duplex;
|
protected Duplex duplex;
|
||||||
protected boolean direct;
|
protected boolean direct;
|
||||||
|
|
||||||
public Server(Arduino arduino, Duplex duplex) {
|
public Server(Arduino arduino, Duplex duplex) {
|
||||||
this(arduino, duplex, DIRECT);
|
this(arduino, duplex, DIRECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Server(Arduino tm1638, Duplex duplex, boolean direct) {
|
public Server(Arduino tm1638, Duplex duplex, boolean direct) {
|
||||||
this.arduino = tm1638;
|
this.arduino = tm1638;
|
||||||
this.duplex = duplex;
|
this.duplex = duplex;
|
||||||
this.direct = direct;
|
this.direct = direct;
|
||||||
arduino.register(this); // Arduino > [input()]
|
arduino.register(this); // Arduino > [input()]
|
||||||
duplex.register(this); // Client > [receive()]
|
duplex.register(this); // Client > [receive()]
|
||||||
}
|
}
|
||||||
|
|
||||||
public void activate() throws ActivateException {
|
public void activate() throws ActivateException {
|
||||||
duplex.start();
|
duplex.start();
|
||||||
super.activate();
|
super.activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deactivate() throws DeactivateException {
|
public void deactivate() throws DeactivateException {
|
||||||
duplex.stop();
|
duplex.stop();
|
||||||
super.deactivate();
|
super.deactivate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receive(byte[] buffer) {
|
public void receive(byte[] buffer) {
|
||||||
// Client > [Server] > Arduino
|
// Client > [Server] > Arduino
|
||||||
if (direct) {
|
if (direct) {
|
||||||
try {
|
try {
|
||||||
arduino.send(buffer);
|
arduino.send(buffer);
|
||||||
} catch (ArduinoException e) {
|
} catch (ArduinoException e) {
|
||||||
logger.error("", e);
|
logger.error("", e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// option to decode() in derivatives?
|
// option to decode() in derivatives?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void input(byte[] buffer) {
|
public void input(byte[] buffer) {
|
||||||
// Arduino > [Server] > Client
|
// Arduino > [Server] > Client
|
||||||
try {
|
try {
|
||||||
duplex.send(buffer);
|
duplex.send(buffer);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("", e);
|
logger.error("", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,9 @@ package com.github.boukefalos.arduino.exception;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class ArduinoException extends IOException {
|
public class ArduinoException extends IOException {
|
||||||
protected static final long serialVersionUID = 1L;
|
protected static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public ArduinoException(String message) {
|
public ArduinoException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,36 +10,35 @@ import com.github.boukefalos.arduino.exception.ArduinoException;
|
|||||||
import com.github.boukefalos.arduino.port.Port;
|
import com.github.boukefalos.arduino.port.Port;
|
||||||
|
|
||||||
public class Local extends AbstractArduino {
|
public class Local extends AbstractArduino {
|
||||||
protected Port arduino;
|
protected Port arduino;
|
||||||
protected OutputStream outputStream;
|
protected OutputStream outputStream;
|
||||||
|
|
||||||
public Local() throws Exception {
|
public Local() throws Exception {
|
||||||
this(Port.getInstance());
|
this(Port.getInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Local(Port arduino) throws ArduinoException {
|
public Local(Port arduino) throws ArduinoException {
|
||||||
this.arduino = arduino;
|
this.arduino = arduino;
|
||||||
outputStream = arduino.getOutputStream();
|
outputStream = arduino.getOutputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(Listen<Object> listen) {
|
public void register(Listen<Object> listen) {
|
||||||
arduino.register(listen);
|
arduino.register(listen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(Listen<Object> listen) {
|
public void remove(Listen<Object> listen) {
|
||||||
arduino.remove(listen);
|
arduino.remove(listen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
arduino.close();
|
arduino.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(byte[] buffer) throws ArduinoException {
|
public void send(byte[] buffer) throws ArduinoException {
|
||||||
try {
|
try {
|
||||||
outputStream.write(buffer);
|
outputStream.write(buffer);
|
||||||
outputStream.flush();
|
} catch (IOException e) {
|
||||||
} catch (IOException e) {
|
throw new ArduinoException("Failed to write to arduino");
|
||||||
throw new ArduinoException("Failed to write to arduino");
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,49 +11,49 @@ import com.github.boukefalos.arduino.AbstractArduino;
|
|||||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||||
|
|
||||||
public class Remote extends AbstractArduino implements Receiver {
|
public class Remote extends AbstractArduino implements Receiver {
|
||||||
protected Duplex duplex;
|
protected Duplex duplex;
|
||||||
|
|
||||||
public Remote(Duplex duplex) {
|
public Remote(Duplex duplex) {
|
||||||
this.duplex = duplex;
|
this.duplex = duplex;
|
||||||
listenList = new ArrayList<Listen<Object>>();
|
listenList = new ArrayList<Listen<Object>>();
|
||||||
duplex.register(this); // Server > [receive()]
|
duplex.register(this); // Server > [receive()]
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
duplex.start();
|
duplex.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
duplex.stop();
|
duplex.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void exit() {
|
public void exit() {
|
||||||
super.stop();
|
super.stop();
|
||||||
duplex.exit();
|
duplex.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(Listen<Object> listen) {
|
public void register(Listen<Object> listen) {
|
||||||
listenList.add(listen);
|
listenList.add(listen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(Listen<Object> listen) {
|
public void remove(Listen<Object> listen) {
|
||||||
listenList.remove(listen);
|
listenList.remove(listen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receive(byte[] buffer) {
|
public void receive(byte[] buffer) {
|
||||||
// Arduino > Server > [Client]
|
// Arduino > Server > [Client]
|
||||||
// Should decode here?
|
// Should decode here?
|
||||||
for (Listen<Object> listen : listenList) {
|
for (Listen<Object> listen : listenList) {
|
||||||
listen.add(buffer);
|
listen.add(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(byte[] buffer) throws ArduinoException {
|
public void send(byte[] buffer) throws ArduinoException {
|
||||||
// [Client] > Server > Arduino
|
// [Client] > Server > Arduino
|
||||||
try {
|
try {
|
||||||
duplex.send(buffer);
|
duplex.send(buffer);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ArduinoException("Failed to send");
|
throw new ArduinoException("Failed to send");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,26 +7,26 @@ import purejavacomm.SerialPortEvent;
|
|||||||
import base.work.Listen;
|
import base.work.Listen;
|
||||||
|
|
||||||
public class ParsingPort extends Port {
|
public class ParsingPort extends Port {
|
||||||
protected Class<?> messageClass;
|
protected Class<?> messageClass;
|
||||||
|
|
||||||
protected ParsingPort(Class<?> messageClass) {
|
protected ParsingPort(Class<?> messageClass) {
|
||||||
this.messageClass = messageClass;
|
this.messageClass = messageClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Port getInstance(Class<?> messageClass) {
|
public static Port getInstance(Class<?> messageClass) {
|
||||||
if (port == null) {
|
if (port == null) {
|
||||||
port = new ParsingPort(messageClass);
|
port = new ParsingPort(messageClass);
|
||||||
}
|
}
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void serialEvent(SerialPortEvent event) {
|
public void serialEvent(SerialPortEvent event) {
|
||||||
try {
|
try {
|
||||||
Method method = messageClass.getMethod("parseDelimitedFrom", InputStream.class);
|
Method method = messageClass.getMethod("parseDelimitedFrom", InputStream.class);
|
||||||
Object object = method.invoke(null, inputStream);
|
Object object = method.invoke(null, inputStream);
|
||||||
for (Listen<Object> listen : listenList) {
|
for (Listen<Object> listen : listenList) {
|
||||||
listen.add(object);
|
listen.add(object);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {} catch (Throwable e) {} finally {}
|
} catch (Exception e) {} catch (Throwable e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import base.work.Listen;
|
|||||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||||
|
|
||||||
public class Port implements SerialPortEventListener {
|
public class Port implements SerialPortEventListener {
|
||||||
public static final int BUFFER_SIZE = 1024;
|
public static final int BUFFER_SIZE = 1024;
|
||||||
public static final int TIME_OUT = 1000;
|
public static final int TIME_OUT = 1000;
|
||||||
public static final String PORT_NAMES[] = {
|
public static final String PORT_NAMES[] = {
|
||||||
"tty.usbmodem", // Mac OS X
|
"tty.usbmodem", // Mac OS X
|
||||||
@@ -31,10 +31,10 @@ public class Port implements SerialPortEventListener {
|
|||||||
"COM3", // Windows
|
"COM3", // Windows
|
||||||
};
|
};
|
||||||
|
|
||||||
protected static Logger logger = LoggerFactory.getLogger(Port.class);
|
protected static Logger logger = LoggerFactory.getLogger(Port.class);
|
||||||
protected static Port port;
|
protected static Port port;
|
||||||
|
|
||||||
protected int bufferSize;
|
protected int bufferSize;
|
||||||
protected SerialPort serialPort = null;
|
protected SerialPort serialPort = null;
|
||||||
protected InputStream inputStream = null;
|
protected InputStream inputStream = null;
|
||||||
protected ArrayList<Listen<Object>> listenList = new ArrayList<Listen<Object>>();
|
protected ArrayList<Listen<Object>> listenList = new ArrayList<Listen<Object>>();
|
||||||
@@ -42,26 +42,26 @@ public class Port implements SerialPortEventListener {
|
|||||||
protected Port() {}
|
protected Port() {}
|
||||||
|
|
||||||
protected Port(int bufferSize) {
|
protected Port(int bufferSize) {
|
||||||
this.bufferSize = bufferSize;
|
this.bufferSize = bufferSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(Listen<Object> listen) {
|
public void register(Listen<Object> listen) {
|
||||||
listenList.add(listen);
|
listenList.add(listen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(Listen<Object> listen) {
|
public void remove(Listen<Object> listen) {
|
||||||
listenList.remove(listen);
|
listenList.remove(listen);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Port getInstance() {
|
public static Port getInstance() {
|
||||||
return getInstance(BUFFER_SIZE);
|
return getInstance(BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Port getInstance(int bufferSize) {
|
public static Port getInstance(int bufferSize) {
|
||||||
if (port == null) {
|
if (port == null) {
|
||||||
port = new Port(bufferSize);
|
port = new Port(bufferSize);
|
||||||
}
|
}
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void connect() throws ArduinoException {
|
protected void connect() throws ArduinoException {
|
||||||
@@ -74,17 +74,17 @@ public class Port implements SerialPortEventListener {
|
|||||||
for ( String portName: PORT_NAMES) {
|
for ( String portName: PORT_NAMES) {
|
||||||
if (portid.getName().equals(portName) || portid.getName().contains(portName)) {
|
if (portid.getName().equals(portName) || portid.getName().contains(portName)) {
|
||||||
try {
|
try {
|
||||||
serialPort = (SerialPort) portid.open("", TIME_OUT);
|
serialPort = (SerialPort) portid.open("", TIME_OUT);
|
||||||
serialPort.setSerialPortParams(19200, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
|
serialPort.setSerialPortParams(4800, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
|
||||||
serialPort.setFlowControlMode(
|
serialPort.setFlowControlMode(
|
||||||
SerialPort.FLOWCONTROL_XONXOFF_IN +
|
SerialPort.FLOWCONTROL_XONXOFF_IN +
|
||||||
SerialPort.FLOWCONTROL_XONXOFF_OUT);
|
SerialPort.FLOWCONTROL_XONXOFF_OUT);
|
||||||
inputStream = serialPort.getInputStream();
|
inputStream = serialPort.getInputStream();
|
||||||
System.out.println("Connected on port: " + portid.getName());
|
System.out.println("Connected on port: " + portid.getName());
|
||||||
serialPort.addEventListener(this);
|
serialPort.addEventListener(this);
|
||||||
} catch (UnsupportedCommOperationException | PortInUseException | IOException | TooManyListenersException e) {
|
} catch (UnsupportedCommOperationException | PortInUseException | IOException | TooManyListenersException e) {
|
||||||
throw new ArduinoException("Failed to connect");
|
throw new ArduinoException("Failed to connect");
|
||||||
}
|
}
|
||||||
serialPort.notifyOnDataAvailable(true);
|
serialPort.notifyOnDataAvailable(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -95,46 +95,46 @@ public class Port implements SerialPortEventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void serialEvent(SerialPortEvent event) {
|
public void serialEvent(SerialPortEvent event) {
|
||||||
switch (event.getEventType()) {
|
switch (event.getEventType()) {
|
||||||
case SerialPortEvent.DATA_AVAILABLE:
|
case SerialPortEvent.DATA_AVAILABLE:
|
||||||
byte[] buffer = new byte[bufferSize];
|
byte[] buffer = new byte[bufferSize];
|
||||||
try {
|
try {
|
||||||
inputStream.read(buffer);
|
inputStream.read(buffer);
|
||||||
for (Listen<Object> listen : listenList) {
|
for (Listen<Object> listen : listenList) {
|
||||||
listen.add(buffer);
|
listen.add(buffer);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("", e);
|
logger.error("", e);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() throws ArduinoException {
|
public InputStream getInputStream() throws ArduinoException {
|
||||||
if (serialPort == null) {
|
if (serialPort == null) {
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return serialPort.getInputStream();
|
return serialPort.getInputStream();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ArduinoException("Failed to get inputstream");
|
throw new ArduinoException("Failed to get inputstream");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutputStream getOutputStream() throws ArduinoException {
|
public OutputStream getOutputStream() throws ArduinoException {
|
||||||
if (serialPort == null) {
|
if (serialPort == null) {
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return serialPort.getOutputStream();
|
return serialPort.getOutputStream();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ArduinoException("Failed to get inputstream");
|
throw new ArduinoException("Failed to get inputstream");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
serialPort.close();
|
serialPort.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.github.boukefalos.arduino.port;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import purejavacomm.SerialPortEvent;
|
||||||
|
import base.work.Listen;
|
||||||
|
|
||||||
|
public class StringPort extends Port {
|
||||||
|
public static Port getInstance() {
|
||||||
|
if (port == null) {
|
||||||
|
port = new StringPort();
|
||||||
|
}
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void serialEvent(SerialPortEvent event) {
|
||||||
|
Scanner scanner = new Scanner(inputStream);
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
for (Listen<Object> listen : listenList) {
|
||||||
|
listen.add(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user