|
3 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; |
4 | 4 | import com.fasterxml.jackson.databind.JsonNode; |
5 | 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.google.common.collect.HashMultimap; |
| 7 | +import com.google.common.collect.Multimap; |
6 | 8 | import org.slf4j.Logger; |
7 | 9 | import org.slf4j.LoggerFactory; |
8 | 10 | import org.teamtators.rotator.operatorInterface.AbstractOperatorInterface; |
9 | 11 | import org.teamtators.rotator.operatorInterface.LogitechF310; |
10 | 12 | import org.teamtators.rotator.scheduler.*; |
11 | 13 |
|
12 | 14 | import javax.inject.Inject; |
| 15 | +import java.util.Collection; |
13 | 16 | import java.util.Map; |
14 | 17 | import java.util.Set; |
15 | 18 |
|
16 | | -public class TriggerBinder { |
| 19 | +public class TriggerBinder implements StateListener { |
17 | 20 | private static final Logger logger = LoggerFactory.getLogger(TriggerBinder.class); |
18 | 21 | private Scheduler scheduler; |
19 | 22 | private CommandStore commandStore; |
20 | 23 | private ObjectMapper objectMapper; |
21 | 24 | private AbstractOperatorInterface operatorInterface; |
| 25 | + private Multimap<RobotState, Command> stateCommands = HashMultimap.create(); |
22 | 26 |
|
23 | 27 | @Inject |
24 | 28 | public TriggerBinder() { |
@@ -53,6 +57,14 @@ public void bindTriggers(TriggersConfig triggersConfig) { |
53 | 57 | bindButtonsToLogitechF310(triggersConfig.driver, operatorInterface.driverJoystick()); |
54 | 58 | bindButtonsToLogitechF310(triggersConfig.gunner, operatorInterface.gunnerJoystick()); |
55 | 59 | registerDefaults(triggersConfig.defaults); |
| 60 | + registerStateCommands(triggersConfig.stateCommands); |
| 61 | + } |
| 62 | + |
| 63 | + private void registerStateCommands(Map<RobotState, String> newStateCommands) { |
| 64 | + for (Map.Entry<RobotState, String> commandEntry : newStateCommands.entrySet()) { |
| 65 | + Command stateCommand = getCommandForBinding(commandEntry.getValue()); |
| 66 | + stateCommands.put(commandEntry.getKey(), stateCommand); |
| 67 | + } |
56 | 68 | } |
57 | 69 |
|
58 | 70 | private void registerDefaults(Set<String> defaults) { |
@@ -154,4 +166,12 @@ private Command getCommandForBinding(String commandName) { |
154 | 166 | } |
155 | 167 | return command; |
156 | 168 | } |
| 169 | + |
| 170 | + public void onEnterState(RobotState state) { |
| 171 | + Collection<Command> thisStateCommands = stateCommands.get(state); |
| 172 | + if (thisStateCommands == null) return; |
| 173 | + for (Command stateCommand : thisStateCommands) { |
| 174 | + scheduler.startCommand(stateCommand); |
| 175 | + } |
| 176 | + } |
157 | 177 | } |
0 commit comments