add Web part
This commit is contained in:
parent
67b8a115d9
commit
34da3f5409
@ -1,10 +1,17 @@
|
|||||||
package ch.hevs.isi.web;
|
package ch.hevs.isi.web;
|
||||||
|
|
||||||
|
import ch.hevs.isi.core.BooleanDataPoint;
|
||||||
import ch.hevs.isi.core.DataPoint;
|
import ch.hevs.isi.core.DataPoint;
|
||||||
import ch.hevs.isi.core.DataPointListener;
|
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 {
|
public class WebConnector implements DataPointListener {
|
||||||
private static WebConnector mySelf = null;
|
private static WebConnector mySelf = null;
|
||||||
|
public WebSocketServer wss = null;
|
||||||
|
|
||||||
private WebConnector (){
|
private WebConnector (){
|
||||||
|
|
||||||
@ -17,13 +24,72 @@ public class WebConnector implements DataPointListener {
|
|||||||
return mySelf;
|
return mySelf;
|
||||||
}
|
}
|
||||||
public void initialize(String host, int port){
|
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){
|
private void pushToWeb(DataPoint dp){
|
||||||
|
wss.broadcast(dp.toString());
|
||||||
System.out.println(dp.toString() + " -> Web");
|
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
|
@Override
|
||||||
public void onNewValue(DataPoint dp) {
|
public void onNewValue(DataPoint dp) {
|
||||||
pushToWeb(dp);
|
pushToWeb(dp);
|
||||||
|
50
src/test/java/Web.java
Normal file
50
src/test/java/Web.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import ch.hevs.isi.MinecraftController;
|
||||||
|
import ch.hevs.isi.core.BooleanDataPoint;
|
||||||
|
import ch.hevs.isi.core.DataPoint;
|
||||||
|
import ch.hevs.isi.core.FloatDataPoint;
|
||||||
|
import ch.hevs.isi.db.DatabaseConnector;
|
||||||
|
import ch.hevs.isi.web.WebConnector;
|
||||||
|
|
||||||
|
public class Web {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true;
|
||||||
|
FloatDataPoint clock = new FloatDataPoint("CLOAK_FLOAT", false);
|
||||||
|
FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true);
|
||||||
|
|
||||||
|
DatabaseConnector.getMySelf().initialize(null);
|
||||||
|
WebConnector.getMySelf().initialize("localhost", 8888);
|
||||||
|
|
||||||
|
float time = 0f;
|
||||||
|
clock.setValue(time);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while (WebConnector.getMySelf().wss != null) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(2000);
|
||||||
|
|
||||||
|
if(WebConnector.getMySelf().wss.getConnections().size() > 0){
|
||||||
|
FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointFromLabel("REMOTE_FACTORY_SP");
|
||||||
|
if(rfSP != null) new FloatDataPoint("FACTORY_ST", true).setValue(rfSP.getValue());
|
||||||
|
|
||||||
|
FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointFromLabel("REMOTE_COAL_SP");
|
||||||
|
if(cfSP != null) new FloatDataPoint("COAL_ST", true).setValue(cfSP.getValue());
|
||||||
|
|
||||||
|
BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointFromLabel("REMOTE_SOLAR_SW");
|
||||||
|
if(solar != null) new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(solar.getValue());
|
||||||
|
|
||||||
|
BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointFromLabel("REMOTE_WIND_SW");
|
||||||
|
if(wind != null) new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(wind.getValue());
|
||||||
|
|
||||||
|
time += 0.1f;
|
||||||
|
clock.setValue(time);
|
||||||
|
gridVoltage.setValue(750 + (100*time)%100);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user