Changeset 363 in tmcsimulator for trunk/webapps/EInotebook/scripts/LoadEvents.js


Ignore:
Timestamp:
04/11/2019 10:35:31 AM (7 years ago)
Author:
jdalbey
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/webapps/EInotebook/scripts/LoadEvents.js

    r361 r363  
    1 var xmlDoc; 
    2 function loadJSON(inFile, callback) 
    3 { 
    4     var xobj = new XMLHttpRequest(); 
    5     // Assume XML unless filename ends with .json 
    6     if (inFile.endsWith(".json")) 
    7     { 
    8         xobj.overrideMimeType("application/json"); 
    9     } 
    10     xobj.open('GET', inFile, true); 
    11     xobj.onreadystatechange = function() 
    12     { 
    13         if (xobj.readyState == 4 && xobj.status == "200") 
    14         { 
    15             callback(xobj.responseText); 
    16         } 
    17     }; 
    18     // We want ajax to ignore any cached responses 
    19     xobj.setRequestHeader('If-Modified-Since', 'Sat, 01 Jan 2000 01:01:01 GMT') 
    20     xobj.send(null); 
    21 } 
     1 
    222 
    233// Parse the entire Incident XML Script file  
     
    277    // Create a parser and grab the high level tag we're interested in 
    288    var parser = new DOMParser(); 
    29     xmlDoc = parser.parseFromString(response,"text/xml"); 
     9    var xmlDoc = parser.parseFromString(response,"text/xml"); 
    3010    var eventTags = xmlDoc.getElementsByTagName("SCRIPT_EVENT"); 
    31  
     11    console.log("parsing incident xml file"); 
    3212    // Process each SCRIPT_EVENT tag 
    3313    for (var i = 0; i < eventTags.length; i++) 
     
    5434                        incidentNum = Number(currEvt.childNodes[child].attributes["LogNum"].value);  
    5535                        break; 
    56                     case "GENERAL_INFO":  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; 
    5743                    case "CAD_DATA":  
    5844                        var caddata = parseCAD(currEvt.childNodes[child]);  
     
    8470        } 
    8571        //console.log(evtTime.format(), incidentNum, proparray.length, evalarray.length); 
    86         if (incidentNum != undefined) 
     72        // Ignore Media Log incident and empty nodes 
     73        if (incidentNum != undefined && incidentNum != 100) 
    8774        { 
    88           events.add(new Event(evtTime, incidents.get(incidentNum), new Properties(proparray),  
     75          // Create new event with fields obtained from xml file 
     76          events.add(new Event(evtTime, incidents.get(incidentNum),  
     77                new Properties(proparray),  
    8978                new Evaluations(evalarray)) ); 
    9079        } 
    9180    } 
     81} 
    9282 
    93 } 
    9483function parseCAD(element) 
    9584{ 
     
    148137 
    149138// MAIN 
    150 loadJSON("full_script.xml", parseXml) 
    151 // Note: the script has the Media Log event removed 
     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) 
    152152 
     153        } catch(e) { 
     154            console.log("Error attempt to parse sim_scriptname.json: "+response) 
     155        } 
     156}); 
     157 
Note: See TracChangeset for help on using the changeset viewer.