Permalink
Browse files

Initial

  • Loading branch information...
mp911de committed Apr 27, 2012
1 parent b1993c7 commit 32e4c065378b85a8a3951f7c180ca99ba6c75eb0
Showing with 599 additions and 0 deletions.
  1. BIN powerflare-server/lib/RXTXcomm.jar
  2. BIN powerflare-server/lib/librxtxSerial.jnilib
  3. BIN powerflare-server/lib/librxtxSerial.so
  4. BIN powerflare-server/out/artifacts/powerflare_server_jar/RXTXcomm.jar
  5. BIN powerflare-server/out/artifacts/powerflare_server_jar/powerflare-server.jar
  6. BIN powerflare-server/out/artifacts/powerflare_server_war_exploded/RXTXcomm.jar
  7. BIN ...ts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/HttpControlInterface.class
  8. BIN ...t/artifacts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/Multiplexer.class
  9. BIN ...er/out/artifacts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/Server.class
  10. BIN ...rver_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/AbstractCommunicationChannel.class
  11. BIN ...owerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/DummyCommChannel.class
  12. BIN ...lare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/ICommunicationChannel.class
  13. BIN ...er_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/SerialPortCommunicationChannel.class
  14. BIN .../powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/ConnectCommand.class
  15. BIN ...werflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/DisconnectCommand.class
  16. BIN ...ifacts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/ICommand.class
  17. BIN ...s/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/RelayCommands.class
  18. BIN ...owerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SendDataCallable.class
  19. BIN ...owerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SwitchOffCommand.class
  20. BIN ...powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SwitchOnCommand.class
  21. BIN powerflare-server/out/artifacts/powerflare_server_war_exploded/WEB-INF/lib/RXTXcomm.jar
  22. +89 −0 powerflare-server/pom.xml
  23. +11 −0 powerflare-server/src/META-INF/MANIFEST.MF
  24. +64 −0 powerflare-server/src/main/java/de/paluch/powerflare/HttpControlInterface.java
  25. +36 −0 powerflare-server/src/main/java/de/paluch/powerflare/Multiplexer.java
  26. +67 −0 powerflare-server/src/main/java/de/paluch/powerflare/Server.java
  27. +48 −0 powerflare-server/src/main/java/de/paluch/powerflare/channel/AbstractCommunicationChannel.java
  28. +16 −0 powerflare-server/src/main/java/de/paluch/powerflare/channel/DummyCommChannel.java
  29. +16 −0 powerflare-server/src/main/java/de/paluch/powerflare/channel/ICommunicationChannel.java
  30. +62 −0 powerflare-server/src/main/java/de/paluch/powerflare/channel/SerialPortCommunicationChannel.java
  31. +27 −0 powerflare-server/src/main/java/de/paluch/powerflare/command/ConnectCommand.java
  32. +27 −0 powerflare-server/src/main/java/de/paluch/powerflare/command/DisconnectCommand.java
  33. +14 −0 powerflare-server/src/main/java/de/paluch/powerflare/command/ICommand.java
  34. +13 −0 powerflare-server/src/main/java/de/paluch/powerflare/command/RelayCommands.java
  35. +42 −0 powerflare-server/src/main/java/de/paluch/powerflare/command/SendDataCallable.java
  36. +34 −0 powerflare-server/src/main/java/de/paluch/powerflare/command/SwitchOffCommand.java
  37. +33 −0 powerflare-server/src/main/java/de/paluch/powerflare/command/SwitchOnCommand.java
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
@@ -0,0 +1,89 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>de.paluch</groupId>
+ <artifactId>powerflare-server</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <dependencies>
+ <dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-server</artifactId>
+ <version>1.8</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-core</artifactId>
+ <version>1.8</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.rxtx</groupId>
+ <artifactId>rxtx</artifactId>
+ <version>2.1.7</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.9.0</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>rxtx</groupId>
+ <artifactId>rxtx</artifactId>
+ <version>2.2pre2</version>
+ <scope>system</scope>
+ <systemPath>${basedir}/lib/RXTXcomm.jar</systemPath>
+ </dependency>
+
+
+ </dependencies>
+
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>1.6</version>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ <configuration>
+
+ <artifactSet>
+ <includes>
+ <include>*:*</include>
+ </includes>
+ </artifactSet>
+ <transformers>
+ <transformer
+ implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+ <mainClass>de.paluch.powerflare.Server</mainClass>
+ </transformer>
+ </transformers>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+
+</project>
@@ -0,0 +1,11 @@
+Manifest-Version: 1.0
+Class-Path: RXTXcomm.jar activation-1.1.1.jar asm-3.3.1.jar FastInfose
+ t-1.2.12.jar grizzly-servlet-webserver-1.9.36.jar jackson-core-asl-1.
+ 8.3.jar jackson-jaxrs-1.8.3.jar jackson-mapper-asl-1.8.3.jar jaxb-api
+ -2.2.4.jar jaxb-impl-2.2.4-1.jar jdom-1.0.jar jersey-bundle-1.10.jar
+ jersey-core-1.10.jar jersey-grizzly-1.10.jar jersey-multipart-1.10.ja
+ r jettison-1.1.jar localizer.jar mail-1.4.4.jar mimepull-1.3.jar pers
+ istence-api-1.0.2.jar rome-1.0.jar stax-api-1.0-2.jar wadl2java.jar w
+ adl-cmdline.jar wadl-core.jar
+Main-Class: de.paluch.powerflare.Server
+
@@ -0,0 +1,64 @@
+package de.paluch.powerflare;
+
+
+import de.paluch.powerflare.command.*;
+
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+
+/**
+ * Created with IntelliJ IDEA. User: mark Date: 25.04.12 Time: 08:02 To change this template use File | Settings | File
+ * Templates.
+ */
+@Path("/")
+public class HttpControlInterface {
+
+ @Path("powerflare/{port:[0-8]}/{state:(ON|OFF)}")
+ public void controlPowerflare(@PathParam("port") byte port, @PathParam("state") String state) {
+ System.out.println("switchPort port|" + port + "|state|" + state);
+
+ if (state == null) {
+ throw new IllegalArgumentException("state is null");
+ }
+
+ Server server = Server.getInstance();
+ ICommand command = null;
+ if (state.equalsIgnoreCase("ON")) {
+ command = new SwitchOnCommand(port);
+ }
+
+ if (state.equalsIgnoreCase("OFF")) {
+ command = new SwitchOffCommand(port);
+ }
+
+ if (command != null) {
+ Multiplexer.getInstance().execute(command);
+ }
+
+ }
+
+ @Path("relay/{port:[0-8]}/{state:(CONNECT|DISCONNECT)}")
+ public void controlPort(@PathParam("port") byte port, @PathParam("state") String state) {
+ System.out.println("controlPort port|" + port + "|state|" + state);
+
+ if (state == null) {
+ throw new IllegalArgumentException("state is null");
+ }
+
+ Server server = Server.getInstance();
+ ICommand command = null;
+ if (state.equalsIgnoreCase("CONNECT")) {
+ command = new ConnectCommand(port);
+ }
+
+ if (state.equalsIgnoreCase("DISCONNECT")) {
+ command = new DisconnectCommand(port);
+ }
+
+ if (command != null) {
+ Multiplexer.getInstance().execute(command);
+ }
+
+ }
+
+}
@@ -0,0 +1,36 @@
+package de.paluch.powerflare;
+
+import de.paluch.powerflare.channel.ICommunicationChannel;
+import de.paluch.powerflare.command.ICommand;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
+/**
+ * Created with IntelliJ IDEA. User: mark Date: 25.04.12 Time: 08:09 To change this template use File | Settings | File
+ * Templates.
+ */
+public class Multiplexer {
+
+ private static Multiplexer instance = new Multiplexer();
+
+ private ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
+ private ICommunicationChannel channel;
+
+
+ public void execute(ICommand command) {
+ command.execute(exec, channel);
+ }
+
+ public ICommunicationChannel getChannel() {
+ return channel;
+ }
+
+ public void setChannel(ICommunicationChannel channel) {
+ this.channel = channel;
+ }
+
+ public static Multiplexer getInstance() {
+ return instance;
+ }
+}
@@ -0,0 +1,67 @@
+package de.paluch.powerflare;
+
+import com.sun.jersey.api.container.httpserver.HttpServerFactory;
+import com.sun.jersey.api.core.ClassNamesResourceConfig;
+import com.sun.jersey.core.impl.provider.entity.StringProvider;
+import com.sun.net.httpserver.HttpServer;
+import de.paluch.powerflare.channel.ICommunicationChannel;
+import de.paluch.powerflare.channel.SerialPortCommunicationChannel;
+
+/**
+ * Created with IntelliJ IDEA. User: mark Date: 25.04.12 Time: 08:31 To change this template use File | Settings | File
+ * Templates.
+ */
+public class Server {
+
+
+ private static Server instance;
+ private ICommunicationChannel channel;
+
+
+ public static void main(String args[]) throws Exception {
+
+ if (args.length == 0) {
+ System.out.println("Usage: Server port (device path/name)");
+ return;
+ }
+ System.out.println("using port " + args[0]);
+
+ instance = new Server(args[0]);
+ instance.run();
+ instance.close();
+ }
+
+ private void run() throws Exception {
+
+ ClassNamesResourceConfig config = new ClassNamesResourceConfig(HttpControlInterface.class);
+ config.getProviderClasses().add(StringProvider.class);
+
+
+ HttpServer server = HttpServerFactory.create("http://localhost:9998/", config);
+ server.start();
+
+ System.out.println("Server running");
+ System.out.println("Visit: http://localhost:9998/powerflare/{port:[0-8]}/{state:(ON|OFF)}");
+ System.out.println("Hit return to stop...");
+ System.in.read();
+ System.out.println("Stopping server");
+ server.stop(0);
+ System.out.println("Server stopped");
+ }
+
+ private void close() {
+
+ channel.close();
+ }
+
+ public Server(String port) throws Exception {
+ channel = new SerialPortCommunicationChannel(port, 19200);
+ //channel = new DummyCommChannel();
+ Multiplexer.getInstance().setChannel(channel);
+ }
+
+ public static Server getInstance() {
+ return instance;
+ }
+
+}
@@ -0,0 +1,48 @@
+package de.paluch.powerflare.channel;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * Created with IntelliJ IDEA. User: mark Date: 27.04.12 Time: 08:39 To change this template use File | Settings | File
+ * Templates.
+ */
+public abstract class AbstractCommunicationChannel implements ICommunicationChannel {
+ private Map<Integer, Lock> locks = new ConcurrentHashMap<Integer, Lock>();
+
+ @Override
+ public void lock(int port) {
+ if (port != 0) {
+ lockInternal(0);
+ }
+ lockInternal(port);
+ }
+
+ private void lockInternal(int port) {
+ if (!locks.containsKey(port)) {
+ locks.put(port, new ReentrantLock());
+ }
+
+ Lock lock = locks.get(port);
+ lock.lock();
+ }
+
+ @Override
+ public void unlock(int port) {
+ if (port != 0) {
+ unlockInternal(0);
+ }
+ unlockInternal(port);
+ }
+
+ private void unlockInternal(int port) {
+ if (!locks.containsKey(port)) {
+ locks.put(port, new ReentrantLock());
+ }
+
+ Lock lock = locks.get(port);
+ lock.unlock();
+ }
+}
@@ -0,0 +1,16 @@
+package de.paluch.powerflare.channel;
+
+/**
+ * Created with IntelliJ IDEA. User: mark Date: 27.04.12 Time: 08:29 To change this template use File | Settings | File
+ * Templates.
+ */
+public class DummyCommChannel extends AbstractCommunicationChannel implements ICommunicationChannel {
+ @Override
+ public void sendData(byte[] data) {
+ System.out.println("Sending: " + new String(data));
+ }
+
+ @Override
+ public void close() {
+ }
+}
@@ -0,0 +1,16 @@
+package de.paluch.powerflare.channel;
+
+/**
+ * Created with IntelliJ IDEA. User: mark Date: 27.04.12 Time: 08:27 To change this template use File | Settings | File
+ * Templates.
+ */
+public interface ICommunicationChannel {
+
+ void sendData(byte[] data);
+
+ void close();
+
+ void lock(int port);
+
+ void unlock(int port);
+}
Oops, something went wrong.

0 comments on commit 32e4c06

Please sign in to comment.