Steuerung der PWM und Restruckturierung des codes und kleinere Visualisierungsänderungen
This commit is contained in:
parent
361d634c22
commit
feda184aa5
@ -95,99 +95,53 @@ public class MainListViewAdapter extends BaseAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public View getView(final int i, View view,
|
public View getView(final int i, View view,
|
||||||
final ViewGroup viewGroup) {
|
final ViewGroup viewGroup) {
|
||||||
view = mainActivity.getLayoutInflater().inflate(R.layout.
|
view = mainActivity.getLayoutInflater().inflate(R.layout.listview_layout, null);
|
||||||
listview_layout, null);
|
TextView textViewName = (TextView) view.findViewById(R.id.textName);
|
||||||
TextView textViewName = (TextView) view.findViewById(
|
LinearLayout linearLayoutPin = (LinearLayout) view.findViewById(R.id.idLinearLayoutPins);
|
||||||
R.id.textName);
|
ImageView buttonViewFavoriten = (ImageView) view.findViewById(R.id.imageButtonFavoriten);
|
||||||
LinearLayout linearLayoutPin = (LinearLayout) view.findViewById(
|
|
||||||
R.id.idLinearLayoutPins);
|
|
||||||
ImageView buttonViewFavoriten = (ImageView) view.findViewById(
|
|
||||||
R.id.imageButtonFavoriten);
|
|
||||||
if (favoriteMode == 0) {
|
if (favoriteMode == 0) {
|
||||||
deviceName = mainActivity.xml.getDeviceNames().get(i);
|
deviceName = mainActivity.xml.getDeviceNames().get(i);
|
||||||
} else {
|
} else {
|
||||||
deviceName = mainActivity.favorite.getFavorites().get(i);
|
deviceName = mainActivity.favorite.getFavorites().get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mainActivity.favorite.getFavorites().contains(
|
if (mainActivity.favorite.getFavorites().contains(deviceName)) {
|
||||||
deviceName)) {
|
|
||||||
favoriteImage = android.R.drawable.btn_star_big_on;
|
favoriteImage = android.R.drawable.btn_star_big_on;
|
||||||
} else {
|
} else {
|
||||||
favoriteImage = android.R.drawable.btn_star_big_off;
|
favoriteImage = android.R.drawable.btn_star_big_off;
|
||||||
}
|
}
|
||||||
|
buttonViewFavoriten.setOnClickListener(new View.OnClickListener() {
|
||||||
buttonViewFavoriten.setOnClickListener(
|
|
||||||
new View.OnClickListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(final View view) {
|
public void onClick(final View view) {
|
||||||
|
|
||||||
if (favoriteMode == 0) {
|
if (favoriteMode == 0) {
|
||||||
deviceName = mainActivity.xml.getDeviceNames().
|
deviceName = mainActivity.xml.getDeviceNames().get(i);
|
||||||
get(i);
|
|
||||||
} else {
|
} else {
|
||||||
List<String> test = favoritenList;
|
List<String> test = favoritenList;
|
||||||
deviceName = favoritenList.get(i);
|
deviceName = favoritenList.get(i);
|
||||||
}
|
}
|
||||||
if (mainActivity.favorite.getFavorites().contains(
|
if (mainActivity.favorite.getFavorites().contains(deviceName)) {
|
||||||
deviceName)) {
|
mainActivity.favorite.removeFavorite(deviceName);
|
||||||
mainActivity.favorite.removeFavorite(
|
favoriteImage = android.R.drawable.btn_star_big_off;buttonViewFavoriten.setImageResource(
|
||||||
deviceName);
|
|
||||||
favoriteImage = android.R.drawable.btn_star_big_off;
|
|
||||||
buttonViewFavoriten.setImageResource(
|
|
||||||
favoriteImage);
|
favoriteImage);
|
||||||
} else {
|
} else {
|
||||||
mainActivity.favorite.addFavorite(
|
mainActivity.favorite.addFavorite(deviceName);
|
||||||
deviceName);
|
|
||||||
favoriteImage = android.R.drawable.btn_star_big_on;
|
favoriteImage = android.R.drawable.btn_star_big_on;
|
||||||
buttonViewFavoriten.setImageResource(
|
buttonViewFavoriten.setImageResource(favoriteImage);
|
||||||
favoriteImage);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
PinView pinView = new PinView(mainActivity, deviceName);
|
||||||
mainActivity.uebergabe.read(deviceName,
|
for (int j = 0; j < ((List<String>) mainActivity.xml.getPort(deviceName).get("pins")).size(); j++) {
|
||||||
new Response.Listener<JSONObject>() {
|
|
||||||
@Override
|
|
||||||
public void onResponse(final JSONObject response) {
|
|
||||||
if (favoriteMode == 0) {
|
|
||||||
deviceName = mainActivity.xml.getDeviceNames().get(i);
|
|
||||||
} else {
|
|
||||||
deviceName = mainActivity.favorite.getFavorites().get(i);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
JSONObject state = response.getJSONObject("state");
|
|
||||||
List<String> pinValues = new ArrayList<>();
|
|
||||||
for (String pinName: ((List<String>) mainActivity.xml.
|
|
||||||
getPort(deviceName).
|
|
||||||
get("pins"))) {
|
|
||||||
pinValues.add(state.getString(pinName));
|
|
||||||
}
|
|
||||||
PinView pinView = new PinView(mainActivity, deviceName,
|
|
||||||
pinValues);
|
|
||||||
for (int j = 0; j < ((List<String>) mainActivity.xml.
|
|
||||||
getPort(deviceName).
|
|
||||||
get("pins")).size(); j++) {
|
|
||||||
linearLayoutPin.addView(pinView.getView(j));
|
linearLayoutPin.addView(pinView.getView(j));
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
|
||||||
// Richtigen log erstellen
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
String name = null;
|
String name = null;
|
||||||
switch ((mainActivity.xml.getPort(deviceName)).get("protocol").
|
switch ((mainActivity.xml.getPort(deviceName)).get("protocol").toString()) {
|
||||||
toString()) {
|
|
||||||
case "PWM":
|
case "PWM":
|
||||||
name = ((mainActivity.xml.getPort(deviceName)).get("protocol").
|
name = ((mainActivity.xml.getPort(deviceName)).get("protocol").toString() + ": " + deviceName + " (" + (mainActivity.xml.getPort(deviceName)).get("frequency").toString() + "Hz)");
|
||||||
toString() + ": " + deviceName + " (" + (mainActivity.
|
|
||||||
xml.getPort(deviceName)).get("frequency").toString()
|
|
||||||
+ "Hz)");
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
name = ((mainActivity.xml.getPort(deviceName)).get("protocol").
|
name = ((mainActivity.xml.getPort(deviceName)).get("protocol").toString() + ": " + deviceName);
|
||||||
toString() + ": " + deviceName);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
textViewName.setText(name);
|
textViewName.setText(name);
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
package com.example.aped.ui.visualization;
|
package com.example.aped.ui.visualization;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.DialogInterface;
|
||||||
|
import android.text.Html;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.EditText;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import com.android.volley.Response;
|
import com.android.volley.Response;
|
||||||
import com.example.aped.MainActivity;
|
import com.example.aped.MainActivity;
|
||||||
import com.example.aped.R;
|
import com.example.aped.R;
|
||||||
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||||
|
import com.google.android.material.slider.RangeSlider;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -15,85 +23,142 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class PinView {
|
public class PinView {
|
||||||
|
/**
|
||||||
|
* Einbinden der MainActivity.
|
||||||
|
*/
|
||||||
private MainActivity mainActivity;
|
private MainActivity mainActivity;
|
||||||
|
/**
|
||||||
|
* Statusbild für den atuellen durchlauf.
|
||||||
|
*/
|
||||||
private int image;
|
private int image;
|
||||||
private List<String> pinValues;
|
/**
|
||||||
|
* Werte der Pins.
|
||||||
|
*/
|
||||||
|
private List<String> pinValues = new ArrayList<>();
|
||||||
|
/**
|
||||||
|
* Gerätenamen.
|
||||||
|
*/
|
||||||
private String deviceName;
|
private String deviceName;
|
||||||
|
/**
|
||||||
|
* DutyCycle.
|
||||||
|
*/
|
||||||
|
private String dutyValue;
|
||||||
|
|
||||||
|
|
||||||
public PinView(MainActivity pMainActivity,String pDevice, List<String> pPinValues){
|
/**
|
||||||
|
* @param pMainActivity
|
||||||
|
* @param pDeviceName
|
||||||
|
*/
|
||||||
|
public PinView(final MainActivity pMainActivity, final String pDeviceName) {
|
||||||
this.mainActivity = pMainActivity;
|
this.mainActivity = pMainActivity;
|
||||||
this.deviceName = pDevice;
|
this.deviceName = pDeviceName;
|
||||||
this.pinValues = pPinValues;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param i
|
||||||
|
* @return view
|
||||||
|
*/
|
||||||
public View getView(final int i) {
|
public View getView(final int i) {
|
||||||
View view = mainActivity.getLayoutInflater().inflate(R.layout.pin_listview_layout,
|
dutyValue = "";
|
||||||
null);
|
View view = mainActivity.getLayoutInflater().inflate(R.layout.pin_listview_layout, null);
|
||||||
ImageView imageView = (ImageView) view.findViewById(
|
TextView textViewPin = (TextView) view.findViewById(R.id.textPin);
|
||||||
R.id.images);
|
textViewPin.setText(((List<String>) (mainActivity.xml.getPort(deviceName)).get("pins")).get(i));
|
||||||
TextView textViewPin = (TextView) view.findViewById(
|
mainActivity.uebergabe.read(deviceName, new Response.Listener<JSONObject>() {
|
||||||
R.id.textPin);
|
@Override
|
||||||
|
public void onResponse(final JSONObject response) {
|
||||||
|
displayLamp(response, view, i);
|
||||||
|
if((mainActivity.xml.getPort(deviceName)).get("protocol").toString().equals("PWM")){
|
||||||
|
textViewPin.setText(Html.fromHtml(((List<String>) (mainActivity.xml.getPort(deviceName)).get("pins")).get(i)+" <b> (DC: " + pinValues.get(i) + "%) </b>"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
view.setOnClickListener(new View.OnClickListener() {
|
view.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(final View view) {
|
public void onClick(final View view) {
|
||||||
if((mainActivity.xml.getPort(deviceName)).get("protocol").toString().equals("DO")) {
|
switch ((mainActivity.xml.getPort(deviceName)).get("protocol").toString()) {
|
||||||
try {// Werte erst aktualisieren
|
case "DO":
|
||||||
|
try {
|
||||||
String setState = null;
|
String setState = null;
|
||||||
if (pinValues.get(i).equals("0")) {
|
if (pinValues.get(i).equals("0")) {
|
||||||
setState = "{\"output\": \"1\"}";
|
setState = "\"1\"";
|
||||||
} else {
|
} else {
|
||||||
setState = "{\"output\": \"0\"}";
|
setState = "\"0\"";
|
||||||
}
|
}
|
||||||
JSONObject jsonObject = new JSONObject(setState);
|
JSONObject jsonObject = new JSONObject("{\"output\":{\"" + ((List<String>) (mainActivity.xml.getPort(deviceName)).get("pins")).get(i) + "\":" + setState + "}}");
|
||||||
mainActivity.uebergabe.write(deviceName, jsonObject, new Response.Listener<JSONObject>() {
|
mainActivity.uebergabe.write(deviceName, jsonObject, new Response.Listener<JSONObject>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(JSONObject response) {
|
public void onResponse(final JSONObject response) {
|
||||||
try {
|
displayLamp(response, view, i);
|
||||||
if (!jsonObject.equals(response)) {
|
|
||||||
// Fehler
|
|
||||||
}
|
|
||||||
switch (response.getInt("output")){
|
|
||||||
case 1:
|
|
||||||
image = R.drawable.green_signal;
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
image = R.drawable.red_signal;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
image = R.drawable.off_signal;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
imageView.setImageResource(image);
|
|
||||||
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}else{
|
case "PWM":
|
||||||
Toast.makeText(mainActivity, deviceName
|
MaterialAlertDialogBuilder alert = new MaterialAlertDialogBuilder(mainActivity);
|
||||||
+ " is not an output " ,
|
View viewAlert = mainActivity.getLayoutInflater().inflate(R.layout.alert_dialog_pwm, null);
|
||||||
Toast.LENGTH_SHORT).show();
|
RangeSlider readDuty = (RangeSlider) viewAlert.findViewById(R.id.range_slider_duty);
|
||||||
}
|
readDuty.setValues((float) Float.valueOf(pinValues.get(i)));
|
||||||
mainActivity.uebergabe.read(deviceName,
|
alert.setView(viewAlert);
|
||||||
new Response.Listener<JSONObject>() {
|
alert.setPositiveButton("set",
|
||||||
|
new DialogInterface.OnClickListener() {
|
||||||
|
public void onClick(
|
||||||
|
final DialogInterface dialog, final int which) {
|
||||||
|
int value = readDuty.getValues().get(0).intValue();
|
||||||
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject("{\"output\":{\"" + ((List<String>) (mainActivity.xml.getPort(deviceName)).get("pins")).get(i) + "\":\"" + value + "\"}}");
|
||||||
|
mainActivity.uebergabe.write(deviceName, jsonObject, new Response.Listener<JSONObject>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(final JSONObject response) {
|
public void onResponse(JSONObject response) {
|
||||||
|
displayLamp(response, view, i);
|
||||||
|
textViewPin.setText(Html.fromHtml(((List<String>) (mainActivity.xml.getPort(deviceName)).get("pins")).get(i)+" <b> (DC: " + pinValues.get(i) + "%) </b>"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();// aaaaa
|
||||||
|
}
|
||||||
|
// Write your code here to execute after dialog
|
||||||
|
dialog.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
alert.setNegativeButton("exit", new DialogInterface.OnClickListener() {
|
||||||
|
public void onClick(
|
||||||
|
final DialogInterface dialog, final int which) {
|
||||||
|
dialog.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
alert.show();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Toast.makeText(mainActivity, deviceName + " is not an output ", Toast.LENGTH_SHORT).show();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return view;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void displayLamp(final JSONObject response, final View view, final int i) {
|
||||||
|
ImageView imageView = (ImageView) view.findViewById(R.id.images);
|
||||||
try {
|
try {
|
||||||
JSONObject state = response.getJSONObject("state");
|
JSONObject state = response.getJSONObject("state");
|
||||||
//List<String> pinValues = new ArrayList<>();
|
if (pinValues.size() != 0) {
|
||||||
pinValues.clear();
|
pinValues.clear();
|
||||||
for(String pinName: ((List<String>)mainActivity.xml.
|
}
|
||||||
getPort(deviceName).
|
for (String pinName: ((List<String>) mainActivity.xml.getPort(deviceName).get("pins"))){
|
||||||
get("pins"))){
|
|
||||||
pinValues.add(state.getString(pinName));
|
pinValues.add(state.getString(pinName));
|
||||||
}
|
}
|
||||||
switch (pinValues.get(i)) {
|
switch (pinValues.get(i)) {
|
||||||
@ -103,6 +168,9 @@ public class PinView {
|
|||||||
case "0":
|
case "0":
|
||||||
image = R.drawable.red_signal;
|
image = R.drawable.red_signal;
|
||||||
break;
|
break;
|
||||||
|
case "100":
|
||||||
|
image = R.drawable.green_signal;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
image = R.drawable.off_signal;
|
image = R.drawable.off_signal;
|
||||||
break;
|
break;
|
||||||
@ -115,25 +183,7 @@ public class PinView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
switch (pinValues.get(i)){
|
|
||||||
case "1":
|
|
||||||
image=R.drawable.green_signal;
|
|
||||||
break;
|
|
||||||
case "0":
|
|
||||||
image=R.drawable.red_signal;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
image=R.drawable.off_signal;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
imageView.setImageResource(image);
|
|
||||||
|
|
||||||
|
|
||||||
textViewPin.setText(((List<String>) (mainActivity.xml.getPort(deviceName)).get("pins")).get(i));
|
|
||||||
return view;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user