| 1 | package tmcsim.cadsimulator.paramicscontrol; |
|---|
| 2 | |
|---|
| 3 | import java.io.ByteArrayInputStream; |
|---|
| 4 | import java.util.logging.Level; |
|---|
| 5 | |
|---|
| 6 | import javax.xml.parsers.SAXParserFactory; |
|---|
| 7 | |
|---|
| 8 | import org.w3c.dom.Node; |
|---|
| 9 | import org.xml.sax.SAXParseException; |
|---|
| 10 | import org.xml.sax.helpers.DefaultHandler; |
|---|
| 11 | |
|---|
| 12 | import tmcsim.cadsimulator.managers.ParamicsSimulationManager; |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * ParamicsCameraStatusReader extends from the Abstract ParamicsReader to |
|---|
| 17 | * provide the methods necessary to read the Paramics Camrea Status file. |
|---|
| 18 | * The receive() method is overloaded to parse the XML node for camrea status |
|---|
| 19 | * information. The ParamicsSimulationManager is notified after each |
|---|
| 20 | * successful parse of a camera status update. |
|---|
| 21 | * |
|---|
| 22 | * @author Matthew Cechini (mcechini@calpoly.edu) |
|---|
| 23 | * @version $Date: 2006/06/06 20:46:40 $ $Revision: 1.4 $ |
|---|
| 24 | */ |
|---|
| 25 | public class ParamicsCameraStatusReader extends ParamicsReader { |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * Reference to the ParamicsSimulationManager used to send notifications |
|---|
| 29 | * of camera status changes. |
|---|
| 30 | */ |
|---|
| 31 | private ParamicsSimulationManager paramicsSimMgr; |
|---|
| 32 | |
|---|
| 33 | /** A SAX Handler that is used to parse received Camera Status Node. */ |
|---|
| 34 | protected CameraStatusHandler csh = null; |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Constructor. |
|---|
| 38 | * |
|---|
| 39 | * @param theParamSimMgr The ParamicsSimulationManager object. |
|---|
| 40 | */ |
|---|
| 41 | public ParamicsCameraStatusReader(ParamicsSimulationManager theParamSimMgr) { |
|---|
| 42 | paramicsSimMgr = theParamSimMgr; |
|---|
| 43 | csh = new CameraStatusHandler(); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * This method parses the received XML node with the local CameraStatusHandler. |
|---|
| 48 | * All updated camera information is sent to the ParamicsSimulationManager. |
|---|
| 49 | */ |
|---|
| 50 | public void receive(Node rxMessage) { |
|---|
| 51 | |
|---|
| 52 | try { |
|---|
| 53 | if(rxMessage.getTextContent().length() > 0) { |
|---|
| 54 | SAXParserFactory.newInstance().newSAXParser().parse( |
|---|
| 55 | new ByteArrayInputStream(rxMessage.getTextContent().getBytes()), csh); |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | catch (Exception e) { |
|---|
| 59 | paramLogger.logp(Level.SEVERE, "ParamicsCameraStatusReader", "receive", |
|---|
| 60 | "Exception in parsing received ParamicsCommMessage.", e); |
|---|
| 61 | paramLogger.logp(Level.INFO, "ParamicsCameraStatusReader", "receive", |
|---|
| 62 | "Invalid received bytes", rxMessage.getTextContent().getBytes()); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Internal SAX Handler used to parse the Camera Status Document read by |
|---|
| 69 | * the remote Status Reader. The schema for this document is: <br/> |
|---|
| 70 | * |
|---|
| 71 | * <Camera_Status> <br> |
|---|
| 72 | * <Camera> <br> |
|---|
| 73 | * <Identifier/> <br> |
|---|
| 74 | * <Route/> <br> |
|---|
| 75 | * <Direction/> <br> |
|---|
| 76 | * <Postmile/> <br> |
|---|
| 77 | * <Ave_Speed_NE/> <br> |
|---|
| 78 | * <Ave_Speed_SW/> <br> |
|---|
| 79 | * </Camera> <br> |
|---|
| 80 | * </Camera_Status> <br> |
|---|
| 81 | */ |
|---|
| 82 | protected class CameraStatusHandler extends DefaultHandler { |
|---|
| 83 | |
|---|
| 84 | private final String CAMERA = "Camera"; |
|---|
| 85 | private final String CAMERA_ID = "Identifier"; |
|---|
| 86 | private final String ROUTE = "Route"; |
|---|
| 87 | private final String DIRECTION = "Direction"; |
|---|
| 88 | private final String POSTMILE = "Postmile"; |
|---|
| 89 | private final String AVG_SPEED_NE = "Ave_Speed_NE"; |
|---|
| 90 | private final String AVG_SPEED_SW = "Ave_Speed_SW"; |
|---|
| 91 | |
|---|
| 92 | private StringBuffer parsedValue = new StringBuffer(); |
|---|
| 93 | |
|---|
| 94 | Integer cameraID = new Integer(-1); |
|---|
| 95 | String route = ""; |
|---|
| 96 | String direction = ""; |
|---|
| 97 | float postmile = 0.0f; |
|---|
| 98 | float avgSpeed_NE = 0.0f; |
|---|
| 99 | float avgSpeed_SW = 0.0f; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | public void startDocument() { |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | public void characters(char[] ch, int start, int length) { |
|---|
| 106 | parsedValue.append(new String(ch, start, length).trim()); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | public void endElement(String uri, String localName, String qName) { |
|---|
| 110 | |
|---|
| 111 | if(qName.equals(CAMERA_ID)) { cameraID = Integer.parseInt(parsedValue.toString()); } |
|---|
| 112 | else if(qName.equals(ROUTE)) { route = parsedValue.toString(); } |
|---|
| 113 | else if(qName.equals(DIRECTION)) { direction = parsedValue.toString(); } |
|---|
| 114 | else if(qName.equals(POSTMILE)) { postmile = Float.parseFloat(parsedValue.toString()); } |
|---|
| 115 | else if(qName.equals(AVG_SPEED_NE)) { avgSpeed_NE = Float.parseFloat(parsedValue.toString()); } |
|---|
| 116 | else if(qName.equals(AVG_SPEED_SW)) { avgSpeed_SW = Float.parseFloat(parsedValue.toString()); } |
|---|
| 117 | else if(qName.equals(CAMERA)) { |
|---|
| 118 | paramicsSimMgr.updateCameraInfo(cameraID, avgSpeed_NE, avgSpeed_SW); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | parsedValue.setLength(0); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | public void error(SAXParseException e) { |
|---|
| 125 | paramLogger.logp(Level.SEVERE, "ParamicsCameraStatusReader:CameraStatusHandler", |
|---|
| 126 | "error", "Error in parsing received ParamicsCommMessage.", e); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public void fatalError(SAXParseException e) { |
|---|
| 130 | paramLogger.logp(Level.SEVERE, "ParamicsCameraStatusReader:CameraStatusHandler", |
|---|
| 131 | "error", "Fatal error in parsing received ParamicsCommMessage.", e); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | public void warning(SAXParseException e) { |
|---|
| 135 | paramLogger.logp(Level.WARNING, "ParamicsCameraStatusReader:CameraStatusHandler", |
|---|
| 136 | "error", "Warning in parsing received ParamicsCommMessage.", e); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | } |
|---|