Skip to content

Commit 2318d81

Browse files
committed
Jackson Message struct replacement in LoRaEngine
1 parent 4334b3b commit 2318d81

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

app/src/main/java/net/sharksystem/asap/android/lora/LoRaBTInputOutputStream.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class LoRaBTInputOutputStream {
4343

4444
public void close() {
4545
try {
46+
//TODO Cleanup all loRaASAPOutputStreams
47+
//TODO Cleanup all loRaASAPInputStreams
4648
if (this.btSocket != null)
4749
btSocket.close();
4850
} catch (IOException e) {
@@ -81,9 +83,12 @@ public void flushASAPOutputStreams() throws IOException {
8183

8284
static class LoRaBTInputStream extends FilterInputStream {
8385

84-
public AbstractASAPLoRaMessage readASAPLoRaMessage(){
86+
public AbstractASAPLoRaMessage readASAPLoRaMessage() throws IOException, ASAPLoRaException {
8587
BufferedReader br = new BufferedReader(new InputStreamReader(this));
86-
return null;//TODO objectMapper.readValue(br.readLine(), AbstractASAPLoRaMessage.class);
88+
String rawASAPLoRaMessage = br.readLine();
89+
Log.i(CLASS_LOG_TAG, "Reading Message from BT Board: " + rawASAPLoRaMessage);
90+
//TODO do not use empty line
91+
return AbstractASAPLoRaMessage.createASAPLoRaMessage(rawASAPLoRaMessage);
8792
}
8893

8994
public LoRaBTInputStream(InputStream in) {

app/src/main/java/net/sharksystem/asap/android/lora/LoRaCommunicationManager.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class LoRaCommunicationManager extends Thread {
2727
* - Discovery neuer Peers und Benachrichtigung der @{@link LoRaEngine}
2828
*/
2929
private static final String CLASS_LOG_TAG = "ASAPLoRaCommManager";
30+
private static final long FLUSH_BUFFER_TIMEOUT = 250;
3031
private static LoRaBTInputOutputStream ioStream = null;
3132
private BluetoothDevice btDevice = null;
3233

@@ -76,29 +77,31 @@ public void run() {
7677
//this.ioStream.getOutputStream().write(new RawASAPLoRaMessage("AT"));
7778
this.ioStream.getOutputStream().write(new DiscoverASAPLoRaMessage()); //TODO, do this periodically?
7879
//this.ioStream.getOutputStream().write(new ASAPLoRaMessage("A2FF", "Hi there!"));
79-
80+
long lastBufferFlush = System.currentTimeMillis();
8081
while (!this.isInterrupted()) {
81-
if (this.ioStream.getInputStream().available() > 0) {
82-
AbstractASAPLoRaMessage asapLoRaMessage = this.ioStream.getInputStream().readASAPLoRaMessage();
83-
Log.i(this.CLASS_LOG_TAG, "Message recieved: " + asapLoRaMessage.toString());
84-
//TODO, this is smelly... visitorpattern? handleMessage() in abstract?
85-
if (asapLoRaMessage instanceof ASAPLoRaMessage) {
86-
//New Message inbound, write to corresponding stream of ASAPPeer
87-
this.ioStream.getASAPInputStream(((ASAPLoRaMessage) asapLoRaMessage).address).appendData(((ASAPLoRaMessage) asapLoRaMessage).message);
88-
} else if (asapLoRaMessage instanceof DeviceDiscoveredASAPLoRaMessage) {
89-
//New Device in Range found
90-
LoRaEngine.getASAPLoRaEngine().tryConnect(((DeviceDiscoveredASAPLoRaMessage) asapLoRaMessage).address);
91-
} else if (asapLoRaMessage instanceof ErrorASAPLoRaMessage) {
92-
//LoRa Error occured
93-
// TODO
82+
//Periodically flush Buffers
83+
if ((System.currentTimeMillis() - lastBufferFlush) > this.FLUSH_BUFFER_TIMEOUT) {
84+
if (this.ioStream.getInputStream().available() > 0) {
85+
AbstractASAPLoRaMessage asapLoRaMessage = this.ioStream.getInputStream().readASAPLoRaMessage();
86+
Log.i(this.CLASS_LOG_TAG, "Message recieved: " + asapLoRaMessage.toString());
87+
//TODO, this is smelly... visitorpattern? handleMessage() in abstract?
88+
if (asapLoRaMessage instanceof ASAPLoRaMessage) {
89+
//New Message inbound, write to corresponding stream of ASAPPeer
90+
this.ioStream.getASAPInputStream(((ASAPLoRaMessage) asapLoRaMessage).address).appendData(((ASAPLoRaMessage) asapLoRaMessage).message);
91+
} else if (asapLoRaMessage instanceof DeviceDiscoveredASAPLoRaMessage) {
92+
//New Device in Range found
93+
LoRaEngine.getASAPLoRaEngine().tryConnect(((DeviceDiscoveredASAPLoRaMessage) asapLoRaMessage).address);
94+
} else if (asapLoRaMessage instanceof ErrorASAPLoRaMessage) {
95+
//LoRa Error occured
96+
// TODO
97+
}
9498
}
95-
}
9699

97-
//Periodically flush Write Buffers
98-
this.ioStream.flushASAPOutputStreams();
99-
//LoRa is slow, we really don't need to run at full steam all the time
100-
try { Thread.sleep(250); } catch (InterruptedException e) {}
100+
this.ioStream.flushASAPOutputStreams();
101+
lastBufferFlush = System.currentTimeMillis();
102+
}
101103
}
104+
Log.i(CLASS_LOG_TAG, "Thread was interrupted, shutting down.");
102105
} catch (IOException | ASAPLoRaException e) {
103106
Log.e(this.CLASS_LOG_TAG, e.getMessage());
104107
//throw new ASAPLoRaException(e);

0 commit comments

Comments
 (0)