Remove TODO file
[remote/remote-gui.git] / src / main / util / MoteControl.java
blobb82f7787612e877507beb2dbc5791b3091a1f90a
1 package util;
2 import diku.distlab.motecontrolclientlib.client.*;
4 import java.io.File;
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)
28 this.mote = mote;
29 this.ledsEnabled = enableLeds;
30 this.moteLeds = new LedsInputStream(null);
31 this.dataReader();
35 public OutputStream getOutputStream()
37 if (os == null)
39 os = new PipedOutputStream();
40 mote.connectDataSource(os);
42 return os;
45 public void attachOutputStream(OutputStream sink)
47 this.sink = 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()
61 try{
62 // First, create a dataSink pipe
63 dataSink = new PipedInputStream();
64 mote.connectDataSink(dataSink);
65 moteIn = dataSink;
67 // Then, attach a log stream if logging is enabled
68 if (logging)
70 moteIn = new InputLogStream(new BufferedOutputStream(new FileOutputStream(logFile,true)),moteIn);
73 // Finally, attach the led stream if leds are enabled
74 if (ledsEnabled)
76 moteLeds.setInputStream(moteIn);
77 moteIn = moteLeds;
80 catch (Exception e)
82 e.printStackTrace();
87 public void run() {
88 this.configureStreams();
89 int b = -1;
90 while(dataSink != null)
92 try
94 b = moteIn.read();
96 catch (IOException e)
98 // e.printStackTrace();
99 break;
102 if (sink != null && b>=0)
104 try {
105 sink.write(b);
107 catch (IOException e)
109 e.printStackTrace();
110 sink = null;
120 if (dataThread != null && dataThread.isAlive())
122 dataThread.interrupt();
125 dataThread = new Thread(r);
126 dataThread.start();
129 public void startLog(File file) throws Exception
131 if (!file.exists()) file.createNewFile();
132 this.logFile = file;
133 this.logging = true;
134 dataReader();
137 public void stopLog()
139 if (!this.logging) return;
140 this.logging = false;
141 dataReader();
144 public SimpleMote getMote() {
145 return mote;
148 public LedsInputStream getMoteLeds() {
149 return moteLeds;
152 public boolean isLedsEnabled() {
153 return ledsEnabled;
156 public void enableLeds() {
157 if (this.ledsEnabled) return;
158 this.ledsEnabled = true;
159 dataReader();
162 public void disableLeds() {
163 if (!this.ledsEnabled) return;
164 this.ledsEnabled = false;
165 dataReader();
168 public boolean isLogging() {
169 return logging;