Reorganise project to implement new loader system for local/remote implementations

This commit is contained in:
2015-06-24 16:48:16 +01:00
parent ae7b8b21c4
commit 55d51fdcd2
25 changed files with 421 additions and 484 deletions

View File

@@ -1,4 +1,4 @@
package com.github.boukefalos.tm1638.helper;
package com.github.boukefalos.tm1638;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -8,31 +8,37 @@ import org.slf4j.LoggerFactory;
import tm1638.Tm1638.Color;
import tm1638.Tm1638.Command;
import tm1638.Tm1638.Command.Type;
import tm1638.Tm1638.Construct;
import tm1638.Tm1638.Ping;
import tm1638.Tm1638.Command.Type;
import tm1638.Tm1638.SetLed;
import base.sender.Sender;
import base.Sender;
public class SenderHelper {
public abstract class AbstractTM1638 implements TM1638, Sender {
public static final int BUFFER_SIZE = 1024;
protected static Logger logger = LoggerFactory.getLogger(SenderHelper.class);
protected Logger logger = LoggerFactory.getLogger(getClass());
public static void command(Sender sender, Command command) {
public void start() {}
public void stop() {}
public void exit() {
stop();
}
public void command(Command command) {
ByteArrayOutputStream output = new ByteArrayOutputStream(BUFFER_SIZE);
try {
command.writeDelimitedTo(output);
byte[] buffer = output.toByteArray();
System.out.println("command() " + new String(buffer).trim());
sender.send(buffer);
send(buffer);
} catch (IOException e) {
logger.error("Failed to send command");
}
}
public static void construct(Sender sender, int dataPin, int clockPin, int strobePin) {
command(sender, Command.newBuilder()
public void construct(int dataPin, int clockPin, int strobePin) {
command(Command.newBuilder()
.setType(Type.CONSTRUCT)
.setConstruct(
Construct.newBuilder()
@@ -41,20 +47,19 @@ public class SenderHelper {
.setStrobePin(strobePin).build()).build());
}
public static void ping(Sender sender, int id) {
command(sender, Command.newBuilder()
public void ping(int id) {
command(Command.newBuilder()
.setType(Type.PING)
.setPing(Ping.newBuilder()
.setId(id)).build());
}
public static void setLed(Sender sender, Color color, int pos) {
command(sender, Command.newBuilder()
public void setLed(Color color, int pos) {
command(Command.newBuilder()
.setType(Type.SET_LED)
.setSetLed(
SetLed.newBuilder()
.setColor(color)
.setPos(pos).build()).build());
}
}

View File

@@ -6,7 +6,8 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import com.github.boukefalos.tm1638.exception.ArduinoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import purejavacomm.CommPortIdentifier;
import purejavacomm.PortInUseException;
@@ -14,9 +15,13 @@ import purejavacomm.SerialPort;
import purejavacomm.SerialPortEvent;
import purejavacomm.SerialPortEventListener;
import purejavacomm.UnsupportedCommOperationException;
import beerduino.Beerduino.Echo;
import tm1638.Tm1638.Echo;
import base.work.Listen;
import com.github.boukefalos.tm1638.exception.ArduinoException;
public class Arduino implements SerialPortEventListener {
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
@@ -26,17 +31,33 @@ public class Arduino implements SerialPortEventListener {
"COM3", // Windows
};
protected static Logger logger = LoggerFactory.getLogger(Arduino.class);
protected static Arduino arduino;
protected int bufferSize;
protected SerialPort port = null;
protected InputStream inputStream = null;
protected ArrayList<EchoReceiver> echoReceiverList = new ArrayList<EchoReceiver>();
protected ArrayList<Listen<Object>> listenList = new ArrayList<Listen<Object>>();
private Arduino() {}
protected Arduino(int bufferSize) {
this.bufferSize = bufferSize;
}
public void register(Listen<Object> listen) {
listenList.add(listen);
}
public void remove(Listen<Object> listen) {
listenList.remove(listen);
}
public static Arduino getInstance() {
return getInstance(BUFFER_SIZE);
}
public static Arduino getInstance(int bufferSize) {
if (arduino == null) {
arduino = new Arduino();
arduino = new Arduino(bufferSize);
}
return arduino;
}
@@ -74,18 +95,19 @@ public class Arduino implements SerialPortEventListener {
try {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
// Where should this be parsed, or should byte[] be passed directly?
Echo echo = Echo.parseDelimitedFrom(inputStream);
for (EchoReceiver echoReceiver : echoReceiverList) {
echoReceiver.receive(echo);
System.err.println(echo.getMessage());
for (Listen<Object> listen : listenList) {
listen.add(echo);
}
break;
default:
break;
}
} catch (IOException e) {
e.printStackTrace();
logger.error("", e);
}
}
public InputStream getInputStream() throws ArduinoException {
@@ -110,14 +132,6 @@ public class Arduino implements SerialPortEventListener {
}
}
public void addReceiver(EchoReceiver receiver) {
echoReceiverList.add(receiver);
}
public void removeReceiver(EchoReceiver receiver) {
echoReceiverList.remove(receiver);
}
public void close() {
port.close();
}

View File

@@ -1,7 +0,0 @@
package com.github.boukefalos.tm1638;
import beerduino.Beerduino.Echo;
public interface EchoReceiver {
public void receive(Echo echo);
}

View File

@@ -3,71 +3,77 @@ package com.github.boukefalos.tm1638;
import java.util.Properties;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.parameters.ComponentParameter;
import org.picocontainer.parameters.ConstantParameter;
import base.exception.LoaderException;
import base.loader.AbstractLoader;
import base.work.Work;
import com.github.boukefalos.tm1638.implementation.Localmplementation;
import com.github.boukefalos.tm1638.implementation.TcpImplementation;
import com.github.boukefalos.tm1638.implementation.UdpImplementation;
import com.github.boukefalos.tm1638.server.TM1638Server;
import com.github.boukefalos.tm1638.server.TM1638TcpServer;
import com.github.boukefalos.tm1638.server.TM1638UdpServer;
import com.github.boukefalos.tm1638.exception.ArduinoException;
import com.github.boukefalos.tm1638.implementation.Local;
import com.github.boukefalos.tm1638.implementation.Remote;
public class Loader extends AbstractLoader {
public class Loader extends AbstractLoader<Loader> {
protected static final String PROPERTIES_FILE = "TM1638.properties";
public Loader(Properties properties) {
public Loader(Properties properties) throws LoaderException {
super();
/* Add implementation */
switch (properties.getProperty("implementation")) {
case "local":
pico.addComponent(Localmplementation.class);
pico.addComponent(TM1638.class, Local.class);
break;
case "remote":
//pico.addComponent(Remote.class);
break;
}
pico.addComponent(TM1638.class, Remote.class);
/* Add protocol */
if (properties.getProperty("protocol") != null) {
switch (properties.getProperty("protocol")) {
case "tcp":
pico.addComponent(TcpImplementation.class, TcpImplementation.class, new Parameter[]{
new ConstantParameter(properties.getProperty("remote.host")),
new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))});
break;
case "udp":
pico.addComponent(UdpImplementation.class, UdpImplementation.class, new Parameter[] {
new ConstantParameter(properties.getProperty("remote.host")),
new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))});
break;
/* Add remote duplex implementation */
try {
String protocol = properties.getOrDefault("server.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) {
switch (properties.getProperty("server.protocol")) {
case "tcp":
pico.addComponent(TM1638TcpServer.class, TM1638TcpServer.class, new Parameter[]{
new ConstantParameter(getTM1638()),
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))});
break;
case "udp":
pico.addComponent(TM1638UdpServer.class, TM1638UdpServer.class, new Parameter[]{
new ConstantParameter(getTM1638()),
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))});
boolean direct = Boolean.parseBoolean(properties.getOrDefault("server.direct", Server.DIRECT).toString());
pico.addComponent(Server.class, Server.class, 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");
}
}
}
public TM1638 getTM1638() {
public TM1638 getTM1638() throws ArduinoException {
try {
return pico.getComponent(TM1638.class);
} catch (PicoCompositionException e) {
throw new ArduinoException("Failed to load");
}
}
public Work getServer() {
return (Work) pico.getComponent(TM1638Server.class);
public Server getServer() throws ArduinoException {
try {
return pico.getComponent(Server.class);
} catch (PicoCompositionException e) {
throw new ArduinoException("Failed to load");
}
}
}

View File

@@ -0,0 +1,96 @@
package com.github.boukefalos.tm1638;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import tm1638.Tm1638.Color;
import tm1638.Tm1638.Command;
import tm1638.Tm1638.Ping;
import tm1638.Tm1638.SetLed;
import base.Control;
import base.Duplex;
import base.Receiver;
import base.exception.worker.ActivateException;
import base.exception.worker.DeactivateException;
import base.work.Listen;
import com.github.boukefalos.tm1638.exception.ArduinoException;
public class Server extends Listen<Object> implements Control, Receiver {
protected static final boolean DIRECT = false;
protected TM1638 tm1638;
protected Duplex duplex;
protected boolean direct;
public Server(TM1638 tm1638, Duplex duplex) {
this(tm1638, duplex, DIRECT);
}
public Server(TM1638 tm1638, Duplex duplex, boolean direct) {
this.tm1638 = tm1638;
this.duplex = duplex;
this.direct = direct;
tm1638.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 receive(byte[] buffer) {
// Client > [Server] > Arduino
if (direct) {
try {
tm1638.send(buffer);
} catch (ArduinoException e) {
logger.error("", e);
}
} else {
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
try {
Command command = Command.parseDelimitedFrom(input);
logger.debug("Command type = " + command.getType().name());
switch (command.getType()) {
case PING:
Ping ping = command.getPing();
tm1638.ping(ping.getId());
break;
case SET_LED:
SetLed setLed = command.getSetLed();
logger.debug("Color = " + setLed.getColor().name());
switch (setLed.getColor()) {
case RED:
tm1638.setLed(Color.RED, 1);
case GREEN:
tm1638.setLed(Color.GREEN, 1);
default:
break;
}
break;
default:
break;
}
} catch (IOException e) {
logger.error("Failed to parse input");
return;
}
}
}
public void input(byte[] buffer) {
// Arduino > [Server] > Client
try {
duplex.send(buffer);
} catch (IOException e) {
logger.error("", e);
}
}
}

View File

@@ -1,25 +1,18 @@
package com.github.boukefalos.tm1638;
import com.github.boukefalos.tm1638.helper.SenderHelper;
import com.github.boukefalos.tm1638.exception.ArduinoException;
import tm1638.Tm1638.Color;
import base.sender.Sender;
import base.Control;
import base.work.Listen;
public abstract class TM1638 implements Sender {
public void ping(int id) {
SenderHelper.ping(this, id);
}
public interface TM1638 extends Control {
public void register(Listen<Object> listen);
public void remove(Listen<Object> listen);
public void setLed(Color color, int pos) {
SenderHelper.setLed(this, color, pos);
}
public void send(byte[] buffer) throws ArduinoException;
public void construct(int dataPin, int clockPin, int strobePin) {
SenderHelper.construct(this, dataPin, clockPin, strobePin);
}
public void start() {}
public void stop() {}
public abstract void addReceiver(EchoReceiver receiver);
public void construct(int dataPin, int clockPin, int strobePin);
public void ping(int i);
public void setLed(Color color, int pos);
}

View File

@@ -1,7 +1,8 @@
package com.github.boukefalos.tm1638.exception;
public class ArduinoException extends Exception {
import java.io.IOException;
public class ArduinoException extends IOException {
protected static final long serialVersionUID = 1L;
public ArduinoException(String message) {

View File

@@ -1,20 +0,0 @@
package com.github.boukefalos.tm1638.helper;
import java.util.ArrayList;
import beerduino.Beerduino.Echo;
import com.github.boukefalos.tm1638.EchoReceiver;
import com.google.protobuf.InvalidProtocolBufferException;
public class ReceiverHelper {
public static void receive(ArrayList<EchoReceiver> echoReceiverList, byte[] buffer) {
try {
Echo echo = Echo.parseFrom(buffer);
for (EchoReceiver echoReceiver : echoReceiverList) {
echoReceiver.receive(echo);
}
} catch (InvalidProtocolBufferException e) {}
}
}

View File

@@ -1,56 +0,0 @@
package com.github.boukefalos.tm1638.helper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tm1638.Tm1638.Color;
import tm1638.Tm1638.Command;
import tm1638.Tm1638.Ping;
import tm1638.Tm1638.Server;
import tm1638.Tm1638.SetLed;
import com.github.boukefalos.tm1638.TM1638;
public class ServerHelper {
protected static Logger logger = LoggerFactory.getLogger(ServerHelper.class);
public static void receive(TM1638 tm1638, byte[] buffer) {
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
System.out.println("receive() " + new String(buffer).trim());
try {
//Command command = Command.parseFrom(buffer);
Command command = Command.parseDelimitedFrom(input);
logger.debug("Command type = " + command.getType().name());
switch (command.getType()) {
case SERVER:
Server server = command.getServer();
// setup new udp server connection, multicast?
break;
case PING:
Ping ping = command.getPing();
tm1638.ping(ping.getId());
break;
case SET_LED:
SetLed setLed = command.getSetLed();
logger.debug("Color = " + setLed.getColor().name());
switch (setLed.getColor()) {
case RED:
tm1638.setLed(Color.RED, 1);
case GREEN:
tm1638.setLed(Color.GREEN, 1);
default:
break;
}
break;
default:
break;
}
} catch (IOException e) {
logger.error("Failed to parse input");
return;
}
}
}

View File

@@ -0,0 +1,44 @@
package com.github.boukefalos.tm1638.implementation;
import java.io.IOException;
import java.io.OutputStream;
import base.work.Listen;
import com.github.boukefalos.tm1638.AbstractTM1638;
import com.github.boukefalos.tm1638.Arduino;
import com.github.boukefalos.tm1638.exception.ArduinoException;
public class Local extends AbstractTM1638 {
protected Arduino arduino;
protected OutputStream outputStream;
public Local() throws Exception {
this(Arduino.getInstance());
}
public Local(Arduino 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");
}
}
}

View File

@@ -1,34 +0,0 @@
package com.github.boukefalos.tm1638.implementation;
import java.io.IOException;
import java.io.OutputStream;
import com.github.boukefalos.tm1638.Arduino;
import com.github.boukefalos.tm1638.EchoReceiver;
import com.github.boukefalos.tm1638.TM1638;
public class Localmplementation extends TM1638 {
protected Arduino arduino;
protected OutputStream outputStream;
public Localmplementation() throws Exception {
this(Arduino.getInstance());
}
public Localmplementation(Arduino arduino) throws Exception {
this.arduino = arduino;
outputStream = arduino.getOutputStream();
}
public void send(byte[] buffer) throws IOException {
outputStream.write(buffer);
}
public void stop() {
arduino.close();
}
public void addReceiver(EchoReceiver receiver) {
arduino.addReceiver(receiver);
}
}

View File

@@ -0,0 +1,60 @@
package com.github.boukefalos.tm1638.implementation;
import java.io.IOException;
import java.util.ArrayList;
import base.Duplex;
import base.Receiver;
import base.work.Listen;
import com.github.boukefalos.tm1638.AbstractTM1638;
import com.github.boukefalos.tm1638.exception.ArduinoException;
public class Remote extends AbstractTM1638 implements Receiver {
protected Duplex duplex;
protected ArrayList<Listen<Object>> listenList;
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");
}
}
}

View File

@@ -1,37 +0,0 @@
package com.github.boukefalos.tm1638.implementation;
import java.io.IOException;
import java.util.ArrayList;
import base.receiver.Receiver;
import base.server.channel.TcpClient;
import com.github.boukefalos.tm1638.EchoReceiver;
import com.github.boukefalos.tm1638.TM1638;
import com.github.boukefalos.tm1638.helper.ReceiverHelper;
public class TcpImplementation extends TM1638 implements Receiver {
protected TcpClient client;
protected ArrayList<EchoReceiver> echoReceiverList = new ArrayList<EchoReceiver>();
public TcpImplementation(String host, int port) {
client = new TcpClient(host, port);
}
public void send(byte[] buffer) throws IOException {
if (!client.active()) {
client.start();
}
client.send(buffer);
}
public void addReceiver(EchoReceiver echoReceiver) {
client.register(this);
echoReceiverList.add(echoReceiver);
}
public void receive(byte[] buffer) {
System.out.println(new String(buffer).trim());
ReceiverHelper.receive(echoReceiverList, buffer);
}
}

View File

@@ -1,34 +0,0 @@
package com.github.boukefalos.tm1638.implementation;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import base.receiver.Receiver;
import base.sender.UdpSender;
import com.github.boukefalos.tm1638.EchoReceiver;
import com.github.boukefalos.tm1638.TM1638;
import com.github.boukefalos.tm1638.helper.ReceiverHelper;
public class UdpImplementation extends TM1638 implements Receiver {
protected UdpSender sender;
protected ArrayList<EchoReceiver> echoReceiverList = new ArrayList<EchoReceiver>();
public UdpImplementation(String host, int port) throws UnknownHostException {
sender = new UdpSender(host, port);
}
public void send(byte[] buffer) throws IOException {
sender.send(buffer);
}
public void addReceiver(EchoReceiver receiver) {
// Start 2-way communication
}
public void receive(byte[] buffer) {
ReceiverHelper.receive(echoReceiverList, buffer);
}
}

View File

@@ -1,5 +0,0 @@
package com.github.boukefalos.tm1638.server;
public interface TM1638Server {
}

View File

@@ -1,19 +0,0 @@
package com.github.boukefalos.tm1638.server;
import java.nio.channels.SocketChannel;
import base.server.channel.TcpServerClient;
public class TM1638TcpClient extends TcpServerClient {
protected TM1638TcpServer server;
public TM1638TcpClient(TM1638TcpServer server, SocketChannel socketChannel, Integer bufferSize) {
super(server, socketChannel, bufferSize);
this.server = server;
}
public void receive(byte[] buffer) {
System.out.println("serverClient.receive() " + new String(buffer).trim());
server.receive(buffer);
}
}

View File

@@ -1,30 +0,0 @@
package com.github.boukefalos.tm1638.server;
import base.server.channel.TcpServerClient;
import base.server.channel.TcpServer;
import com.github.boukefalos.tm1638.TM1638;
import com.github.boukefalos.tm1638.helper.ServerHelper;
public class TM1638TcpServer extends TcpServer implements TM1638Server {
protected TM1638 tm1638;
public TM1638TcpServer(TM1638 tm1638, int port) {
this(tm1638, port, BUFFER_SIZE);
}
public TM1638TcpServer(TM1638 tm1638, int port, int bufferSize) {
super(port, TM1638TcpClient.class, bufferSize);
this.tm1638 = tm1638;
//addReceiver(this); make sure client messages get passed on to arduino
}
protected void initClient(TcpServerClient serverClient) {
}
protected void receive(byte[] buffer) {
// write directly to arduino instead of decoding?
ServerHelper.receive(tm1638, buffer);
}
}

View File

@@ -1,26 +0,0 @@
package com.github.boukefalos.tm1638.server;
import base.receiver.Receiver;
import base.server.datagram.UdpServer;
import com.github.boukefalos.tm1638.TM1638;
import com.github.boukefalos.tm1638.helper.ServerHelper;
public class TM1638UdpServer extends UdpServer implements TM1638Server, Receiver {
protected TM1638 tm1638;
public TM1638UdpServer(TM1638 tm1638, int port) {
this(tm1638, port, BUFFER_SIZE);
}
public TM1638UdpServer(TM1638 tm1638, int port, int bufferSize) {
super(port, bufferSize);
this.tm1638 = tm1638;
this.bufferSize = bufferSize;
addReceiver(this);
}
public void receive(byte[] buffer) {
ServerHelper.receive(tm1638, buffer);
}
}

View File

@@ -1,26 +1,36 @@
package test;
import test.dummy.DummyEchoReceiver;
import tm1638.Tm1638.Color;
import tm1638.Tm1638.Echo;
import base.work.Listen;
import com.github.boukefalos.tm1638.TM1638;
import com.github.boukefalos.tm1638.implementation.Localmplementation;
import com.github.boukefalos.tm1638.implementation.Local;
public class TestLocal {
public void start() throws Exception {
int i = 123;
TM1638 TM1638 = new Localmplementation();
TM1638.addReceiver(new DummyEchoReceiver());
public class TestLocal extends Listen<Object> {
public static void main(String[] args) throws Exception {
TM1638 TM1638 = new Local();
main(TM1638);
}
public static void main(TM1638 TM1638) throws InterruptedException {
TM1638.register(new TestLocal());
TM1638.construct(8, 9, 7);
int i = 123;
while (i < 10000) {
TM1638.ping(i++);
TM1638.setLed(i % 3 == 0 ? Color.GREEN : Color.RED, i % 7);
Thread.sleep(1000);
}
TM1638.stop();
}
public static void main(String[] args) throws Exception {
new TestLocal().start();
public TestLocal() {
super();
start();
}
public void input(Echo echo) {
System.out.println("> " + echo.getMessage() + " " + echo.getId());
}
}

View File

@@ -0,0 +1,35 @@
package test;
import java.util.Properties;
import base.exception.worker.ActivateException;
import com.github.boukefalos.tm1638.Loader;
import com.github.boukefalos.tm1638.Server;
import com.github.boukefalos.tm1638.TM1638;
import com.github.boukefalos.tm1638.exception.ArduinoException;
public class TestRemoteImplementation extends TestLocal {
protected TM1638 tm1638;
public TestRemoteImplementation(Loader loader) throws ArduinoException {
tm1638 = loader.getTM1638();
tm1638.register(this);
}
public void activate() throws ActivateException {
tm1638.start();
super.activate();
}
public static void main(Properties localProperties, Properties remoteProperties) throws Exception {
Loader localLoader = new Loader(localProperties);
Loader remoteLoader = new Loader(remoteProperties);
Server server = localLoader.getServer();
server.start();
TM1638 TM1638 = remoteLoader.getTM1638();
main(TM1638);
}
}

View File

@@ -1,49 +0,0 @@
package test;
import java.util.Properties;
import test.dummy.DummyEchoReceiver;
import tm1638.Tm1638.Color;
import base.work.Work;
import com.github.boukefalos.tm1638.Loader;
import com.github.boukefalos.tm1638.TM1638;
public class TestTcpCommunication {
public static void main(String[] args) {
try {
Properties localProperties = new Properties();
localProperties.setProperty("implementation", "local");
localProperties.setProperty("server", "true");
localProperties.setProperty("server.port", "8883");
localProperties.setProperty("server.protocol", "tcp");
Properties remoteProperties = new Properties();
remoteProperties.setProperty("implementation", "remote");
remoteProperties.setProperty("protocol", "tcp");
remoteProperties.setProperty("remote.host", "localhost");
remoteProperties.setProperty("remote.port", "8883");
Loader localLoader = new Loader(localProperties);
Loader remoteLoader = new Loader(remoteProperties);
TM1638 localTM1638 = localLoader.getTM1638();
TM1638 remoteTM1638 = remoteLoader.getTM1638();
Work server = localLoader.getServer();
server.start();
remoteTM1638.addReceiver(new DummyEchoReceiver());
remoteTM1638.start();
remoteTM1638.construct(8, 9, 7);
int i = 123;
while (i < 10000) {
remoteTM1638.ping(i++);
remoteTM1638.setLed(i % 3 == 0 ? Color.GREEN : Color.RED, i % 7);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,28 @@
package test;
import java.util.Properties;
import com.github.boukefalos.tm1638.exception.ArduinoException;
public class TestTcpImplementation {
public static void main(String[] args) throws Exception {
Properties localProperties = new Properties();
localProperties.setProperty("implementation", "local");
localProperties.setProperty("server", "true");
localProperties.setProperty("server.direct", "false");
localProperties.setProperty("server.port", "8883");
localProperties.setProperty("server.protocol", "tcp");
Properties remoteProperties = new Properties();
remoteProperties.setProperty("implementation", "remote");
remoteProperties.setProperty("protocol", "tcp");
remoteProperties.setProperty("remote.host", "localhost");
remoteProperties.setProperty("remote.port", "8883");
try {
TestRemoteImplementation.main(localProperties, remoteProperties);
} catch (ArduinoException e) {
System.err.println(e.getMessage());
}
}
}

View File

@@ -1,49 +0,0 @@
package test;
import java.util.Properties;
import test.dummy.DummyEchoReceiver;
import tm1638.Tm1638.Color;
import base.work.Work;
import com.github.boukefalos.tm1638.Loader;
import com.github.boukefalos.tm1638.TM1638;
public class TestUdpCommunication {
public static void main(String[] args) {
try {
Properties localProperties = new Properties();
localProperties.setProperty("implementation", "local");
localProperties.setProperty("server", "true");
localProperties.setProperty("server.port", "8883");
localProperties.setProperty("server.protocol", "udp");
Properties remoteProperties = new Properties();
remoteProperties.setProperty("implementation", "remote");
remoteProperties.setProperty("protocol", "udp");
remoteProperties.setProperty("remote.host", "255.255.255.255");
remoteProperties.setProperty("remote.port", "8883");
Loader localLoader = new Loader(localProperties);
Loader remoteLoader = new Loader(remoteProperties);
TM1638 localTM1638 = localLoader.getTM1638();
TM1638 remoteTM1638 = remoteLoader.getTM1638();
Work server = localLoader.getServer();
server.start();
remoteTM1638.addReceiver(new DummyEchoReceiver());
remoteTM1638.start();
remoteTM1638.construct(8, 9, 7);
int i = 123;
while (i < 10000) {
remoteTM1638.ping(i++);
remoteTM1638.setLed(i % 3 == 0 ? Color.GREEN : Color.RED, i % 7);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,22 @@
package test;
import java.util.Properties;
public class TestUdpImplementation {
public static void main(String[] args) throws Exception {
Properties localProperties = new Properties();
localProperties.setProperty("implementation", "local");
localProperties.setProperty("server", "true");
localProperties.setProperty("server.direct", "false");
localProperties.setProperty("server.port", "8883");
localProperties.setProperty("server.protocol", "udp");
Properties remoteProperties = new Properties();
remoteProperties.setProperty("implementation", "remote");
remoteProperties.setProperty("protocol", "udp");
remoteProperties.setProperty("remote.host", "localhost");
remoteProperties.setProperty("remote.port", "8883");
TestRemoteImplementation.main(localProperties, remoteProperties);
}
}

View File

@@ -1,11 +0,0 @@
package test.dummy;
import beerduino.Beerduino.Echo;
import com.github.boukefalos.tm1638.EchoReceiver;
public class DummyEchoReceiver implements EchoReceiver {
public void receive(Echo echo) {
System.out.println("> " + echo.getMessage() + " " + echo.getId());
}
}