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

Revision 363, 5.9 KB checked in by jdalbey, 7 years ago (diff)

Implement #136 to read script file name from a config file. EInotebook v0.2

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