Real-time Weather with Flash and Java

January 22nd, 2010 by Rob
1-Wire Weather Station

1-Wire Weather Station

This is a real-time weather station I built using an AAG electronica instrument and an EEE 701 for the server.

I coded the server in Java using Apache’s Mina library for non-blocking sockets. The client is FLEX, and uses Socket (rather than HTTP). The webcam service consists essentially of a Java class that reads the video4linux data as raw RGB and saves it to a JPEG which is then streamed over the socket along with all the other data (I built the server to be pluggable, so each of these little add-ons is a plugin that shares the stream with the other plugins). The archival data lives in MySQL.

Read the rest of this entry »

Flex Earthquake Map

January 22nd, 2010 by Rob

Shows the world’s earthquakes during the last 24 hours. Blue markers are small magnitude quakes, red markers are large magnitude.

Data updates every 30 seconds and comes from here: http://earthquake.usgs.gov/earthquakes/catalogs/

AppendToGatewayUrl

June 22nd, 2009 by Rob

If you’re using BlazeDS and seeing an error involving a callback called “AppendToGatewayUrl” this might help.

AppendToGatewayUrl modifies the gateway of a NetConnection object with the session id (in this case “jsessionid”) of the current session. If you reconnect with this value appended to the gateway URL, your session can be identified, even on clients that do not use cookies.

There are two ways to handle this:

  • set NetConnection.client to an object that implements the AppendToGatewayUrl function, or;
  • subclass NetConnection to implement it (since NetConnection.client points to the NetConnection instance by default, you can not reassign the client field if you want this method to work).

There’s one caveat to both approaches, though. It has been my experience that if you call NetConnection.connect while you are receiving a response to bundled remote methods, the data may be lost or a “Client.Data.UnderFlow” error thrown. This occurs because AppendToGatewayUrl gets called after the first result, and the immediate call to NetConnection.connect aborts the rest of the transfer.

Here’s a sample implementation of option A:

public class Sample{

  private var conn : NetConnection;

  public function Sample(){
    conn = new NetConnection();
    conn .client = this;
    conn .connect(‘http://localhost/gateway’);
  }

  public function AppendToGatewayUrl(extra:String):void{
    conn .connect(conn.uri + extra);
  }

  public function load():void{
    conn .call(’service.load’,new Responder(loadResult,error));
  }

  private function loadResult(value:*):void{
    trace(‘value’,value);
  }

  private function error(err:*):void{
    trace(‘error’,err.message);
  }

}

To get around the problem with bundled calls, you could save the session id in a field and reconnect before the next call.

Why Does Flash Delete your Timeline Code?

June 15th, 2009 by Rob

If you’ve tried to load one swf into another and found that the loaded clip’s timeline code has disappeared, you’re not alone. Fortunately, the explanation is simple.

Usually what happens is, you’ve tried to cast the reference to the loaded swf to its document type, like so:

var page : MyDocumentClass = loader.content as MyDocumentClass;
page.doSomeMethod();

If doSomeMethod, for example, plays an animation on the timeline that you have governed with “stop” commands, or that calls methods in the document class, you’ll find that it doesn’t work.

Why? Because you’ve embedded the class definition in the parent movie, and it doesn’t know about the timeline code. Think about it: adding timeline code to a movie effectively makes that movie a subclass of its document class. When you embed a reference to the document class in the parent, the version associated with the child movie (with the timeline code) isn’t used.

An associated problem is that you’ve defeated some of the key reasons for modularizing your sites – to make updates easier, and load code only when it’s needed, not before. Embedding the document classes of your loaded movies defeats the former – because you have to recompile the parent as well as the child when you update the child, and the latter – because you’re loading all the child classes in the parent.

How to solve the problem, then? use interfaces. Make all of your document classes implement a single interface and make absolutely sure that no references to the document classes exist anywhere in the parent movie.

For example, here’s a child movie class:

public class MyChild extends MovieClip implements IChild{
  public function transitionIn():void{
    gotoAndPlay(’somewhere’);
  }
}

As you can see, MyChild implements IChild, an interface:

public interface IChild extends IEventDispatcher{
   function transitionIn():void;
}

Now, in your parent movie, you’ll use something like this:

private function movieLoaded(evt:Event):void{
  var child : IChild = loader.content as IChild;
  child.transitionIn();
}

Notice that you’re calling a method on the interface, not the document class. In fact, the parent movie should be completely agnostic where the doc classes are concerned. The interface is enough.

Update: Using PixelBender to Crunch Numbers

March 7th, 2009 by Rob

I didn’t realize it, but there’s a bug in PixelBender for Flash when using image4 input. In my case, I have x-y coordinates, and I was trying to operate on two at a time using image4. Can’t do it (yet). The kernel craps out after 60 elements.

Of course, you’re not allowed to output 2- or 1-element pixels, so you have to use image3 for input. What to do? It’s simple really: Say you have a Vector of numbers representing pairs (coordinates, in my case). The first pixel with contain x,y,x and the second y,x,y and so on. All you have to do is check the current pixel’s x-coordinate (assuming the height of your PixelBender input is set to 1) and do the calculations in one or the other order.

Note that in PixelBender, the x,y of the pixel (as reported by outCoord()) is the midpoint of the pixel, so x=0.5,1.5,2.5 and so on. To determine which order to take the calculations in, you just need to know whether x is even or odd.

Read the rest of this entry »

Using Pixel Bender for Math in Flash/FLEX

February 27th, 2009 by Rob

Pixel Bender has applications other than manipulating images in Flash/FLEX. While working on some map projection experiments, I decided to look for a way to offload some of the computations to avoid freezing up the display or simply taking too long. Turns out, Pixel Bender not only does math several times faster than Flash, it can also be run asynchronously!

Read the rest of this entry »

A GPX Parser in ActionScript 3

January 14th, 2009 by Rob

GPX is the standard XML format used to store routes, tracks, waypoints and other data for GPS units.

I’m working on a parser in AS3 that will load a GPX file and expose all the information inside as ActionScript objects. See a test using Google Maps below.

Click “Load” to load the file from my server (this is a file containing a short bike ride around Victoria, BC), or click “Browse…” to load your own GPX file.

I haven’t tested this very extensively, so if your file has features that mine doesn’t, it may fail. If it does, please leave a comment! I’ll be uploading this to Google Code as soon as I do some more tests.

Note: you’ll need Flash Player 10 to use the browse button.

On-the-fly Localization

August 18th, 2008 by Rob

So, what if you want your users to be able to change locales on-the-fly? I’ve put together a simple component that loads and manages translations via XML.

Read the rest of this entry »

Changing Character Set Embedding in a Flash Document

August 15th, 2008 by Rob

Here’s a little JSFL script that you can use to change the character-set embedding in all the input and dynamic TextFields in an entire Flash document. When you run it, it’ll pick the active document and pop up a prompt asking for the character-set IDs.

Read the rest of this entry »

Dynamic Font Loading in Flash 9

June 18th, 2008 by Rob

One of the biggest headaches in localizing Flash sites is setting up to dynamically load and easily use embedded fonts. Recently, in order to build a site that will need localization for an as-yet-undecided number of countries, I finally decided to dive in and solve this problem once and for all.

Read the rest of this entry »