Snapshot, replace tabs with spaces

This commit is contained in:
2015-09-01 13:19:40 +01:00
parent 394a413691
commit 46b377bf9a
10 changed files with 291 additions and 257 deletions

View File

@@ -9,19 +9,28 @@ import base.work.Listen;
import base.worker.Worker;
public abstract class AbstractArduino extends Listen<Object> implements Arduino {
public static final int BUFFER_SIZE = 1024;
protected Logger logger = LoggerFactory.getLogger(getClass());
protected ArrayList<Listen<Object>> listenList;
public static final int BUFFER_SIZE = 1024;
protected Logger logger = LoggerFactory.getLogger(getClass());
protected ArrayList<Listen<Object>> listenList;
public AbstractArduino() {
super(Worker.Type.DIRECT);
}
public AbstractArduino() {
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() {
stop();
}
public void start() {}
public void stop() {}
public void exit() {
stop();
}
}

View File

@@ -6,8 +6,8 @@ import base.work.Listen;
import com.github.boukefalos.arduino.exception.ArduinoException;
public interface Arduino extends Control {
public void register(Listen<Object> listen);
public void remove(Listen<Object> listen);
public void register(Listen<Object> listen);
public void remove(Listen<Object> listen);
public void send(byte[] buffer) throws ArduinoException;
public void send(byte[] buffer) throws ArduinoException;
}

View File

@@ -18,64 +18,64 @@ public class Loader extends AbstractLoader<Loader> {
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");
}
}
}

View File

@@ -12,53 +12,53 @@ import base.work.Listen;
import com.github.boukefalos.arduino.exception.ArduinoException;
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 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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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<Object> listen) {
arduino.register(listen);
}
public void register(Listen<Object> listen) {
arduino.register(listen);
}
public void remove(Listen<Object> listen) {
arduino.remove(listen);
}
public void remove(Listen<Object> 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");
}
}
}

View File

@@ -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<Listen<Object>>();
duplex.register(this); // Server > [receive()]
}
public Remote(Duplex duplex) {
this.duplex = duplex;
listenList = new ArrayList<Listen<Object>>();
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<Object> listen) {
listenList.add(listen);
}
public void register(Listen<Object> listen) {
listenList.add(listen);
}
public void remove(Listen<Object> listen) {
listenList.remove(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 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");
}
}
public void send(byte[] buffer) throws ArduinoException {
// [Client] > Server > Arduino
try {
duplex.send(buffer);
} catch (IOException e) {
throw new ArduinoException("Failed to send");
}
}
}

View File

@@ -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<Object> 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<Object> listen : listenList) {
listen.add(object);
}
} catch (Exception e) {} catch (Throwable e) {}
}
}

View File

@@ -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<Listen<Object>> listenList = new ArrayList<Listen<Object>>();
@@ -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<Object> listen) {
public void register(Listen<Object> listen) {
listenList.add(listen);
}
}
public void remove(Listen<Object> listen) {
listenList.remove(listen);
}
public void remove(Listen<Object> 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<Object> 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<Object> 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();
}
}

View File

@@ -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();
}
}