diff --git a/src/main/java/com/github/boukefalos/arduino/AbstractArduino.java b/src/main/java/com/github/boukefalos/arduino/AbstractArduino.java index cc7a36d..5574d00 100644 --- a/src/main/java/com/github/boukefalos/arduino/AbstractArduino.java +++ b/src/main/java/com/github/boukefalos/arduino/AbstractArduino.java @@ -9,19 +9,28 @@ import base.work.Listen; import base.worker.Worker; public abstract class AbstractArduino extends Listen implements Arduino { - public static final int BUFFER_SIZE = 1024; - protected Logger logger = LoggerFactory.getLogger(getClass()); - protected ArrayList> listenList; + public static final int BUFFER_SIZE = 1024; + protected Logger logger = LoggerFactory.getLogger(getClass()); + protected ArrayList> listenList; - public AbstractArduino() { - super(Worker.Type.DIRECT); - } + public AbstractArduino() { + super(Worker.Type.DIRECT); + listenList = new ArrayList>(); + } - public void start() {} + public void register(Listen listen) { + listenList.add(listen); + } - public void stop() {} + public void remove(Listen listen) { + listenList.remove(listen); + } - public void exit() { - stop(); - } + public void start() {} + + public void stop() {} + + public void exit() { + stop(); + } } diff --git a/src/main/java/com/github/boukefalos/arduino/Arduino.java b/src/main/java/com/github/boukefalos/arduino/Arduino.java index 4ac9a45..f059b5c 100644 --- a/src/main/java/com/github/boukefalos/arduino/Arduino.java +++ b/src/main/java/com/github/boukefalos/arduino/Arduino.java @@ -6,8 +6,8 @@ import base.work.Listen; import com.github.boukefalos.arduino.exception.ArduinoException; public interface Arduino extends Control { - public void register(Listen listen); - public void remove(Listen listen); + public void register(Listen listen); + public void remove(Listen listen); - public void send(byte[] buffer) throws ArduinoException; + public void send(byte[] buffer) throws ArduinoException; } \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/arduino/Loader.java b/src/main/java/com/github/boukefalos/arduino/Loader.java index 8b7f271..6fb7e3f 100644 --- a/src/main/java/com/github/boukefalos/arduino/Loader.java +++ b/src/main/java/com/github/boukefalos/arduino/Loader.java @@ -18,64 +18,64 @@ public class Loader extends AbstractLoader { protected static final String PROPERTIES_FILE = "arduino.properties"; 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 { - /* Add implementation */ - switch (properties.getProperty("implementation")) { - case "local": - pico.addComponent(localClass); - break; - case "remote": - pico.addComponent(remoteClass); + public Loader(Class localClass, Class remoteClass, Class serverClass, Properties properties) throws LoaderException { + /* Add implementation */ + switch (properties.getProperty("implementation")) { + case "local": + pico.addComponent(localClass); + break; + case "remote": + pico.addComponent(remoteClass); - /* Add remote duplex implementation */ - try { - String protocol = properties.getOrDefault("protocol", "tcp").toString(); - String implementation = properties.getOrDefault("tcp.implementation", "socket").toString(); - String host = properties.getProperty("remote.host"); - int port = Integer.valueOf(properties.getProperty("remote.port")); - addClientDuplex(protocol, implementation, host, port); - } catch (NumberFormatException e) { - throw new LoaderException("Failed to parse remote.port"); - } - break; - } + /* Add remote duplex implementation */ + try { + String protocol = properties.getOrDefault("protocol", "tcp").toString(); + String implementation = properties.getOrDefault("tcp.implementation", "socket").toString(); + String host = properties.getProperty("remote.host"); + int port = Integer.valueOf(properties.getProperty("remote.port")); + addClientDuplex(protocol, implementation, host, port); + } catch (NumberFormatException e) { + throw new LoaderException("Failed to parse remote.port"); + } + break; + } - /* Add server */ - if (properties.getProperty("server") != null) { - boolean direct = Boolean.parseBoolean(properties.getOrDefault("server.direct", Server.DIRECT).toString()); - pico.addComponent(serverClass, serverClass, new Parameter[] { - new ComponentParameter(), - new ComponentParameter(), - new ConstantParameter(direct)}); + /* Add server */ + if (properties.getProperty("server") != null) { + boolean direct = Boolean.parseBoolean(properties.getOrDefault("server.direct", Server.DIRECT).toString()); + pico.addComponent(serverClass, serverClass, new Parameter[] { + new ComponentParameter(), + new ComponentParameter(), + new ConstantParameter(direct)}); - /* Add server forwarder implementation */ - try { - String protocol = properties.getOrDefault("server.protocol", "tcp").toString(); - String implementation = properties.getOrDefault("tcp.implementation", "socket").toString(); - int port = Integer.valueOf(properties.getProperty("server.port")); - addServerDuplex(protocol, implementation, port); - } catch (NumberFormatException e) { - throw new LoaderException("Failed to parse server.port"); - } - } - } + /* Add server forwarder implementation */ + try { + String protocol = properties.getOrDefault("server.protocol", "tcp").toString(); + String implementation = properties.getOrDefault("tcp.implementation", "socket").toString(); + int port = Integer.valueOf(properties.getProperty("server.port")); + addServerDuplex(protocol, implementation, port); + } catch (NumberFormatException e) { + throw new LoaderException("Failed to parse server.port"); + } + } + } - public Arduino getArduino() throws ArduinoException { - try { - return (Arduino) pico.getComponent(Arduino.class); - } catch (PicoCompositionException e) { - throw new ArduinoException("Failed to load"); - } + public Arduino getArduino() throws ArduinoException { + try { + return (Arduino) pico.getComponent(Arduino.class); + } catch (PicoCompositionException e) { + throw new ArduinoException("Failed to load"); + } } public Server getServer() throws ArduinoException { - try { - return pico.getComponent(Server.class); - } catch (PicoCompositionException e) { - throw new ArduinoException("Failed to load"); - } + try { + return pico.getComponent(Server.class); + } catch (PicoCompositionException e) { + throw new ArduinoException("Failed to load"); + } } } diff --git a/src/main/java/com/github/boukefalos/arduino/Server.java b/src/main/java/com/github/boukefalos/arduino/Server.java index 5867927..55e0cb5 100644 --- a/src/main/java/com/github/boukefalos/arduino/Server.java +++ b/src/main/java/com/github/boukefalos/arduino/Server.java @@ -12,53 +12,53 @@ import base.work.Listen; import com.github.boukefalos.arduino.exception.ArduinoException; public class Server extends Listen implements Control, Receiver { - protected static final boolean DIRECT = false; + protected static final boolean DIRECT = false; protected Arduino arduino; - protected Duplex duplex; - protected boolean direct; + protected Duplex duplex; + protected boolean direct; - public Server(Arduino arduino, Duplex duplex) { - this(arduino, duplex, DIRECT); - } + public Server(Arduino arduino, Duplex duplex) { + this(arduino, duplex, DIRECT); + } - public Server(Arduino tm1638, Duplex duplex, boolean direct) { - this.arduino = tm1638; - this.duplex = duplex; - this.direct = direct; - arduino.register(this); // Arduino > [input()] - duplex.register(this); // Client > [receive()] - } + public Server(Arduino tm1638, Duplex duplex, boolean direct) { + this.arduino = tm1638; + this.duplex = duplex; + this.direct = direct; + arduino.register(this); // Arduino > [input()] + duplex.register(this); // Client > [receive()] + } - public void activate() throws ActivateException { - duplex.start(); - super.activate(); - } - - public void deactivate() throws DeactivateException { - duplex.stop(); - super.deactivate(); - } + public void activate() throws ActivateException { + duplex.start(); + super.activate(); + } + + public void deactivate() throws DeactivateException { + duplex.stop(); + super.deactivate(); + } - public void receive(byte[] buffer) { - // Client > [Server] > Arduino - if (direct) { - try { - arduino.send(buffer); - } catch (ArduinoException e) { - logger.error("", e); - } - } else { - // option to decode() in derivatives? - } - } + public void receive(byte[] buffer) { + // Client > [Server] > Arduino + if (direct) { + try { + arduino.send(buffer); + } catch (ArduinoException e) { + logger.error("", e); + } + } else { + // option to decode() in derivatives? + } + } - public void input(byte[] buffer) { - // Arduino > [Server] > Client - try { - duplex.send(buffer); - } catch (IOException e) { - logger.error("", e); - } - } + public void input(byte[] buffer) { + // Arduino > [Server] > Client + try { + duplex.send(buffer); + } catch (IOException e) { + logger.error("", e); + } + } } \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/arduino/exception/ArduinoException.java b/src/main/java/com/github/boukefalos/arduino/exception/ArduinoException.java index 440d0df..3749a49 100644 --- a/src/main/java/com/github/boukefalos/arduino/exception/ArduinoException.java +++ b/src/main/java/com/github/boukefalos/arduino/exception/ArduinoException.java @@ -3,9 +3,9 @@ package com.github.boukefalos.arduino.exception; import java.io.IOException; public class ArduinoException extends IOException { - protected static final long serialVersionUID = 1L; - - public ArduinoException(String message) { - super(message); - } + protected static final long serialVersionUID = 1L; + + public ArduinoException(String message) { + super(message); + } } diff --git a/src/main/java/com/github/boukefalos/arduino/implementation/Local.java b/src/main/java/com/github/boukefalos/arduino/implementation/Local.java index 4984f46..62a0381 100644 --- a/src/main/java/com/github/boukefalos/arduino/implementation/Local.java +++ b/src/main/java/com/github/boukefalos/arduino/implementation/Local.java @@ -10,36 +10,35 @@ 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; + protected Port arduino; + protected OutputStream outputStream; - public Local() throws Exception { - this(Port.getInstance()); - } + public Local() throws Exception { + this(Port.getInstance()); + } - public Local(Port arduino) throws ArduinoException { - this.arduino = arduino; - outputStream = arduino.getOutputStream(); - } + public Local(Port arduino) throws ArduinoException { + this.arduino = arduino; + outputStream = arduino.getOutputStream(); + } - public void register(Listen listen) { - arduino.register(listen); - } + public void register(Listen listen) { + arduino.register(listen); + } - public void remove(Listen listen) { - arduino.remove(listen); - } + public void remove(Listen listen) { + arduino.remove(listen); + } - public void stop() { - arduino.close(); - } + public void stop() { + arduino.close(); + } - public void send(byte[] buffer) throws ArduinoException { - try { - outputStream.write(buffer); - outputStream.flush(); - } catch (IOException e) { - throw new ArduinoException("Failed to write to arduino"); - } - } + public void send(byte[] buffer) throws ArduinoException { + try { + outputStream.write(buffer); + } catch (IOException e) { + throw new ArduinoException("Failed to write to arduino"); + } + } } diff --git a/src/main/java/com/github/boukefalos/arduino/implementation/Remote.java b/src/main/java/com/github/boukefalos/arduino/implementation/Remote.java index fab5a44..470440d 100644 --- a/src/main/java/com/github/boukefalos/arduino/implementation/Remote.java +++ b/src/main/java/com/github/boukefalos/arduino/implementation/Remote.java @@ -11,49 +11,49 @@ import com.github.boukefalos.arduino.AbstractArduino; import com.github.boukefalos.arduino.exception.ArduinoException; public class Remote extends AbstractArduino implements Receiver { - protected Duplex duplex; + protected Duplex duplex; - public Remote(Duplex duplex) { - this.duplex = duplex; - listenList = new ArrayList>(); - duplex.register(this); // Server > [receive()] - } + public Remote(Duplex duplex) { + this.duplex = duplex; + listenList = new ArrayList>(); + duplex.register(this); // Server > [receive()] + } - public void start() { - duplex.start(); - } + public void start() { + duplex.start(); + } - public void stop() { - duplex.stop(); - } + public void stop() { + duplex.stop(); + } - public void exit() { - super.stop(); - duplex.exit(); - } + public void exit() { + super.stop(); + duplex.exit(); + } - public void register(Listen listen) { - listenList.add(listen); - } + public void register(Listen listen) { + listenList.add(listen); + } - public void remove(Listen listen) { - listenList.remove(listen); - } + public void remove(Listen listen) { + listenList.remove(listen); + } - public void receive(byte[] buffer) { - // Arduino > Server > [Client] - // Should decode here? - for (Listen listen : listenList) { - listen.add(buffer); - } - } + public void receive(byte[] buffer) { + // Arduino > Server > [Client] + // Should decode here? + for (Listen 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"); - } - } + public void send(byte[] buffer) throws ArduinoException { + // [Client] > Server > Arduino + try { + duplex.send(buffer); + } catch (IOException e) { + throw new ArduinoException("Failed to send"); + } + } } \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/arduino/port/ParsingPort.java b/src/main/java/com/github/boukefalos/arduino/port/ParsingPort.java index 525f934..f2ddeb7 100644 --- a/src/main/java/com/github/boukefalos/arduino/port/ParsingPort.java +++ b/src/main/java/com/github/boukefalos/arduino/port/ParsingPort.java @@ -7,26 +7,26 @@ import purejavacomm.SerialPortEvent; import base.work.Listen; public class ParsingPort extends Port { - protected Class messageClass; + protected Class messageClass; - protected ParsingPort(Class messageClass) { - this.messageClass = messageClass; - } + protected ParsingPort(Class messageClass) { + this.messageClass = messageClass; + } - public static Port getInstance(Class messageClass) { - if (port == null) { - port = new ParsingPort(messageClass); - } - return port; - } + public static Port getInstance(Class messageClass) { + if (port == null) { + port = new ParsingPort(messageClass); + } + return port; + } - public void serialEvent(SerialPortEvent event) { - try { - Method method = messageClass.getMethod("parseDelimitedFrom", InputStream.class); - Object object = method.invoke(null, inputStream); - for (Listen listen : listenList) { - listen.add(object); - } - } catch (Exception e) {} catch (Throwable e) {} finally {} - } + public void serialEvent(SerialPortEvent event) { + try { + Method method = messageClass.getMethod("parseDelimitedFrom", InputStream.class); + Object object = method.invoke(null, inputStream); + for (Listen listen : listenList) { + listen.add(object); + } + } catch (Exception e) {} catch (Throwable e) {} + } } diff --git a/src/main/java/com/github/boukefalos/arduino/port/Port.java b/src/main/java/com/github/boukefalos/arduino/port/Port.java index cd1c001..2708aaa 100644 --- a/src/main/java/com/github/boukefalos/arduino/port/Port.java +++ b/src/main/java/com/github/boukefalos/arduino/port/Port.java @@ -21,7 +21,7 @@ import base.work.Listen; import com.github.boukefalos.arduino.exception.ArduinoException; 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 String PORT_NAMES[] = { "tty.usbmodem", // Mac OS X @@ -31,10 +31,10 @@ public class Port implements SerialPortEventListener { "COM3", // Windows }; - protected static Logger logger = LoggerFactory.getLogger(Port.class); - protected static Port port; + protected static Logger logger = LoggerFactory.getLogger(Port.class); + protected static Port port; - protected int bufferSize; + protected int bufferSize; protected SerialPort serialPort = null; protected InputStream inputStream = null; protected ArrayList> listenList = new ArrayList>(); @@ -42,26 +42,26 @@ public class Port implements SerialPortEventListener { protected Port() {} protected Port(int bufferSize) { - this.bufferSize = bufferSize; + this.bufferSize = bufferSize; } - public void register(Listen listen) { + public void register(Listen listen) { listenList.add(listen); - } + } - public void remove(Listen listen) { - listenList.remove(listen); - } + public void remove(Listen listen) { + listenList.remove(listen); + } public static Port getInstance() { - return getInstance(BUFFER_SIZE); + return getInstance(BUFFER_SIZE); } public static Port getInstance(int bufferSize) { - if (port == null) { - port = new Port(bufferSize); - } - return port; + if (port == null) { + port = new Port(bufferSize); + } + return port; } protected void connect() throws ArduinoException { @@ -74,17 +74,17 @@ public class Port implements SerialPortEventListener { for ( String portName: PORT_NAMES) { if (portid.getName().equals(portName) || portid.getName().contains(portName)) { try { - serialPort = (SerialPort) portid.open("", TIME_OUT); - serialPort.setSerialPortParams(19200, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); - serialPort.setFlowControlMode( - SerialPort.FLOWCONTROL_XONXOFF_IN + - SerialPort.FLOWCONTROL_XONXOFF_OUT); - inputStream = serialPort.getInputStream(); - System.out.println("Connected on port: " + portid.getName()); - serialPort.addEventListener(this); - } catch (UnsupportedCommOperationException | PortInUseException | IOException | TooManyListenersException e) { - throw new ArduinoException("Failed to connect"); - } + serialPort = (SerialPort) portid.open("", TIME_OUT); + serialPort.setSerialPortParams(4800, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); + serialPort.setFlowControlMode( + SerialPort.FLOWCONTROL_XONXOFF_IN + + SerialPort.FLOWCONTROL_XONXOFF_OUT); + inputStream = serialPort.getInputStream(); + System.out.println("Connected on port: " + portid.getName()); + serialPort.addEventListener(this); + } catch (UnsupportedCommOperationException | PortInUseException | IOException | TooManyListenersException e) { + throw new ArduinoException("Failed to connect"); + } serialPort.notifyOnDataAvailable(true); return; } @@ -95,46 +95,46 @@ public class Port implements SerialPortEventListener { } public void serialEvent(SerialPortEvent event) { - switch (event.getEventType()) { - case SerialPortEvent.DATA_AVAILABLE: - byte[] buffer = new byte[bufferSize]; - try { - inputStream.read(buffer); - for (Listen listen : listenList) { - listen.add(buffer); - } - } catch (IOException e) { - logger.error("", e); - } - break; - default: - break; - } + switch (event.getEventType()) { + case SerialPortEvent.DATA_AVAILABLE: + byte[] buffer = new byte[bufferSize]; + try { + inputStream.read(buffer); + for (Listen listen : listenList) { + listen.add(buffer); + } + } catch (IOException e) { + logger.error("", e); + } + break; + default: + break; + } } - public InputStream getInputStream() throws ArduinoException { - if (serialPort == null) { - connect(); - } - try { - return serialPort.getInputStream(); - } catch (IOException e) { - throw new ArduinoException("Failed to get inputstream"); - } - } + public InputStream getInputStream() throws ArduinoException { + if (serialPort == null) { + connect(); + } + try { + return serialPort.getInputStream(); + } catch (IOException e) { + throw new ArduinoException("Failed to get inputstream"); + } + } - public OutputStream getOutputStream() throws ArduinoException { - if (serialPort == null) { - connect(); - } - try { - return serialPort.getOutputStream(); - } catch (IOException e) { - throw new ArduinoException("Failed to get inputstream"); - } - } + public OutputStream getOutputStream() throws ArduinoException { + if (serialPort == null) { + connect(); + } + try { + return serialPort.getOutputStream(); + } catch (IOException e) { + throw new ArduinoException("Failed to get inputstream"); + } + } - public void close() { - serialPort.close(); - } + public void close() { + serialPort.close(); + } } \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/arduino/port/StringPort.java b/src/main/java/com/github/boukefalos/arduino/port/StringPort.java new file mode 100644 index 0000000..c540fbf --- /dev/null +++ b/src/main/java/com/github/boukefalos/arduino/port/StringPort.java @@ -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 listen : listenList) { + listen.add(line); + } + } + scanner.close(); + } +}