Changeset 654 in tmcsimulator for trunk/src/tmcsim/cadsimulator/managers/TrafficModelManager.java
- Timestamp:
- 09/09/2022 04:11:08 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tmcsim/cadsimulator/managers/TrafficModelManager.java
r524 r654 30 30 import javax.swing.JWindow; 31 31 import javax.swing.Timer; 32 import java.util.TimerTask; 32 33 import javax.swing.UIManager; 33 34 import tmcsim.cadsimulator.Coordinator; … … 146 147 loadEvents(); 147 148 149 java.util.Timer t = new java.util.Timer(); 150 148 151 // Create a timer that fetches the simulation time every second. 149 Timer timer = new Timer(ONE_SECOND, new ActionListener() 150 { 151 // Every second, see if an event should be launched 152 public void actionPerformed(ActionEvent e) 152 t.scheduleAtFixedRate( 153 new TimerTask() 153 154 { 154 String currentATMStime = ""; 155 Date simClock = new Date(); 156 // Obtain the simulation time from the CAD server 157 try 155 // Every second, see if an event should be launched 156 public void run() 158 157 { 159 long simtime = theCoordinator.getCurrentSimulationTime(); 160 currentClock = theCoordinator.formatTimeInSeconds(simtime); 161 // For Debugging, show the ATMS time 162 // long ATMStime = theCoorInt.getATMStime(); 163 // Date atmsdate = new Date(ATMStime); 164 // currentATMStime = formatter.format(atmsdate); 158 String currentATMStime = ""; 159 Date simClock = new Date(); 160 // Obtain the simulation time from the CAD server 165 161 try 166 162 { 167 simClock = formatter.parse(currentClock); 163 long simtime = theCoordinator.getCurrentSimulationTime(); 164 currentClock = theCoordinator.formatTimeInSeconds(simtime); 165 // For Debugging, show the ATMS time 166 // long ATMStime = theCoorInt.getATMStime(); 167 // Date atmsdate = new Date(ATMStime); 168 // currentATMStime = formatter.format(atmsdate); 169 try 170 { 171 simClock = formatter.parse(currentClock); 172 } 173 catch (ParseException ex) 174 { 175 Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 176 System.out.println("Invalid simulation clock time found"); 177 System.exit(-1); 178 } 168 179 } 169 catch ( ParseException ex)180 catch (RemoteException ex) 170 181 { 182 System.out.println("Remote Exception reading sim or ATMS clock time"); 171 183 Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 172 System.out.println("Invalid simulation clock time found"); 173 System.exit(-1); 184 } 185 // If we have any events left to process 186 if (!eventQueue.isEmpty()) 187 { 188 // Get the time to launch the next event 189 TrafficEvent nextEvent = eventQueue.peek(); 190 Date eventTime = nextEvent.eventDate; 191 // Check the queue of events to see if the first 192 // item should be launched. IF so, 193 // issue that command and remove it from queue. 194 if (eventTime.before(simClock) || eventTime.equals(simClock)) 195 { 196 System.out.println("LAUNCHING EVENT: " + nextEvent.toString()); 197 // apply colorization to highways 198 highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, 199 nextEvent.postmile, nextEvent.range, nextEvent.color); 200 // Remove this event from the queue, we're done with it. 201 eventQueue.remove(); 202 setChanged(); 203 // Send updated list to view 204 // notifyObservers(getEventQueue()); 205 // Notify view it should scroll to next event 206 notifyObservers(new Integer(0)); 207 } 208 setChanged(); 209 notifyObservers(currentClock); 174 210 } 175 211 } 176 catch (RemoteException ex) 177 { 178 System.out.println("Remote Exception reading sim or ATMS clock time"); 179 Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 180 } 181 // If we have any events left to process 182 if (!eventQueue.isEmpty()) 183 { 184 // Get the time to launch the next event 185 TrafficEvent nextEvent = eventQueue.peek(); 186 Date eventTime = nextEvent.eventDate; 187 // Check the queue of events to see if the first 188 // item should be launched. IF so, 189 // issue that command and remove it from queue. 190 if (eventTime.before(simClock) || eventTime.equals(simClock)) 191 { 192 System.out.println("LAUNCHING EVENT: " + nextEvent.toString()); 193 // apply colorization to highways 194 highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, 195 nextEvent.postmile, nextEvent.range, nextEvent.color); 196 // Remove this event from the queue, we're done with it. 197 eventQueue.remove(); 198 setChanged(); 199 // Send updated list to view 200 // notifyObservers(getEventQueue()); 201 // Notify view it should scroll to next event 202 notifyObservers(new Integer(0)); 203 } 204 setChanged(); 205 notifyObservers(currentClock); 206 } 207 } 208 }); 209 timer.start(); 210 211 // Always write to json for google map display 212 Thread wtJson = new WriteToJsonThread(); 212 }, 213 0, 214 ONE_SECOND 215 ); 216 217 // Also write to json for google map display, every ten seconds. 218 WriteToJsonTask wtJson = new WriteToJsonTask(); 213 219 wtJson.start(); 220 System.out.println("Traffic Model Mgr init complete."); 214 221 } 215 222 /** Accessor to event queue … … 401 408 * by Google Maps. 402 409 */ 403 class WriteToJsonThread extends Thread 404 { 405 406 public void run() 407 { 408 System.out.println("WriteToJson Thread starting."); 409 // Run indefinitely 410 while (true) 411 { 412 // Write the highway network status to Json 413 String geojson = highways.toJson(); 414 PrintWriter out; 415 try 410 class WriteToJsonTask 411 { 412 void start() 413 { 414 System.out.println("WriteToJson Task starting."); 415 java.util.Timer tasktimer = new java.util.Timer(); 416 tasktimer.scheduleAtFixedRate( 417 new TimerTask() 416 418 { 417 // currently writes to local file 418 out = new PrintWriter(jsonPath); 419 out.print(geojson); 420 out.close(); 421 } 422 catch (FileNotFoundException ex) 423 { 424 Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 425 } 426 // Wait for Google Map to process the data we just sent 427 try 428 { 429 Thread.sleep(10000); 430 } 431 catch (InterruptedException ie) 432 { 433 ie.printStackTrace(); 434 } 435 } 436 419 public void run() 420 { 421 String geojson = highways.toJson(); 422 PrintWriter out; 423 try 424 { 425 // currently writes to local file 426 out = new PrintWriter(jsonPath); 427 out.print(geojson); 428 out.close(); 429 } 430 catch (FileNotFoundException ex) 431 { 432 Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, ex); 433 } 434 } 435 }, 436 0, // run first occurrence now 437 10000); // run every ten seconds 437 438 } 438 439 }
Note: See TracChangeset
for help on using the changeset viewer.
