source: tmcsimulator/trunk/webapps/einotebook/scripts/LoadEvents.js @ 429

Revision 429, 8.2 KB checked in by jdalbey, 7 years ago (diff)

Add CMS Evaluations to LoadEvents? for EINotebook. Fixes #147.

Line 
1/* author: jdalbey    date: 4/10/2019 */
2
3// Parse the entire Incident XML Script file
4// Extract the Incidents and Events and create lists of each
5function parseXml(response)
6{
7    // Create a parser and grab the high level tag we're interested in
8    var parser = new DOMParser();
9    var xmlDoc = parser.parseFromString(response,"text/xml");
10    var eventTags = xmlDoc.getElementsByTagName("SCRIPT_EVENT");
11    console.log("parsing incident xml file");
12    // Process each SCRIPT_EVENT tag
13    for (var i = 0; i < eventTags.length; i++)
14    {
15        var currEvt = eventTags[i];
16        // local variable declarations
17        var timeFields; var evtTime; var incidentNum; var incidentTitle; var cadProp; var telProp;
18        var proparray = new Array();
19        var evalarray = new Array();
20        // Process all the children of one event
21        for (var child = 1; child < currEvt.childNodes.length; child++)
22        {
23            // Ignore undefined nodes
24            if (currEvt.childNodes[child].localName != undefined)
25            {
26                var tagName = currEvt.childNodes[child].localName.toUpperCase();
27                // Determine the tag type and dispatch it for further processing
28                switch(tagName)
29                {
30                    case "TIME_INDEX": 
31                        timeFields = currEvt.childNodes[child].textContent.split(":"); 
32                        evtTime = new Time(Number(timeFields[0]), Number(timeFields[1]), Number(timeFields[2]));
33                        break;
34                    case "INCIDENT":
35                        incidentNum = Number(currEvt.childNodes[child].attributes["LogNum"].value);
36                        incidentTitle =  currEvt.childNodes[child].textContent;
37                        break;
38                    case "GENERAL_INFO": 
39                        /* This tag identifies a new incident.  As long as it appears before any
40                        other tags, we can use it to create a new incident. (The alternative is to
41                        have a separate function that parses xml to extract incidents before
42                        we load events.)  Creating an event (below) requires that the incident has already
43                        been created. */
44                        var desc = currEvt.childNodes[child].getElementsByTagName("TEXT")[0].textContent;
45                        var theIncident = new Incident(evtTime, incidentNum, incidentTitle, desc);
46                        incidents.add(theIncident);
47                        break;
48                    case "CAD_DATA": 
49                        var caddata = parseCAD(currEvt.childNodes[child]); 
50                        if (caddata.length > 0)
51                        {
52                            cadProp = new Property("CHP CAD", caddata ); 
53                            proparray.push(cadProp);
54                        }
55                        break;
56                    case "TELEPHONE": 
57                        telProp = new Evaluation("TELEPHONE CONVERSATION", 
58                                 parseTelephone(currEvt.childNodes[child]) ); 
59                        evalarray.push(telProp);
60                        break;
61                    case "CHP_RADIO": 
62                        var chpradio = parseCHPradio(currEvt.childNodes[child]); 
63                        if (chpradio.length > 0)
64                        {
65                            cadProp = new Property("CHP RADIO", chpradio ); 
66                            proparray.push(cadProp);
67                        }
68                        break;
69
70                    case "TMT_RADIO": 
71                    case "MAINTENANCE_RADIO": 
72                        var result = new Array();   
73                        result.push("Details:");
74                        result.push(currEvt.childNodes[child].textContent.trim());
75                        var radProp = new Property(tagName, result ); 
76                        proparray.push(radProp);
77                        break;
78
79                    case "FACILITATOR_EVALUATION": 
80                    case "CAD_EVALUATION":
81                    case "ATMS_EVALUATION":
82                    case "ACTIVITY_LOG_EVALUATION":
83                    case "RADIO_EVALUATION":
84                        // remove the suffix
85                        var evalType = tagName.replace("_EVALUATION","");
86                        // Build the evaluation item
87                        var evalItem = new Evaluation(evalType, 
88                                 parseEvaluation(currEvt.childNodes[child]) ); 
89                        evalarray.push(evalItem);
90                        break;
91                       
92                    case "CMS_EVALUATION":
93                        var cmsEval = new Evaluation("CMS", parseCMSEvaluation(currEvt.childNodes[child]));
94                        evalarray.push(cmsEval);
95                        break;
96                }
97            }
98        }
99        //console.log(evtTime.format(), incidentNum, proparray.length, evalarray.length);
100        // Ignore Media Log incident and empty nodes
101        if (incidentNum != undefined && incidentNum != 100)
102        {
103          // Create new event with fields obtained from xml file
104          events.add(new Event(evtTime, incidents.get(incidentNum), 
105                new Properties(proparray), 
106                new Evaluations(evalarray)) );
107        }
108    }
109}
110
111function parseCAD(element)
112{
113    var result = new Array();
114    var details = element.getElementsByTagName("DETAIL");
115    if (details.length > 0)
116    {
117        for (detail in details)
118        {
119            if (details[detail].textContent != undefined)
120            {               
121                result.push("Detail:");
122                result.push(details[detail].textContent);
123            }
124        }
125    }
126    return result;
127}
128function parseTelephone(element)
129{
130    var result = new Array();
131    for (var child = 1; child < element.childNodes.length; child++)
132    {
133        if (element.childNodes[child].localName != undefined)
134        {
135            if (element.childNodes[child].localName == "INSTRUCTOR")
136            {
137                result.push(element.childNodes[child].attributes["Role"].value);
138            }
139            else
140            {
141                result.push(element.childNodes[child].localName);   
142            }
143            result.push(element.childNodes[child].textContent);
144        }
145    }
146    return result;
147}
148function parseCHPradio(element)
149{
150    var result = new Array();
151    var dialog = element.getElementsByTagName("LINE");
152    if (dialog.length > 0)
153    {
154        for (line in dialog)
155        {
156            if (dialog[line].textContent != undefined)
157            {               
158                result.push(dialog[line].attributes["Role"].value);
159                result.push(dialog[line].textContent);
160            }
161        }
162    }
163    return result;
164}
165function parseEvaluation(element)
166{
167    var result = new Array();
168    var details = element.getElementsByTagName("EXPECTED_ACTION");
169    if (details.length > 0)
170    {
171        for (detail in details)
172        {
173            if (details[detail].textContent != undefined)
174            {               
175                result.push("Expected Action:");
176                result.push(details[detail].textContent.trim());
177            }
178        }
179    }
180    return result;
181}
182function parseCMSEvaluation(element)
183{
184    var result = new Array();
185    var locations = element.getElementsByTagName("LOCATION");
186    if (locations.length > 0)
187    {
188        result.push("Sign Location:");
189        result.push(locations[0].textContent);
190    }
191    var details = element.getElementsByTagName("CMS_LINE");
192    if (details.length > 0)
193    {
194        for (detail in details)
195        {
196            if (details[detail].textContent != undefined)
197            {               
198                result.push("Sample Message:");
199                result.push(details[detail].textContent.trim());
200            }
201        }
202    }
203    return result;
204}
205// MAIN
206//console.log("starting LoadEvents");
207try {
208    // the script must be located where accessible by the web server
209    var scriptFilename = "../dynamicdata/incident_script.xml";
210    console.log("Attempting to load ", scriptFilename);
211    // Now load the Incident Script and go parse it
212    loadJSON(scriptFilename, parseXml)
213
214    } catch(e) {
215        console.log("Error attempt to parse incident script "+response)
216    }
217
218
Note: See TracBrowser for help on using the repository browser.