7.2 Example: Alarm Ratio Bar Application

Source for Alarm Ratio Bar application, which implements the Alarm Ratio Bar component:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
  xmlns:mx="http://www.adobe.com/2006/mxml"
  xmlns:mosh="managedobjects.visualization.*"
  backgroundColor="#ffffff"
  backgroundGradientColors="#ffffff,#ffffff"
  backgroundGradientAlphas="0.0,0.0"
  applicationComplete="main()"
  >
  <mx:Script>
    <![CDATA[
      import mx.controls.Alert;
      import mx.rpc.http.HTTPService;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;
      private var okayCount : int = 10;
      private var infoCount : int = 20;
      private var minorCount : int = 30;
      private var majorCount : int = 40;
      private var criticalCount : int = 10;
      // Constants
      private const RPC_REST_ROOT : String = "/moweb2cn/rest/";
      private const DEFAULT_REFRESH_INTERVAL : Number = 30;
      // Private
      private var rpcUsername : String;
      private var rpcPassword : String;
      private var rpcIdentity : String;
      private var captionText : String;
      private var refreshTimer : Timer;
      private var refreshInterval : Number;
      // Functions
      private function terminateSession() : void
      {
        var rpc : HTTPService = new HTTPService();
        rpc.addEventListener(ResultEvent.RESULT, terminateSessionHandler);
        rpc.headers = {"X-HTTP-Method-Override" : "DELETE"};
        rpc.method = "POST";
        rpc.contentType = "application/xml";
        rpc.resultFormat = "xml";
        rpc.url = RPC_REST_ROOT + "session";
        rpc.send(new XML("<response/>"));
      }
      private function terminateSessionHandler(event : ResultEvent) : void
      {
        // Do nothing
      }
      private function createSession(username : String, password : String) : void
      {
        var rpc : HTTPService = new HTTPService();
        var url : String;
        url = RPC_REST_ROOT + "session?username=" + username + "&password=" + password;
        rpc.addEventListener(ResultEvent.RESULT, createSessionHandler);
        rpc.method = "GET";
        rpc.url = url;
        rpc.send(null);
      }
      private function createSessionHandler(event : ResultEvent) : void
      {
        this.updateAlarmConditionCounts(this.rpcIdentity);
      }
      private function updateAlarmConditionCounts(identity : String) : void
      {
        var rpc : HTTPService = new HTTPService();
        var url : String;
        url = RPC_REST_ROOT + "element/attributes/";
        url += identity;
        url += "?attributeNames=okayCount,infoCount,minorCount,majorCount,criticalCount";
        rpc.addEventListener(ResultEvent.RESULT, alarmConditionCountsHandler);
        rpc.resultFormat = "object";
        rpc.method = "GET";
        rpc.url = url;
        rpc.send(null);
      }
      private function alarmConditionCountsHandler(event : ResultEvent) : void
      {
        this.terminateSession();
        var a : Array = [0,1,2,3,4];
        a[0] = new Number(event.result.attributes.attribute[0].stringValue);
        a[1] = new Number(event.result.attributes.attribute[1].stringValue);
        a[2] = new Number(event.result.attributes.attribute[2].stringValue);
        a[3] = new Number(event.result.attributes.attribute[3].stringValue);
        a[4] = new Number(event.result.attributes.attribute[4].stringValue);
        this.alarmRatioBar.setCountValues(a);       
        this.alarmRatioBar.CaptionText = this.captionText;
        this.refreshTimer.start();
      }
      private function refreshTimerHandler(event : TimerEvent) : void
      {
        this.refreshTimer.stop();
        this.alarmRatioBar.CaptionText = "Refreshing...";
        createSession(this.rpcUsername, this.rpcPassword);
      }
      private function configureComponentFromQueryString() : void
      {
        this.rpcUsername = mx.core.Application.application.parameters.Username;
        this.rpcPassword = mx.core.Application.application.parameters.Password;
        this.rpcIdentity = mx.core.Application.application.parameters.Identity;
        this.captionText = mx.core.Application.application.parameters.CaptionText;
        this.alarmRatioBar.CaptionText = captionText;
        this.alarmRatioBar.CaptionFontSize = mx.core.Application.application.parameters.CaptionFontSize;
        this.refreshInterval = new Number(mx.core.Application.application.parameters.RefreshInterval);
        this.refreshInterval = isNaN(refreshInterval) ? DEFAULT_REFRESH_INTERVAL : refreshInterval;
      }
      private function main() : void
      {
        configureComponentFromQueryString();
        this.refreshTimer = new Timer(this.refreshInterval * 1000);
        this.refreshTimer.addEventListener(TimerEvent.TIMER, refreshTimerHandler);
        createSession(this.rpcUsername, this.rpcPassword);      
      }
    ]]>
  </mx:Script>
  <mosh:IAlarmRatioBar 
    id="alarmRatioBar" 
    OkayCount="10"
    InfoCount="10"
    MinorCount="10"
    MajorCount="10"
    CriticalCount="10"    
    CaptionText="Alarm Ratios"
    CaptionColor="#ffffff" 
    CaptionFontFamily="Tahoma"
    CaptionFontSize="48"
    CaptionAlign="center"
    FrameColor="#ffffff"
    FrameThickness="5"   
    ShowShadows="true" 
    width="100%"
    height="100%"
  />
  <mx:Label id="debugOutput"/>
</mx:Application>