add comments on Web part
This commit is contained in:
parent
1984b8f261
commit
c3eccbbf35
@ -11,23 +11,49 @@ import org.java_websocket.server.WebSocketServer;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
public class WebConnector implements DataPointListener {
|
public class WebConnector implements DataPointListener {
|
||||||
private static WebConnector mySelf = null;
|
private static WebConnector mySelf = null; // Singleton
|
||||||
public WebSocketServer wss = null;
|
public WebSocketServer wss = null; // Websocket server
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor
|
||||||
|
* Subscribe to the update of DataPoints
|
||||||
|
*/
|
||||||
private WebConnector (){
|
private WebConnector (){
|
||||||
|
|
||||||
// Subscribe to the update of DataPoints
|
// Subscribe to the update of DataPoints
|
||||||
DataPointListener.subscribeUpdate(this);
|
DataPointListener.subscribeUpdate(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the singleton instance
|
||||||
|
* create it if it does not exist
|
||||||
|
* @return the singleton instance
|
||||||
|
*/
|
||||||
public static WebConnector getMySelf(){
|
public static WebConnector getMySelf(){
|
||||||
if (mySelf == null){
|
if (mySelf == null){
|
||||||
mySelf = new WebConnector();
|
mySelf = new WebConnector();
|
||||||
}
|
}
|
||||||
return mySelf;
|
return mySelf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize webConnector by initializing the websocket server
|
||||||
|
* - define all the callbacks for the websocket server (onOpen, onClose, onMessage, onError, onStart)
|
||||||
|
* - start the websocket server
|
||||||
|
* - wait for the first connection
|
||||||
|
* @param host url of the host (localhost)
|
||||||
|
* @param port port for the websocket server (8888)
|
||||||
|
*/
|
||||||
public void initialize(String host, int port){
|
public void initialize(String host, int port){
|
||||||
InetSocketAddress address = new InetSocketAddress(host, port);
|
InetSocketAddress address = new InetSocketAddress(host, port);
|
||||||
|
/**
|
||||||
|
* Define all the callbacks for the websocket server
|
||||||
|
* - onOpen (send a message to the client)
|
||||||
|
* - onClose (stop the websocket server)
|
||||||
|
* - onMessage (update the dataPoint)
|
||||||
|
* - onError (print the error)
|
||||||
|
* - onStart (print a message)
|
||||||
|
*/
|
||||||
wss = new WebSocketServer(address) {
|
wss = new WebSocketServer(address) {
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(WebSocket conn, ClientHandshake handshake) {
|
public void onOpen(WebSocket conn, ClientHandshake handshake) {
|
||||||
@ -73,18 +99,30 @@ public class WebConnector implements DataPointListener {
|
|||||||
wss.start();
|
wss.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push the dataPoint to the web
|
||||||
|
* use the websocket server to broadcast the dataPoint
|
||||||
|
* @param dp dataPoint to push
|
||||||
|
*/
|
||||||
private void pushToWeb(DataPoint dp){
|
private void pushToWeb(DataPoint dp){
|
||||||
wss.broadcast(dp.toString());
|
wss.broadcast(dp.toString());
|
||||||
Utility.pDebug(dp.toString() + " -> Web");
|
Utility.pDebug(dp.toString() + " -> Web");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the dataPoint with the message received from the web
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
private void updateDataPoint(String message){
|
private void updateDataPoint(String message){
|
||||||
|
|
||||||
|
// Split the message to get the label and the value
|
||||||
String label = message.split("=")[0];
|
String label = message.split("=")[0];
|
||||||
Utility.pDebug("Label: " + label);
|
Utility.pDebug("Label: " + label);
|
||||||
|
|
||||||
String value = message.split("=")[1];
|
String value = message.split("=")[1];
|
||||||
Utility.pDebug("Value: " + value);
|
Utility.pDebug("Value: " + value);
|
||||||
|
|
||||||
|
// Check if the value is a boolean or a float and update the dataPoint
|
||||||
if( (value.equals("true")) || value.equals("false") ) {
|
if( (value.equals("true")) || value.equals("false") ) {
|
||||||
new BooleanDataPoint(label, true).setValue(Boolean.parseBoolean(value));
|
new BooleanDataPoint(label, true).setValue(Boolean.parseBoolean(value));
|
||||||
} else {
|
} else {
|
||||||
@ -93,6 +131,10 @@ public class WebConnector implements DataPointListener {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback for the update of a dataPoint
|
||||||
|
* @param dp dataPoint updated
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onNewValue(DataPoint dp) {
|
public void onNewValue(DataPoint dp) {
|
||||||
pushToWeb(dp);
|
pushToWeb(dp);
|
||||||
|
@ -5,15 +5,27 @@ import ch.hevs.isi.core.FloatDataPoint;
|
|||||||
import ch.hevs.isi.db.DatabaseConnector;
|
import ch.hevs.isi.db.DatabaseConnector;
|
||||||
import ch.hevs.isi.web.WebConnector;
|
import ch.hevs.isi.web.WebConnector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used to test the web interface.
|
||||||
|
* It creates a web server on localhost:8888 and sends data to it.
|
||||||
|
* - Values are sent every 2 seconds
|
||||||
|
* - A variation of the values is simulated (see the code)
|
||||||
|
* - Some values are set to the same value as the remote values (see the code)
|
||||||
|
*/
|
||||||
public class Web {
|
public class Web {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
// set erase on true
|
||||||
MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true;
|
MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true;
|
||||||
FloatDataPoint clock = new FloatDataPoint("CLOAK_FLOAT", false);
|
|
||||||
|
// create data points for the web
|
||||||
|
FloatDataPoint clock = new FloatDataPoint("CLOCK_FLOAT", false);
|
||||||
FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true);
|
FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true);
|
||||||
|
|
||||||
|
// initialize the database and the web
|
||||||
DatabaseConnector.getMySelf().initialize(null);
|
DatabaseConnector.getMySelf().initialize(null);
|
||||||
WebConnector.getMySelf().initialize("localhost", 8888);
|
WebConnector.getMySelf().initialize("localhost", 8888);
|
||||||
|
|
||||||
|
// initialize time to 0 and set the value
|
||||||
float time = 0f;
|
float time = 0f;
|
||||||
clock.setValue(time);
|
clock.setValue(time);
|
||||||
|
|
||||||
@ -21,23 +33,34 @@ public class Web {
|
|||||||
|
|
||||||
while (WebConnector.getMySelf().wss != null) {
|
while (WebConnector.getMySelf().wss != null) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
// wait 2 seconds
|
||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
|
|
||||||
if(WebConnector.getMySelf().wss.getConnections().size() > 0){
|
if(WebConnector.getMySelf().wss.getConnections().size() > 0){
|
||||||
|
|
||||||
|
// Set the REMOTE_FACTORY_SP value to FACTORY_ST
|
||||||
FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_FACTORY_SP");
|
FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_FACTORY_SP");
|
||||||
if(rfSP != null) new FloatDataPoint("FACTORY_ST", true).setValue(rfSP.getValue());
|
if(rfSP != null) new FloatDataPoint("FACTORY_ST", true).setValue(rfSP.getValue());
|
||||||
|
|
||||||
|
// Set the REMOTE_COAL_SP value to COAL_ST
|
||||||
FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_COAL_SP");
|
FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_COAL_SP");
|
||||||
if(cfSP != null) new FloatDataPoint("COAL_ST", true).setValue(cfSP.getValue());
|
if(cfSP != null) new FloatDataPoint("COAL_ST", true).setValue(cfSP.getValue());
|
||||||
|
|
||||||
|
// Set the REMOTE_SOLAR_SP value to SOLAR_ST
|
||||||
BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_SOLAR_SW");
|
BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_SOLAR_SW");
|
||||||
if(solar != null) new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(solar.getValue());
|
if(solar != null) new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(solar.getValue());
|
||||||
|
|
||||||
|
// Set the REMOTE_WIND_SP value to WIND_ST
|
||||||
BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_WIND_SW");
|
BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_WIND_SW");
|
||||||
if(wind != null) new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(wind.getValue());
|
if(wind != null) new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(wind.getValue());
|
||||||
|
|
||||||
|
|
||||||
|
// increment time and set the value
|
||||||
time += 0.1f;
|
time += 0.1f;
|
||||||
clock.setValue(time);
|
clock.setValue(time);
|
||||||
|
|
||||||
|
// set the grid voltage value with a modulated value
|
||||||
gridVoltage.setValue(750 + (100*time)%100);
|
gridVoltage.setValue(750 + (100*time)%100);
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
Reference in New Issue
Block a user