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

Revision 468, 9.5 KB checked in by jdalbey, 7 years ago (diff)

einotebook - multifile commit. Redesign to fix timing problem on some machines as well as improve file naming. Fixed #176.

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 containing "+eventTags.length+" event tags.");
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                        /* This tag identifies a new incident.  As long as it appears before any
36                        other tags, we can use it to create a new incident. (The alternative is to
37                        have a separate function that parses xml to extract incidents before
38                        we load events.)  Creating an event (below) requires that the incident has already
39                        been created. */
40                        incidentNum = Number(currEvt.childNodes[child].attributes["LogNum"].value);
41                        // If this incident number doesn't exist
42                        if (incidents.get(incidentNum) == undefined)
43                        {
44                            incidentTitle =  currEvt.childNodes[child].textContent;
45                            // Construct the incident
46                            var theIncident = new Incident(evtTime, incidentNum, incidentTitle, "");
47                            // Add the incident to the list of incidents
48                            incidents.add(theIncident);
49                        }
50                        break;
51                    case "GENERAL_INFO": 
52                        // Add the summary description to the incident
53                        var desc = currEvt.childNodes[child].getElementsByTagName("TEXT")[0].textContent;
54                        incidentToUpdate = incidents.get(incidentNum);
55                        incidentToUpdate.setSummary(desc);                       
56                        // Create an entry showing the Incident start description. Fixes ticket #164
57                        var result = new Array();   
58                        result.push("Description:");
59                        result.push(desc);
60                        proparray.push(new Property("Incident Start",result));
61                        break;
62                    case "CAD_DATA": 
63                        var caddata = parseCAD(currEvt.childNodes[child]); 
64                        if (caddata.length > 0)
65                        {
66                            cadProp = new Property("CHP CAD", caddata ); 
67                            proparray.push(cadProp);
68                        }
69                        break;
70                    case "TELEPHONE": 
71                        telProp = new Evaluation("TELEPHONE CONVERSATION", 
72                                 parseTelephone(currEvt.childNodes[child]) ); 
73                        evalarray.push(telProp);
74                        break;
75                    case "CHP_RADIO": 
76                        var chpradio = parseCHPradio(currEvt.childNodes[child]); 
77                        if (chpradio.length > 0)
78                        {
79                            cadProp = new Property("CHP RADIO", chpradio ); 
80                            proparray.push(cadProp);
81                        }
82                        break;
83
84                    case "TMT_RADIO": 
85                    case "MAINTENANCE_RADIO": 
86                        var result = new Array();   
87                        result.push("Details:");
88                        result.push(currEvt.childNodes[child].textContent.trim());
89                        var radProp = new Property(tagName, result ); 
90                        proparray.push(radProp);
91                        break;
92
93                    case "FACILITATOR_EVALUATION": 
94                    case "CAD_EVALUATION":
95                    case "ATMS_EVALUATION":
96                    case "ACTIVITY_LOG_EVALUATION":
97                    case "RADIO_EVALUATION":
98                        // remove the suffix
99                        var evalType = tagName.replace("_EVALUATION","");
100                        // Build the evaluation item
101                        var evalItem = new Evaluation(evalType, 
102                                 parseEvaluation(currEvt.childNodes[child]) ); 
103                        evalarray.push(evalItem);
104                        break;
105                       
106                    case "CMS_EVALUATION":
107                        var cmsEval = new Evaluation("CMS", parseCMSEvaluation(currEvt.childNodes[child]));
108                        evalarray.push(cmsEval);
109                        break;
110                }
111            }
112        }//end one event
113        // console.log(evtTime.format(), incidentNum, proparray.length, evalarray.length);
114        // Ignore Media Log incident and empty nodes
115        if (incidentNum != undefined && incidentNum != 100)
116        {
117          // Create new event with fields obtained from xml file
118          events.add(new Event(evtTime, incidents.get(incidentNum), 
119                new Properties(proparray), 
120                new Evaluations(evalarray)) );
121        }
122    }// end all events
123    console.log("Done parsing xml, " + events.length + " events and " +incidents.length + " incidents saved.");
124   
125    // NOW THAT WE HAVE THE EVENT LIST WE CAN PERFORM SETUP
126    setupNotebook();
127}
128
129function parseCAD(element)
130{
131    var result = new Array();
132    var details = element.getElementsByTagName("DETAIL");
133    if (details.length > 0)
134    {
135        for (detail in details)
136        {
137            if (details[detail].textContent != undefined)
138            {               
139                result.push("Detail:");
140                result.push(details[detail].textContent);
141            }
142        }
143    }
144    return result;
145}
146function parseTelephone(element)
147{
148    var result = new Array();
149    for (var child = 1; child < element.childNodes.length; child++)
150    {
151        if (element.childNodes[child].localName != undefined)
152        {
153            if (element.childNodes[child].localName == "INSTRUCTOR")
154            {
155                result.push(element.childNodes[child].attributes["Role"].value);
156            }
157            else
158            {
159                result.push(element.childNodes[child].localName);   
160            }
161            result.push(element.childNodes[child].textContent);
162        }
163    }
164    return result;
165}
166function parseCHPradio(element)
167{
168    var result = new Array();
169    var dialog = element.getElementsByTagName("LINE");
170    if (dialog.length > 0)
171    {
172        for (line in dialog)
173        {
174            if (dialog[line].textContent != undefined)
175            {               
176                result.push(dialog[line].attributes["Role"].value);
177                result.push(dialog[line].textContent);
178            }
179        }
180    }
181    return result;
182}
183function parseEvaluation(element)
184{
185    var result = new Array();
186    var details = element.getElementsByTagName("EXPECTED_ACTION");
187    if (details.length > 0)
188    {
189        for (detail in details)
190        {
191            if (details[detail].textContent != undefined)
192            {               
193                result.push("Expected Action:");
194                result.push(details[detail].textContent.trim());
195            }
196        }
197    }
198    return result;
199}
200function parseCMSEvaluation(element)
201{
202    var result = new Array();
203    var locations = element.getElementsByTagName("LOCATION");
204    if (locations.length > 0)
205    {
206        result.push("Sign Location:");
207        result.push(locations[0].textContent);
208    }
209    var details = element.getElementsByTagName("CMS_LINE");
210    if (details.length > 0)
211    {
212        for (detail in details)
213        {
214            if (details[detail].textContent != undefined)
215            {               
216                result.push("Sample Message:");
217                result.push(details[detail].textContent.trim());
218            }
219        }
220    }
221    return result;
222}
223// MAIN ENTRY POINT for this application
224function init()
225{
226    try {
227        // the script must be located where accessible by the web server
228        var scriptFilename = "../dynamicdata/incident_script.xml";
229        console.log("LoadEvents.js main Attempting to load ", scriptFilename);
230        // Now load the Incident Script and go parse it
231        // NB: This is an async function, so all other notebook setup must be in the callback.
232        loadJSON(scriptFilename, parseXml)
233
234        } catch(e) {
235            console.log("Error attempting to parse incident script "+response)
236        }
237}
238
Note: See TracBrowser for help on using the repository browser.