| 1 | package tmcsim.cadsimulator.managers; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.io.FileInputStream; |
|---|
| 5 | import java.util.Observable; |
|---|
| 6 | import java.util.Observer; |
|---|
| 7 | import java.util.Properties; |
|---|
| 8 | import java.util.logging.Level; |
|---|
| 9 | import java.util.logging.Logger; |
|---|
| 10 | |
|---|
| 11 | import javax.swing.JOptionPane; |
|---|
| 12 | import javax.swing.JWindow; |
|---|
| 13 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 14 | |
|---|
| 15 | import tmcsim.cadsimulator.db.DVDPlayerDB; |
|---|
| 16 | import tmcsim.cadsimulator.db.StillImagesDB; |
|---|
| 17 | import tmcsim.cadsimulator.stillimagecontrol.ImageController; |
|---|
| 18 | import tmcsim.cadsimulator.videocontrol.DVDController; |
|---|
| 19 | import tmcsim.cadsimulator.videocontrol.DVDStatusUpdate; |
|---|
| 20 | import tmcsim.cadsimulator.videocontrol.DVDTitleUpdate; |
|---|
| 21 | import tmcsim.cadsimulator.viewer.CADSimulatorViewer; |
|---|
| 22 | import tmcsim.common.CCTVDirections; |
|---|
| 23 | import tmcsim.common.CCTVInfo; |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * MediaManager manages the control of media associated with the speed |
|---|
| 27 | * information received from the ParamicsCommunicator. The MediaManager |
|---|
| 28 | * instantiates the DVDPlayerDB and StillImageDB objects, which are initialized |
|---|
| 29 | * from XML data files to hold the information regarding which DVD titles and |
|---|
| 30 | * Still Images are available for display. Each title or image is associated |
|---|
| 31 | * with a specific speed range or Incident log number. The triggerIncident() |
|---|
| 32 | * method is called to notify the MediaManager that incident media is to be |
|---|
| 33 | * displayed. The updateCameraInfo() method is called when speed information |
|---|
| 34 | * is received for a specific camera. In both methods, a Controller object |
|---|
| 35 | * is found for the unique camera ID number. This Controller is updated with |
|---|
| 36 | * the new speed data. If the update causes a change in media to be displayed, |
|---|
| 37 | * the change is made.<br> |
|---|
| 38 | * <br> |
|---|
| 39 | * The MediaManager implements the Observer interface and observes the |
|---|
| 40 | * DVDPlayerDB and StillImageDB for updates. These updates are received, action |
|---|
| 41 | * is taken, and then the update is passed to the CADSimulatorViewer for display. |
|---|
| 42 | * |
|---|
| 43 | * @see tmcsim.cadsimulator.viewer.CADSimulatorViewer |
|---|
| 44 | * @author Matthew Cechini |
|---|
| 45 | * @version |
|---|
| 46 | */ |
|---|
| 47 | public class MediaManager implements Observer { |
|---|
| 48 | |
|---|
| 49 | /** Error Logger. */ |
|---|
| 50 | private static Logger mediaLogger = Logger.getLogger("tmcsim.simulator"); |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Enumeration of property names. |
|---|
| 54 | * @author Matthew Cechini |
|---|
| 55 | */ |
|---|
| 56 | private static enum MEDIA_PROPERTIES { |
|---|
| 57 | /** Filepath for xml file containing dvd control data. */ |
|---|
| 58 | DVD_XML_FILE ("DVDPlayerXML"), |
|---|
| 59 | /** Filepath for xml file containing still image control data. */ |
|---|
| 60 | IMAGE_XML_FILE ("StillImagesXML"); |
|---|
| 61 | |
|---|
| 62 | public String name; |
|---|
| 63 | |
|---|
| 64 | private MEDIA_PROPERTIES(String n) { |
|---|
| 65 | name = n; |
|---|
| 66 | } |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | /** Instance of the DVD Player Database. */ |
|---|
| 70 | private DVDPlayerDB theDVD_DB = null; |
|---|
| 71 | |
|---|
| 72 | /** Instance of the Still Image Database. */ |
|---|
| 73 | private StillImagesDB theImage_DB = null; |
|---|
| 74 | |
|---|
| 75 | /** Reference to the CADSimulatorViewer. */ |
|---|
| 76 | private CADSimulatorViewer theViewer; |
|---|
| 77 | |
|---|
| 78 | /** Properties object for the Media portion of the CAD. */ |
|---|
| 79 | private Properties mediaProperties; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Constructor. The target properties file is loaded and the DVD and Still |
|---|
| 84 | * Image DB objects are initialized with the XML data files containing |
|---|
| 85 | * the respective data. |
|---|
| 86 | * |
|---|
| 87 | * @param propertiesFile File path for target properties file. |
|---|
| 88 | * @param theATMSManager ATMSManager object. |
|---|
| 89 | * @param viewer CADSimulatorViewer object. |
|---|
| 90 | */ |
|---|
| 91 | public MediaManager(String propertiesFile, ATMSManager theATMSManager, |
|---|
| 92 | CADSimulatorViewer viewer) { |
|---|
| 93 | theDVD_DB = new DVDPlayerDB(); |
|---|
| 94 | theImage_DB = new StillImagesDB(); |
|---|
| 95 | |
|---|
| 96 | theViewer = viewer; |
|---|
| 97 | |
|---|
| 98 | try { |
|---|
| 99 | mediaProperties = new Properties(); |
|---|
| 100 | mediaProperties.load(new FileInputStream(new File(propertiesFile))); |
|---|
| 101 | } |
|---|
| 102 | catch (Exception e) { |
|---|
| 103 | mediaLogger.logp(Level.SEVERE, "MediaManager", "Constructor", |
|---|
| 104 | "Exception in loading properties file.", e); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | // Load DVD Player Information from the XML file |
|---|
| 108 | try { |
|---|
| 109 | if (mediaProperties.getProperty(MEDIA_PROPERTIES.DVD_XML_FILE.name) != null) { |
|---|
| 110 | theDVD_DB.addObserver(this); |
|---|
| 111 | theDVD_DB.loadFromXML(DocumentBuilderFactory |
|---|
| 112 | .newInstance().newDocumentBuilder() |
|---|
| 113 | .parse(new File(mediaProperties.getProperty( |
|---|
| 114 | MEDIA_PROPERTIES.DVD_XML_FILE.name)))); |
|---|
| 115 | |
|---|
| 116 | } |
|---|
| 117 | } catch (Exception e) { |
|---|
| 118 | mediaLogger.logp(Level.SEVERE, "MediaManager", "Constructor", |
|---|
| 119 | "Exception in parsing DVDPlayer xml file.", e); |
|---|
| 120 | |
|---|
| 121 | JOptionPane.showMessageDialog(new JWindow(), "An error occured " + |
|---|
| 122 | "opening and parsing the DVD xml data file: " + |
|---|
| 123 | mediaProperties.getProperty(MEDIA_PROPERTIES.DVD_XML_FILE.name), |
|---|
| 124 | "Initialization Error", JOptionPane.WARNING_MESSAGE); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // Load Still Image Information from the XML file |
|---|
| 128 | try { |
|---|
| 129 | if (mediaProperties |
|---|
| 130 | .getProperty(MEDIA_PROPERTIES.IMAGE_XML_FILE.name) != null) { |
|---|
| 131 | theImage_DB.addObserver(this); |
|---|
| 132 | theImage_DB.loadFromXML(DocumentBuilderFactory |
|---|
| 133 | .newInstance().newDocumentBuilder() |
|---|
| 134 | .parse(new File(mediaProperties.getProperty( |
|---|
| 135 | MEDIA_PROPERTIES.IMAGE_XML_FILE.name))), |
|---|
| 136 | theATMSManager); |
|---|
| 137 | } |
|---|
| 138 | } catch (Exception e) { |
|---|
| 139 | mediaLogger.logp(Level.SEVERE, "MediaManager", "Constructor", |
|---|
| 140 | "Exception in parsing StillImages xml file.", e); |
|---|
| 141 | |
|---|
| 142 | JOptionPane.showMessageDialog(new JWindow(), "An error occured " + |
|---|
| 143 | "opening and parsing the Image xml data file: " + |
|---|
| 144 | mediaProperties.getProperty(MEDIA_PROPERTIES.IMAGE_XML_FILE.name), |
|---|
| 145 | "Initialization Error", JOptionPane.WARNING_MESSAGE); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * Observer method. The update objects accepted and their associated |
|---|
| 151 | * actions are shown below.<br> |
|---|
| 152 | * <br> |
|---|
| 153 | * <ul> |
|---|
| 154 | * <li>DVDStatusUpdate - If the update contains an exception, log the |
|---|
| 155 | * exception to the local logger. The update object is then sent |
|---|
| 156 | * to the CADSimulatorViewer for display.</li> |
|---|
| 157 | * <li>DVDTitleUpdate - The update object is sent |
|---|
| 158 | * to the CADSimulatorViewer for display.</li> |
|---|
| 159 | * </ul> |
|---|
| 160 | */ |
|---|
| 161 | public void update(Observable o, Object arg) { |
|---|
| 162 | if(arg instanceof DVDStatusUpdate) { |
|---|
| 163 | if(((DVDStatusUpdate)arg).exception != null) { |
|---|
| 164 | mediaLogger.logp(Level.SEVERE, "MediaManager", "update", |
|---|
| 165 | "Exception from DVD Controller.", |
|---|
| 166 | ((DVDStatusUpdate)arg).exception); |
|---|
| 167 | } |
|---|
| 168 | theViewer.updateDVDStatus((DVDStatusUpdate)arg); |
|---|
| 169 | } |
|---|
| 170 | else if(arg instanceof DVDTitleUpdate) { |
|---|
| 171 | theViewer.updateDVDTitle((DVDTitleUpdate)arg); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | * This method triggers the DVD and Image Controllers for a specific camera |
|---|
| 177 | * to display an Incident title or image for the parameter Incident log |
|---|
| 178 | * number. |
|---|
| 179 | * |
|---|
| 180 | * @param info Information of the CCTV Camera to display incident images. |
|---|
| 181 | * @param logNumber Log number of the Incident being triggered. |
|---|
| 182 | */ |
|---|
| 183 | public void triggerIncident(CCTVInfo info, Integer logNumber) { |
|---|
| 184 | try { |
|---|
| 185 | DVDController dvdCntrl = theDVD_DB.getController(info.cctv_id, |
|---|
| 186 | info.direction); |
|---|
| 187 | |
|---|
| 188 | if(dvdCntrl != null) { |
|---|
| 189 | dvdCntrl.toggleIncident(logNumber, info.toggle); |
|---|
| 190 | } |
|---|
| 191 | } catch (Exception e) { |
|---|
| 192 | mediaLogger.logp(Level.SEVERE, "MediaManager", "triggerIncident", |
|---|
| 193 | "Exception in toggling an incident on the DVD controller.", e); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | try { |
|---|
| 197 | ImageController imageCntrl = theImage_DB.getController(info.cctv_id, info.direction); |
|---|
| 198 | |
|---|
| 199 | if(imageCntrl != null) { |
|---|
| 200 | imageCntrl.toggleIncident(logNumber, info.toggle); |
|---|
| 201 | } |
|---|
| 202 | } catch (Exception e) { |
|---|
| 203 | mediaLogger.logp(Level.SEVERE, "MediaManager", "triggerIncident", |
|---|
| 204 | "Exception in toggling an incident on the Image controller.", e); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /** |
|---|
| 210 | * This method receives updated speed information for a specific controller |
|---|
| 211 | * and updates the DVD Title and Still Image being currently displayed for |
|---|
| 212 | * that camera. |
|---|
| 213 | * |
|---|
| 214 | * @param cameraID CCTV camera ID |
|---|
| 215 | * @param avgSpeed_NE Average speed of traffic flowing N or E |
|---|
| 216 | * @param avgSpeed_SW Average speed of traffic flowing S or W |
|---|
| 217 | */ |
|---|
| 218 | public void updateCameraInfo(Integer cameraID, float avgSpeed_NE, float avgSpeed_SW) { |
|---|
| 219 | updateDVD(cameraID, avgSpeed_NE, avgSpeed_SW); |
|---|
| 220 | updateStillImage(cameraID, avgSpeed_NE, avgSpeed_SW); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | /** |
|---|
| 224 | * This method accepts updated speed information for a specific DVD |
|---|
| 225 | * player. The DVDController object corresponding to the parameter camera |
|---|
| 226 | * ID is found and updated with the new speed. If this update causes the |
|---|
| 227 | * dvd controller to change titles, the new title is played. |
|---|
| 228 | * |
|---|
| 229 | * @param cameraID CCTV camera ID |
|---|
| 230 | * @param avgSpeed_NE Average speed of traffic flowing N or E |
|---|
| 231 | * @param avgSpeed_SW Average speed of traffic flowing S or W |
|---|
| 232 | */ |
|---|
| 233 | protected void updateDVD(Integer cameraID, float avgSpeed_NE, float avgSpeed_SW) { |
|---|
| 234 | |
|---|
| 235 | for(CCTVDirections dir : CCTVDirections.values()) { |
|---|
| 236 | DVDController dvdCntrl = theDVD_DB.getController(cameraID, dir); |
|---|
| 237 | |
|---|
| 238 | //if Controller exists, update. |
|---|
| 239 | if(dvdCntrl != null) { |
|---|
| 240 | |
|---|
| 241 | if( ((dir == CCTVDirections.NORTH || dir == CCTVDirections.EAST) && |
|---|
| 242 | (dvdCntrl.updatePlayer(avgSpeed_NE))) || |
|---|
| 243 | ((dir == CCTVDirections.SOUTH || dir == CCTVDirections.WEST) && |
|---|
| 244 | (dvdCntrl.updatePlayer(avgSpeed_SW)))) { |
|---|
| 245 | |
|---|
| 246 | try { |
|---|
| 247 | dvdCntrl.playCurrentTitle(); |
|---|
| 248 | } |
|---|
| 249 | catch (Exception e) { |
|---|
| 250 | mediaLogger.logp(Level.SEVERE, "ParamicsCameraStatusReader", "updateDVD", |
|---|
| 251 | "Exception in updating DVD player with new speed.", e); |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /** |
|---|
| 259 | * This method accepts updated speed information for a specific CCTV camera. |
|---|
| 260 | * The ImageController object corresponding to the parameter camera ID |
|---|
| 261 | * is found and updated with the new speed. If this update causes the image |
|---|
| 262 | * controller to change images, the new image is shown. |
|---|
| 263 | * |
|---|
| 264 | * @param cameraID Unique CCTV camera ID |
|---|
| 265 | * @param avgSpeed_NE Average speed of traffic flowing N or E |
|---|
| 266 | * @param avgSpeed_SW Average speed of traffic flowing S or W |
|---|
| 267 | */ |
|---|
| 268 | protected void updateStillImage(Integer cameraID, float avgSpeed_NE, float avgSpeed_SW) { |
|---|
| 269 | |
|---|
| 270 | for(CCTVDirections dir : CCTVDirections.values()) { |
|---|
| 271 | ImageController imageCntrl = theImage_DB.getController(cameraID, dir); |
|---|
| 272 | |
|---|
| 273 | //if Controller exists, update. |
|---|
| 274 | if(imageCntrl != null) { |
|---|
| 275 | |
|---|
| 276 | if( ((dir == CCTVDirections.NORTH || dir == CCTVDirections.EAST) && |
|---|
| 277 | (imageCntrl.updateImage(avgSpeed_NE))) || |
|---|
| 278 | ((dir == CCTVDirections.SOUTH || dir == CCTVDirections.WEST) && |
|---|
| 279 | (imageCntrl.updateImage(avgSpeed_SW)))) { |
|---|
| 280 | |
|---|
| 281 | try { |
|---|
| 282 | imageCntrl.showCurrentImage(); |
|---|
| 283 | } |
|---|
| 284 | catch (Exception e) { |
|---|
| 285 | mediaLogger.logp(Level.SEVERE, "ParamicsCameraStatusReader", "updateStillImage", |
|---|
| 286 | "Exception in updating image controller with new speed.", e); |
|---|
| 287 | } |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | } |
|---|