2 import diku
.distlab
.motecontrolclientlib
.client
.*;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.PipedOutputStream
;
8 import java
.io
.PipedInputStream
;
9 import java
.io
.BufferedOutputStream
;
10 import java
.io
.OutputStream
;
11 import java
.io
.InputStream
;
13 public class MoteControl
{
15 protected SimpleMote mote
;
16 protected LedsInputStream moteLeds
;
17 protected PipedOutputStream os
;
18 protected PipedInputStream dataSink
= null;
19 protected OutputStream sink
;
20 protected InputStream moteIn
;
21 protected boolean ledsEnabled
= false;
22 protected boolean logging
= false;
23 protected File logFile
;
24 protected Thread dataThread
= null;
26 MoteControl(SimpleMote mote
, boolean enableLeds
)
29 this.ledsEnabled
= enableLeds
;
30 this.moteLeds
= new LedsInputStream(null);
35 public OutputStream
getOutputStream()
39 os
= new PipedOutputStream();
40 mote
.connectDataSource(os
);
45 public void attachOutputStream(OutputStream sink
)
50 public void attachInputStream(InputStream source
)
52 this.mote
.connectDataSource(source
);
55 protected void dataReader()
57 Runnable r
= new Runnable() {
59 private void configureStreams()
62 // First, create a dataSink pipe
63 dataSink
= new PipedInputStream();
64 mote
.connectDataSink(dataSink
);
67 // Then, attach a log stream if logging is enabled
70 moteIn
= new InputLogStream(new BufferedOutputStream(new FileOutputStream(logFile
,true)),moteIn
);
73 // Finally, attach the led stream if leds are enabled
76 moteLeds
.setInputStream(moteIn
);
88 this.configureStreams();
90 while(dataSink
!= null)
98 // e.printStackTrace();
102 if (sink
!= null && b
>=0)
107 catch (IOException e
)
120 if (dataThread
!= null && dataThread
.isAlive())
122 dataThread
.interrupt();
125 dataThread
= new Thread(r
);
129 public void startLog(File file
) throws Exception
131 if (!file
.exists()) file
.createNewFile();
137 public void stopLog()
139 if (!this.logging
) return;
140 this.logging
= false;
144 public SimpleMote
getMote() {
148 public LedsInputStream
getMoteLeds() {
152 public boolean isLedsEnabled() {
156 public void enableLeds() {
157 if (this.ledsEnabled
) return;
158 this.ledsEnabled
= true;
162 public void disableLeds() {
163 if (!this.ledsEnabled
) return;
164 this.ledsEnabled
= false;
168 public boolean isLogging() {