| 1 | package tmcsim.cadsimulator.stillimagecontrol; |
|---|
| 2 | |
|---|
| 3 | import java.util.Observable; |
|---|
| 4 | import java.util.Vector; |
|---|
| 5 | |
|---|
| 6 | import tmcsim.cadsimulator.managers.ATMSManager; |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * ImageController is used for controlling which images are shown on the ATMS |
|---|
| 10 | * Server. Communication with the ATMS server is performed through the |
|---|
| 11 | * ATMSManager. |
|---|
| 12 | * |
|---|
| 13 | * The addRange() and addIncident() methods are used to register more images with |
|---|
| 14 | * the ImageController class. The updateImage() and toggleIncident() methods are |
|---|
| 15 | * used to control which images are being shown. An image will remain showing |
|---|
| 16 | * until a new image is chosen through one of these methods. |
|---|
| 17 | * |
|---|
| 18 | * @author Matthew Cechini |
|---|
| 19 | * @version |
|---|
| 20 | */ |
|---|
| 21 | public class ImageController extends Observable { |
|---|
| 22 | |
|---|
| 23 | /** Number of consecutive requests for change until the image will change. */ |
|---|
| 24 | private static final int CHANGE_TOLERANCE = 2; |
|---|
| 25 | |
|---|
| 26 | /** ATMS CCTV unique ID. */ |
|---|
| 27 | protected Integer atms_cctv_id = null; |
|---|
| 28 | |
|---|
| 29 | /** Vector of ImageRange objects handled by this controller. */ |
|---|
| 30 | protected Vector<ImageRange> ranges = null; |
|---|
| 31 | |
|---|
| 32 | /** Vector of ImageIncident objects handled by this controller. */ |
|---|
| 33 | protected Vector<ImageIncident> incidents = null; |
|---|
| 34 | |
|---|
| 35 | /** Current range being shown. Null if none have been shown. */ |
|---|
| 36 | protected ImageRange currentRange = null; |
|---|
| 37 | |
|---|
| 38 | /** Current incident iamge being shown. Null if none have been shown. */ |
|---|
| 39 | protected ImageIncident currentIncident = null; |
|---|
| 40 | |
|---|
| 41 | /** Boolean flag determining whether an incident is currently being shown. */ |
|---|
| 42 | protected boolean isShowingIncident = false; |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * ImageRange object to cache the last "new range" that the controller |
|---|
| 46 | * has chosen as a result of a speed update. This allows for a tolerance |
|---|
| 47 | * value to be used to control frequent title changes. |
|---|
| 48 | */ |
|---|
| 49 | protected ImageRange changeRange; |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Count value to count the number of consecutive updates that |
|---|
| 53 | * result in the same ImageRange. |
|---|
| 54 | */ |
|---|
| 55 | protected int changeRangeCounter; |
|---|
| 56 | |
|---|
| 57 | /** Manager used to handle communication with the ATMS. */ |
|---|
| 58 | protected ATMSManager theATMSManager; |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Constructor. |
|---|
| 62 | * |
|---|
| 63 | * @param cctv_id ATMS CCTV ID. |
|---|
| 64 | */ |
|---|
| 65 | public ImageController(Integer cctv_id, ATMSManager atmsManager) { |
|---|
| 66 | theATMSManager = atmsManager; |
|---|
| 67 | |
|---|
| 68 | ranges = new Vector<ImageRange>(); |
|---|
| 69 | incidents = new Vector<ImageIncident>(); |
|---|
| 70 | atms_cctv_id = cctv_id; |
|---|
| 71 | |
|---|
| 72 | changeRange = null; |
|---|
| 73 | changeRangeCounter = 0; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Add a new ImageRange to the local list. |
|---|
| 78 | * |
|---|
| 79 | * @param newRange ImageRange to add. |
|---|
| 80 | */ |
|---|
| 81 | public void addRange(ImageRange newRange) { |
|---|
| 82 | ranges.add(newRange); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Add a new ImageIncident to the local list. |
|---|
| 87 | * |
|---|
| 88 | * @param newRange ImageIncident to add. |
|---|
| 89 | */ |
|---|
| 90 | public void addIncident(ImageIncident newIncident) { |
|---|
| 91 | incidents.add(newIncident); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Toggle the image controller to begin or stop showing the image associated |
|---|
| 96 | * with an incident. The boolean parameter is used to designate whether the |
|---|
| 97 | * incident is being toggled to start(true) or stop (false). |
|---|
| 98 | * If the toggle flag is true, then the ImageIncident with log number equal |
|---|
| 99 | * to the parameter log_num will be shown. The currentIncident and |
|---|
| 100 | * isShowingIncident member data objects are updated. |
|---|
| 101 | * |
|---|
| 102 | * If the toggle flag is false, and the incident being toggled is shown, |
|---|
| 103 | * then it is stopped. |
|---|
| 104 | * |
|---|
| 105 | * @param log_num Incident log number to toggle. |
|---|
| 106 | * @param toggle Boolean flag. True = start incident, false = stop incident. |
|---|
| 107 | * @throws Exception if the method is unable to toggle the incident. |
|---|
| 108 | */ |
|---|
| 109 | public void toggleIncident(int log_num, boolean toggle) throws Exception { |
|---|
| 110 | if(toggle) { |
|---|
| 111 | for(ImageIncident incident : incidents) { |
|---|
| 112 | if(incident.incidentNumber == log_num) { |
|---|
| 113 | currentIncident = incident; |
|---|
| 114 | isShowingIncident = true; |
|---|
| 115 | |
|---|
| 116 | showCurrentImage(); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | else if(currentIncident != null && currentIncident.incidentNumber == log_num) { |
|---|
| 121 | isShowingIncident = false; |
|---|
| 122 | |
|---|
| 123 | showCurrentImage(); |
|---|
| 124 | } |
|---|
| 125 | else { |
|---|
| 126 | throw new Exception("ImageController: Unable to toggle incident #" + log_num); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * Update the image with a new traffic speed after the tolerance of |
|---|
| 132 | * range changes has been met. If the parameter speed falls within a |
|---|
| 133 | * different ImageRange than is being shown, remember the new range. If this |
|---|
| 134 | * new range was not detected during the last update, reset the change |
|---|
| 135 | * counter to 0. If this range was detected during the last update, |
|---|
| 136 | * increment the change counter. If the change counter is greater than |
|---|
| 137 | * the tolerance value specified, set the current range to the new range, |
|---|
| 138 | * reset the change counter, and set the updated flag to true. If the range |
|---|
| 139 | * has been updated, and an incident is not being shown, return true. Else |
|---|
| 140 | * return false. |
|---|
| 141 | * |
|---|
| 142 | * @param newSpeed New traffic speed for this Image's camera location. |
|---|
| 143 | * @return True if a new ImageRange is to be shown, false if not. |
|---|
| 144 | */ |
|---|
| 145 | public boolean updateImage(float newSpeed) { |
|---|
| 146 | |
|---|
| 147 | boolean updated = false; |
|---|
| 148 | ImageRange newRange = null; |
|---|
| 149 | |
|---|
| 150 | for(ImageRange range : ranges) { |
|---|
| 151 | if(range.isWithin(newSpeed)) { |
|---|
| 152 | if(currentRange == null || !currentRange.equals(range)) { |
|---|
| 153 | newRange = range; |
|---|
| 154 | break; |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | if(newRange == null) { |
|---|
| 160 | //do nothing |
|---|
| 161 | } |
|---|
| 162 | else if(changeRange == null || !changeRange.equals(newRange)) { |
|---|
| 163 | changeRange = newRange; |
|---|
| 164 | changeRangeCounter = 0; |
|---|
| 165 | } |
|---|
| 166 | else { |
|---|
| 167 | changeRangeCounter++; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | if(changeRangeCounter >= CHANGE_TOLERANCE) { |
|---|
| 171 | changeRangeCounter = 0; |
|---|
| 172 | currentRange = changeRange; |
|---|
| 173 | updated = true; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | //even if the currentRange is updated, an incident trumps this |
|---|
| 178 | return updated & !isShowingIncident; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | /** |
|---|
| 182 | * This method shows a new image. If the Image Controller is showing an incident, |
|---|
| 183 | * then the incident image will be shown. Else, if an Image range has been found, |
|---|
| 184 | * then it's image will be shown. Image control is done through the ATMSCommunicator |
|---|
| 185 | * singleton instance. |
|---|
| 186 | * |
|---|
| 187 | * @throws Exception if the method is not able to play the current title. |
|---|
| 188 | */ |
|---|
| 189 | public void showCurrentImage() throws Exception { |
|---|
| 190 | //TODO THIS IS HACK??!! |
|---|
| 191 | if (isShowingIncident) { |
|---|
| 192 | theATMSManager.showImage(atms_cctv_id, currentIncident.fileName); |
|---|
| 193 | } else if (currentRange != null) { |
|---|
| 194 | theATMSManager.showImage(atms_cctv_id, currentRange.fileName); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | } |
|---|