diff --git a/Dockerfile b/Dockerfile index f8bdeb4..a1fece8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,4 @@ ENV BROKER tcp://localhost:1883 ENV REFBOX localhost ENV TEAM GRIPS ENV KEY randomkey -ENTRYPOINT ["sh", "-c", "java -jar /home/gradle/src/app/build/libs/mqtt-bridge-0.5-all.jar -b $BROKER -r $REFBOX -t $TEAM -k $KEY"] +ENTRYPOINT ["sh", "-c", "java -jar /home/gradle/src/app/build/libs/mqtt-bridge-0.6-all.jar -b $BROKER -r $REFBOX -t $TEAM -k $KEY"] diff --git a/app/build.gradle b/app/build.gradle index 85da6fd..862663a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,7 +17,7 @@ plugins { group = 'io.github.robocup-logistics' archivesBaseName = "mqtt-bridge" -version = '0.5' +version = '0.6' repositories { // Use Maven Central for resolving dependencies. diff --git a/app/src/main/java/com/rcll/java/App.java b/app/src/main/java/com/rcll/java/App.java index 7c7e560..70211b2 100644 --- a/app/src/main/java/com/rcll/java/App.java +++ b/app/src/main/java/com/rcll/java/App.java @@ -40,7 +40,7 @@ public static void main(String[] args) throws MqttException, InterruptedExceptio IMqttClient publisher = new MqttClient(brokerUrl, publisherId); try { publisher.connect(); - } catch(MqttException me) { + } catch (MqttException me) { log.error("Error on connecting to mqtt broker[" + brokerUrl + "] - is it Running? Msg: " + me.getMessage(), me.getCause()); System.exit(1); } @@ -52,12 +52,21 @@ public static void main(String[] args) throws MqttException, InterruptedExceptio TeamConfig teamConfig = new TeamConfig(parsed.getOptionValue("k"), parsed.getOptionValue("t")); RefboxHandler privateHandler = new RefboxHandler(); RefboxHandler publicHandler = new RefboxHandler(); - RefboxMqttHandler privateMqttHandler = new RefboxMqttHandler(privateHandler, "private", publisher); - RefboxMqttHandler publicMqttHandler = new RefboxMqttHandler(publicHandler, "public", publisher); RefboxClient refboxClient = new RefboxClient(connectionConfig, teamConfig, privateHandler, publicHandler, 2000); refboxClient.startServer(); RefboxTeamHandler refboxTeamHandler = new RefboxTeamHandler(publisher, refboxClient, teamConfig.getName()); refboxTeamHandler.start(); + + while (refboxClient.getTeamColor().isEmpty()) { + log.info("Waiting for team color..."); + Thread.sleep(1000); + } + + RefboxMqttHandler privateMqttHandler = new RefboxMqttHandler(privateHandler, "private", publisher, + refboxClient.getTeamColor().get(), teamConfig.getName()); + RefboxMqttHandler publicMqttHandler = new RefboxMqttHandler(publicHandler, "public", publisher, + refboxClient.getTeamColor().get(), teamConfig.getName()); + while (publisher.isConnected()) { Thread.sleep(1000); } diff --git a/app/src/main/java/com/rcll/java/RefboxMqttHandler.java b/app/src/main/java/com/rcll/java/RefboxMqttHandler.java index fcdc7d1..3d818d2 100644 --- a/app/src/main/java/com/rcll/java/RefboxMqttHandler.java +++ b/app/src/main/java/com/rcll/java/RefboxMqttHandler.java @@ -2,6 +2,7 @@ import com.google.protobuf.MessageOrBuilder; import com.google.protobuf.util.JsonFormat; +import com.rcll.domain.TeamColor; import com.rcll.refbox.RefboxHandler; import lombok.SneakyThrows; import org.eclipse.paho.client.mqttv3.IMqttClient; @@ -9,17 +10,23 @@ import org.robocup_logistics.llsf_msgs.*; import java.io.IOException; +import java.util.Optional; public class RefboxMqttHandler { private final RefboxHandler handler; private final String prefix; private final IMqttClient mqttClient; + private final TeamColor teamColor; + private final String teamName; - public RefboxMqttHandler(RefboxHandler handler, String prefix, IMqttClient mqttClient) { + public RefboxMqttHandler(RefboxHandler handler, String prefix, IMqttClient mqttClient, TeamColor teamColor, + String teamName) { this.handler = handler; this.prefix = prefix; this.mqttClient = mqttClient; + this.teamColor = teamColor; + this.teamName = teamName; this.handler.setGameStateCallback(this::handleGameState); this.handler.setMachineInfoCallback(this::handleMchineInfo); this.handler.setOrderInfoCallback(this::handleOrderInfo); @@ -65,6 +72,13 @@ private void handleExplorationInfo(ExplorationInfoProtos.ExplorationInfo explora @SneakyThrows private void handleBeaconSignal(BeaconSignalProtos.BeaconSignal beaconSignal) { + if (!beaconSignal.hasTeamColor() && !beaconSignal.hasTeamName()) { + if (teamColor.equals(TeamColor.CYAN)) { + beaconSignal = beaconSignal.toBuilder().setTeamColor(TeamProtos.Team.CYAN).setTeamName(teamName).build(); + } else { + beaconSignal = beaconSignal.toBuilder().setTeamColor(TeamProtos.Team.MAGENTA).setTeamName(teamName).build(); + } + } this.mqttClient.publish(this.prefix + "/beacon_signal", new MqttMessage(toJson(beaconSignal).getBytes())); }