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
|
||||
public View getView(final int i, View view,
|
||||
final ViewGroup viewGroup) {
|
||||
view = mainActivity.getLayoutInflater().inflate(R.layout.
|
||||
listview_layout, null);
|
||||
TextView textViewName = (TextView) view.findViewById(
|
||||
R.id.textName);
|
||||
LinearLayout linearLayoutPin = (LinearLayout) view.findViewById(
|
||||
R.id.idLinearLayoutPins);
|
||||
ImageView buttonViewFavoriten = (ImageView) view.findViewById(
|
||||
R.id.imageButtonFavoriten);
|
||||
view = mainActivity.getLayoutInflater().inflate(R.layout.listview_layout, null);
|
||||
TextView textViewName = (TextView) view.findViewById(R.id.textName);
|
||||
LinearLayout linearLayoutPin = (LinearLayout) view.findViewById(R.id.idLinearLayoutPins);
|
||||
ImageView buttonViewFavoriten = (ImageView) view.findViewById(R.id.imageButtonFavoriten);
|
||||
if (favoriteMode == 0) {
|
||||
deviceName = mainActivity.xml.getDeviceNames().get(i);
|
||||
} else {
|
||||
deviceName = mainActivity.favorite.getFavorites().get(i);
|
||||
}
|
||||
|
||||
if (mainActivity.favorite.getFavorites().contains(
|
||||
deviceName)) {
|
||||
if (mainActivity.favorite.getFavorites().contains(deviceName)) {
|
||||
favoriteImage = android.R.drawable.btn_star_big_on;
|
||||
} else {
|
||||
favoriteImage = android.R.drawable.btn_star_big_off;
|
||||
}
|
||||
|
||||
buttonViewFavoriten.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
buttonViewFavoriten.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(final View view) {
|
||||
|
||||
if (favoriteMode == 0) {
|
||||
deviceName = mainActivity.xml.getDeviceNames().
|
||||
get(i);
|
||||
deviceName = mainActivity.xml.getDeviceNames().get(i);
|
||||
} else {
|
||||
List<String> test = favoritenList;
|
||||
deviceName = favoritenList.get(i);
|
||||
}
|
||||
if (mainActivity.favorite.getFavorites().contains(
|
||||
deviceName)) {
|
||||
mainActivity.favorite.removeFavorite(
|
||||
deviceName);
|
||||
favoriteImage = android.R.drawable.btn_star_big_off;
|
||||
buttonViewFavoriten.setImageResource(
|
||||
if (mainActivity.favorite.getFavorites().contains(deviceName)) {
|
||||
mainActivity.favorite.removeFavorite(deviceName);
|
||||
favoriteImage = android.R.drawable.btn_star_big_off;buttonViewFavoriten.setImageResource(
|
||||
favoriteImage);
|
||||
} else {
|
||||
mainActivity.favorite.addFavorite(
|
||||
deviceName);
|
||||
mainActivity.favorite.addFavorite(deviceName);
|
||||
favoriteImage = android.R.drawable.btn_star_big_on;
|
||||
buttonViewFavoriten.setImageResource(
|
||||
favoriteImage);
|
||||
buttonViewFavoriten.setImageResource(favoriteImage);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mainActivity.uebergabe.read(deviceName,
|
||||
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));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
// Richtigen log erstellen
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
PinView pinView = new PinView(mainActivity, deviceName);
|
||||
for (int j = 0; j < ((List<String>) mainActivity.xml.getPort(deviceName).get("pins")).size(); j++) {
|
||||
linearLayoutPin.addView(pinView.getView(j));
|
||||
}
|
||||
String name = null;
|
||||
switch ((mainActivity.xml.getPort(deviceName)).get("protocol").
|
||||
toString()) {
|
||||
switch ((mainActivity.xml.getPort(deviceName)).get("protocol").toString()) {
|
||||
case "PWM":
|
||||
name = ((mainActivity.xml.getPort(deviceName)).get("protocol").
|
||||
toString() + ": " + deviceName + " (" + (mainActivity.
|
||||
xml.getPort(deviceName)).get("frequency").toString()
|
||||
+ "Hz)");
|
||||
name = ((mainActivity.xml.getPort(deviceName)).get("protocol").toString() + ": " + deviceName + " (" + (mainActivity.xml.getPort(deviceName)).get("frequency").toString() + "Hz)");
|
||||
break;
|
||||
default:
|
||||
name = ((mainActivity.xml.getPort(deviceName)).get("protocol").
|
||||
toString() + ": " + deviceName);
|
||||
name = ((mainActivity.xml.getPort(deviceName)).get("protocol").toString() + ": " + deviceName);
|
||||
break;
|
||||
}
|
||||
textViewName.setText(name);
|
||||
|
@ -1,13 +1,21 @@
|
||||
package com.example.aped.ui.visualization;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.DialogInterface;
|
||||
import android.text.Html;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.android.volley.Response;
|
||||
import com.example.aped.MainActivity;
|
||||
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.JSONObject;
|
||||
|
||||
@ -15,125 +23,167 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PinView {
|
||||
/**
|
||||
* Einbinden der MainActivity.
|
||||
*/
|
||||
private MainActivity mainActivity;
|
||||
|
||||
/**
|
||||
* Statusbild für den atuellen durchlauf.
|
||||
*/
|
||||
private int image;
|
||||
private List<String> pinValues;
|
||||
/**
|
||||
* Werte der Pins.
|
||||
*/
|
||||
private List<String> pinValues = new ArrayList<>();
|
||||
/**
|
||||
* Gerätenamen.
|
||||
*/
|
||||
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.deviceName = pDevice;
|
||||
this.pinValues = pPinValues;
|
||||
this.deviceName = pDeviceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param i
|
||||
* @return view
|
||||
*/
|
||||
public View getView(final int i) {
|
||||
View view = mainActivity.getLayoutInflater().inflate(R.layout.pin_listview_layout,
|
||||
null);
|
||||
ImageView imageView = (ImageView) view.findViewById(
|
||||
R.id.images);
|
||||
TextView textViewPin = (TextView) view.findViewById(
|
||||
R.id.textPin);
|
||||
dutyValue = "";
|
||||
View view = mainActivity.getLayoutInflater().inflate(R.layout.pin_listview_layout, null);
|
||||
TextView textViewPin = (TextView) view.findViewById(R.id.textPin);
|
||||
textViewPin.setText(((List<String>) (mainActivity.xml.getPort(deviceName)).get("pins")).get(i));
|
||||
mainActivity.uebergabe.read(deviceName, new Response.Listener<JSONObject>() {
|
||||
@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() {
|
||||
@Override
|
||||
public void onClick(final View view) {
|
||||
if((mainActivity.xml.getPort(deviceName)).get("protocol").toString().equals("DO")) {
|
||||
try {// Werte erst aktualisieren
|
||||
switch ((mainActivity.xml.getPort(deviceName)).get("protocol").toString()) {
|
||||
case "DO":
|
||||
try {
|
||||
String setState = null;
|
||||
if(pinValues.get(i).equals("0")) {
|
||||
setState = "{\"output\": \"1\"}";
|
||||
}else{
|
||||
setState = "{\"output\": \"0\"}";
|
||||
if (pinValues.get(i).equals("0")) {
|
||||
setState = "\"1\"";
|
||||
} else {
|
||||
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>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
try {
|
||||
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();
|
||||
}
|
||||
public void onResponse(final JSONObject response) {
|
||||
displayLamp(response, view, i);
|
||||
}
|
||||
});
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}else{
|
||||
Toast.makeText(mainActivity, deviceName
|
||||
+ " is not an output " ,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
mainActivity.uebergabe.read(deviceName,
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(final JSONObject response) {
|
||||
try {
|
||||
JSONObject state = response.getJSONObject("state");
|
||||
//List<String> pinValues = new ArrayList<>();
|
||||
pinValues.clear();
|
||||
for(String pinName: ((List<String>)mainActivity.xml.
|
||||
getPort(deviceName).
|
||||
get("pins"))){
|
||||
pinValues.add(state.getString(pinName));
|
||||
break;
|
||||
case "PWM":
|
||||
MaterialAlertDialogBuilder alert = new MaterialAlertDialogBuilder(mainActivity);
|
||||
View viewAlert = mainActivity.getLayoutInflater().inflate(R.layout.alert_dialog_pwm, null);
|
||||
RangeSlider readDuty = (RangeSlider) viewAlert.findViewById(R.id.range_slider_duty);
|
||||
readDuty.setValues((float) Float.valueOf(pinValues.get(i)));
|
||||
alert.setView(viewAlert);
|
||||
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
|
||||
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
|
||||
}
|
||||
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);
|
||||
|
||||
} catch (JSONException e) {
|
||||
// Richtigen log erstellen
|
||||
e.printStackTrace();
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void displayLamp(final JSONObject response, final View view, final int i) {
|
||||
ImageView imageView = (ImageView) view.findViewById(R.id.images);
|
||||
try {
|
||||
JSONObject state = response.getJSONObject("state");
|
||||
if (pinValues.size() != 0) {
|
||||
pinValues.clear();
|
||||
}
|
||||
for (String pinName: ((List<String>) mainActivity.xml.getPort(deviceName).get("pins"))){
|
||||
pinValues.add(state.getString(pinName));
|
||||
}
|
||||
switch (pinValues.get(i)) {
|
||||
case "1":
|
||||
image = R.drawable.green_signal;
|
||||
break;
|
||||
case "0":
|
||||
image = R.drawable.red_signal;
|
||||
break;
|
||||
case "100":
|
||||
image = R.drawable.green_signal;
|
||||
break;
|
||||
default:
|
||||
image = R.drawable.off_signal;
|
||||
break;
|
||||
}
|
||||
imageView.setImageResource(image);
|
||||
|
||||
} catch (JSONException e) {
|
||||
// Richtigen log erstellen
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user