1  
     2  /**
     3   * com.sekati.services.AmbientOrb
     4   * @version 1.0.5
     5   * @author jason m horwitz | sekati.com
     6   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     7   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     8   */
     9   
    10  /**
    11   * Ambient Orb developer channel controller class
    12   * {@code Usage:
    13   * var orb:AmbientOrb = new AmbientOrb("AAA-BBB-CCC");
    14   * orb.config ( getColorByProp("name","red"), getAnimByProp("name","heartbeat"), "Orb Test" );
    15   * }
    16   * @see <a href="http://www.ambientdevices.com/developer/Tech%20FAQ.html">http://www.ambientdevices.com/developer/Tech%20FAQ.html</a>
    17   */
    18  class com.sekati.services.AmbientOrb {
    19  
    20  	private var _devId:String;
    21  	private static var _URI:String = "http://myambient.com:8080/java/my_devices/submitdata.jsp";	
    22  	private static var _SPECTRUM:Array = [ {id:"0", hex:"0xFF0000", name:"red"}, 
    23  								 {id:"1", hex:"0xFF2B00", name:"light red"}, 
    24  								 {id:"2", hex:"0xFF5500", name:"dark orange"}, 
    25  								 {id:"3", hex:"0xFF8000", name:"orange"}, 
    26  								 {id:"4", hex:"0xFFAA00", name:"light orange"}, 
    27  								 {id:"5", hex:"0xFFD500", name:"dark yellow"}, 
    28  								 {id:"6", hex:"0xFFFF00", name:"yellow"}, 
    29  								 {id:"7", hex:"0xD4FF00", name:"lime green"}, 
    30  								 {id:"8", hex:"0xAAFF00", name:"pale green"}, 
    31  								 {id:"9", hex:"0x80FF00", name:"green -3"}, 
    32  								 {id:"10", hex:"0x55FF00", name:"green -2"}, 
    33  								 {id:"11", hex:"0x2BFF00", name:"green -1"}, 
    34  								 {id:"12", hex:"0x00FF00", name:"green"}, 
    35  								 {id:"13", hex:"0x00FF2A", name:"green +1"}, 
    36  								 {id:"14", hex:"0x00FF55", name:"green +2"}, 
    37  								 {id:"15", hex:"0x00FF80", name:"pale aqua"}, 
    38  								 {id:"16", hex:"0x00FFAA", name:"aqua"}, 
    39  								 {id:"17", hex:"0x00FFD4", name:"dark aqua"}, 
    40  								 {id:"18", hex:"0x00FFFF", name:"cyan"}, 
    41  								 {id:"19", hex:"0x00D4FF", name:"dark cyan"}, 
    42  								 {id:"20", hex:"0x00AAFF", name:"light blue"}, 
    43  								 {id:"21", hex:"0x0080FF", name:"sky blue"}, 
    44  								 {id:"22", hex:"0x0055FF", name:"blue -2"}, 
    45  								 {id:"23", hex:"0x002AFF", name:"blue -1"}, 
    46  								 {id:"24", hex:"0x0000FF", name:"blue"}, 
    47  								 {id:"25", hex:"0x2A00FF", name:"deep blue"}, 
    48  								 {id:"26", hex:"0x5500FF", name:"very deep blue"}, 
    49  								 {id:"27", hex:"0x8000FF", name:"violet"}, 
    50  								 {id:"28", hex:"0xAA00FF", name:"purple"}, 
    51  								 {id:"29", hex:"0xD500FF", name:"light purple"}, 
    52  								 {id:"30", hex:"0xFF00FF", name:"magenta"}, 
    53  								 {id:"31", hex:"0xFF00D5", name:"magenta +1"}, 
    54  								 {id:"32", hex:"0xFF00AA", name:"magenta +2"}, 
    55  								 {id:"33", hex:"0xFF0080", name:"magenta +3"}, 
    56  								 {id:"34", hex:"0xFF0055", name:"magenta +4"}, 
    57  								 {id:"35", hex:"0xFF002B", name:"magenta +5"}, 
    58  								 {id:"36", hex:"0xFFFFFF", name:"white"} ];
    59  	private static var _ANIM:Array = [ {id:"0", name:"none"}, 
    60  							 {id:"1", name:"very slow"}, 
    61  							 {id:"2", name:"slow"}, 
    62  							 {id:"3", name:"medium slow"}, 
    63  							 {id:"4", name:"medium"}, 
    64  							 {id:"5", name:"medium fast"}, 
    65  							 {id:"6", name:"fast"}, 
    66  							 {id:"7", name:"very fast"}, 
    67  							 {id:"8", name:"crescendo"}, 
    68  							 {id:"9", name:"heartbeat"} ];
    69  	private static var _CODE:Array = [ {id:"0", type:"OK", desc:"data inserted OK"}, 
    70  							 {id:"1", type:"error", desc:"'color' term not defined. Must be number between 0 - 36."}, 
    71  							 {id:"2", type:"error", desc:"error parsing 'color' term. Must be number between 0 - 36."}, 
    72  							 {id:"3", type:"error", desc:"'color' value out of range. . Must be number between 0 - 36."}, 
    73  							 {id:"4", type:"warning", desc:"'anim' term not defined. Must be number between 0 - 9. Assuming 'none'."}, 
    74  							 {id:"5", type:"error", desc:"error parsing 'anim' term. Must be number between 0 - 9."}, 
    75  							 {id:"6", type:"error", desc:"'anim' value out of range. . Must be number between 0 - 9."}, 
    76  							 {id:"7", type:"error", desc:"The term 'devID' was not specified."}, 
    77  							 {id:"8", type:"error", desc:"The specified devID is not a premium account."}, 
    78  							 {id:"9", type:"warning", desc:"The specified devID is not set to the developer channel."}, 
    79  							 {id:"10", type:"error", desc:"The specified devID is not in the Ambient database."}, 
    80  							 {id:"11", type:"warning", desc:"The comment contains invalid characters. Ignoring comment."}, 
    81  							 {id:"12", type:"error", desc:"Unspecific error"}, 
    82  							 {id:"13", type:"error", desc:"You are not authorized to submit data to this account (not yet implemented)"} ];
    83  
    84  	/**
    85  	 * Constructor
    86  	 * @param devId (String) - orb device id to be controlled by this instance
    87  	 * @return Void
    88  	 */
    89  	public function AmbientOrb(devId:String) {
    90  		_devId = devId;
    91  	}
    92  
    93  	/**
    94  	 * devId setter
    95  	 * @param (String) instance device Id
    96  	 * @return Void
    97  	 */
    98  	public function set devId(devId:String):Void {
    99  		if (devId) {
   100  			_devId = devId;
   101  		}
   102  	}
   103  
   104  	/**
   105  	 * devId getter
   106  	 * @return String - instance device id
   107  	 */
   108  	public function get devId():String {
   109  		return _devId;	
   110  	}
   111  
   112  	/**
   113  	 * spectrum getter
   114  	 */
   115  	public function get spectrum():Array {
   116  		return _SPECTRUM;
   117  	}
   118  
   119  	/**
   120  	 * anim getter
   121  	 */
   122  	public function get anim():Array {
   123  		return _ANIM;
   124  	}
   125  
   126  	/**
   127  	 * server code getter
   128  	 */
   129  	public function get code():Array {
   130  		return _CODE;
   131  	}		
   132  
   133  	/**
   134  	 * locate a color object by one of its properties
   135  	 * @param prop (String) - acceptable props: "id", "hex", "name"
   136  	 * @param key (String) - value to match by, e.g: "0xFF00FF"
   137  	 * @return Object - the matched _SPECTRUM object
   138  	 * @throws Error on failed match
   139  	 */
   140  	public function getColorByProp(prop:String,key:String):Object {
   141  		for (var i:Number = 0; i < _SPECTRUM.length ; i++) {
   142  			var o:Object = _SPECTRUM[i];
   143  			if(o[prop].toUpperCase( ) == key.toUpperCase( )) {
   144  				return o;	
   145  			}
   146  		}
   147  		throw new Error( "@@@ com.sekati.services.AmbientOrb Error: could not find match for " + prop + ": " + key );
   148  	}
   149  
   150  	/**
   151  	 * locate an animation by one of its properties
   152  	 * @param prop (String) - acceptable props: "id", "name"
   153  	 * @param key (String) - value to match by, e.g: "slow"
   154  	 * @return Object - the matched _ANIM object
   155  	 * @throws Error on failed match
   156  	 */
   157  	public function getAnimByProp(prop:String,key:String):Object {
   158  		for (var i:Number = 0; i < _ANIM.length ; i++) {
   159  			var o:Object = _ANIM[i];
   160  			if(o[prop].toUpperCase( ) == key.toUpperCase( )) {
   161  				return o;	
   162  			}
   163  		}
   164  		throw new Error( "@@@ com.sekati.services.AmbientOrb Error: could not find match for " + prop + ": " + key );		
   165  	}
   166  
   167  	/**
   168  	 * locate a server code by one of its properties
   169  	 * @param prop (String) - acceptable props: "id", "desc"
   170  	 * @param key (String) - value to match by, e.g: "Unspecific error"
   171  	 * @return Object - the matched _CODE object
   172  	 * @throws Error on failed match
   173  	 */
   174  	 
   175  	public function getCodeByProp(prop:String,key:String):Object {
   176  		for (var i:Number = 0; i < _CODE.length ; i++) {
   177  			var o:Object = _CODE[i];
   178  			if(o[prop].toUpperCase( ) == key.toUpperCase( )) {
   179  				return o;	
   180  			}
   181  		}
   182  		throw new Error( "@@@ com.sekati.services.AmbientOrb Error: could not find match for " + prop + ": " + key );		
   183  	}
   184  
   185  	/**
   186  	 * send new configuration to the ambient orb device id
   187  	 * @param colorId (String)
   188  	 * @param animId (String)
   189  	 * @param comment (String)
   190  	 * @return Void
   191  	 * @throws Error if instance device id, colorId,animId params have not been set
   192  	 * {@code Usage:
   193  	 * orb.config ( getColorByProp("name","red"), getAnimByProp("name","heartbeat"), "Orb Test" );
   194  	 * }
   195  	 */
   196  	public function config(colorId:String, animId:String, comment:String):Void {
   197  		if (!_devId) {
   198  			throw new Error( "@@@ com.sekati.services.AmbientOrb Error: Device ID not set" );
   199  			return;
   200  		}
   201  		if (!colorId) {
   202  			throw new Error( "@@@ com.sekati.services.AmbientOrb Error: invalid config colorId arg" );
   203  			return;
   204  		}
   205  		if (!animId) {
   206  			throw new Error( "@@@ com.sekati.services.AmbientOrb Error: invalid config animId arg" );
   207  			return;
   208  		}
   209  		if (!comment) {
   210  			comment = "com.sekati.services.AmbientOrb";
   211  		}
   212  		var xm:XML = new XML( );
   213  		xm.ignoreWhite = true;
   214  		var query:String = _URI + "?devID=" + _devId + "&anim=" + animId + "&color=" + colorId + "&comment=" + escape( comment );
   215  		trace( "query string: " + query );
   216  		xm.onLoad = function (success:Boolean):Void {
   217  			var xObj:Object = xparse( xm );
   218  			trace( "object returned: " + xObj );
   219  		};
   220  		xm.load( query );
   221  	}
   222  
   223  	/**
   224  	 * xml parser
   225  	 */
   226  	private function xparse(n:XML):Object {
   227  		var o:String = new String( n.firstChild.nodeValue ), s:Object, i:Number, t:Object;
   228  		for (s = (o == "null") ? n.firstChild : n.childNodes[1]; s != null ; s = s.nextSibling) {
   229  			t = s.childNodes.length > 0 ? arguments.callee( s ) : new String( s.nodeValue );
   230  			if (s.firstChild.nodeValue != undefined) {
   231  				t.val = s.firstChild.nodeValue;
   232  			}
   233  			for (i in s.attributes) {
   234  				t[i] = s.attributes[i];
   235  			}
   236  			if (o[s.nodeName] != undefined) {
   237  				if (!(o[s.nodeName] instanceof Array)) {
   238  					o[s.nodeName] = [ o[s.nodeName] ];
   239  				}
   240  				o[s.nodeName].push( t );
   241  			} else {
   242  				o[s.nodeName] = t;
   243  			}
   244  		}
   245  		return o;
   246  	}
   247  }