| 1 | package tmcsim.cadsimulator.db; |
|---|
| 2 | |
|---|
| 3 | import java.util.Observable; |
|---|
| 4 | import java.util.Observer; |
|---|
| 5 | import java.util.TreeMap; |
|---|
| 6 | |
|---|
| 7 | import org.w3c.dom.Document; |
|---|
| 8 | import org.w3c.dom.Element; |
|---|
| 9 | import org.w3c.dom.NodeList; |
|---|
| 10 | |
|---|
| 11 | import tmcsim.cadsimulator.managers.ATMSManager; |
|---|
| 12 | import tmcsim.cadsimulator.stillimagecontrol.ImageController; |
|---|
| 13 | import tmcsim.cadsimulator.stillimagecontrol.ImageIncident; |
|---|
| 14 | import tmcsim.cadsimulator.stillimagecontrol.ImageRange; |
|---|
| 15 | import tmcsim.common.CCTVDirections; |
|---|
| 16 | import tmcsim.common.ScriptException; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * StilImagesDB acts as a repository for all StillImage data read during |
|---|
| 20 | * initialization. An xml file read at startup details which CCTV cameras |
|---|
| 21 | * will be monitored during the simulator. An ImageController object is |
|---|
| 22 | * created for each camera, uniquely identified by an ID and direction. |
|---|
| 23 | * Each camera is also registered with a list of speed ranges and/or incidents |
|---|
| 24 | * for which the ImageController will perform needed action to swap images on |
|---|
| 25 | * the ATMS server. |
|---|
| 26 | * |
|---|
| 27 | * TODO Observer/Observable |
|---|
| 28 | * |
|---|
| 29 | * @author Matthew Cechini |
|---|
| 30 | * @version |
|---|
| 31 | */ |
|---|
| 32 | public class StillImagesDB extends Observable implements Observer { |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Enumeration containing XML tag names used to parse the XML |
|---|
| 36 | * document containing information about Still Image control. |
|---|
| 37 | * @author Matthew Cechini |
|---|
| 38 | */ |
|---|
| 39 | private static enum IMAGE_DB_TAGS { |
|---|
| 40 | /** This is the top level tag. */ |
|---|
| 41 | STILL_IMAGES ("STILL_IMAGES"), |
|---|
| 42 | /** CCTV camera. */ |
|---|
| 43 | CCTV ("CCTV"), |
|---|
| 44 | /** CCTV camera unique id number. */ |
|---|
| 45 | CCTV_ID ("id"), |
|---|
| 46 | /** CCTV camera unique id string on ATMS server. */ |
|---|
| 47 | ATMS_CCTV_ID ("atms_id"), |
|---|
| 48 | /** CCTV camera direction value. */ |
|---|
| 49 | DIRECTION ("dir"), |
|---|
| 50 | /** Speed range for associated image. */ |
|---|
| 51 | RANGE ("RANGE"), |
|---|
| 52 | /** Minimum value of speed range. */ |
|---|
| 53 | MIN_SPEED ("min_speed"), |
|---|
| 54 | /** Maximum value of speed range. */ |
|---|
| 55 | MAX_SPEED ("max_speed"), |
|---|
| 56 | /** Incident associated image. */ |
|---|
| 57 | INCIDENT ("INCIDENT"), |
|---|
| 58 | /** Log number for incident. */ |
|---|
| 59 | LOG_NUM ("log_num"), |
|---|
| 60 | /** Image filename. */ |
|---|
| 61 | FILENAME ("filename"); |
|---|
| 62 | |
|---|
| 63 | /** XML Tag name. */ |
|---|
| 64 | public String tag; |
|---|
| 65 | |
|---|
| 66 | private IMAGE_DB_TAGS(String t) { |
|---|
| 67 | tag = t; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * The map containing all current CCTV Camera IDs(key) and the corresponding |
|---|
| 74 | * Still Image Controller(value) that have been registered with the system. |
|---|
| 75 | */ |
|---|
| 76 | private TreeMap<CCTVInfo, ImageController> stillImageMap = null; |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Constructor. Initialize the map of ImageControllers. |
|---|
| 80 | */ |
|---|
| 81 | public StillImagesDB() { |
|---|
| 82 | stillImageMap = new TreeMap<CCTVInfo, ImageController>(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Implementation of the Observer update() method. All received update |
|---|
| 87 | * objects are sent to Observers of this DB. |
|---|
| 88 | */ |
|---|
| 89 | public void update(Observable o, Object arg) { |
|---|
| 90 | setChanged(); |
|---|
| 91 | notifyObservers(arg); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Load the Still Images map from an xml document, adhering to the |
|---|
| 96 | * following schema. When parsing has finished, the map of |
|---|
| 97 | * ImageController and CCTVInfo objects will contain objects |
|---|
| 98 | * representing all control objects specified in the XML<br> |
|---|
| 99 | * |
|---|
| 100 | * <STILL_IMAGES> <br> |
|---|
| 101 | * <IMAGE_PLAYER type="" username="" pwd="" host="" basedir =""> |
|---|
| 102 | * <CCTV id="" atms_id="" dir=""> |
|---|
| 103 | * <RANGE min_speed="" max_speed="" filename=""/> |
|---|
| 104 | * <INCIDENT log_num="" filename=""/> |
|---|
| 105 | * </CCTV> |
|---|
| 106 | * </IMAGE_PLAYER> |
|---|
| 107 | *</STILL_IMAGES> <br> |
|---|
| 108 | * |
|---|
| 109 | * @throws ScriptException if there is an error in parsing the Node. |
|---|
| 110 | */ |
|---|
| 111 | public void loadFromXML(Document newDoc, ATMSManager theATMSManager) |
|---|
| 112 | throws ScriptException |
|---|
| 113 | { |
|---|
| 114 | |
|---|
| 115 | Element rootElement = newDoc.getDocumentElement(); |
|---|
| 116 | Element cameraElement = null; |
|---|
| 117 | Element rangeElement = null; |
|---|
| 118 | Element incidentElement = null; |
|---|
| 119 | |
|---|
| 120 | NodeList cameras = rootElement.getElementsByTagName( |
|---|
| 121 | IMAGE_DB_TAGS.CCTV.tag); |
|---|
| 122 | NodeList ranges = null; |
|---|
| 123 | NodeList incidents = null; |
|---|
| 124 | |
|---|
| 125 | Integer cctvID = null; |
|---|
| 126 | Integer ATMS_cctvID = null; |
|---|
| 127 | CCTVDirections cctvDirection = null; |
|---|
| 128 | ImageController theController = null; |
|---|
| 129 | |
|---|
| 130 | //for all CCTV camera elements, parse out data |
|---|
| 131 | for(int j = 0; j < cameras.getLength(); j++) { |
|---|
| 132 | cameraElement = (Element)cameras.item(j); |
|---|
| 133 | |
|---|
| 134 | cctvID = Integer.parseInt(cameraElement.getAttribute( |
|---|
| 135 | IMAGE_DB_TAGS.CCTV_ID.tag)); |
|---|
| 136 | ATMS_cctvID = Integer.parseInt(cameraElement.getAttribute( |
|---|
| 137 | IMAGE_DB_TAGS.ATMS_CCTV_ID.tag)); |
|---|
| 138 | cctvDirection = CCTVDirections.fromChar(cameraElement.getAttribute( |
|---|
| 139 | IMAGE_DB_TAGS.DIRECTION.tag).charAt(0)); |
|---|
| 140 | |
|---|
| 141 | ranges = cameraElement.getElementsByTagName( |
|---|
| 142 | IMAGE_DB_TAGS.RANGE.tag); |
|---|
| 143 | incidents = cameraElement.getElementsByTagName( |
|---|
| 144 | IMAGE_DB_TAGS.INCIDENT.tag); |
|---|
| 145 | |
|---|
| 146 | theController = new ImageController(ATMS_cctvID, theATMSManager); |
|---|
| 147 | |
|---|
| 148 | for(int k = 0; k < ranges.getLength(); k++) { |
|---|
| 149 | rangeElement = (Element)ranges.item(k); |
|---|
| 150 | |
|---|
| 151 | theController.addRange(new ImageRange( |
|---|
| 152 | Float.parseFloat(rangeElement.getAttribute( |
|---|
| 153 | IMAGE_DB_TAGS.MIN_SPEED.tag)), |
|---|
| 154 | Float.parseFloat(rangeElement.getAttribute( |
|---|
| 155 | IMAGE_DB_TAGS.MAX_SPEED.tag)), |
|---|
| 156 | rangeElement.getAttribute( |
|---|
| 157 | IMAGE_DB_TAGS.FILENAME.tag))); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | for(int k = 0; k < incidents.getLength(); k++) { |
|---|
| 161 | incidentElement = (Element)incidents.item(k); |
|---|
| 162 | |
|---|
| 163 | theController.addIncident(new ImageIncident( |
|---|
| 164 | Integer.parseInt(incidentElement.getAttribute( |
|---|
| 165 | IMAGE_DB_TAGS.LOG_NUM.tag)), |
|---|
| 166 | incidentElement.getAttribute( |
|---|
| 167 | IMAGE_DB_TAGS.FILENAME.tag))); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | stillImageMap.put(new CCTVInfo(cctvID, cctvDirection), theController); |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | * Gets the ImageController associated with the parameter CCTV_ID number and direction. |
|---|
| 176 | * |
|---|
| 177 | * @param cctv_id Integer value of the CCTV camera. |
|---|
| 178 | * @param dir Direction value of the CCTV camera. |
|---|
| 179 | * @return ImageController object for CCTV_ID and direction, or null if no object is found. |
|---|
| 180 | */ |
|---|
| 181 | public ImageController getController(Integer cctv_id, CCTVDirections dir) { |
|---|
| 182 | return stillImageMap.get(new CCTVInfo(cctv_id, dir)); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|