Die Java Anwendung muss sich ebenfalls bei dem Python Websocket melden, um Nachrichten an die Komponenten Senden und Empfangen zu können. Der Client ist dabei einfach zu realisieren.
package diy.robot.websocketClients;
import java.net.URI;
import java.nio.ByteBuffer;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import diy.robot.domain.Sensors;
public class PythonWebSocketClient extends WebSocketClient {
public PythonWebSocketClient(URI serverURI) {
super(serverURI);
}
@Override
public void onOpen(ServerHandshake handshakedata) {
System.out.println("new connection opened");
}
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("closed with exit code " + code + " additional info: " + reason);
}
@Override
public void onMessage(String message) {
Sensors.setSensorValueAutmaticale(message);
}
@Override
public void onMessage(ByteBuffer message) {
System.out.println("received ByteBuffer");
}
@Override
public void onError(Exception ex) {
System.err.println("an error occurred:" + ex);
}
}
Die Verbindung zum Webserver wird in der Controller (BaseController.java) Klasse realisiert. Diese wird später vorgestellt.
Die Nachrichten zur Steuerung der Motoren (Befehle etc) wird dann in der Klasse KeyMapValuesSenderToPython realisiert:
package diy.robot.python;
import diy.robot.controller.BaseController;
import diy.robot.domain.KeyMap;
import diy.robot.domain.KeyMap.Direction;
import diy.robot.domain.KeyMap.LASER;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class KeyMapValuesSenderToPython {
private static Direction oldDirection;
private static LASER oldLaserValue;
public static void sendPythonCommand(Direction direction, int speed) { // TODO Refactor, to general-> send command
// to motors
if (!(oldDirection == KeyMap.getDirection())) {
log.info("Sending Mocing Command: " + KeyMap.getDirection());
sendCommandToMotors();
oldDirection = KeyMap.getDirection();
}
if (!(oldLaserValue == KeyMap.getLaser())) {
log.info("Sending Command to Laser, Laser: " + KeyMap.getLaser());
sendCommandToLaser();
oldLaserValue = KeyMap.getLaser();
}
// Robot arm needs a continous sending command
sendCommandToRobotArm();
}
private static void sendCommandToMotors() {
BaseController.sendMessageOnPythonWS("motorControllWS" + ":" + KeyMap.getDirection() + ":" + 50);
}
// TODO senden schaune obs klapp, Websocket Client splitten und enprtechend
// senden, Arm selber müsste berets klappen
private static void sendCommandToRobotArm() {
log.info("robotArm" + ":" + KeyMap.getRobotArmMotor() + ":" + KeyMap.getRobotArmCommand());
BaseController.sendMessageOnPythonWS(
"robotArm" + ":" + KeyMap.getRobotArmMotor() + ":" + KeyMap.getRobotArmCommand());
}
private static void sendCommandToLaser() {
log.info("laser" + ":" + KeyMap.getLaser());
BaseController.sendMessageOnPythonWS("laser" + ":" + KeyMap.getLaser());
}
}