From ea925011a2cc15fcfeb9b41585acea4e37630b4e Mon Sep 17 00:00:00 2001 From: Galadril Date: Tue, 27 Jun 2023 23:04:49 +0200 Subject: [PATCH] #695 Adding thermostat mode --- .../domoticz/adapters/UtilityAdapter.java | 83 ++++++++-- .../domoticz/fragments/Utilities.java | 33 +++- .../interfaces/UtilityClickListener.java | 1 + .../main/res/layout/utilities_row_default.xml | 19 +++ .../layout/utilities_row_default_noads.xml | 142 ------------------ app/src/paid/AndroidManifest.xml | 5 +- app/version.properties | 4 +- .../domoticzapi/Containers/UtilitiesInfo.java | 44 ++++++ .../nl/hnogames/domoticzapi/DomoticzUrls.java | 11 ++ .../hnogames/domoticzapi/DomoticzValues.java | 9 ++ domoticzapi/version.properties | 2 +- 11 files changed, 189 insertions(+), 164 deletions(-) delete mode 100644 app/src/main/res/layout/utilities_row_default_noads.xml diff --git a/app/src/main/java/nl/hnogames/domoticz/adapters/UtilityAdapter.java b/app/src/main/java/nl/hnogames/domoticz/adapters/UtilityAdapter.java index 30c3f6459..b1626d088 100644 --- a/app/src/main/java/nl/hnogames/domoticz/adapters/UtilityAdapter.java +++ b/app/src/main/java/nl/hnogames/domoticz/adapters/UtilityAdapter.java @@ -26,11 +26,14 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Filter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; +import android.widget.Spinner; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; @@ -59,6 +62,7 @@ import nl.hnogames.domoticz.helpers.ItemMoveAdapter; import nl.hnogames.domoticz.interfaces.UtilityClickListener; import nl.hnogames.domoticz.utils.SharedPrefUtil; +import nl.hnogames.domoticzapi.Containers.DevicesInfo; import nl.hnogames.domoticzapi.Containers.UtilitiesInfo; import nl.hnogames.domoticzapi.Domoticz; import nl.hnogames.domoticzapi.DomoticzIcons; @@ -108,14 +112,7 @@ public void setData(ArrayList data) { private ArrayList SortData(ArrayList dat) { ArrayList data = new ArrayList<>(); - if (Build.VERSION.SDK_INT >= 21) { - data = dat; - } else { - for (UtilitiesInfo d : dat) { - if (d.getIdx() != MainActivity.ADS_IDX) - data.add(d); - } - } + data = dat; ArrayList customdata = new ArrayList<>(); if (mSharedPrefs.enableCustomSorting() && mCustomSorting != null) { UtilitiesInfo adView = null; @@ -149,13 +146,8 @@ public Filter getFilter() { @Override public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; - if (Build.VERSION.SDK_INT >= 21) { - view = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.utilities_row_default, parent, false); - } else { - view = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.utilities_row_default_noads, parent, false); - } + view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.utilities_row_default, parent, false); return new DataObjectHolder(view); } @@ -173,6 +165,10 @@ public void onBindViewHolder(final DataObjectHolder holder, final int position) (mUtilitiesInfo.getSubType() != null && DomoticzValues.Device.Utility.SubType.SMARTWARES.equalsIgnoreCase(mUtilitiesInfo.getSubType()))) { setButtons(holder, Buttons.THERMOSTAT); CreateThermostatRow(holder, mUtilitiesInfo, setPoint); + } else if ((mUtilitiesInfo.getType() != null && DomoticzValues.Device.Utility.Type.GENERAL.equalsIgnoreCase(mUtilitiesInfo.getType())) && + (mUtilitiesInfo.getSubType() != null && DomoticzValues.Device.Utility.SubType.THERMOSTAT_MODE.equalsIgnoreCase(mUtilitiesInfo.getSubType()))) { + setButtons(holder, Buttons.THERMOSTAT_MODE); + CreateThermostatRow(holder, mUtilitiesInfo, setPoint); } else { if (DomoticzValues.Device.Utility.SubType.TEXT.equalsIgnoreCase(mUtilitiesInfo.getSubType()) || DomoticzValues.Device.Utility.SubType.ALERT.equalsIgnoreCase(mUtilitiesInfo.getSubType())) { CreateTextRow(holder, mUtilitiesInfo); @@ -401,6 +397,37 @@ private void CreateThermostatRow(DataObjectHolder holder, UtilitiesInfo mUtiliti holder.on_button.setId(mUtilitiesInfo.getIdx()); holder.on_button.setOnClickListener(v -> handleThermostatClick(v.getId())); + int loadMode = mUtilitiesInfo.getModeId(); + final ArrayList modes = mUtilitiesInfo.getModes(); + if (modes != null && modes.size() > 0) { + holder.spSelector.setId(mUtilitiesInfo.getIdx()); + holder.spSelector.setVisibility(View.VISIBLE); + ArrayAdapter dataAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, modes); + dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + holder.spSelector.setAdapter(dataAdapter); + holder.spSelector.setSelection(loadMode); + holder.spSelector.setTag(mUtilitiesInfo); + } + else{ + holder.spSelector.setVisibility(View.GONE); + } + + holder.spSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView arg0, View arg1, + int arg2, long arg3) { + if ((holder.spSelector.getId()) == mUtilitiesInfo.getIdx()) { + holder.spSelector.setId(mUtilitiesInfo.getIdx() * 3); + } else { + String selValue = holder.spSelector.getItemAtPosition(arg2).toString(); + handleSelectorChange(mUtilitiesInfo, arg2, selValue); + } + } + + @Override + public void onNothingSelected(AdapterView arg0) {} + }); + holder.dayButton.setId(mUtilitiesInfo.getIdx()); holder.dayButton.setOnClickListener(v -> { for (UtilitiesInfo t : filteredData) { @@ -442,12 +469,19 @@ private void CreateThermostatRow(DataObjectHolder holder, UtilitiesInfo mUtiliti listener.onLogClick(t, DomoticzValues.Graph.Range.YEAR); } }); + holder.name.setText(mUtilitiesInfo.getName()); holder.hardware.setText(mUtilitiesInfo.getLastUpdate()); holder.data.setText(context.getString(R.string.set_point) + ": " + setPoint); Picasso.get().load(DomoticzIcons.getDrawableIcon(mUtilitiesInfo.getTypeImg(), mUtilitiesInfo.getType(), mUtilitiesInfo.getSubType(), false, false, null)).into(holder.iconRow); } + private void handleSelectorChange(UtilitiesInfo device, int id, String mode) { + if (device != null) { + listener.OnModeChanged(device, id, mode); + } + } + public void handleThermostatClick(int idx) { listener.onThermostatClick(idx); } @@ -481,7 +515,9 @@ public void setButtons(DataObjectHolder holder, int button) { if (holder.on_button != null) { holder.on_button.setVisibility(View.GONE); } - + if (holder.spSelector != null) { + holder.spSelector.setVisibility(View.GONE); + } switch (button) { case Buttons.ADS: if (holder.adview != null) @@ -517,6 +553,18 @@ public void setButtons(DataObjectHolder holder, int button) { if (holder.adview != null) holder.adview.setVisibility(View.GONE); break; + case Buttons.THERMOSTAT_MODE: + if (holder.contentWrapper != null) + holder.contentWrapper.setVisibility(View.VISIBLE); + holder.on_button.setVisibility(View.VISIBLE); + holder.dayButton.setVisibility(View.VISIBLE); + holder.monthButton.setVisibility(View.VISIBLE); + holder.weekButton.setVisibility(View.VISIBLE); + holder.yearButton.setVisibility(View.VISIBLE); + holder.spSelector.setVisibility(View.VISIBLE); + if (holder.adview != null) + holder.adview.setVisibility(View.GONE); + break; } } @@ -571,6 +619,7 @@ interface Buttons { int TEXT = 1; int THERMOSTAT = 2; int ADS = 3; + int THERMOSTAT_MODE = 4; } public static class DataObjectHolder extends RecyclerView.ViewHolder implements RVHViewHolder { @@ -591,6 +640,7 @@ public static class DataObjectHolder extends RecyclerView.ViewHolder implements LinearLayout extraPanel; TemplateView adview; RelativeLayout contentWrapper; + Spinner spSelector; public DataObjectHolder(View itemView) { super(itemView); @@ -610,6 +660,7 @@ public DataObjectHolder(View itemView) { buttonLog = itemView.findViewById(R.id.log_button); data = itemView.findViewById(R.id.utilities_data); hardware = itemView.findViewById(R.id.utilities_hardware); + spSelector = itemView.findViewById(R.id.spSelector); extraPanel = itemView.findViewById(R.id.extra_panel); if (extraPanel != null) diff --git a/app/src/main/java/nl/hnogames/domoticz/fragments/Utilities.java b/app/src/main/java/nl/hnogames/domoticz/fragments/Utilities.java index ba0ee9b1f..95018e433 100644 --- a/app/src/main/java/nl/hnogames/domoticz/fragments/Utilities.java +++ b/app/src/main/java/nl/hnogames/domoticz/fragments/Utilities.java @@ -337,6 +337,38 @@ private UtilitiesInfo getUtility(int idx) { public void onClick(UtilitiesInfo utility) { } + @Override + public void OnModeChanged(UtilitiesInfo utility, int id, String mode) { + UserInfo user = getCurrentUser(mContext, StaticHelper.getDomoticz(mContext)); + if (user != null && user.getRights() <= 0) { + UsefulBits.showSnackbar(mContext, frameLayout, mContext.getString(R.string.security_no_rights), Snackbar.LENGTH_SHORT); + if (getActivity() instanceof MainActivity) + ((MainActivity) getActivity()).Talk(R.string.security_no_rights); + refreshFragment(); + return; + } + + addDebugText("OnModeChanged"); + addDebugText("Set idx " + utility.getIdx() + " to " + mode); + StaticHelper.getDomoticz(mContext).setAction(utility.getIdx(), + DomoticzValues.Json.Url.Set.THERMOSTAT, + DomoticzValues.Device.Thermostat.Action.TMODE, + id, + null, + new setCommandReceiver() { + @Override + public void onReceiveResult(String result) { + successHandling(result, false); + } + + @Override + + public void onError(Exception error) { + errorHandling(error); + } + }); + } + @Override public void onLogClick(final UtilitiesInfo utility, final String range) { @@ -373,7 +405,6 @@ public void onLogClick(final UtilitiesInfo utility, final String range) { } @Override - public void onThermostatClick(final int idx) { UserInfo user = getCurrentUser(mContext, StaticHelper.getDomoticz(mContext)); if (user != null && user.getRights() <= 0) { diff --git a/app/src/main/java/nl/hnogames/domoticz/interfaces/UtilityClickListener.java b/app/src/main/java/nl/hnogames/domoticz/interfaces/UtilityClickListener.java index 5506efd5b..bcaec3783 100644 --- a/app/src/main/java/nl/hnogames/domoticz/interfaces/UtilityClickListener.java +++ b/app/src/main/java/nl/hnogames/domoticz/interfaces/UtilityClickListener.java @@ -27,6 +27,7 @@ public interface UtilityClickListener { void onClick(UtilitiesInfo utility); + void OnModeChanged(UtilitiesInfo utility, int id, String mode); void onLogClick(UtilitiesInfo utility, String range); diff --git a/app/src/main/res/layout/utilities_row_default.xml b/app/src/main/res/layout/utilities_row_default.xml index 35009086f..b8871a04a 100644 --- a/app/src/main/res/layout/utilities_row_default.xml +++ b/app/src/main/res/layout/utilities_row_default.xml @@ -136,7 +136,26 @@ android:textSize="10dp" /> + + + + diff --git a/app/src/main/res/layout/utilities_row_default_noads.xml b/app/src/main/res/layout/utilities_row_default_noads.xml deleted file mode 100644 index 380193e16..000000000 --- a/app/src/main/res/layout/utilities_row_default_noads.xml +++ /dev/null @@ -1,142 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/paid/AndroidManifest.xml b/app/src/paid/AndroidManifest.xml index e66752b3c..2fd023254 100644 --- a/app/src/paid/AndroidManifest.xml +++ b/app/src/paid/AndroidManifest.xml @@ -34,8 +34,6 @@ - - @@ -50,6 +48,9 @@ + + + getModes() { + if (UsefulBits.isEmpty(Modes)) + return null; + String[] names = Pattern.compile(";", Pattern.LITERAL).split(Modes); + + ArrayList newNames = new ArrayList(); + for (String value : names) { + try { + Integer.parseInt(value); + } catch (NumberFormatException e) { + newNames.add(Html.fromHtml(value).toString()); + } + } + + return newNames; + } + public String getName() { return Name; } diff --git a/domoticzapi/src/main/java/nl/hnogames/domoticzapi/DomoticzUrls.java b/domoticzapi/src/main/java/nl/hnogames/domoticzapi/DomoticzUrls.java index 7f4b5f6fb..8e52648e9 100644 --- a/domoticzapi/src/main/java/nl/hnogames/domoticzapi/DomoticzUrls.java +++ b/domoticzapi/src/main/java/nl/hnogames/domoticzapi/DomoticzUrls.java @@ -120,6 +120,10 @@ public String constructSetUrl(int jsonSetUrl, int idx, int action, double value) actionUrl = DomoticzValues.Url.Switch.DIM_LEVEL + String.valueOf(value); break; + case DomoticzValues.Device.Thermostat.Action.TMODE: + actionUrl = DomoticzValues.Url.Thermostat.TMODE + String.valueOf(value); + break; + case DomoticzValues.Device.Dimmer.Action.COLOR: actionUrl = DomoticzValues.Url.Switch.COLOR; break; @@ -177,6 +181,13 @@ public String constructSetUrl(int jsonSetUrl, int idx, int action, double value) + DomoticzValues.Url.Switch.CMD + actionUrl; break; + case DomoticzValues.Json.Url.Set.THERMOSTAT: + url = DomoticzValues.Url.Thermostat.SETUSED; + jsonUrl = url + + String.valueOf(idx) + + actionUrl; + break; + case DomoticzValues.Json.Url.Set.SWITCHES: url = DomoticzValues.Url.Switch.GET; jsonUrl = url diff --git a/domoticzapi/src/main/java/nl/hnogames/domoticzapi/DomoticzValues.java b/domoticzapi/src/main/java/nl/hnogames/domoticzapi/DomoticzValues.java index 0bdc5f586..5c0676fbf 100644 --- a/domoticzapi/src/main/java/nl/hnogames/domoticzapi/DomoticzValues.java +++ b/domoticzapi/src/main/java/nl/hnogames/domoticzapi/DomoticzValues.java @@ -90,6 +90,7 @@ interface Utility { interface Type { String HEATING = "Heating"; String THERMOSTAT = "Thermostat"; + String GENERAL = "General"; } interface SubType { @@ -141,6 +142,7 @@ interface Thermostat { interface Action { int MIN = 50; int PLUS = 51; + int TMODE = 52; } } @@ -320,6 +322,7 @@ interface Set { int WWCOLOR = 112; int KELVIN = 113; int LOG = 114; + int THERMOSTAT = 115; } } @@ -423,6 +426,12 @@ interface Category { String SCENETIMER = "/json.htm?type=scenetimers&idx="; } + @SuppressWarnings("SpellCheckingInspection") + interface Thermostat { + String SETUSED = "/json.htm?type=setused&idx="; + String TMODE = "&tmode="; + } + @SuppressWarnings({"SpellCheckingInspection", "unused"}) interface Switch { String DIM_LEVEL = "Set%20Level&level="; diff --git a/domoticzapi/version.properties b/domoticzapi/version.properties index 586f585a9..7558c465c 100644 --- a/domoticzapi/version.properties +++ b/domoticzapi/version.properties @@ -1,4 +1,4 @@ -#Tue Jun 27 20:02:55 CEST 2023 +#Tue Jun 27 20:56:52 CEST 2023 VERSION_BUILD=3675 VERSION_CODE=254 VERSION_PATCH=254