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

Revision 515, 9.8 KB checked in by jdalbey, 7 years ago (diff)

Event.js, Incident.js, Incidents.js, LoadEvents?.js modified to implement ticket #158: color coding incidents.

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