22
33import android .util .Log ;
44
5+ import org .json .JSONException ;
6+ import org .json .JSONObject ;
7+
58import java .io .IOException ;
69import java .nio .ByteBuffer ;
710import java .nio .ByteOrder ;
811import java .nio .charset .Charset ;
912import java .util .Arrays ;
1013
14+ import io .pslab .interfaces .HttpCallback ;
15+ import io .pslab .others .ScienceLabCommon ;
16+
1117/**
1218 * Created by viveksb007 on 28/3/17.
1319 */
@@ -24,18 +30,19 @@ public class PacketHandler {
2430 private CommandsProto mCommandsProto ;
2531 private int timeout = 500 , VERSION_STRING_LENGTH = 15 ;
2632 ByteBuffer burstBuffer = ByteBuffer .allocate (2000 );
33+ private HttpAsyncTask httpAsyncTask ;
2734
2835 public PacketHandler (int timeout , CommunicationHandler communicationHandler ) {
2936 this .loadBurst = false ;
3037 this .connected = false ;
3138 this .timeout = timeout ;
3239 this .mCommandsProto = new CommandsProto ();
3340 this .mCommunicationHandler = communicationHandler ;
34- connected = mCommunicationHandler .isConnected ();
41+ connected = ( mCommunicationHandler .isConnected () || ScienceLabCommon . isWifiConnected () );
3542 }
3643
3744 public boolean isConnected () {
38- connected = mCommunicationHandler .isConnected ();
45+ connected = ( mCommunicationHandler .isConnected () || ScienceLabCommon . isWifiConnected () );
3946 return connected ;
4047 }
4148
@@ -44,7 +51,7 @@ public String getVersion() {
4451 sendByte (mCommandsProto .COMMON );
4552 sendByte (mCommandsProto .GET_VERSION );
4653 // Read "<PSLAB Version String>\n"
47- mCommunicationHandler . read ( buffer , VERSION_STRING_LENGTH + 1 , timeout );
54+ commonRead ( VERSION_STRING_LENGTH + 1 );
4855 version = new String (Arrays .copyOfRange (buffer , 0 , VERSION_STRING_LENGTH ), Charset .forName ("UTF-8" ));
4956 } catch (IOException e ) {
5057 Log .e ("Error in Communication" , e .toString ());
@@ -58,7 +65,7 @@ public void sendByte(int val) throws IOException {
5865 }
5966 if (!loadBurst ) {
6067 try {
61- mCommunicationHandler . write (new byte []{(byte ) (val & 0xff )}, timeout );
68+ commonWrite (new byte []{(byte ) (val & 0xff )});
6269 } catch (IOException | NullPointerException e ) {
6370 Log .e ("Error in sending byte" , e .toString ());
6471 e .printStackTrace ();
@@ -74,7 +81,7 @@ public void sendInt(int val) throws IOException {
7481 }
7582 if (!loadBurst ) {
7683 try {
77- mCommunicationHandler . write (new byte []{(byte ) (val & 0xff ), (byte ) ((val >> 8 ) & 0xff )}, timeout );
84+ commonWrite (new byte []{(byte ) (val & 0xff ), (byte ) ((val >> 8 ) & 0xff )});
7885 } catch (IOException e ) {
7986 Log .e ("Error in sending int" , e .toString ());
8087 e .printStackTrace ();
@@ -97,7 +104,7 @@ public int getAcknowledgement() {
97104 return 1 ;
98105 } else {
99106 try {
100- mCommunicationHandler . read ( buffer , 1 , timeout );
107+ commonRead ( 1 );
101108 return buffer [0 ];
102109 } catch (IOException | NullPointerException e ) {
103110 e .printStackTrace ();
@@ -108,7 +115,7 @@ public int getAcknowledgement() {
108115
109116 public byte getByte () {
110117 try {
111- int numByteRead = mCommunicationHandler . read ( buffer , 1 , timeout );
118+ int numByteRead = commonRead ( 1 );
112119 if (numByteRead == 1 ) {
113120 return buffer [0 ];
114121 } else {
@@ -123,7 +130,7 @@ public byte getByte() {
123130 int getVoltageSummation () {
124131 try {
125132 // Note : bytesToBeRead has to be +1 than the requirement
126- int numByteRead = mCommunicationHandler . read ( buffer , 3 , timeout );
133+ int numByteRead = commonRead ( 3 );
127134 if (numByteRead == 3 ) {
128135 return (buffer [0 ] & 0xff ) | ((buffer [1 ] << 8 ) & 0xff00 );
129136 } else {
@@ -137,7 +144,7 @@ int getVoltageSummation() {
137144
138145 public int getInt () {
139146 try {
140- int numByteRead = mCommunicationHandler . read ( buffer , 2 , timeout );
147+ int numByteRead = commonRead ( 2 );
141148 if (numByteRead == 2 ) {
142149 // LSB is read first
143150 return (buffer [0 ] & 0xff ) | ((buffer [1 ] << 8 ) & 0xff00 );
@@ -152,7 +159,7 @@ public int getInt() {
152159
153160 public long getLong () {
154161 try {
155- int numByteRead = mCommunicationHandler . read ( buffer , 4 , timeout );
162+ int numByteRead = commonRead ( 4 );
156163 if (numByteRead == 4 ) {
157164 // C++ has long of 4-bytes but in Java int has 4-bytes
158165 // refer "https://stackoverflow.com/questions/7619058/convert-a-byte-array-to-integer-in-java-and-vice-versa" for Endian
@@ -171,7 +178,7 @@ public boolean waitForData() {
171178 }
172179
173180 public int read (byte [] dest , int bytesToRead ) throws IOException {
174- int numBytesRead = mCommunicationHandler . read ( buffer , bytesToRead , timeout );
181+ int numBytesRead = commonRead ( bytesToRead );
175182 for (int i = 0 ; i < bytesToRead ; i ++) {
176183 dest [i ] = buffer [i ];
177184 }
@@ -185,10 +192,10 @@ public int read(byte[] dest, int bytesToRead) throws IOException {
185192
186193 public byte [] sendBurst () {
187194 try {
188- mCommunicationHandler . write (burstBuffer .array (), timeout );
195+ commonWrite (burstBuffer .array ());
189196 burstBuffer .clear ();
190197 loadBurst = false ;
191- int bytesRead = mCommunicationHandler . read ( buffer , inputQueueSize , timeout );
198+ int bytesRead = commonRead ( inputQueueSize );
192199 inputQueueSize = 0 ;
193200 return Arrays .copyOfRange (buffer , 0 , bytesRead );
194201 } catch (IOException e ) {
@@ -197,4 +204,51 @@ public byte[] sendBurst() {
197204 return new byte []{-1 };
198205 }
199206
207+ private int commonRead (int bytesToRead ) throws IOException {
208+ final int [] bytesRead = {0 };
209+ if (mCommunicationHandler .isConnected ()) {
210+ bytesRead [0 ] = mCommunicationHandler .read (buffer , bytesToRead , timeout );
211+ } else if (ScienceLabCommon .isWifiConnected ()) {
212+ httpAsyncTask = new HttpAsyncTask (ScienceLabCommon .getEspIP (), new HttpCallback <JSONObject >() {
213+ @ Override
214+ public void success (JSONObject jsonObject ) {
215+ try {
216+ //Server will send byte array
217+ buffer = (byte [])jsonObject .get ("data" );
218+ bytesRead [0 ] = buffer .length ;
219+ } catch (JSONException e ) {
220+ e .printStackTrace ();
221+ }
222+ }
223+
224+ @ Override
225+ public void error (Exception e ) {
226+ Log .e (TAG , "Error reading data over ESP" );
227+ }
228+ });
229+ httpAsyncTask .execute (new byte []{});
230+ }
231+ return bytesRead [0 ];
232+ }
233+
234+ private void commonWrite (byte [] data ) throws IOException {
235+ if (mCommunicationHandler .isConnected ()) {
236+ mCommunicationHandler .write (data , timeout );
237+ } else if (ScienceLabCommon .isWifiConnected ()) {
238+ httpAsyncTask = new HttpAsyncTask (ScienceLabCommon .getEspIP (), new HttpCallback <JSONObject >() {
239+ @ Override
240+ public void success (JSONObject jsonObject ) {
241+ Log .v (TAG , "write response:" + jsonObject .toString ());
242+ }
243+
244+ @ Override
245+ public void error (Exception e ) {
246+ Log .e (TAG , "Error writing data over ESP" );
247+ }
248+ });
249+
250+ httpAsyncTask .execute (data );
251+ }
252+
253+ }
200254}
0 commit comments