Ignore:
Timestamp:
06/23/2016 06:30:20 PM (10 years ago)
Author:
jdalbey
Message:

MultiFile? commit: Add CADViewer Interface and change CADSimulator to Observer Pattern.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/cadsimulator/managers/ParamicsSimulationManager.java

    r20 r44  
    66import java.util.logging.Level; 
    77import java.util.logging.Logger; 
    8  
    98import tmcsim.cadmodels.CMSInfo; 
    109import tmcsim.cadsimulator.Coordinator; 
     
    1312import tmcsim.cadsimulator.paramicscontrol.ParamicsIncidentWriter; 
    1413import tmcsim.cadsimulator.paramicscontrol.ParamicsStatusReader; 
     14import tmcsim.common.CADEnums.PARAMICS_STATUS; 
    1515import tmcsim.common.SimulationException; 
    1616import tmcsim.common.XMLIncident; 
    17 import tmcsim.common.CADEnums.PARAMICS_STATUS; 
    1817 
    1918/** 
     
    2221 * Upon construction all ParamicsReaders and ParamicsWriters are initialized 
    2322 * with data parsed from the Properties file. When a connection is established 
    24  * the ParamicsStatusReader is registered to read Paramics Status updates.   
    25  * These updates are received through the updateParamicsStatus() method, which  
    26  * notifies the Coordinator of the new status.  The loadParamicsNetwork() must  
    27  * be called to register the ParamicsIncidentWriter which will write the 
    28  * information to cause Paramics to begin loading a traffic network.  When the 
     23 * the ParamicsStatusReader is registered to read Paramics Status updates. These 
     24 * updates are received through the updateParamicsStatus() method, which 
     25 * notifies the Coordinator of the new status. The loadParamicsNetwork() must be 
     26 * called to register the ParamicsIncidentWriter which will write the 
     27 * information to cause Paramics to begin loading a traffic network. When the 
    2928 * status becomes LOADED, the ParamicsCameraStatusReader is registered and 
    30  * begins reading.  All camera speed updates are received through the 
    31  * updateCameraInfo() method.  The startSimulation(), resetSimulation(), and  
    32  * sendIncidentUpdate() methods are used to control the flow of Incident  
    33  * update information to Paramics.  The updateIncident() and updateDiversion()  
    34  * methods are used to update the information that is sent to Paramics. 
    35  *   
    36  *       
     29 * begins reading. All camera speed updates are received through the 
     30 * updateCameraInfo() method. The startSimulation(), resetSimulation(), and 
     31 * sendIncidentUpdate() methods are used to control the flow of Incident update 
     32 * information to Paramics. The updateIncident() and updateDiversion() methods 
     33 * are used to update the information that is sent to Paramics. 
     34 * 
     35 * 
    3736 * @see PARAMICS_STATUS 
    3837 * @author Matthew Cechini 
    3938 * @version 
    4039 */ 
    41 public class ParamicsSimulationManager { 
    42  
    43     /**  Error logger.  */ 
     40public class ParamicsSimulationManager 
     41{ 
     42 
     43    /** 
     44     * Error logger. 
     45     */ 
    4446    private static Logger paramLogger = Logger.getLogger("tmcsim.cadsimulator.paramicscontrol"); 
    45      
     47 
    4648    /** 
    4749     * Enumeration containing property names for Properties parsing. 
     50     * 
    4851     * @author Matthew Cechini 
    4952     */ 
    50     private static enum PROPERTIES { 
    51         PARAMICS_HOST        ("ParamicsCommHost"), 
    52         PARAMICS_PORT        ("ParamicsCommPort"), 
    53         INCIDENT_UPDATE_INT  ("IncidentUpdateInterval"), 
    54         INCIDENT_UPDATE_FILE ("IncidentUpdateFile"), 
    55         PARAMICS_STATUS_INT  ("ParamicsStatusInterval"), 
    56         PARAMICS_STATUS_FILE ("ParamicsStatusFile"), 
    57         CAMERA_STATUS_INT    ("CameraStatusInterval"), 
    58         CAMERA_STATUS_FILE   ("CameraStatusFile"); 
    59          
     53    private static enum PROPERTIES 
     54    { 
     55 
     56        PARAMICS_HOST("ParamicsCommHost"), 
     57        PARAMICS_PORT("ParamicsCommPort"), 
     58        INCIDENT_UPDATE_INT("IncidentUpdateInterval"), 
     59        INCIDENT_UPDATE_FILE("IncidentUpdateFile"), 
     60        PARAMICS_STATUS_INT("ParamicsStatusInterval"), 
     61        PARAMICS_STATUS_FILE("ParamicsStatusFile"), 
     62        CAMERA_STATUS_INT("CameraStatusInterval"), 
     63        CAMERA_STATUS_FILE("CameraStatusFile"); 
    6064        String name; 
    61          
    62         private PROPERTIES(String n) { 
     65 
     66        private PROPERTIES(String n) 
     67        { 
    6368            name = n; 
    6469        } 
    6570    }; 
    66      
    67     /** ParamicsCommunicator Object used for communication. */ 
     71    /** 
     72     * ParamicsCommunicator Object used for communication. 
     73     */ 
    6874    private ParamicsCommunicator theCommunicator; 
    69  
    70     /** Reference to the Coordinator Object. */ 
     75    /** 
     76     * Reference to the Coordinator Object. 
     77     */ 
    7178    private Coordinator theCoordinator; 
    72  
    73     /** Reference to the MediaManager Object. */ 
     79    /** 
     80     * Reference to the MediaManager Object. 
     81     */ 
    7482    private MediaManager theMediaMgr; 
    75      
    76     /** ParamicsIncidentWriter used to send incident updates to Paramics. */ 
    77     private ParamicsIncidentWriter paramicsIncidentWriter;     
    78      
    79     /** ParamicsStatusReader used to read status information from Paramics. */ 
     83    /** 
     84     * ParamicsIncidentWriter used to send incident updates to Paramics. 
     85     */ 
     86    private ParamicsIncidentWriter paramicsIncidentWriter; 
     87    /** 
     88     * ParamicsStatusReader used to read status information from Paramics. 
     89     */ 
    8090    private ParamicsStatusReader paramicsStatusReader; 
    81      
    82     /** ParamicsCameraStatusReader used to read speed information from Paramics. */ 
     91    /** 
     92     * ParamicsCameraStatusReader used to read speed information from Paramics. 
     93     */ 
    8394    private ParamicsCameraStatusReader paramicsCameraStatusReader; 
    84  
    85     /** Properties file. */ 
     95    /** 
     96     * Properties file. 
     97     */ 
    8698    private Properties paramicsProperties; 
    87      
    88      
    89     /** 
    90      * Constructor.  Loads the Properties file and initializes all Paramics 
     99 
     100    /** 
     101     * Constructor. Loads the Properties file and initializes all Paramics 
    91102     * Writers and Readers with the parsed data. 
    92      *  
     103     * 
    93104     * @param propertiesFile Target file path of properties file. 
    94105     * @param coor Coordinator Object. 
    95106     * @param mediaMgr MediaManager Object. 
    96107     */ 
    97     public ParamicsSimulationManager(String propertiesFile, Coordinator coor, MediaManager mediaMgr) { 
    98          
    99         try { 
     108    public ParamicsSimulationManager(String propertiesFile, Coordinator coor, MediaManager mediaMgr) 
     109    { 
     110 
     111        try 
     112        { 
    100113            paramicsProperties = new Properties(); 
    101114            paramicsProperties.load(new FileInputStream(propertiesFile)); 
    102              
     115 
    103116            theCoordinator = coor; 
    104             theMediaMgr    = mediaMgr; 
    105                    
    106             theCommunicator = new ParamicsCommunicator(this,  
     117            theMediaMgr = mediaMgr; 
     118 
     119            theCommunicator = new ParamicsCommunicator(this, 
    107120                    paramicsProperties.getProperty(PROPERTIES.PARAMICS_HOST.name), 
    108121                    Integer.parseInt(paramicsProperties.getProperty( 
    109                             PROPERTIES.PARAMICS_PORT.name).trim())); 
    110                      
     122                    PROPERTIES.PARAMICS_PORT.name).trim())); 
     123 
    111124            paramicsIncidentWriter = new ParamicsIncidentWriter(Integer.parseInt( 
    112                     paramicsProperties.getProperty(PROPERTIES.INCIDENT_UPDATE_INT.name).trim()));   
    113             paramicsIncidentWriter.writerID   = theCommunicator.nextID(); 
     125                    paramicsProperties.getProperty(PROPERTIES.INCIDENT_UPDATE_INT.name).trim())); 
     126            paramicsIncidentWriter.writerID = theCommunicator.nextID(); 
    114127            paramicsIncidentWriter.targetFile = paramicsProperties.getProperty( 
    115                     PROPERTIES.INCIDENT_UPDATE_FILE.name);       
    116              
     128                    PROPERTIES.INCIDENT_UPDATE_FILE.name); 
     129 
    117130            paramicsStatusReader = new ParamicsStatusReader(this); 
    118             paramicsStatusReader.readerID   = theCommunicator.nextID(); 
    119             paramicsStatusReader.interval   = paramicsProperties.getProperty( 
     131            paramicsStatusReader.readerID = theCommunicator.nextID(); 
     132            paramicsStatusReader.interval = paramicsProperties.getProperty( 
    120133                    PROPERTIES.PARAMICS_STATUS_INT.name).trim(); 
    121134            paramicsStatusReader.targetFile = paramicsProperties.getProperty( 
    122135                    PROPERTIES.PARAMICS_STATUS_FILE.name); 
    123              
     136 
    124137            paramicsCameraStatusReader = new ParamicsCameraStatusReader(this); 
    125             paramicsCameraStatusReader.readerID   = theCommunicator.nextID(); 
    126             paramicsCameraStatusReader.interval   = paramicsProperties.getProperty( 
     138            paramicsCameraStatusReader.readerID = theCommunicator.nextID(); 
     139            paramicsCameraStatusReader.interval = paramicsProperties.getProperty( 
    127140                    PROPERTIES.CAMERA_STATUS_INT.name).trim(); 
    128141            paramicsCameraStatusReader.targetFile = paramicsProperties.getProperty( 
    129                     PROPERTIES.CAMERA_STATUS_FILE.name);     
    130         } 
    131         catch (Exception e) { 
    132             paramLogger.logp(Level.SEVERE, "ParamicsSimulationManager",  
     142                    PROPERTIES.CAMERA_STATUS_FILE.name); 
     143        } catch (Exception e) 
     144        { 
     145            paramLogger.logp(Level.SEVERE, "ParamicsSimulationManager", 
    133146                    "Constructor", "Exception in parsing properties file.", e); 
    134147        } 
    135          
    136     } 
    137     
     148 
     149    } 
     150 
    138151    /** 
    139152     * Returns whether a connection exists to the remote ParamicsCommunicator. 
     153     * 
    140154     * @return true if a connection exists, false if not. 
    141155     */ 
    142     public boolean isConnected() { 
     156    public boolean isConnected() 
     157    { 
    143158        return theCommunicator.isConnected(); 
    144159    } 
    145      
     160 
    146161    /** 
    147162     * Returns the integer Network ID that has been loaded into Paramics. 
     163     * 
    148164     * @return Network ID. 
    149165     */ 
    150     public int getParamicsNetworkLoaded() { 
     166    public int getParamicsNetworkLoaded() 
     167    { 
    151168        return paramicsStatusReader.getNetworkID(); 
    152169    } 
    153      
     170 
    154171    /** 
    155172     * Returns the current status of the Paramics traffic modeler. 
    156      * @return Current PARAMICS_STATUS  
    157      */ 
    158     public PARAMICS_STATUS getParamicsStatus() { 
     173     * 
     174     * @return Current PARAMICS_STATUS 
     175     */ 
     176    public PARAMICS_STATUS getParamicsStatus() 
     177    { 
    159178        return paramicsStatusReader.getStatus(); 
    160179    } 
    161      
     180 
    162181    /** 
    163182     * Updates the ParamicsIncidentWriter with new Incident information. 
     183     * 
    164184     * @param xmlInc Updated Incident information. 
    165185     */ 
    166     public void updateIncident(XMLIncident xmlInc) { 
     186    public void updateIncident(XMLIncident xmlInc) 
     187    { 
    167188        paramicsIncidentWriter.updateIncident(xmlInc); 
    168189    } 
    169      
     190 
    170191    /** 
    171192     * Updates the ParamicsIncidentWriter with the new Diversion information. 
     193     * 
    172194     * @param theDiversion Updated diversion information. 
    173195     */ 
    174     public void updateDiversion(CMSInfo theDiversion) { 
     196    public void updateDiversion(CMSInfo theDiversion) 
     197    { 
    175198        paramicsIncidentWriter.updateDiversion(theDiversion); 
    176199    } 
    177      
     200 
    178201    /** 
    179202     * Notifies the ParamicsIncidentWriter to start the simulation. 
    180203     */ 
    181     public void startSimulation() { 
     204    public void startSimulation() 
     205    { 
    182206        paramicsIncidentWriter.startSimulation(); 
    183207    } 
    184      
     208 
    185209    /** 
    186210     * Notifies the ParamicsIncidentWriter to reset the simulation. 
    187211     */ 
    188     public void resetSimulation() { 
     212    public void resetSimulation() 
     213    { 
    189214        paramicsIncidentWriter.resetSimulation(); 
    190215    } 
     
    192217    /** 
    193218     * Notifies the ParamicsIncidentWriter to send an Incident update. 
     219     * 
    194220     * @param currentSimTime Current simulation time (in seconds). 
    195221     */ 
    196     public void sendIncidentUpdate(long currentSimTime) { 
     222    public void sendIncidentUpdate(long currentSimTime) 
     223    { 
    197224        paramicsIncidentWriter.sendUpdate(currentSimTime); 
    198225    } 
    199      
    200     /** 
    201      * Establish a connection to the remote ParamicsCommunicator.  Register 
    202      * the ParamicsStatusReader and update the current status to CONNECTED. 
    203      * If an exception occurs in connecting to the ParamicsCommunicator, 
    204      * update the current status to UNREACHABLE. 
    205      */ 
    206     public void connectToParamics() { 
    207         try {            
    208             theCommunicator.connect();           
     226 
     227    /** 
     228     * Establish a connection to the remote ParamicsCommunicator. Register the 
     229     * ParamicsStatusReader and update the current status to CONNECTED. If an 
     230     * exception occurs in connecting to the ParamicsCommunicator, update the 
     231     * current status to UNREACHABLE. 
     232     */ 
     233    public void connectToParamics() 
     234    { 
     235        try 
     236        { 
     237            theCommunicator.connect(); 
    209238            theCommunicator.registerReader(paramicsStatusReader); 
    210              
     239 
    211240            updateParamicsStatus(PARAMICS_STATUS.CONNECTED); 
    212         } 
    213         catch (IOException ioe) { 
    214             paramLogger.logp(Level.SEVERE, "Coordinator", "connectToParamics",  
     241        } catch (IOException ioe) 
     242        { 
     243            paramLogger.logp(Level.SEVERE, "Coordinator", "connectToParamics", 
    215244                    "Communication error in connecting to Paramics.", ioe); 
    216245 
    217246            updateParamicsStatus(PARAMICS_STATUS.UNREACHABLE); 
    218         }        
    219     } 
    220  
    221     /** 
    222      * Close the connection to the remote ParamicsCommunicator.  Unregister 
    223      * all Readers and Wrtiers from the ParamicsCommunicator. Reset the status 
    224      * information in the ParamicsStatusReader.  Update the current status to  
     247        } 
     248    } 
     249 
     250    /** 
     251     * Close the connection to the remote ParamicsCommunicator. Unregister all 
     252     * Readers and Wrtiers from the ParamicsCommunicator. Reset the status 
     253     * information in the ParamicsStatusReader. Update the current status to 
    225254     * DISCONNECTED. 
    226255     */ 
    227     public void disconnectFromParamics() {  
    228          
     256    public void disconnectFromParamics() 
     257    { 
     258 
    229259        theCommunicator.disconnect(); 
    230260        theCommunicator.unregisterReader(paramicsStatusReader); 
    231         theCommunicator.unregisterWriter(paramicsIncidentWriter);    
     261        theCommunicator.unregisterWriter(paramicsIncidentWriter); 
    232262        theCommunicator.unregisterReader(paramicsCameraStatusReader); 
    233                      
     263 
    234264        paramicsStatusReader.resetStatusInfo(); 
    235265 
    236266        updateParamicsStatus(PARAMICS_STATUS.DISCONNECTED); 
    237267    } 
    238      
    239     /** 
    240      * Updates the current paramics status.  If the new status is LOADED, 
    241      * then notify the ParamicsIncidentWriter that the network has been loaded 
    242      * and register the ParamicsCameraStatusReader with the ParamicsCommunicator. 
     268 
     269    /** 
     270     * Updates the current paramics status. If the new status is LOADED, then 
     271     * notify the ParamicsIncidentWriter that the network has been loaded and 
     272     * register the ParamicsCameraStatusReader with the ParamicsCommunicator. 
    243273     * Notify the Coordinator of all Paramics status updates. 
    244274     * 
    245      * @param  newStatus New Paramics status. 
    246      */  
    247     public void updateParamicsStatus(PARAMICS_STATUS newStatus) { 
    248                          
     275     * @param newStatus New Paramics status. 
     276     */ 
     277    public void updateParamicsStatus(PARAMICS_STATUS newStatus) 
     278    { 
     279 
    249280        //the network has finished loading 
    250         if(newStatus == PARAMICS_STATUS.LOADED) { 
    251          
     281        if (newStatus == PARAMICS_STATUS.LOADED) 
     282        { 
     283 
    252284            paramicsIncidentWriter.networkLoaded(); 
    253          
    254             theCommunicator.registerReader(paramicsCameraStatusReader);                      
    255         } 
    256         else if(newStatus == PARAMICS_STATUS.DROPPED) { 
    257             paramLogger.logp(Level.WARNING, "Coordinator", "updateParamicsStatus",  
     285 
     286            theCommunicator.registerReader(paramicsCameraStatusReader); 
     287 
     288        } else if (newStatus == PARAMICS_STATUS.DROPPED) 
     289        { 
     290            paramLogger.logp(Level.WARNING, "Coordinator", "updateParamicsStatus", 
    258291                    "Connection to Paramics has been dropped."); 
    259         }else 
    260         { 
    261             paramLogger.logp(Level.INFO, "ParamicsSimulationManager", "updateParamicsStatus", 
    262                     "Received: " + newStatus); 
    263         } 
    264          
    265         theCoordinator.setParamicsStatus(newStatus);         
    266  
    267     }  
    268      
    269     /** 
    270      * If a connection has been made, register the ParamicsIncidentWriter 
    271      * and use it to notify Paramics of the Network ID to load. 
    272      *  
     292        } 
     293 
     294        theCoordinator.setParamicsStatus(newStatus); 
     295 
     296    } 
     297 
     298    /** 
     299     * If a connection has been made, register the ParamicsIncidentWriter and 
     300     * use it to notify Paramics of the Network ID to load. 
     301     * 
    273302     * @param networkID Network ID to load. 
    274      * @throws SimulationException if a connection has not been made to the  
    275      *         remote ParamicsCommunicator. 
    276      */ 
    277     public void loadParamicsNetwork(int networkID) throws SimulationException { 
    278              
    279         if(theCommunicator.isConnected()) { 
     303     * @throws SimulationException if a connection has not been made to the 
     304     * remote ParamicsCommunicator. 
     305     */ 
     306    public void loadParamicsNetwork(int networkID) throws SimulationException 
     307    { 
     308 
     309        if (theCommunicator.isConnected()) 
     310        { 
    280311            theCommunicator.registerWriter(paramicsIncidentWriter); 
    281312            paramicsIncidentWriter.loadNetwork(networkID); 
    282         } 
    283         else  
     313        } else 
     314        { 
    284315            throw new SimulationException(SimulationException.PARAMICS_NOT_CONNECTED); 
    285     } 
    286      
    287     /** 
    288      * Receive updated camera speed information. Notify the MediaManager with  
    289      * the updated data.  
    290      *  
     316        } 
     317    } 
     318 
     319    /** 
     320     * Receive updated camera speed information. Notify the MediaManager with 
     321     * the updated data. 
     322     * 
    291323     * @param cameraID CCTV camera ID 
    292324     * @param avgSpeed_NE Average speed of traffic flowing N or E 
    293325     * @param avgSpeed_SW Average speed of traffic flowing S or W 
    294326     */ 
    295     public void updateCameraInfo(Integer cameraID, float avgSpeed_NE, float avgSpeed_SW) { 
     327    public void updateCameraInfo(Integer cameraID, float avgSpeed_NE, float avgSpeed_SW) 
     328    { 
    296329        theMediaMgr.updateCameraInfo(cameraID, avgSpeed_NE, avgSpeed_SW); 
    297330    } 
Note: See TracChangeset for help on using the changeset viewer.