Skip to content

Commit ef93cf1

Browse files
committed
Started Jackson polymorphy replacement
1 parent 8514860 commit ef93cf1

File tree

7 files changed

+52
-39
lines changed

7 files changed

+52
-39
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void setup() throws IOException, InterruptedException {
6060
BasicCommunicationTest.AliceSocket.connect();
6161
BasicCommunicationTest.BobSocket.connect();
6262

63-
Thread.sleep(5000); //Give the BT Modules some time to stabilize
63+
Thread.sleep(1000); //Give the BT Modules some time to stabilize
6464
}
6565

6666
@Test
@@ -72,7 +72,7 @@ public void usesAppContext() {
7272

7373
@Test(timeout=20000)
7474
public void deviceDiscoveryTest() throws IOException {
75-
this.AliceSocket.getOutputStream().write("{\"COMMAND\":\".DiscoverASAPLoRaMessage\"}".getBytes());
75+
this.AliceSocket.getOutputStream().write("DSCVR\n".getBytes());
7676

7777
while(true){
7878
if(this.BobSocket.getInputStream().available() > 0) {
@@ -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\":\"1000\"}", deviceResponse);
87+
assertEquals("DVDCR: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\":\"1001\"}", deviceResponse);
102+
assertEquals("DVDCR: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\":\"1001\",\"message\":\"Hello World!\"}".getBytes());
110+
this.AliceSocket.getOutputStream().write("MSSGE@1001:Hello World!\n".getBytes());
111111

112112
while(true){
113113
if(this.BobSocket.getInputStream().available() > 0) {
@@ -127,7 +127,7 @@ public void simpleAliceToBobMessageTest() throws IOException {
127127

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

132132
while(true){
133133
if(this.AliceSocket.getInputStream().available() > 0) {
@@ -147,13 +147,13 @@ public void simpleBobToAliceMessageTest() throws IOException {
147147

148148
@Test(timeout=20000)
149149
public void simultaneousMessageTest() throws IOException {
150-
this.BobSocket.getOutputStream().write("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1000\",\"message\":\"Hello World!\"}".getBytes());
150+
this.BobSocket.getOutputStream().write("MSSGE@1000:Hello World!\n".getBytes());
151151
try {
152152
Thread.sleep(500);
153153
} catch (InterruptedException e) {
154154
e.printStackTrace();
155155
}
156-
this.AliceSocket.getOutputStream().write("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1001\",\"message\":\"Hello World!\"}".getBytes());
156+
this.AliceSocket.getOutputStream().write("MSSGE@1001:Hello World!\n".getBytes());
157157

158158
while(true){
159159
if(this.BobSocket.getInputStream().available() > 0) {

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

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import net.sharksystem.asap.android.lora.messages.ASAPLoRaMessage;
77
import net.sharksystem.asap.android.lora.messages.AbstractASAPLoRaMessage;
8-
import net.sharksystem.asap.android.lora.messages.RawASAPLoRaMessage;
98

109
import java.io.BufferedOutputStream;
1110
import java.io.BufferedReader;
@@ -30,36 +29,32 @@ public class LoRaBTInputOutputStream {
3029
* Syntax (erstidee): "ADDR:datadatadatadata"
3130
*/
3231
private static final String CLASS_LOG_TAG = "ASAPLoRaBTIOStream";
33-
//TODO private final ObjectMapper objectMapper = new ObjectMapper();
34-
private BluetoothSocket btSocket;
35-
private LoRaBTInputStream is;
36-
private LoRaBTOutputStream os;
37-
private HashMap<String, LoRaASAPInputStream> loRaASAPInputStreams = new HashMap<>();
38-
private HashMap<String, BufferedOutputStream> loRaASAPOutputStreams = new HashMap<String, BufferedOutputStream>();
32+
private final BluetoothSocket btSocket;
33+
private final LoRaBTInputStream is;
34+
private final LoRaBTOutputStream os;
35+
private final HashMap<String, LoRaASAPInputStream> loRaASAPInputStreams = new HashMap<>();
36+
private final HashMap<String, BufferedOutputStream> loRaASAPOutputStreams = new HashMap<>();
3937

4038
LoRaBTInputOutputStream(BluetoothSocket btSocket) throws IOException {
4139
this.btSocket = btSocket;
4240
this.is = new LoRaBTInputStream(btSocket.getInputStream());
4341
this.os = new LoRaBTOutputStream(btSocket.getOutputStream());
44-
45-
//Use Polymorphic Type Detection for JSON Object Mapping
46-
//TODO objectMapper.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build(), ObjectMapper.DefaultTyping.NON_FINAL);
4742
}
4843

4944
public void close() {
5045
try {
5146
if (this.btSocket != null)
5247
btSocket.close();
5348
} catch (IOException e) {
54-
Log.e(this.CLASS_LOG_TAG, e.getMessage());
49+
Log.e(CLASS_LOG_TAG, e.getMessage());
5550
}
5651
}
5752

5853
public OutputStream getASAPOutputStream(String mac) {
5954
if (this.loRaASAPOutputStreams.containsKey(mac))
6055
return this.loRaASAPOutputStreams.get(mac);
6156

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

@@ -80,13 +75,13 @@ public LoRaBTOutputStream getOutputStream() {
8075
}
8176

8277
public void flushASAPOutputStreams() throws IOException {
83-
for(BufferedOutputStream bufferedOutputStream : this.loRaASAPOutputStreams.values())
78+
for (BufferedOutputStream bufferedOutputStream : this.loRaASAPOutputStreams.values())
8479
bufferedOutputStream.flush();
8580
}
8681

87-
class LoRaBTInputStream extends FilterInputStream {
82+
static class LoRaBTInputStream extends FilterInputStream {
8883

89-
public AbstractASAPLoRaMessage readASAPLoRaMessage() throws IOException {
84+
public AbstractASAPLoRaMessage readASAPLoRaMessage(){
9085
BufferedReader br = new BufferedReader(new InputStreamReader(this));
9186
return null;//TODO objectMapper.readValue(br.readLine(), AbstractASAPLoRaMessage.class);
9287
}
@@ -96,26 +91,22 @@ public LoRaBTInputStream(InputStream in) {
9691
}
9792
}
9893

99-
class LoRaBTOutputStream extends FilterOutputStream {
94+
static class LoRaBTOutputStream extends FilterOutputStream {
10095
private static final String CLASS_LOG_TAG = "ASAPLoRaBTOutputStream";
10196

10297
public LoRaBTOutputStream(OutputStream out) {
10398
super(out);
10499
}
105100

106-
public void write(AbstractASAPLoRaMessage msg) throws IOException {
107-
if (msg instanceof RawASAPLoRaMessage)
108-
this.write(msg.toString().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-
}
101+
public void write(AbstractASAPLoRaMessage msg) throws IOException, ASAPLoRaException {
102+
String msgString = msg.getPayload();
103+
Log.i(CLASS_LOG_TAG, "Writing Message to BT Board: " + msgString);
104+
this.write(msgString.getBytes());
114105
this.write('\n');
115106
}
116107
}
117108

118-
class LoRaASAPInputStream extends InputStream {
109+
static class LoRaASAPInputStream extends InputStream {
119110
private final String LoRaAddress;
120111

121112
private SequenceInputStream sis;
@@ -132,7 +123,7 @@ public synchronized void appendData(byte[] data) {
132123

133124
@Override
134125
public synchronized int read() throws IOException {
135-
while(sis.available() <= 0) { //TODO Timeout
126+
while (sis.available() <= 0) { //TODO Timeout
136127
try {
137128
Thread.sleep(100);
138129
} catch (InterruptedException e) {
@@ -156,7 +147,7 @@ public synchronized void write(byte[] b, int off, int len) {
156147
//TODO...? Ist das sinnig?
157148
try {
158149
LoRaBTInputOutputStream.this.getOutputStream().write(new ASAPLoRaMessage(this.LoRaAddress, b));
159-
} catch (IOException e) {
150+
} catch (IOException | ASAPLoRaException e) {
160151
e.printStackTrace(); //TODO...
161152
}
162153
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void run() {
9999
//LoRa is slow, we really don't need to run at full steam all the time
100100
try { Thread.sleep(250); } catch (InterruptedException e) {}
101101
}
102-
} catch (IOException e) {
102+
} catch (IOException | ASAPLoRaException e) {
103103
Log.e(this.CLASS_LOG_TAG, e.getMessage());
104104
//throw new ASAPLoRaException(e);
105105
} finally {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ public class ASAPLoRaMessage extends AbstractASAPLoRaMessage {
66
public byte[] message;
77

88
//Constructor for Jackson
9-
public ASAPLoRaMessage(){
9+
public ASAPLoRaMessage() {
1010
this.address = "";
11-
this.message = new byte[0];
11+
this.message = new byte[0];
1212
}
13-
public ASAPLoRaMessage(String address, byte[] message){
13+
14+
public ASAPLoRaMessage(String address, byte[] message) {
1415
this.address = address;
1516
this.message = message;
1617
}
1718

19+
@Override
20+
public String getPayload() {
21+
return "MSSGE@" + this.address + ":" + this.message; //TODO Base64 the message?
22+
}
23+
1824
@Override
1925
public String toString() {
2026
return "ASAPLoRaMessage (" + this.address + "): " + this.message;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package net.sharksystem.asap.android.lora.messages;
22

3+
import net.sharksystem.asap.android.lora.ASAPLoRaException;
4+
35
//TODO
46
//@JsonTypeInfo(use=JsonTypeInfo.Id.MINIMAL_CLASS, property="COMMAND")
57
//@JsonSubTypes( {@JsonSubTypes.Type(DiscoverASAPLoRaMessage.class), @JsonSubTypes.Type(ASAPLoRaMessage.class)})
68
public abstract class AbstractASAPLoRaMessage {
9+
10+
public String getPayload() throws ASAPLoRaException {
11+
throw new ASAPLoRaException("Trying to call getPayload() on non-outgoing ASAP Message. This should never happen.");
12+
}
13+
714
@Override
815
public String toString() {
916
return "AbstractASAPLoRaMessage derived class: " + this.getClass().getName();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
package net.sharksystem.asap.android.lora.messages;
22

33
public class DiscoverASAPLoRaMessage extends AbstractASAPLoRaMessage {
4+
@Override
5+
public String getPayload() {
6+
return "DSCVR";
7+
}
48
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public RawASAPLoRaMessage(String msg){
88
this.rawMessage = msg;
99
}
1010

11+
@Override
12+
public String getPayload() {
13+
return this.rawMessage;
14+
}
15+
1116
@Override
1217
public String toString() {
1318
return this.rawMessage;

0 commit comments

Comments
 (0)