add Web part
This commit is contained in:
		| @@ -1,10 +1,17 @@ | ||||
| package ch.hevs.isi.web; | ||||
|  | ||||
| import ch.hevs.isi.core.BooleanDataPoint; | ||||
| import ch.hevs.isi.core.DataPoint; | ||||
| import ch.hevs.isi.core.DataPointListener; | ||||
| import ch.hevs.isi.core.FloatDataPoint; | ||||
| import org.java_websocket.WebSocket; | ||||
| import org.java_websocket.handshake.ClientHandshake; | ||||
| import org.java_websocket.server.WebSocketServer; | ||||
| import java.net.InetSocketAddress; | ||||
|  | ||||
| public class WebConnector implements DataPointListener { | ||||
|     private static WebConnector mySelf = null; | ||||
|     public WebSocketServer wss = null; | ||||
|  | ||||
|     private WebConnector (){ | ||||
|  | ||||
| @@ -17,13 +24,72 @@ public class WebConnector implements DataPointListener { | ||||
|         return mySelf; | ||||
|     } | ||||
|     public void initialize(String host, int port){ | ||||
|         InetSocketAddress address = new InetSocketAddress(host, port); | ||||
|         wss = new WebSocketServer(address) { | ||||
|             @Override | ||||
|             public void onOpen(WebSocket conn, ClientHandshake handshake) { | ||||
|                 conn.send("Hello Minecraft World 5 !"); | ||||
|                 broadcast( "new connection: " + handshake.getResourceDescriptor() ); //This method sends a message to all clients connected | ||||
|                 System.out.println("New connection from " + conn.getRemoteSocketAddress().getAddress().getHostAddress()); | ||||
|                 System.out.println("Handshake message: " + handshake.getResourceDescriptor()); | ||||
|             } | ||||
|  | ||||
|             @Override | ||||
|             public void onClose(WebSocket conn, int code, String reason, boolean remote) { | ||||
|                 System.out.println("closed " + conn.getRemoteSocketAddress() + " with exit code " + code + " additional info: " + reason); | ||||
|                 try { | ||||
|                     stop(500); | ||||
|                     System.exit(0); | ||||
|                 } catch (InterruptedException e) { | ||||
|                     System.out.println("Error: " + e.getMessage()); | ||||
|                     throw new RuntimeException(e); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             @Override | ||||
|             public void onMessage(WebSocket conn, String message) { | ||||
|                 System.out.println("received message from "	+ conn.getRemoteSocketAddress() + ": " + message); | ||||
|                 updateDataPoint(message); | ||||
|             } | ||||
|  | ||||
|             @Override | ||||
|             public void onError(WebSocket conn, Exception ex) { | ||||
|                 try { | ||||
|                     System.err.println("an error occurred on connection " + conn.getRemoteSocketAddress()  + ":" + ex); | ||||
|                 } catch (Exception e) { | ||||
|                     System.out.println("Error: " + e.getMessage()); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             @Override | ||||
|             public void onStart() { | ||||
|                 System.out.println("Websocket server started"); | ||||
|             } | ||||
|         }; | ||||
|  | ||||
|         wss.start(); | ||||
|     } | ||||
|  | ||||
|     private void pushToWeb(DataPoint dp){ | ||||
|         wss.broadcast(dp.toString()); | ||||
|         System.out.println(dp.toString() + " -> Web"); | ||||
|     } | ||||
|  | ||||
|     private void updateDataPoint(String message){ | ||||
|         String label = message.split("=")[0]; | ||||
|         System.out.println("Label: " + label); | ||||
|  | ||||
|         String value = message.split("=")[1]; | ||||
|         System.out.println("Value: " + value); | ||||
|  | ||||
|         if( (value.equals("true")) || value.equals("false") ) { | ||||
|             new BooleanDataPoint(label, true).setValue(Boolean.parseBoolean(value)); | ||||
|         } else { | ||||
|             new FloatDataPoint(label, true).setValue(Float.parseFloat(value)); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onNewValue(DataPoint dp) { | ||||
|         pushToWeb(dp); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user