Skip to content

Commit 8514860

Browse files
committed
Remove Jackson dependency
1 parent 9b437b1 commit 8514860

File tree

6 files changed

+95
-30
lines changed

6 files changed

+95
-30
lines changed

app/src/androidTest/java/net/sharksystem/asap/android/LoRaEngine/BasicCommunicationTest.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void deviceDiscoveryTest() throws IOException {
8484
String deviceResponse = sb.toString().trim();
8585
System.out.print("ASAP LoRaEngine Test Device Response: ");
8686
System.out.println(deviceResponse);
87-
assertEquals("{\"COMMAND\":\".DeviceDiscoveredASAPLoRaMessage\",\"address\":\"0000\"}", deviceResponse);
87+
assertEquals("{\"COMMAND\":\".DeviceDiscoveredASAPLoRaMessage\",\"address\":\"1000\"}", deviceResponse);
8888
break;
8989
}
9090
}
@@ -99,15 +99,15 @@ public void deviceDiscoveryTest() throws IOException {
9999
String deviceResponse = sb.toString().trim();
100100
System.out.print("ASAP LoRaEngine Test Device Response: ");
101101
System.out.println(deviceResponse);
102-
assertEquals("{\"COMMAND\":\".DeviceDiscoveredASAPLoRaMessage\",\"address\":\"0001\"}", deviceResponse);
102+
assertEquals("{\"COMMAND\":\".DeviceDiscoveredASAPLoRaMessage\",\"address\":\"1001\"}", deviceResponse);
103103
break;
104104
}
105105
}
106106
}
107107

108108
@Test(timeout=10000)
109109
public void simpleAliceToBobMessageTest() throws IOException {
110-
this.AliceSocket.getOutputStream().write("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"0001\",\"message\":\"Hello World!\"}".getBytes());
110+
this.AliceSocket.getOutputStream().write("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1001\",\"message\":\"Hello World!\"}".getBytes());
111111

112112
while(true){
113113
if(this.BobSocket.getInputStream().available() > 0) {
@@ -119,15 +119,15 @@ public void simpleAliceToBobMessageTest() throws IOException {
119119
String deviceResponse = sb.toString().trim();
120120
System.out.print("ASAP LoRaEngine Test Device Response: ");
121121
System.out.println(deviceResponse);
122-
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"0000\",\"message\":\"Hello World!\"}", deviceResponse);
122+
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1000\",\"message\":\"Hello World!\"}", deviceResponse);
123123
break;
124124
}
125125
}
126126
}
127127

128128
@Test(timeout=10000)
129129
public void simpleBobToAliceMessageTest() throws IOException {
130-
this.BobSocket.getOutputStream().write("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"0000\",\"message\":\"Hello World!\"}".getBytes());
130+
this.BobSocket.getOutputStream().write("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1000\",\"message\":\"Hello World!\"}".getBytes());
131131

132132
while(true){
133133
if(this.AliceSocket.getInputStream().available() > 0) {
@@ -139,7 +139,48 @@ public void simpleBobToAliceMessageTest() throws IOException {
139139
String deviceResponse = sb.toString().trim();
140140
System.out.print("ASAP LoRaEngine Test Device Response: ");
141141
System.out.println(deviceResponse);
142-
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"0001\",\"message\":\"Hello World!\"}", deviceResponse);
142+
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1001\",\"message\":\"Hello World!\"}", deviceResponse);
143+
break;
144+
}
145+
}
146+
}
147+
148+
@Test(timeout=20000)
149+
public void simultaneousMessageTest() throws IOException {
150+
this.BobSocket.getOutputStream().write("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1000\",\"message\":\"Hello World!\"}".getBytes());
151+
try {
152+
Thread.sleep(500);
153+
} catch (InterruptedException e) {
154+
e.printStackTrace();
155+
}
156+
this.AliceSocket.getOutputStream().write("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1001\",\"message\":\"Hello World!\"}".getBytes());
157+
158+
while(true){
159+
if(this.BobSocket.getInputStream().available() > 0) {
160+
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
161+
StringBuilder sb = new StringBuilder(this.BobSocket.getInputStream().available());
162+
do {
163+
sb.append(br.readLine()).append("\n");
164+
} while(br.ready());
165+
String deviceResponse = sb.toString().trim();
166+
System.out.print("ASAP LoRaEngine Test Device Response: ");
167+
System.out.println(deviceResponse);
168+
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1000\",\"message\":\"Hello World!\"}", deviceResponse);
169+
break;
170+
}
171+
}
172+
173+
while(true){
174+
if(this.AliceSocket.getInputStream().available() > 0) {
175+
BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
176+
StringBuilder sb = new StringBuilder(this.AliceSocket.getInputStream().available());
177+
do {
178+
sb.append(br.readLine()).append("\n");
179+
} while(br.ready());
180+
String deviceResponse = sb.toString().trim();
181+
System.out.print("ASAP LoRaEngine Test Device Response: ");
182+
System.out.println(deviceResponse);
183+
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1001\",\"message\":\"Hello World!\"}", deviceResponse);
143184
break;
144185
}
145186
}

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import android.bluetooth.BluetoothSocket;
44
import android.util.Log;
55

6-
import com.fasterxml.jackson.databind.ObjectMapper;
7-
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
8-
96
import net.sharksystem.asap.android.lora.messages.ASAPLoRaMessage;
107
import net.sharksystem.asap.android.lora.messages.AbstractASAPLoRaMessage;
118
import net.sharksystem.asap.android.lora.messages.RawASAPLoRaMessage;
129

10+
import java.io.BufferedOutputStream;
1311
import java.io.BufferedReader;
1412
import java.io.ByteArrayInputStream;
1513
import java.io.ByteArrayOutputStream;
@@ -32,20 +30,20 @@ public class LoRaBTInputOutputStream {
3230
* Syntax (erstidee): "ADDR:datadatadatadata"
3331
*/
3432
private static final String CLASS_LOG_TAG = "ASAPLoRaBTIOStream";
35-
private final ObjectMapper objectMapper = new ObjectMapper();
33+
//TODO private final ObjectMapper objectMapper = new ObjectMapper();
3634
private BluetoothSocket btSocket;
3735
private LoRaBTInputStream is;
3836
private LoRaBTOutputStream os;
3937
private HashMap<String, LoRaASAPInputStream> loRaASAPInputStreams = new HashMap<>();
40-
private HashMap<String, LoRaASAPOutputStream> loRaASAPOutputStreams = new HashMap<>();
38+
private HashMap<String, BufferedOutputStream> loRaASAPOutputStreams = new HashMap<String, BufferedOutputStream>();
4139

4240
LoRaBTInputOutputStream(BluetoothSocket btSocket) throws IOException {
4341
this.btSocket = btSocket;
4442
this.is = new LoRaBTInputStream(btSocket.getInputStream());
4543
this.os = new LoRaBTOutputStream(btSocket.getOutputStream());
4644

4745
//Use Polymorphic Type Detection for JSON Object Mapping
48-
objectMapper.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build(), ObjectMapper.DefaultTyping.NON_FINAL);
46+
//TODO objectMapper.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build(), ObjectMapper.DefaultTyping.NON_FINAL);
4947
}
5048

5149
public void close() {
@@ -57,11 +55,11 @@ public void close() {
5755
}
5856
}
5957

60-
public LoRaASAPOutputStream getASAPOutputStream(String mac) {
58+
public OutputStream getASAPOutputStream(String mac) {
6159
if (this.loRaASAPOutputStreams.containsKey(mac))
6260
return this.loRaASAPOutputStreams.get(mac);
6361

64-
this.loRaASAPOutputStreams.put(mac, new LoRaASAPOutputStream(mac));
62+
this.loRaASAPOutputStreams.put(mac, new BufferedOutputStream(new LoRaASAPOutputStream(mac),20)); //TODO increase buffer size
6563
return this.getASAPOutputStream(mac); //TODO rewrite to make sure to never have endless loop?
6664
}
6765

@@ -81,11 +79,16 @@ public LoRaBTOutputStream getOutputStream() {
8179
return os;
8280
}
8381

82+
public void flushASAPOutputStreams() throws IOException {
83+
for(BufferedOutputStream bufferedOutputStream : this.loRaASAPOutputStreams.values())
84+
bufferedOutputStream.flush();
85+
}
86+
8487
class LoRaBTInputStream extends FilterInputStream {
8588

8689
public AbstractASAPLoRaMessage readASAPLoRaMessage() throws IOException {
8790
BufferedReader br = new BufferedReader(new InputStreamReader(this));
88-
return objectMapper.readValue(br.readLine(), AbstractASAPLoRaMessage.class);
91+
return null;//TODO objectMapper.readValue(br.readLine(), AbstractASAPLoRaMessage.class);
8992
}
9093

9194
public LoRaBTInputStream(InputStream in) {
@@ -94,15 +97,21 @@ public LoRaBTInputStream(InputStream in) {
9497
}
9598

9699
class LoRaBTOutputStream extends FilterOutputStream {
100+
private static final String CLASS_LOG_TAG = "ASAPLoRaBTOutputStream";
101+
97102
public LoRaBTOutputStream(OutputStream out) {
98103
super(out);
99104
}
100105

101106
public void write(AbstractASAPLoRaMessage msg) throws IOException {
102107
if (msg instanceof RawASAPLoRaMessage)
103108
this.write(msg.toString().getBytes());
104-
else
105-
this.write(objectMapper.writeValueAsString(msg).getBytes());
109+
else {
110+
String msgString = ""; //TODO objectMapper.writeValueAsString(msg);
111+
Log.i(this.CLASS_LOG_TAG, "Writing Message to BT Board: "+msgString);
112+
this.write(msgString.getBytes());
113+
}
114+
this.write('\n');
106115
}
107116
}
108117

@@ -123,6 +132,13 @@ public synchronized void appendData(byte[] data) {
123132

124133
@Override
125134
public synchronized int read() throws IOException {
135+
while(sis.available() <= 0) { //TODO Timeout
136+
try {
137+
Thread.sleep(100);
138+
} catch (InterruptedException e) {
139+
//return -1; //No Data
140+
}
141+
}
126142
return sis.read();
127143
}
128144
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.bluetooth.BluetoothAdapter;
44
import android.bluetooth.BluetoothDevice;
55
import android.bluetooth.BluetoothSocket;
6+
import android.os.Build;
67
import android.util.Log;
78

89
import net.sharksystem.asap.android.lora.messages.ASAPLoRaMessage;
@@ -14,6 +15,7 @@
1415
import java.io.IOException;
1516
import java.io.InputStream;
1617
import java.io.OutputStream;
18+
import java.util.Base64;
1719
import java.util.UUID;
1820

1921
public class LoRaCommunicationManager extends Thread {
@@ -78,21 +80,24 @@ public void run() {
7880
while (!this.isInterrupted()) {
7981
if (this.ioStream.getInputStream().available() > 0) {
8082
AbstractASAPLoRaMessage asapLoRaMessage = this.ioStream.getInputStream().readASAPLoRaMessage();
81-
83+
Log.i(this.CLASS_LOG_TAG, "Message recieved: " + asapLoRaMessage.toString());
8284
//TODO, this is smelly... visitorpattern? handleMessage() in abstract?
8385
if (asapLoRaMessage instanceof ASAPLoRaMessage) {
8486
//New Message inbound, write to corresponding stream of ASAPPeer
85-
Log.i(this.CLASS_LOG_TAG, asapLoRaMessage.toString());
8687
this.ioStream.getASAPInputStream(((ASAPLoRaMessage) asapLoRaMessage).address).appendData(((ASAPLoRaMessage) asapLoRaMessage).message);
8788
} else if (asapLoRaMessage instanceof DeviceDiscoveredASAPLoRaMessage) {
8889
//New Device in Range found
89-
Log.i(this.CLASS_LOG_TAG, asapLoRaMessage.toString());
9090
LoRaEngine.getASAPLoRaEngine().tryConnect(((DeviceDiscoveredASAPLoRaMessage) asapLoRaMessage).address);
9191
} else if (asapLoRaMessage instanceof ErrorASAPLoRaMessage) {
9292
//LoRa Error occured
93-
Log.i(this.CLASS_LOG_TAG, asapLoRaMessage.toString()); //TODO
93+
// TODO
9494
}
9595
}
96+
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) {}
96101
}
97102
} catch (IOException e) {
98103
Log.e(this.CLASS_LOG_TAG, e.getMessage());

app/src/main/java/net/sharksystem/asap/android/lora/messages/ASAPLoRaMessage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class ASAPLoRaMessage extends AbstractASAPLoRaMessage {
88
//Constructor for Jackson
99
public ASAPLoRaMessage(){
1010
this.address = "";
11-
this.message = new byte[0];
11+
this.message = new byte[0];
1212
}
1313
public ASAPLoRaMessage(String address, byte[] message){
1414
this.address = address;
@@ -17,6 +17,6 @@ public ASAPLoRaMessage(String address, byte[] message){
1717

1818
@Override
1919
public String toString() {
20-
return this.address + ": " + this.message;
20+
return "ASAPLoRaMessage (" + this.address + "): " + this.message;
2121
}
2222
}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package net.sharksystem.asap.android.lora.messages;
22

3-
import com.fasterxml.jackson.annotation.JsonSubTypes;
4-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5-
6-
@JsonTypeInfo(use=JsonTypeInfo.Id.MINIMAL_CLASS, property="COMMAND")
7-
@JsonSubTypes( {@JsonSubTypes.Type(DiscoverASAPLoRaMessage.class), @JsonSubTypes.Type(ASAPLoRaMessage.class)})
8-
public abstract class AbstractASAPLoRaMessage {}
3+
//TODO
4+
//@JsonTypeInfo(use=JsonTypeInfo.Id.MINIMAL_CLASS, property="COMMAND")
5+
//@JsonSubTypes( {@JsonSubTypes.Type(DiscoverASAPLoRaMessage.class), @JsonSubTypes.Type(ASAPLoRaMessage.class)})
6+
public abstract class AbstractASAPLoRaMessage {
7+
@Override
8+
public String toString() {
9+
return "AbstractASAPLoRaMessage derived class: " + this.getClass().getName();
10+
}
11+
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
jcenter()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:4.0.1'
10+
classpath 'com.android.tools.build:gradle:4.1.1'
1111

1212

1313
// NOTE: Do not place your application dependencies here; they belong

0 commit comments

Comments
 (0)