@@ -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