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

View File

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

View File

@ -1,5 +1,9 @@
package com.example.aped.communication;
import com.android.volley.Response;
import org.json.JSONObject;
import java.io.File;
public interface IIO
@ -9,7 +13,7 @@ public interface IIO
* @param deviceName the name of the device to read from
* @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
@ -17,18 +21,18 @@ public interface IIO
* @param value the value to write to the device
* @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
* @param xmlFile the config.xml
* @return 0 if everything worked
*/
int uploadXML(File xmlFile);
void uploadXML(File xmlFile);
/**
* downloads the current config.xml from the raspberry
* @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
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;
}
if (MainActivity.favorite.getFavorites().contains(MainActivity.xml.getDeviceNames().get(i))) {
favorite_image = android.R.drawable.btn_star_big_on;
}else {
@ -104,7 +97,6 @@ public class AllIOsFragment extends Fragment {
});
imageView.setImageResource(image);
TextView_Name.setText(MainActivity.xml.getDeviceNames().get(i));
TextView_State.setText(MainActivity.uebergabe.read(MainActivity.xml.getDeviceNames().get(i)));
ButtonView_Favoriten.setImageResource(favorite_image);
return view;
}