The New AirStore in Sun SPOT SDK
By user9157252 on Apr 26, 2010
Many of you may not know this, or may not have noticed, since ... well, we haven't made much noise about it, and it's only available in the Developer Releases so far, but it's just too cool not to say something about.
AirStore. What is AirStore? It's pretty much just what it sounds like. Storing stuff "in the air." And that's pretty much how it works. From one SPOT I can take some information and put it into the AirStore. Then from any other SPOT on in the area, I can just get values out of that AirStore. No writing the tiring code of connecting the radios, of making sure you're connecting to the right SPOT, etc. Just get some values.
And it really is that easy. A few steps:
- Add these lines to your project's build.properties file:
- Next, in your 'sending' project, just add an AirStore and put values in it:
int x = 49;
int y = 47;
int z = 97;
Record r = new Record();
- In your 'receiving' project, add an AirStore and get the values from it:
RecordTemplate rt = new RecordTemplate();
Record r = AirStore.getMatch(rt);
int x = r.getInt("X");
int y = r.getInt("Y");
int z = r.getInt("Z");
- That's it!
By using the built-in (in the Demos directory) AirStore Inspector Application you can see the values being stored in the AirStore:
If you deploy code containing step 2 to one SPOT, and code containing step 3 to another SPOT, the value of X from SPOT 1 will be sent to SPOT 2 (or, rather, any number of SPOTs running the code in step 3!). You'll see those values 'stored' in the air, and 'retrieved' from the air as well.
Now, there's something in there that needs a bit more explaining. The RecordTemplate part. This is a nifty bit of work that allows you to only retrieve records from the AirStore that match a particular pattern. In the code-example above, only a record in the AirStore that has X, Y and Z Keys will 'match' the template, and will be retrieved. So if I have another SPOT storing values into the AirStore as "A", "B" and "C", they won't get mixed up and mis-read here.
You can run the AirStore Inspector Application by running 'ant host-run' in the Demos/AirStore/AirStore_Inspector directory that comes with your SDK (starting with Red). With the AirStore Inspector running (see above) you can see the values you are writing to the AirStore.
This is a simple example of making a simple program to transmit values from one SPOT to another. Notice how much easier this is than the old way of establishing radio connections, etc. AirStore handles all of those details for you 'under the covers' so all you have to do is deal with your application code.
It is possible to 'remove' records from the AirStore rather than just getting them, and I'll cover that in a subsequent blog post.