1 /* 2 * Copyright © 2016 Greg Chabala 3 * 4 * This file is part of brick-control-lab. 5 * 6 * brick-control-lab is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as 8 * published by the Free Software Foundation, either version 3 of the 9 * License, or (at your option) any later version. 10 * 11 * brick-control-lab is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with brick-control-lab. If not, see http://www.gnu.org/licenses/. 18 */ 19 package org.chabala.brick.controllab; 20 21 import java.io.IOException; 22 import java.util.Collections; 23 import java.util.EnumSet; 24 import java.util.Set; 25 26 /** 27 * Handle for an output port (or group of ports) on a specific control lab instance. 28 * Obtain via {@link ControlLab#getOutput(OutputId)} or {@link ControlLab#getOutput(Set)}. 29 */ 30 public class Output { 31 32 private final ControlLab controlLab; 33 private final Set<OutputId> outputIdSet; 34 35 /** 36 * Called internally by {@link ControlLab#getOutput(OutputId)}. 37 * @param controlLab the control lab this output is a handle to 38 * @param outputId the output port ID on that control lab 39 */ 40 Output(ControlLab controlLab, OutputId outputId) { 41 this(controlLab, EnumSet.of(outputId)); 42 } 43 44 /** 45 * Called internally by {@link ControlLab#getOutput(Set)}. 46 * @param controlLab the control lab this output is a handle to 47 * @param outputIdSet the output IDs this output should reference 48 */ 49 Output(ControlLab controlLab, Set<OutputId> outputIdSet) { 50 this.controlLab = controlLab; 51 this.outputIdSet = outputIdSet; 52 } 53 54 /** 55 * Returns set of {@link OutputId}s this {@link Output} relates to. In normal 56 * usage, this is unlikely to be called as one would obtain the {@link Output} 57 * from {@link ControlLab#getOutput(OutputId)} and immediately chain one of the 58 * fluent method calls. But it can useful to inspect the {@link OutputId}s if the 59 * {@link Output} reference is retained and seperated from the creation site. 60 * @return set of {@link OutputId}s this {@link Output} relates to 61 */ 62 public Set<OutputId> getOutputIdSet() { 63 return Collections.unmodifiableSet(outputIdSet); 64 } 65 66 /** 67 * Stops sending power to this output. 68 * @return self reference to the output for chaining 69 * @throws IOException if any number of possible communication issues occurs 70 */ 71 public Output turnOff() throws IOException { 72 controlLab.turnOutputOff(outputIdSet); 73 return this; 74 } 75 76 /** 77 * Starts sending power to this output. 78 * @return self reference to the output for chaining 79 * @throws IOException if any number of possible communication issues occurs 80 */ 81 public Output turnOn() throws IOException { 82 controlLab.turnOutputOn(outputIdSet); 83 return this; 84 } 85 86 /** 87 * Sets the {@link Direction} of this output. Direction may be changed 88 * while the output is powered or unpowered. 89 * @param direction desired direction 90 * @return self reference to the output for chaining 91 * @throws IOException if any number of possible communication issues occurs 92 */ 93 public Output setDirection(Direction direction) throws IOException { 94 controlLab.setOutputDirection(direction, outputIdSet); 95 return this; 96 } 97 98 /** 99 * Reverses the {@link Direction} of this output. This is a convenience method 100 * that is the same as 101 * {@link Output#setDirection(Direction) setDirection(}{@link Direction#REVERSE Direction.REVERSE)}. 102 * @return self reference to the output for chaining 103 * @throws IOException if any number of possible communication issues occurs 104 */ 105 public Output reverseDirection() throws IOException { 106 return setDirection(Direction.REVERSE); 107 } 108 109 /** 110 * Sets the {@link PowerLevel} of this output. Power level may be changed 111 * while the output is powered or unpowered. 112 * @param powerLevel desired power level 113 * @return self reference to the output for chaining 114 * @throws IOException if any number of possible communication issues occurs 115 */ 116 public Output setPowerLevel(PowerLevel powerLevel) throws IOException { 117 controlLab.setOutputPowerLevel(powerLevel, outputIdSet); 118 return this; 119 } 120 121 /** {@inheritDoc} */ 122 @Override 123 public String toString() { 124 return "Output{" + 125 "controlLab=" + controlLab + 126 ", outputIdSet=" + outputIdSet + 127 '}'; 128 } 129 }