From cbd20cd4d24488790d2109c12b9a141327b400d1 Mon Sep 17 00:00:00 2001 From: Peter Kohout Date: Mon, 12 Dec 2022 23:46:43 +0100 Subject: [PATCH] did some code cleanup. --- build.gradle | 2 +- .../rcll/domain/CapStationInstruction.java | 6 + src/main/java/com/rcll/domain/Machine.java | 5 + .../com/rcll/domain/MachineClientUtils.java | 92 ------------ .../java/com/rcll/domain/MachineName.java | 16 +-- .../java/com/rcll/domain/MachineSide.java | 6 + .../java/com/rcll/domain/MachineState.java | 13 ++ src/main/java/com/rcll/domain/Order.java | 10 +- ...Complexity.java => ProductComplexity.java} | 2 +- src/main/java/com/rcll/domain/Ring.java | 4 +- src/main/java/com/rcll/domain/RingColor.java | 8 ++ .../java/com/rcll/refbox/MachineClient.java | 131 ++++++++++++------ .../java/com/rcll/refbox/OrderService.java | 2 +- .../rcll/refbox/RefBoxConnectionManager.java | 2 +- .../java/com/rcll/refbox/RefboxClient.java | 16 +-- .../com/rcll/refbox/RefboxConnection.java | 2 +- .../com/rcll/refbox/RobotClientUtils.java | 4 - .../com/rcll/refbox/MachineClientTest.java | 5 +- 18 files changed, 157 insertions(+), 169 deletions(-) create mode 100644 src/main/java/com/rcll/domain/CapStationInstruction.java create mode 100644 src/main/java/com/rcll/domain/Machine.java delete mode 100644 src/main/java/com/rcll/domain/MachineClientUtils.java create mode 100644 src/main/java/com/rcll/domain/MachineSide.java create mode 100644 src/main/java/com/rcll/domain/MachineState.java rename src/main/java/com/rcll/domain/{Complexity.java => ProductComplexity.java} (64%) create mode 100644 src/main/java/com/rcll/domain/RingColor.java delete mode 100644 src/main/java/com/rcll/refbox/RobotClientUtils.java diff --git a/build.gradle b/build.gradle index 69b6af2..19ef3ed 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { group = 'io.github.robocup-logistics' archivesBaseName = "java-sdk" -version = '0.1.9.6' +version = '0.1.10' description = "" diff --git a/src/main/java/com/rcll/domain/CapStationInstruction.java b/src/main/java/com/rcll/domain/CapStationInstruction.java new file mode 100644 index 0000000..379174c --- /dev/null +++ b/src/main/java/com/rcll/domain/CapStationInstruction.java @@ -0,0 +1,6 @@ +package com.rcll.domain; + +public enum CapStationInstruction { + RetrieveCap, + MountCap +} diff --git a/src/main/java/com/rcll/domain/Machine.java b/src/main/java/com/rcll/domain/Machine.java new file mode 100644 index 0000000..e267c7c --- /dev/null +++ b/src/main/java/com/rcll/domain/Machine.java @@ -0,0 +1,5 @@ +package com.rcll.domain; + +public enum Machine { + BS, DS, RS1, RS2, CS1, CS2, SS; +} diff --git a/src/main/java/com/rcll/domain/MachineClientUtils.java b/src/main/java/com/rcll/domain/MachineClientUtils.java deleted file mode 100644 index 2a6cbb4..0000000 --- a/src/main/java/com/rcll/domain/MachineClientUtils.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.rcll.domain; - -import org.robocup_logistics.llsf_msgs.ProductColorProtos; - -public class MachineClientUtils { - - public static MachineState parseMachineState(String state) { - switch (state) { - case "READY-AT-OUTPUT": - return MachineState.READY_AT_OUTPUT; - case "IDLE": - return MachineState.IDLE; - case "DOWN": - return MachineState.DOWN; - case "BROKEN": - return MachineState.BROKEN; - case "PREPARED": - return MachineState.PREPARED; - case "PROCESSING": - return MachineState.PROCESSING; - case "PROCESSED": - return MachineState.PROCESSED; - case "WAIT-IDLE": - return MachineState.WAIT_IDLE; - case "AVAILABLE": - return MachineState.AVAILABLE; - } - if (state.equals("")) { - return MachineState.UNDEFINED; - } - throw new IllegalArgumentException("Unkown Machine state: " + state); - } - - public static Machine parseMachineWithColor(String machine) { - if (machine.contains("BS")) { - return Machine.BS; - } else if (machine.contains("DS")) { - return Machine.DS; - } else if (machine.contains("CS1")) { - return Machine.CS1; - } else if (machine.contains("CS2")) { - return Machine.CS2; - } else if (machine.contains("RS1")) { - return Machine.RS1; - } else if (machine.contains("RS2")) { - return Machine.RS2; - } else if (machine.contains("SS")) { - return Machine.SS; - } - throw new IllegalArgumentException("Unkown machine: " + machine); - } - - public enum Machine { - BS, DS, RS1, RS2, CS1, CS2, SS; - } - - public enum MachineSide { - Input, - Output - } - - public enum BaseColor { - BASE_RED, - BASE_BLACK, - BASE_SILVER - } - - public enum RingColor { - Blue, - Green, - Orange, - Yellow - } - - public enum MachineState { - READY_AT_OUTPUT, - IDLE, - WAIT_IDLE, - DOWN, - BROKEN, - PREPARED, - PROCESSING, - PROCESSED, - AVAILABLE, - UNDEFINED - } - - public enum CSOp { - RETRIEVE_CAP, - MOUNT_CAP - } -} diff --git a/src/main/java/com/rcll/domain/MachineName.java b/src/main/java/com/rcll/domain/MachineName.java index 71b81d8..3e9a811 100644 --- a/src/main/java/com/rcll/domain/MachineName.java +++ b/src/main/java/com/rcll/domain/MachineName.java @@ -69,31 +69,31 @@ public boolean isStorageStation() { return this.rawMachineName.contains("SS"); } - public MachineClientUtils.Machine asMachineEnum() { + public Machine asMachineEnum() { if (isBaseStation()) { - return MachineClientUtils.Machine.BS; + return Machine.BS; } if (isDeliveryStation()) { - return MachineClientUtils.Machine.DS; + return Machine.DS; } if (isRingStation()) { if (this.rawMachineName.contains("1")) { - return MachineClientUtils.Machine.RS1; + return Machine.RS1; } if (this.rawMachineName.contains("2")) { - return MachineClientUtils.Machine.RS2; + return Machine.RS2; } } if (isCapStation()) { if (this.rawMachineName.contains("1")) { - return MachineClientUtils.Machine.CS1; + return Machine.CS1; } if (this.rawMachineName.contains("2")) { - return MachineClientUtils.Machine.CS2; + return Machine.CS2; } } if (isStorageStation()) { - return MachineClientUtils.Machine.SS; + return Machine.SS; } throw new RuntimeException("Unkonwn Machine: " + this.rawMachineName); } diff --git a/src/main/java/com/rcll/domain/MachineSide.java b/src/main/java/com/rcll/domain/MachineSide.java new file mode 100644 index 0000000..8115afc --- /dev/null +++ b/src/main/java/com/rcll/domain/MachineSide.java @@ -0,0 +1,6 @@ +package com.rcll.domain; + +public enum MachineSide { + Input, + Output +} diff --git a/src/main/java/com/rcll/domain/MachineState.java b/src/main/java/com/rcll/domain/MachineState.java new file mode 100644 index 0000000..b733572 --- /dev/null +++ b/src/main/java/com/rcll/domain/MachineState.java @@ -0,0 +1,13 @@ +package com.rcll.domain; +public enum MachineState { + READY_AT_OUTPUT, + IDLE, + WAIT_IDLE, + DOWN, + BROKEN, + PREPARED, + PROCESSING, + PROCESSED, + AVAILABLE, + UNDEFINED +} diff --git a/src/main/java/com/rcll/domain/Order.java b/src/main/java/com/rcll/domain/Order.java index 777bafa..9589d9a 100644 --- a/src/main/java/com/rcll/domain/Order.java +++ b/src/main/java/com/rcll/domain/Order.java @@ -23,16 +23,16 @@ public class Order { private Base base; private boolean competitive; - public Complexity getComplexity() { + public ProductComplexity getComplexity() { if (ring3 != null) { - return Complexity.C3; + return ProductComplexity.C3; } if (ring2 != null) { - return Complexity.C2; + return ProductComplexity.C2; } if (ring1 != null) { - return Complexity.C1; + return ProductComplexity.C1; } - return Complexity.C0; + return ProductComplexity.C0; } } diff --git a/src/main/java/com/rcll/domain/Complexity.java b/src/main/java/com/rcll/domain/ProductComplexity.java similarity index 64% rename from src/main/java/com/rcll/domain/Complexity.java rename to src/main/java/com/rcll/domain/ProductComplexity.java index 24900c5..ef5c047 100644 --- a/src/main/java/com/rcll/domain/Complexity.java +++ b/src/main/java/com/rcll/domain/ProductComplexity.java @@ -1,6 +1,6 @@ package com.rcll.domain; -public enum Complexity { +public enum ProductComplexity { C0, C1, C2, diff --git a/src/main/java/com/rcll/domain/Ring.java b/src/main/java/com/rcll/domain/Ring.java index ad3e7ff..d159bd6 100644 --- a/src/main/java/com/rcll/domain/Ring.java +++ b/src/main/java/com/rcll/domain/Ring.java @@ -8,7 +8,7 @@ @Setter @AllArgsConstructor public class Ring { - MachineClientUtils.Machine machine; - MachineClientUtils.RingColor color; + Machine machine; + RingColor color; int rawMaterial; } diff --git a/src/main/java/com/rcll/domain/RingColor.java b/src/main/java/com/rcll/domain/RingColor.java new file mode 100644 index 0000000..319851b --- /dev/null +++ b/src/main/java/com/rcll/domain/RingColor.java @@ -0,0 +1,8 @@ +package com.rcll.domain; + +public enum RingColor { + Blue, + Green, + Orange, + Yellow +} diff --git a/src/main/java/com/rcll/refbox/MachineClient.java b/src/main/java/com/rcll/refbox/MachineClient.java index 2f54894..ae72db3 100644 --- a/src/main/java/com/rcll/refbox/MachineClient.java +++ b/src/main/java/com/rcll/refbox/MachineClient.java @@ -2,9 +2,8 @@ import com.google.protobuf.GeneratedMessageV3; import com.google.protobuf.InvalidProtocolBufferException; -import com.rcll.domain.MachineClientUtils; -import com.rcll.domain.Ring; -import com.rcll.domain.TeamColor; +import com.rcll.domain.*; +import com.rcll.domain.RingColor; import com.rcll.protobuf_lib.RobotMessageRegister; import lombok.extern.java.Log; import org.robocup_logistics.llsf_comm.ProtobufMessage; @@ -21,10 +20,10 @@ @Log class MachineClient { private final TeamColor teamColor; - private final Map sendQueue; - private final Map machineStates; - private final Map ringColorToMachine; - private final Map ringColorToCost; + private final Map sendQueue; + private final Map machineStates; + private final Map ringColorToMachine; + private final Map ringColorToCost; public MachineClient(TeamColor teamColor) { this.teamColor = teamColor; @@ -34,7 +33,7 @@ public MachineClient(TeamColor teamColor) { this.ringColorToCost = new ConcurrentHashMap<>(); } - public void sendResetMachine(MachineClientUtils.Machine machine) { + public void sendResetMachine(Machine machine) { MachineInstructionProtos.ResetMachine reset = MachineInstructionProtos.ResetMachine.newBuilder() .setMachine(machineNameForMsg(machine, teamColor)) .setTeamColor(TeamProtos.Team.valueOf(teamColor.toString())) @@ -43,12 +42,12 @@ public void sendResetMachine(MachineClientUtils.Machine machine) { addMessageToSendQueue(machine, reset); } - public void sendPrepareBS(MachineClientUtils.MachineSide side, MachineClientUtils.BaseColor base_color) { - MachineClientUtils.Machine machine = MachineClientUtils.Machine.BS; + public void sendPrepareBS(MachineSide side, Base base_color) { + Machine machine = Machine.BS; MachineInstructionProtos.PrepareInstructionBS bsInstruction = MachineInstructionProtos.PrepareInstructionBS.newBuilder() //.setSide(task.getSide() == SubProductionTask.MachineSide.INPUT ? MachineDescriptionProtos.MachineSide.INPUT : MachineDescriptionProtos.MachineSide.OUTPUT) - .setSide(side == MachineClientUtils.MachineSide.Input ? MachineInstructionProtos.MachineSide.INPUT : MachineInstructionProtos.MachineSide.OUTPUT) + .setSide(side == MachineSide.Input ? MachineInstructionProtos.MachineSide.INPUT : MachineInstructionProtos.MachineSide.OUTPUT) .setColor(BaseColor.valueOf(base_color.toString())) .build(); MachineInstructionProtos.PrepareMachine prepareMachineMsg = @@ -62,7 +61,7 @@ public void sendPrepareBS(MachineClientUtils.MachineSide side, MachineClientUtil } public void sendPrepareDS(int gate, int orderId) { - MachineClientUtils.Machine machine = MachineClientUtils.Machine.DS; + Machine machine = Machine.DS; MachineInstructionProtos.PrepareInstructionDS dsInstruction = MachineInstructionProtos.PrepareInstructionDS.newBuilder() //.setGate(gate) @@ -78,8 +77,8 @@ public void sendPrepareDS(int gate, int orderId) { addMessageToSendQueue(machine, prepareMachineMsg); } - public void sendPrepareRS(MachineClientUtils.Machine machine, MachineClientUtils.RingColor ringColor) { - RingColor refbox_color = toRefboxRingColor(ringColor); + public void sendPrepareRS(Machine machine, RingColor ringColor) { + ProductColorProtos.RingColor refbox_color = toRefboxRingColor(ringColor); MachineInstructionProtos.PrepareInstructionRS rsInstruction = MachineInstructionProtos.PrepareInstructionRS.newBuilder() .setRingColor(refbox_color) @@ -95,13 +94,13 @@ public void sendPrepareRS(MachineClientUtils.Machine machine, MachineClientUtils addMessageToSendQueue(machine, prepareMachineMsg); } - public void sendPrepareCS(MachineClientUtils.Machine machine, MachineClientUtils.CSOp operation) { + public void sendPrepareCS(Machine machine, CapStationInstruction operation) { MachineDescriptionProtos.CSOp refbox_operation; switch (operation) { - case RETRIEVE_CAP: + case RetrieveCap: refbox_operation = MachineDescriptionProtos.CSOp.RETRIEVE_CAP; break; - case MOUNT_CAP: + case MountCap: refbox_operation = MachineDescriptionProtos.CSOp.MOUNT_CAP; break; default: @@ -134,7 +133,7 @@ public void sendPrepareCS(MachineClientUtils.Machine machine, MachineClientUtils addMessageToSendQueue(machine, prepareMachineMsg); } - public void sendPrepareSS(MachineClientUtils.Machine machine, int shelf, int slot) { + public void sendPrepareSS(Machine machine, int shelf, int slot) { MachineInstructionProtos.PrepareInstructionSS ssInstruction = MachineInstructionProtos.PrepareInstructionSS.newBuilder() .setShelf(shelf) @@ -150,7 +149,7 @@ public void sendPrepareSS(MachineClientUtils.Machine machine, int shelf, int slo addMessageToSendQueue(machine, prepareMachineMsg); } - public void stopMessageForMachine(MachineClientUtils.Machine machine) { + public void stopMessageForMachine(Machine machine) { sendQueue.remove(machine); } @@ -162,15 +161,15 @@ public List fetchResetMessages() { return fetchMsgForType(MachineInstructionProtos.ResetMachine.class); } - public Set fetchMachinesPreparing() { + public Set fetchMachinesPreparing() { return fetchMachinesForType(MachineInstructionProtos.PrepareMachine.class); } - public Set fetchMachinesResetting() { + public Set fetchMachinesResetting() { return fetchMachinesForType(MachineInstructionProtos.ResetMachine.class); } - public Optional getStateForMachine(MachineClientUtils.Machine machine) { + public Optional getStateForMachine(Machine machine) { return Optional.ofNullable(this.machineStates.get(machine)); } @@ -198,8 +197,8 @@ public void updateMessages() { .filter(machineStates::containsKey) .filter(sendQueue::containsKey) .forEach(x -> { - if (!machineStates.get(x).equals(MachineClientUtils.MachineState.IDLE)) { - if (!machineStates.get(x).equals(MachineClientUtils.MachineState.DOWN)) { + if (!machineStates.get(x).equals(MachineState.IDLE)) { + if (!machineStates.get(x).equals(MachineState.DOWN)) { sendQueue.remove(x); } else { log.info("Machine " + x + " is down, not removing from send queue!"); @@ -210,51 +209,51 @@ public void updateMessages() { .filter(machineStates::containsKey) .filter(sendQueue::containsKey) .forEach(x -> { - if (machineStates.get(x).equals(MachineClientUtils.MachineState.BROKEN)) { + if (machineStates.get(x).equals(MachineState.BROKEN)) { sendQueue.remove(x); } }); } - public RingColor toRefboxRingColor(MachineClientUtils.RingColor ringColor) { + public ProductColorProtos.RingColor toRefboxRingColor(RingColor ringColor) { switch (ringColor) { case Blue: - return RingColor.RING_BLUE; + return ProductColorProtos.RingColor.RING_BLUE; case Green: - return RingColor.RING_GREEN; + return ProductColorProtos.RingColor.RING_GREEN; case Orange: - return RingColor.RING_ORANGE; + return ProductColorProtos.RingColor.RING_ORANGE; case Yellow: - return RingColor.RING_YELLOW; + return ProductColorProtos.RingColor.RING_YELLOW; } throw new IllegalArgumentException("RingColor not mapped: " + ringColor); } - public MachineClientUtils.RingColor fromRefboxRingColor(RingColor ringColor) { + public RingColor fromRefboxRingColor(ProductColorProtos.RingColor ringColor) { switch (ringColor) { case RING_BLUE: - return MachineClientUtils.RingColor.Blue; + return RingColor.Blue; case RING_GREEN: - return MachineClientUtils.RingColor.Green; + return RingColor.Green; case RING_ORANGE: - return MachineClientUtils.RingColor.Orange; + return RingColor.Orange; case RING_YELLOW: - return MachineClientUtils.RingColor.Yellow; + return RingColor.Yellow; } throw new IllegalArgumentException("RingColor not mapped: " + ringColor); } private void updateMachineStatus(MachineInfoProtos.Machine machineInfo) { - MachineClientUtils.Machine machine = MachineClientUtils.parseMachineWithColor(machineInfo.getName()); - MachineClientUtils.MachineState state = MachineClientUtils.parseMachineState(machineInfo.getState()); - if (machine == MachineClientUtils.Machine.RS1 || machine == MachineClientUtils.Machine.RS2) { + Machine machine = parseMachineWithColor(machineInfo.getName()); + MachineState state = parseMachineState(machineInfo.getState()); + if (machine == Machine.RS1 || machine == Machine.RS2) { this.ringColorToMachine.put(fromRefboxRingColor(machineInfo.getRingColors(0)), machine); this.ringColorToMachine.put(fromRefboxRingColor(machineInfo.getRingColors(1)), machine); } machineStates.put(machine, state); } - public Ring getRingForColor(MachineClientUtils.RingColor ringColor) { + public Ring getRingForColor(RingColor ringColor) { if (!ringColorToMachine.containsKey(ringColor)) { throw new RuntimeException("Don't know which machine Ring is at: " + ringColor); } @@ -264,7 +263,7 @@ public Ring getRingForColor(MachineClientUtils.RingColor ringColor) { return new Ring(ringColorToMachine.get(ringColor),ringColor, ringColorToCost.get(ringColor)); } - private String machineNameForMsg(MachineClientUtils.Machine machine, TeamColor color) { + private String machineNameForMsg(Machine machine, TeamColor color) { StringBuilder returner = new StringBuilder(); switch (color) { case CYAN: @@ -285,8 +284,8 @@ private List fetchMsgForType(Class fetchMachinesForType(Class clazz) { - Set returner = new HashSet<>(); + private Set fetchMachinesForType(Class clazz) { + Set returner = new HashSet<>(); this.sendQueue.forEach((machine, msg) -> { if (msg.getClass().equals(clazz)) { returner.add(machine); @@ -295,7 +294,7 @@ private Set fetchMachinesForType(Class getStateForMachine(MachineClientUtils.Machine machine) { + public Optional getStateForMachine(Machine machine) { if (privateServerStarted) { return this.machineClient.orElseThrow().getStateForMachine(machine); } else { @@ -225,12 +225,12 @@ public List getAllOrders() { return orderService.orElseThrow().getOrders(); } - public Ring getRingByColor(MachineClientUtils.RingColor ringColor) { + public Ring getRingByColor(RingColor ringColor) { return machineClient.orElseThrow().getRingForColor(ringColor); } public List getAllRings() { - return Arrays.stream(MachineClientUtils.RingColor.values()) + return Arrays.stream(RingColor.values()) .map(this::getRingByColor) .collect(Collectors.toList()); } diff --git a/src/main/java/com/rcll/refbox/RefboxConnection.java b/src/main/java/com/rcll/refbox/RefboxConnection.java index 392abfb..725facd 100644 --- a/src/main/java/com/rcll/refbox/RefboxConnection.java +++ b/src/main/java/com/rcll/refbox/RefboxConnection.java @@ -11,7 +11,7 @@ @CommonsLog @Getter -public class RefboxConnection { +class RefboxConnection { private ProtobufUpdBroadcastConnection peer; public RefboxConnection(String ip, int sendPort, int receivePort, diff --git a/src/main/java/com/rcll/refbox/RobotClientUtils.java b/src/main/java/com/rcll/refbox/RobotClientUtils.java deleted file mode 100644 index 7b5645e..0000000 --- a/src/main/java/com/rcll/refbox/RobotClientUtils.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.rcll.refbox; - -public class RobotClientUtils { -} diff --git a/src/test/java/com/rcll/refbox/MachineClientTest.java b/src/test/java/com/rcll/refbox/MachineClientTest.java index 9f8375e..5494460 100644 --- a/src/test/java/com/rcll/refbox/MachineClientTest.java +++ b/src/test/java/com/rcll/refbox/MachineClientTest.java @@ -1,6 +1,7 @@ package com.rcll.refbox; -import com.rcll.domain.MachineClientUtils; +import com.rcll.domain.CapStationInstruction; +import com.rcll.domain.Machine; import lombok.SneakyThrows; import lombok.extern.apachecommons.CommonsLog; import org.junit.jupiter.api.Disabled; @@ -28,7 +29,7 @@ public void testPrepareCS() { MachineClient machineClient = new MachineClient(CYAN); assertThat(machineClient.fetchMachinesPreparing()).isEmpty(); assertThat(machineClient.fetchPrepareMessages()).isEmpty(); - machineClient.sendPrepareCS(MachineClientUtils.Machine.CS2, MachineClientUtils.CSOp.RETRIEVE_CAP); + machineClient.sendPrepareCS(Machine.CS2, CapStationInstruction.RetrieveCap); RefBoxConnectionManager rbcm = new RefBoxConnectionManager(