source: tmcsimulator/trunk/webapps/EInotebook/scripts/LoadEvents.js @ 364

Revision 364, 6.3 KB checked in by jdalbey, 7 years ago (diff)

Parse incidents from script to replace PopulateIncidents?.js. Fade event colors. EInotebook v0.3.

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                // Determine the tag type and dispatch it for further processing
27                switch(currEvt.childNodes[child].localName)
28                {
29                    case "TIME_INDEX": 
30                        timeFields = currEvt.childNodes[child].textContent.split(":"); 
31                        evtTime = new Time(Number(timeFields[0]), Number(timeFields[1]), Number(timeFields[2]));
32                        break;
33                    case "INCIDENT":
34                        incidentNum = Number(currEvt.childNodes[child].attributes["LogNum"].value);
35                        incidentTitle =  currEvt.childNodes[child].textContent;
36                        break;
37                    case "GENERAL_INFO": 
38                        /* This tag identifies a new incident.  As long as it appears before any
39                        other tags, we can use it to create a new incident. (The alternative is to
40                        have a separate function that parses xml to extract incidents before
41                        we load events.)  Creating an event (below) requires that the incident has already
42                        been created. */
43                        var desc = currEvt.childNodes[child].getElementsByTagName("TEXT")[0].textContent;
44                        var theIncident = new Incident(evtTime, incidentNum, incidentTitle, desc);
45                        incidents.add(theIncident);
46                        break;
47                    case "CAD_DATA": 
48                        var caddata = parseCAD(currEvt.childNodes[child]); 
49                        if (caddata.length > 0)
50                        {
51                            cadProp = new Property("CHP CAD", caddata ); 
52                            proparray.push(cadProp);
53                        }
54                        break;
55                    case "TELEPHONE": 
56                        telProp = new Evaluation("Telephone Conversation", 
57                                 parseTelephone(currEvt.childNodes[child]) ); 
58                        evalarray.push(telProp);
59                        break;
60                    case "CHP_RADIO": 
61                        var chpradio = parseCHPradio(currEvt.childNodes[child]); 
62                        if (chpradio.length > 0)
63                        {
64                            cadProp = new Property("CHP RADIO", chpradio ); 
65                            proparray.push(cadProp);
66                        }
67                        break;
68                    case "TMT_RADIO":  break;
69                    case "MAINTENANCE_RADIO": break;
70                    // case *_EVALUATION: break;
71
72                }
73            }
74        }
75        //console.log(evtTime.format(), incidentNum, proparray.length, evalarray.length);
76        // Ignore Media Log incident and empty nodes
77        if (incidentNum != undefined && incidentNum != 100)
78        {
79          // Create new event with fields obtained from xml file
80          events.add(new Event(evtTime, incidents.get(incidentNum), 
81                new Properties(proparray), 
82                new Evaluations(evalarray)) );
83        }
84    }
85}
86
87function parseCAD(element)
88{
89    var result = new Array();
90    var details = element.getElementsByTagName("DETAIL");
91    if (details.length > 0)
92    {
93        for (detail in details)
94        {
95            if (details[detail].textContent != undefined)
96            {               
97                result.push("Detail:");
98                result.push(details[detail].textContent);
99            }
100        }
101    }
102    return result;
103}
104function parseTelephone(element)
105{
106    var result = new Array();
107    for (var child = 1; child < element.childNodes.length; child++)
108    {
109        if (element.childNodes[child].localName != undefined)
110        {
111            if (element.childNodes[child].localName == "INSTRUCTOR")
112            {
113                result.push(element.childNodes[child].attributes["Role"].value);
114            }
115            else
116            {
117                result.push(element.childNodes[child].localName);   
118            }
119            result.push(element.childNodes[child].textContent);
120        }
121    }
122    return result;
123}
124function parseCHPradio(element)
125{
126    var result = new Array();
127    var dialog = element.getElementsByTagName("LINE");
128    if (dialog.length > 0)
129    {
130        for (line in dialog)
131        {
132            if (dialog[line].textContent != undefined)
133            {               
134                result.push(dialog[line].attributes["Role"].value);
135                result.push(dialog[line].textContent);
136            }
137        }
138    }
139    return result;
140}
141
142// MAIN
143console.log("starting LoadEvents");
144/** Load the sim script name file and extract the filename of the Incident Script */
145loadJSON("../sim_scriptname.json", function(response)
146{
147    console.log("scriptname file loaded");
148    try {
149        var scriptnamejson = JSON.parse(response);
150        // For now the script must be located in the EInotebook folder.
151        // It would be better to use the file located in trunk/scripts folder.
152        var scriptFilename = scriptnamejson.filename;
153        console.log("Attempting to load", scriptFilename);
154        // Now load the Incident Script and go parse it
155        loadJSON(scriptFilename, parseXml)
156
157        } catch(e) {
158            console.log("Error attempt to parse sim_scriptname.json: "+response)
159        }
160});
161
Note: See TracBrowser for help on using the repository browser.