55
66import net .sharksystem .asap .android .lora .messages .ASAPLoRaMessage ;
77import net .sharksystem .asap .android .lora .messages .AbstractASAPLoRaMessage ;
8- import net .sharksystem .asap .android .lora .messages .RawASAPLoRaMessage ;
98
109import java .io .BufferedOutputStream ;
1110import 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 }
0 commit comments