basic implementation of read and write on the communicator

This commit is contained in:
paul-loedige 2020-12-29 11:46:48 +01:00
parent 658d83c695
commit da811f4a84
5 changed files with 43 additions and 93 deletions

View File

@ -2,19 +2,17 @@ package com.example.aped;
import android.Manifest; import android.Manifest;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.res.AssetManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.Menu; import android.view.Menu;
import android.widget.Toast; import android.widget.Toast;
import com.android.volley.Response;
import com.example.aped.communication.Communicator; import com.example.aped.communication.Communicator;
import com.example.aped.communication.IIO; import com.example.aped.communication.IIO;
import com.example.aped.utils.ExternalStorageHandler; import com.example.aped.utils.ExternalStorageHandler;
import com.example.aped.communication.TestIO;
import com.example.aped.utils.IFAVORITES; import com.example.aped.utils.IFAVORITES;
import com.example.aped.utils.IXML; import com.example.aped.utils.IXML;
import com.example.aped.utils.TestFavorites; import com.example.aped.utils.TestFavorites;
@ -33,6 +31,8 @@ import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import java.io.File; import java.io.File;
@ -52,8 +52,6 @@ public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration; private AppBarConfiguration mAppBarConfiguration;
/** zur Verwendung von xml anstatt der direkten Einbindung.*/ /** zur Verwendung von xml anstatt der direkten Einbindung.*/
public static IXML xml = new TestXML(); public static IXML xml = new TestXML();
/** zur Verwendung von uebergabe anstatt der direkten Einbindung.*/
public static IIO uebergabe = new TestIO();
/** zur Verwendung von favorite anstatt der direkten Einbindung.*/ /** zur Verwendung von favorite anstatt der direkten Einbindung.*/
public static IFAVORITES favorite = new TestFavorites(); public static IFAVORITES favorite = new TestFavorites();
/** allgemeines.*/ /** allgemeines.*/
@ -66,9 +64,18 @@ public class MainActivity extends AppCompatActivity {
setXML(); setXML();
/** THIS IS A TEST **/ /** THIS IS A TEST **/
IIO io = new Communicator("192.168.2.246",8080,this); Communicator communicator = new Communicator("192.168.2.246",8080,this);
String returnString = io.read("sensorarray"); try {
System.out.println(returnString); JSONObject json = new JSONObject("{\"output\":\"1\"}");
communicator.write("sensorarray", json, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
}
});
} catch (JSONException e) {
e.printStackTrace();
}
/** THIS IS A TEST **/ /** THIS IS A TEST **/
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
@ -118,16 +125,12 @@ public class MainActivity extends AppCompatActivity {
break; break;
case R.id.action_download_xml: case R.id.action_download_xml:
if (uebergabe.downloadXML() == 0) {
Toast.makeText(this, "Downloaded .xml", Toast.makeText(this, "Downloaded .xml",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
}
break; break;
case R.id.action_upload_xml: case R.id.action_upload_xml:
if (uebergabe.uploadXML(new File("")) == 0) {
Toast.makeText(this, "Upload .xml", Toast.makeText(this, "Upload .xml",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
}
break; break;
default: default:
Log.e("MainActivity", "unknown item: " Log.e("MainActivity", "unknown item: "

View File

@ -1,6 +1,7 @@
package com.example.aped.communication; package com.example.aped.communication;
import android.content.Context; import android.content.Context;
import android.util.Log;
import com.android.volley.Request; import com.android.volley.Request;
import com.android.volley.RequestQueue; import com.android.volley.RequestQueue;
@ -22,7 +23,7 @@ import java.net.URL;
import kotlin.NotImplementedError; import kotlin.NotImplementedError;
public class Communicator implements IIO{ public class Communicator{
private String address; private String address;
private int port; private int port;
private RequestQueue requestQueue; private RequestQueue requestQueue;
@ -33,42 +34,35 @@ public class Communicator implements IIO{
requestQueue = Volley.newRequestQueue(context); requestQueue = Volley.newRequestQueue(context);
} }
@Override public void read(String deviceName, Response.Listener<JSONObject> responseListener) {
public String read(String deviceName) { String requestString = "http://" + address + ":" + port + "/device/" + deviceName + "/";
String requestString = "http://" + address + ":" + port + "/device/" + deviceName;
JsonObjectRequest request = new JsonObjectRequest( JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET, Request.Method.GET,
requestString, requestString,
null, null,
new Response.Listener<JSONObject>() { responseListener,
@Override error -> Log.e("Communicator","Error during READ: " + error.getMessage())
public void onResponse(JSONObject response) {
System.out.println(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage());
}
}
); );
requestQueue.add(request); requestQueue.add(request);
return "";
} }
@Override public void write(String deviceName, JSONObject message, Response.Listener<JSONObject> responseListener) {
public String write(String deviceName, String value) { String requestString = "http://" + address + ":" + port + "/device/" + deviceName + "/";
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
requestString,
message,
responseListener,
error -> {Log.e("Communicator", "Error during WRITE: " + error.getMessage()); error.printStackTrace();}
);
requestQueue.add(request);
}
public void uploadXML(File xmlFile) {
throw new NotImplementedError(); throw new NotImplementedError();
} }
@Override public void downloadXML() {
public int uploadXML(File xmlFile) {
throw new NotImplementedError();
}
@Override
public int downloadXML() {
throw new NotImplementedError(); throw new NotImplementedError();
} }
} }

View File

@ -1,5 +1,9 @@
package com.example.aped.communication; package com.example.aped.communication;
import com.android.volley.Response;
import org.json.JSONObject;
import java.io.File; import java.io.File;
public interface IIO public interface IIO
@ -9,7 +13,7 @@ public interface IIO
* @param deviceName the name of the device to read from * @param deviceName the name of the device to read from
* @return the value read * @return the value read
*/ */
String read(String deviceName); void read(String deviceName, Response.Listener<JSONObject> responseListener);
/** /**
* writes a value to a device connected to the raspberry * writes a value to a device connected to the raspberry
@ -17,18 +21,18 @@ public interface IIO
* @param value the value to write to the device * @param value the value to write to the device
* @return the value written * @return the value written
*/ */
String write(String deviceName, String value); void write(String deviceName, String value, Response.Listener<JSONObject> responseListener);
/** /**
* uploads the config.xml to the raspberry * uploads the config.xml to the raspberry
* @param xmlFile the config.xml * @param xmlFile the config.xml
* @return 0 if everything worked * @return 0 if everything worked
*/ */
int uploadXML(File xmlFile); void uploadXML(File xmlFile);
/** /**
* downloads the current config.xml from the raspberry * downloads the current config.xml from the raspberry
* @return 0 if everything worked * @return 0 if everything worked
*/ */
int downloadXML(); void downloadXML();
} }

View File

@ -1,43 +0,0 @@
package com.example.aped.communication;
import android.widget.Toast;
import com.example.aped.R;
import java.io.File;
public class TestIO implements IIO {
@Override
public String read(String deviceName) {
String Wert;
switch (deviceName) {
case "stoff":
Wert = "TRUE";
break;
case "schnaps":
Wert = "FALSE";
break;
default:
Wert="default";
}
return Wert;
}
@Override
public String write(String deviceName, String value) {
return "TRUE";//Wenn erfolgreich gesendet
}
@Override
public int uploadXML(File xmlFile) {
return 0;
}
@Override
public int downloadXML() {
return 0;
}
}

View File

@ -69,14 +69,7 @@ public class AllIOsFragment extends Fragment {
@Override @Override
public View getView(int i, View view, ViewGroup viewGroup) { public View getView(int i, View view, ViewGroup viewGroup) {
if(MainActivity.uebergabe.read(MainActivity.xml.getDeviceNames().get(i))=="TRUE") {
image=R.drawable.green_signal;
}else if (MainActivity.uebergabe.read(MainActivity.xml.getDeviceNames().get(i))=="FALSE") {
image = R.drawable.red_signal;
}
else{
image = R.drawable.off_signal; image = R.drawable.off_signal;
}
if (MainActivity.favorite.getFavorites().contains(MainActivity.xml.getDeviceNames().get(i))) { if (MainActivity.favorite.getFavorites().contains(MainActivity.xml.getDeviceNames().get(i))) {
favorite_image = android.R.drawable.btn_star_big_on; favorite_image = android.R.drawable.btn_star_big_on;
}else { }else {
@ -104,7 +97,6 @@ public class AllIOsFragment extends Fragment {
}); });
imageView.setImageResource(image); imageView.setImageResource(image);
TextView_Name.setText(MainActivity.xml.getDeviceNames().get(i)); TextView_Name.setText(MainActivity.xml.getDeviceNames().get(i));
TextView_State.setText(MainActivity.uebergabe.read(MainActivity.xml.getDeviceNames().get(i)));
ButtonView_Favoriten.setImageResource(favorite_image); ButtonView_Favoriten.setImageResource(favorite_image);
return view; return view;
} }