?? tinydb.html
字號:
Note that you can use a <code>WHERE</code> clause over nodeid to send queries
to only a subset of the network.
<p>
<b> Aggregation Predicates </b>:
TinyDB also allows you to compute <i>aggregates</i> over readings being reported by
several nodes in a query. For example, to compute the average temperature reading of all
the sensors where the light is above 400, you would issue the query:
<blockquote>
<table border=0 hspace="4" cellspacing=2 width="50%" cellpadding=3>
<tr bgcolor="#e0e0e0">
<td width="100%"><pre>
SELECT
AVG(temp) FROM
sensors WHERE
light > 400
SAMPLE PERIOD 1024
</pre></td></tr></table>
</blockquote>
<p>
To specify this query, select "AVG" in the aggregation operator menu before moving
temp into the projected attributes list:
<p>
<IMG src="imgs/tinydb-aggr.jpg"></IMG>
<p>
TinyDB computes this query via an efficient <i>in-network</i> approach, where sensors
aggregate their own readings with readings from their neighbors and forward those
aggregate values towards the base station.
<p>
You've now seen how to pose a simple query with filtration and aggregation predicates.
In the next section, you'll learn how to write a small, standalone program to run
TinyDB queries.
<p>
<table border=0 hspace="4" cellspacing=2 width="100%" cellpadding=3>
<tr bgcolor="#e0e0ff">
<td width="100%"><nobr> <b> <font
face="arial,helvetica">A Simple Java Program to Use TinyDB</font> </b></nobr></td>
</tr> </table></p>
In this section, you will learn how to write a simple standalone Java
program to run a query in TinyDB. This program runs the query <code>SELECT light FROM sensors</code>.
First, we'll look at the whole program and then we'll see how the individual pieces work (this
application is available in tools/java/net/tinyos/tinydb/DemoApp.java):
<blockquote>
<table border=0 hspace="4" cellspacing=2 width="80%" cellpadding=3>
<tr bgcolor="#e0e0e0">
<td width="100%"><pre>
package net.tinyos.tinydb;
import net.tinyos.tinydb.parser.*;
import java.util.Vector;
import java.io.*;
public class DemoApp implements ResultListener{
public DemoApp() {
try {
TinyDBMain.initMain(); //parse the query
q = SensorQueryer.translateQuery("SELECT light", (byte)1);
//inject the query, registering ourselves as a listener for result
System.out.println("Sending query.");
TinyDBMain.injectQuery( q, this);
} catch (IOException e) {
System.out.println("Network error.");
} catch (ParseException e) {
System.out.println("Invalid Query.");
}
}
/* ResultListener method called whenever a result arrives */
public void addResult(QueryResult qr) {
Vector v = qr.resultVector(); //print the result
for (int i = 0; i < v.size(); i++) {
System.out.print("\t" + v.elementAt(i) + "\t|");
}
System.out.println();
}
public static void main(String argv[]) {
new DemoApp();
}
TinyDBQuery q;
}
</pre></td></tr></table>
</blockquote>
To try out this program, set up your motes as above (making sure you close any open TinyDB windows)
and type <code>java.net.tinyos.tinydb.DemoApp</code> from the <code>tools/java/</code>
directory. You should see output like:
<blockquote>
<table border=0 hspace="4" cellspacing=2 width="80%" cellpadding=3>
<tr bgcolor="#e0e0e0">
<td width="100%"><pre>
Listening for client connections on port 9000
SerialPortIO: initializing
Successfully opened COM1
client connected from localhost.localdomain (127.0.0.1)
Sending query.
1 | 835 |
2 | 833 |
3 | 833 |
4 | 833 |
5 | 833 |
6 | 833 |
7 | 833 |
...
</pre></td></tr></table>
</blockquote>
Now, let's look at the program in more detail. First, notice that the
<code>DemoApp</code> class implements the <code>ResultListener</code>
interface. The <code>addResult(...)</code> method is the only member
of this interface, and it will be called whenever a result arrives for
any query which DemoApp has registered. We'll see how registration
works in a moment.
<p>
The first lines of the <code>DemoApp()</code> constructor initialize TinyDB,
parse the query, and inject the query into the network:
<blockquote>
<table border=0 hspace="4" cellspacing=2 width="80%" cellpadding=3>
<tr bgcolor="#e0e0e0">
<td width="100%"><pre>
TinyDBMain.initMain();
//parse the query
q = SensorQueryer.translateQuery("SELECT light", (byte)1);
//inject the query, registering ourselves as a listener for result
System.out.println("Sending query.");
TinyDBMain.injectQuery( q, this);
</pre></td></tr></table>
</blockquote>
The call to <code>TinyDB.initMain()</code> reads the TinyDB configuration file
and sets up network communication. By default, it opens its own connection
to the serial port, although it can be configured to share that connection
via a <code>SerialForwarder</code> (see the <A href="../tinydb.pdf">TinyDB documentation</A>
on the configuration file.)
<p>
Next, the <code>SensorQueryer.translateQuery(..)</code> call converts
the specified SQL query into a <code>TinyDBQuery</code> object; the second parameter
(<code>(byte)1</code>) specifies that the query id for this query should be 1; this
id can be used to cancel or modify the query after it has been injected.
<p>
Finally, the <code>TinyDBMain.injectQuery(..)</code> actually sends the query into
the network and starts it running. The second parameter, <code>this</code>,
specifies that the <code>DemoApp</code> object should be registered as
a listener for results from this query.
<p>
That's it! The query is now running. Whenever a result arrives for this query,
the <code>addResult</code> method will be called to print out each field of
each result that arrives.
<p>
By now, you've learned how to use the TinyDB GUI and write simple applications
that interact with TinyDB motes. In the next section, you'll learn how to extend
TinyDB with new attributes that can be queried via the TinyDB query interface.
<p>
<table border=0 hspace="4" cellspacing=2 width="100%" cellpadding=3>
<tr bgcolor="#e0e0ff">
<td width="100%"><nobr> <b> <font
face="arial,helvetica">Adding an Attribute</font> </b></nobr></td>
</tr> </table></p>
<P>TinyDB comes with a builtin command for adding constant
attributes. To add a constant attribute, click the "Add Attribute" button
in the Mote Commands window. A dialog box will pop up asking you to fill
in the attribute name, type and a constant value. Simply fill them in and
click the "OK" button. The green LED on the motes should toggle once
if they receive the "Add Attribute" command. The new attribute should be
ready for use. You can now run queries over that attribute.</P>
<P><IMG alt="" hspace=100 src="imgs\tinydb-cmd.jpg" ><IMG id=IMG1 alt=""
src="imgs\tinydb-addattr.jpg" ></P>
<P>To add an attribute for a specific mote, you can uncheck the "Broadcast"
check box in the Mote Commands window, then fill in a Target Id. If a
constant attribute of the same name has already been registered, the old
constant value of the attribute will be overwritten with the new value.</P>
<P>Adding non-constant attributes will involve some NesC programming using the
TinySchema API. The easiest way to get started is to copy and modify some
builtin attributes implemented in tos/lib/Attributes.</P>
<p>
<table border=0 hspace="4" cellspacing=2 width="100%" cellpadding=3>
<tr bgcolor="#e0e0ff">
<td width="100%"><nobr> <b> <font
face="arial,helvetica">Where To Look For More Information</font> </b></nobr></td>
</tr> </table></p>
There are a number of TinyDB related resources available to users of the system:
<ul>
<li> <a href="http://telegraph.cs.berkeley.edu/tinydb">The TinyDB Website</a>: Look here for information about the latest release of TinyDB, as well as links to media, source code, and standalone images that can be programmed into a mote.
<li> <A href="../tinydb.pdf">The TinyDB User Manual</A>: A much more thorough guide to installing, using, and modifying TinyDB. Be sure to check out the
FAQ at the end if you are having difficulty!
<li> <A href="../tinyschema.pdf">The TinySchema Manual</A>: A guide to using
attributes and commands (with or without TinyDB).</li>
</ul>
<p>
<hr>
<b><A href="index.html"> Tutorial Index</A></b>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -