4.4 Example Code for JavaScript Library

The following HTML/JavaScript source provides a simple test harness for the JavaScript library functions:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <title>Web 2.0 Connect</title>
    <style type="text/css">
      BODY
      {
        background-color: #242424;
        color: #FFAA00;
        font-family: Verdana;
        font-size: 10pt;
      }
      div#ContentContainer
      {
        margin: 0 auto 0 auto;
        width: 800px;
        height: 90%;
      }
      div#OuputContainer
      {
        background-color: #101010;
        padding: 4px 4px 4px 4px;
        text-align: left;
      }
      input
      {
        background-color: #101010;
        color: #FFAA00;
        border: solid 0px black;
        padding-bottom: 2px;
        margin: 2px 1px 2px 1px;
        border: solid 1px #202020;
        font-family: Arial;
      }
      input[type="button"]
      {
        background-color: #141414;
        color: #FFAA00;
        font-size: 12pt;
        border: solid 0px black;
        padding-bottom: 2px;
        margin: 2px 1px 2px 1px;
        border: solid 1px #202020;
        cursor: pointer;
      }
      input[type="text"]
      {
        background-color: #141414;
        color: #FFCC00;
        font-size: 10pt;
        width: 99%;
        border: solid 0px black;
        margin: 1px 1px 1px 1px;
        padding: 0px 4px 0px 4px;
      }
      input[type="radio"]
      {
        color: #FFCC00;
        font-size: 8pt;
      }
      textarea#debugData
      {
        background-color: black;
        color: #FFFF00;
        border: solid 0px black;
        margin: 4px 4px 8px 4px;
        overflow: auto;
      }
      h1[id="appTitle"]
      {
        font-family: Courier New, Verdana;
        font-size: 18pt;
        font-weight: bold;
        color: #FFBB00;
      }
    </style>
    <script type="text/javascript" src="scripts/1.0/bsm.web2connect.js"></script>
    <script type="text/javascript">

      var BSM_HOST = document.location.hostname;
      var BSM_PORT = document.location.port; 

      var testElementIdentity;
      var rootElementIdentity;
      var debug;
      var w;    
        
      function pageLoad()
      {
        document.getElementById("host").value = BSM_HOST;
        document.getElementById("port").value = BSM_PORT;

        testElementIdentity = document.getElementById("testElementIdentity");
        rootElementIdentity = document.getElementById("rootElementIdentity");

        debug = document.getElementById("debugData");
        debug.value = "Ready";
        
        w = new BSM.Web2Connect();

        setFormatJson();
        
        doCreateSession();
      } 
      
      function doCreateSession()
      {
        w.Session.Protocol = w.HttpProtocol.HTTP;
        w.Session.Host = document.getElementById("host").value;
        w.Session.Port = document.getElementById("port").value;
        w.Session.Username = document.getElementById("username").value;
        w.Session.Password = document.getElementById("password").value;
        
        w.createSession(createSessionHandler, createSessionFaultHandler);
      }
      
      function createSessionHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += getLastSynchronicityMessage() + "\n";
        debug.value += "Session created " + Date() + "\n" + response;
      }
      
      function createSessionFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += getLastSynchronicityMessage() + "\n";
        debug.value += response;
      }
      
      function doDeleteSession()
      {
        w.deleteSession(deleteSessionHandler, deleteSessionFaultHandler);
      }

      function deleteSessionHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += "Session deleted " + Date() + "\n" + response;
      }
      
      function deleteSessionFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += response;
      }

      function doSessionStatus()
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += getLastSynchronicityMessage() + "\n";
        debug.value += w.Session.getStatus() + "\n";
      }

      function doUserInfo(synchronicity) 
      {
        w.readUser(userHandler, userFaultHandler, synchronicity);
      }

      function userHandler(response) 
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += "User info " + Date() + "\n" + response;
      }

      function userFaultHandler(response) 
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += response;
      }

      function doPing(synchronicity)
      {
        w.ping(pingHandler, pingFaultHandler, synchronicity);
      }

      function pingHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += getLastSynchronicityMessage() + "\n";
        debug.value += "Pinged at " + Date() + "\n" + response;
      }
      
      function pingFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += getLastSynchronicityMessage() + "\n";
        debug.value += response;
      }

      function doElementOperation()
      {
        var identity = new String(rootElementIdentity.value);
        w.performElementOperation(identity, "Debug", elementOperationHandler, elementOperationFaultHandler);
      }

      function elementOperationHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += "Operation performed " + Date() + "\n" + response;
      }
      
      function elementOperationFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += response;
      }

      function doElementValue()
      {
        var identity = new String(testElementIdentity.value);
        w.readElementValue(identity, elementValueHandler, elementValueFaultHandler);
      }

      function elementValueHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += "Read element value " + Date() + "\n" + response;
      }
      
      function elementValueFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += response;
      }

      function doElementChildren()
      {
        var identity = new String(testElementIdentity.value);
        w.readElementChildren(identity, elementChildrenHandler, elementChildrenFaultHandler);
      }

      function elementChildrenHandler(response, elements)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";

        if (w.Session.ApiFormat == w.ApiFormat.JSON)
        {
          debug.value += "\nelements.count is " + elements.count;

          if (elements.count > 0)
          {
            for (var i = 1; i <= elements.count; i++)
            {
              debug.value += "\nelements.getItem(" + i + ").name = " + elements.getItem(i).name;
            }
          }
        }

        debug.value += "\n\nRead element children " + Date() + "\n" + response;
      }
      
      function elementChildrenFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += response;
      }

      function doElementAttributes()
      {
        var identity = new String(testElementIdentity.value);
        var attributes = new w.Utils.AssociativeList();
        
        attributes.add("lastAlarmSummary");
        attributes.add("childCount");
        attributes.add("relationshipCount");
        attributes.add("alarmCount");
        
        w.readElementAttributes(identity, attributes, elementAttributesHandler, elementAttributesFaultHandler);
      }

      function elementAttributesHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += "Read element attributes " + Date() + "\n" + response;
      }
      
      function elementAttributesFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += response;
      }

      function doElementAlarmsRealtime()
      {
        var identity = new String(testElementIdentity.value);
        
        w.readElementAlarmsRealtime(identity, w.AlarmChannel.GENERAL, null, elementAlarmsHandler, elementAlarmsFaultHandler);
      }

      function elementAlarmsHandler(response, alarms)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";

        if (w.Session.ApiFormat == w.ApiFormat.JSON)
        {
          if (alarms.count > 0)
          {
            debug.value += "\nalarms.count is " + alarms.count;

            for (var i = 1; i <= alarms.count; i++)
            {
              debug.value += "\nalarms.getItem(" + i + ").affectedElementName = " + alarms.getItem(i).affectedElementName;
            }
          }
        }

        debug.value += "\n\nRead element alarms realtime " + Date() + "\n" + response;
      }
      
      function elementAlarmsFaultHandler(response)
      {
        debug.value = "Exception [" + response.exception + "]\n";
        debug.value += w.Internal.RemoteServiceUrl + "\n";
        debug.value += response
      }

      function doElementQueryPerformanceInfo()
      {
        var identity = new String(testElementIdentity.value);       
        w.readElementPerformanceInfo(identity, elementPerformanceInfoHandler, elementPerformanceInfoFaultHandler);
      }

      function elementPerformanceInfoHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += "Read element performance information " + Date() + "\n" + response;
      }
      
      function elementPerformanceInfoFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += "\nEnsure Data Warehouse is enabled\n\n";
        debug.value += response;
      }

      function doElementQueryPerformanceSeries()
      {
        var identity = new String(testElementIdentity.value);
        var query = new w.Utils.PerformanceSeriesQuery();

        query.profileName = "Demo";
        query.expressionName = "Script";
        query.fromTimeEpoch = 1220482800000;
        query.toTimeEpoch = 1220569200000;
        query.limitCount = 200;
 
        w.readElementPerformanceSeries(identity, query, elementPerformanceSeriesHandler, elementPerformanceSeriesFaultHandler);
      }

      function elementPerformanceSeriesHandler(response, series)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        
        if (w.Session.ApiFormat == w.ApiFormat.JSON)
        { 
          if (series.count > 0)
          {
            debug.value += "\nseries.count is " + series.count;
            debug.value += "\nseries.Item(1).dateValue is " + series.getItem("1").dateValue;
            debug.value += "\nseries.getItemByDateEpoch(series.getItem(1).dateValue).seriesValue is " + series.getItemByDateEpoch(series.Item(1).dateValue).seriesValue;
            debug.value += "\nseries.earliestDate is " + series.earliestDate;
            debug.value += "\nseries.latestDate is " + series.latestDate;
            debug.value += "\nseries.lowestValue is " + series.lowestValue;
            debug.value += "\nseries.highestValue is " + series.highestValue;
          }  
        }  

        debug.value += "\n\nRead element performance series " + Date() + "\n" + response;
      }
      
      function elementPerformanceSeriesFaultHandler(response)
      {
        debug.value = w.Internal.RemoteServiceUrl + "\n";
        debug.value += "\nEnsure Data Warehouse is enabled\n\n";
        debug.value += response;
      }
      
      function getLastSynchronicityMessage()
      {
        return "Internal.LastSynchronicity " + w.Internal.LastSynchronicity.toString().toUpperCase();
      }
      
      function setFormatXml()
      {
        w.Session.ApiFormat = w.ApiFormat.XML;
        document.getElementById("radioXML").checked = true;
      }
      
      function setFormatJson()
      {
        w.Session.ApiFormat = w.ApiFormat.JSON;
        document.getElementById("radioJSON").checked = true;
      }
    </script>
  </head>
  <body onload="pageLoad()">
    <div id="ContentContainer">
      <h1 id="appTitle">// Web 2.0 Connect JavaScript Library</h1>
      <div id="OuputContainer">
        <textarea rows="15" cols="95" id="debugData"></textarea>
        <br />
        Response Format
        <input type="radio" onclick="setFormatXml()" value="XML" name="ApiFormat" id="radioXML" />XML
        <input type="radio" onclick="setFormatJson()" value="JSON" name="ApiFormat" id="radioJSON" />JSON
        <br />
        <table width="100%">
          <tr>
            <td width="20">
              Host
            </td>
            <td>
              <input type="text" size="60" id="host" value="localhost" />
            </td>
          </tr>
          <tr>
            <td width="20">
              Port
            </td>
            <td>
              <input type="text" size="30" id="port" value="8080" />
            </td>
          </tr>
          <tr>
            <td>Username</td>
            <td><input type="text" size="90" id="username" value="admin" /></td>
          </tr>
          <tr>
            <td>Password</td>
            <td><input type="text" size="90" id="password" value="formula" /></td>
          </tr>
          <tr>
            <td>Element</td>
            <td><input type="text" size="90" id="testElementIdentity" value="Test=Test/root=Organizations" /></td>
          </tr>
          <tr>
            <td>
              Root
            </td>
            <td><input type="text" size="90" id="rootElementIdentity" value="root=Organizations" /></td>
          </tr>
        </table>
        <br/>
        <input type="button" onclick="doCreateSession()" value="Start Session"/>
        <input type="button" onclick="doDeleteSession()" value="End Session"/>
        <input type="button" onclick="doSessionStatus()" value="Session Status"/>
        <input type="button" onclick="doUserInfo(true)" value="User Info" />
        <br/>
        <input type="button" onclick="doPing(true)" value="Ping Async"/>
        <input type="button" onclick="doPing(false)" value="Ping Sync"/>
        <input type="button" onclick="doElementOperation()" value="Perform Operation"/>
        <br/>
        <input type="button" onclick="doElementValue()" value="Element Value"/>
        <input type="button" onclick="doElementAttributes()" value="Element Attributes"/>
        <input type="button" onclick="doElementChildren()" value="Element Children"/>
        <input type="button" onclick="doElementAlarmsRealtime()" value="Element Alarms Realtime"/>
        <br/>
        <input type="button" onclick="doElementQueryPerformanceInfo()" value="Element Query Performance Info"/>
        <input type="button" onclick="doElementQueryPerformanceSeries()" value="Element Query Performance Series"/>
      </div>
    </div>
  </body>
</html>

4.4.1 Example Data

The configuration store export in the following example defines an element named AlarmCounts with five attributes used by the JavaScript library example. Save the following XML text as AlarmCounts.config.xml and import it into Operations Center:

<configstore>
  <entry>
    <id>26</id>
    <name>Enterprise</name>
    <className>root</className>
  </entry>
  <entry>
    <id>220</id>
    <parent>26</parent>
    <name>Organizations</name>
    <className>root</className>
  </entry>
  <entry>
    <id>2785</id>
    <parent>220</parent>
    <name>AlarmCounts</name>
    <className>visualization</className>
    <attributes>
      <entry>
        <string>okayCount</string>
        <string>50</string>
      </entry>
      <entry>

        <string>criticalCount</string>
        <string>10</string>
      </entry>      <entry>
        <string>infoCount</string>
        <string>5</string>
      </entry>
      <entry>
        <string>majorCount</string>
        <string>30</string>
      </entry>
      <entry>
        <string>minorCount</string>
        <string>50</string>
      </entry>
      <entry>
        <string>Flags</string>
        <long>16</long>
      </entry>
    </attributes>
    <exportRoot>true</exportRoot>
  </entry>
  <relationship>
    <left>220</left>
    <right>2785</right>
    <id>2786</id>
    <name>internal:match:static</name>
  </relationship>
</configstore>