Advertisement
crookedparadigm

weatherservice

Apr 17th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.  
  3.     import fl.controls.TextArea;
  4.     import flash.display.*;
  5.     import flash.text.*;
  6.     import flash.xml.*;
  7.     import flash.events.*;
  8.     import flash.net.URLLoader;
  9.     import flash.net.URLRequest;
  10.     import flash.utils.*;
  11.    
  12. public class WeatherService extends MovieClip {
  13.     public var xml_Weather:XML  = null;
  14.     public var str_Location:String   = null;
  15.     public var str_Temparature:String  = null;
  16.     public var num_Temparature:Number = 0;
  17.     public var str_ImageURL:String = null;
  18.     public var str_ImageName:String = null;
  19.     public var ta_TextArea:TextArea = new TextArea();
  20.     public var ldr_Loader:URLLoader = new URLLoader();
  21.     public var req_URLRequest:URLRequest = new URLRequest();
  22.    
  23.     public function WeatherService ():void {
  24.        
  25.      init();
  26.      var myTimer:Timer = new Timer (600000);
  27.      myTimer.addEventListener(TimerEvent.TIMER, callTime);
  28.      myTimer.start();
  29.      
  30.     }
  31.    
  32.     public function init():void {
  33.         trace ("init got called");
  34.        
  35.       req_URLRequest = new URLRequest("http://www.weather.gov/xml/current_obs/KAUW.xml");
  36.      
  37.  
  38.       ldr_Loader.addEventListener(Event.COMPLETE, f_XMLParser);
  39.       ldr_Loader.load(req_URLRequest);
  40.      
  41.        
  42.     }
  43.    
  44.     public function callTime (event:TimerEvent):void {
  45.        
  46.         init();
  47.        
  48.     }
  49.        
  50.    
  51.     public function f_XMLParser(event:Event):void {
  52.          if (ldr_Loader != null)
  53.             {
  54.            
  55.             xml_Weather = new XML(event.target.data);
  56.            
  57.             str_Location      = xml_Weather.location;
  58.             str_Temparature   = xml_Weather.temperature_string;
  59.            
  60.    
  61.             var firstChar_str:String = str_Temparature.charAt(0);
  62.             var secondChar_str:String = str_Temparature.charAt(1);
  63.             trace(firstChar_str);
  64.             trace(secondChar_str);
  65.             var tempString:String = (firstChar_str + secondChar_str);
  66.             trace ("this is it " + tempString);
  67.            
  68.            
  69.             var myNumber:Number = Number(tempString);
  70.             trace (myNumber);
  71.             moveRed(myNumber);
  72.            
  73.             trace ("this is " + str_Temparature);
  74.             str_ImageURL      = xml_Weather.icon_url_base;
  75.             str_ImageName     = xml_Weather.icon_url_name;
  76.             trace (str_ImageURL);
  77.             trace (str_ImageName);
  78.          
  79.             mcHolder.therm_mc.temp_txt.text = str_Temparature.toString();
  80.             mcHolder.therm_mc.location_txt.text = str_Location.toString();
  81.            
  82.            
  83.             var i =new Loader();
  84.             i.load(new URLRequest(str_ImageURL.concat(str_ImageName)));
  85.             i.x = 77;
  86.             i.y = 82;
  87.             mcHolder.addChild(i)
  88.            
  89.             if (myNumber < 80) {
  90.                 trace ("it is cold");
  91.             }
  92.             if (myNumber >= 50 && myNumber <= 70) //if true, calls the function to pass data to Flex
  93.             {
  94.                 tempAlert();
  95.             }
  96.  
  97.  
  98.         }
  99.         else {
  100.    
  101.                         trace("Text not parseable into XML");
  102.              }
  103.        
  104.        
  105.         }
  106.     public function moveRed(tempNum:Number):void{
  107.         if (tempNum < 0){
  108.                 mcHolder.therm_mc.red_mc.y = 420;
  109.             }
  110.             else if(tempNum >=0 && tempNum<10){
  111.                 mcHolder.therm_mc.red_mc.y = 407
  112.             }
  113.             else if(tempNum >=10 && tempNum<20){
  114.                 mcHolder.therm_mc.red_mc.y = 367
  115.             }
  116.             else if(tempNum >=20 && tempNum<30){
  117.                 mcHolder.therm_mc.red_mc.y = 327
  118.             }
  119.             else if(tempNum >=30 && tempNum<40){
  120.                 mcHolder.therm_mc.red_mc.y = 287
  121.             }
  122.             else if(tempNum >=40 && tempNum<50){
  123.                 mcHolder.therm_mc.red_mc.y = 247
  124.             }
  125.             else if(tempNum >=50 && tempNum<60){
  126.                 mcHolder.therm_mc.red_mc.y = 207
  127.             }
  128.             else if(tempNum >=60 && tempNum<70){
  129.                 mcHolder.therm_mc.red_mc.y = 167
  130.             }
  131.             else if(tempNum >=70 && tempNum<80){
  132.                 mcHolder.therm_mc.red_mc.y = 127
  133.             }
  134.             else if(tempNum >=80 && tempNum<90){
  135.                 mcHolder.therm_mc.red_mc.y = 87
  136.             }
  137.             else if(tempNum >=90 && tempNum<100){
  138.                 mcHolder.therm_mc.red_mc.y = 47
  139.             }
  140.             else if(tempNum > 100){
  141.                 mcHolder.therm_mc.red_mc.y = 27
  142.             }
  143.             else{
  144.                 trace("tempNum is not a valid temperature");
  145.             }
  146.     }
  147.     public function tempAlert():void //dispatches a new Event named tempAlert that the Flex app looks for
  148.     {
  149.        
  150.         dispatchEvent(new Event("tempAlert", true));
  151.         trace("data passed")
  152.     }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement