Skip to content

Commit

Permalink
#695 Changes for Thermostat mode devices
Browse files Browse the repository at this point in the history
  • Loading branch information
galadril committed Jul 5, 2023
1 parent ce8d620 commit 643c971
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import nl.hnogames.domoticz.utils.UsefulBits;
import nl.hnogames.domoticzapi.Containers.ConfigInfo;
import nl.hnogames.domoticzapi.Containers.DevicesInfo;
import nl.hnogames.domoticzapi.Containers.UtilitiesInfo;
import nl.hnogames.domoticzapi.Domoticz;
import nl.hnogames.domoticzapi.DomoticzIcons;
import nl.hnogames.domoticzapi.DomoticzValues;
Expand Down Expand Up @@ -248,10 +249,15 @@ private void setSwitchRowData(DevicesInfo mDeviceInfo,
if (mDeviceInfo.getType() != null && mDeviceInfo.getType().equals("advertisement")) {
setButtons(holder, Buttons.ADS);
setAdsLayout(holder);
} else if (mDeviceInfo.getSubType() != null && mDeviceInfo.getSubType().equals(DomoticzValues.Device.Utility.SubType.SMARTWARES)) {
} else if ((mDeviceInfo.getType() != null && DomoticzValues.Device.Utility.Type.THERMOSTAT.equalsIgnoreCase(mDeviceInfo.getType())) ||
(mDeviceInfo.getSubType() != null && DomoticzValues.Device.Utility.SubType.SMARTWARES.equalsIgnoreCase(mDeviceInfo.getSubType()))) {
setButtons(holder, Buttons.BUTTON_ON);
setThermostatRowData(mDeviceInfo, holder);
} else {
}else if ((mDeviceInfo.getType() != null && DomoticzValues.Device.Utility.Type.GENERAL.equalsIgnoreCase(mDeviceInfo.getType())) &&
(mDeviceInfo.getSubType() != null && DomoticzValues.Device.Utility.SubType.THERMOSTAT_MODE.equalsIgnoreCase(mDeviceInfo.getSubType()))) {
setButtons(holder, Buttons.SELECTOR);
setThermostatRowData(mDeviceInfo, holder);
} else {
switch (mDeviceInfo.getType()) {
case DomoticzValues.Scene.Type.GROUP:
setButtons(holder, Buttons.BUTTONS);
Expand Down Expand Up @@ -780,6 +786,7 @@ private void setThermostatRowData(DevicesInfo mDeviceInfo, DataObjectHolder hold
final double setPoint = mDeviceInfo.getSetPoint();
if (holder.isProtected)
holder.buttonOn.setEnabled(false);

holder.buttonOn.setText(context.getString(R.string.set_temperature));
holder.buttonOn.setOnClickListener(v -> handleThermostatClick((DevicesInfo) v.getTag()));
holder.buttonOn.setTag(mDeviceInfo);
Expand All @@ -794,10 +801,45 @@ private void setThermostatRowData(DevicesInfo mDeviceInfo, DataObjectHolder hold
holder.signal_level.setText(text);
}

if (holder.switch_battery_level != null) {
String setPointText =
context.getString(R.string.set_point) + ": " + setPoint;
holder.switch_battery_level.setText(setPointText);
int loadMode = mDeviceInfo.getModeId();
final ArrayList<String> modes = mDeviceInfo.getModes();
if (modes != null && modes.size() > 0) {
holder.spSelector.setId(mDeviceInfo.getIdx());
holder.spSelector.setVisibility(View.VISIBLE);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(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(mDeviceInfo);

if (holder.switch_battery_level != null) {
String modeText =
context.getString(R.string.set_mode) + ": " + modes.get(loadMode);
holder.switch_battery_level.setText(modeText);
}

holder.spSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if ((holder.spSelector.getId()) == mDeviceInfo.getIdx()) {
holder.spSelector.setId(mDeviceInfo.getIdx() * 3);
} else {
String selValue = holder.spSelector.getItemAtPosition(arg2).toString();
handleModeSelectorChange(mDeviceInfo, arg2, selValue);
}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
else{
holder.spSelector.setVisibility(View.GONE);
if (holder.switch_battery_level != null) {
String setPointText =
context.getString(R.string.set_point) + ": " + setPoint;
holder.switch_battery_level.setText(setPointText);
}
}

Picasso.get().load(DomoticzIcons.getDrawableIcon(
Expand Down Expand Up @@ -858,6 +900,12 @@ public void onAdFailedToLoad(LoadAdError errorCode) {
}
}

private void handleModeSelectorChange(DevicesInfo device, int id, String mode) {
if (device != null) {
listener.OnModeChanged(device, id, mode);
}
}

/**
* Set the data for temperature devices
*
Expand Down Expand Up @@ -1825,7 +1873,6 @@ public void setButtons(DataObjectHolder holder, int button) {
holder.contentWrapper.setVisibility(View.VISIBLE);
if (holder.adview != null)
holder.adview.setVisibility(View.GONE);
break;
case Buttons.SELECTOR:
if (holder.contentWrapper != null)
holder.contentWrapper.setVisibility(View.VISIBLE);
Expand Down
49 changes: 20 additions & 29 deletions app/src/main/java/nl/hnogames/domoticz/adapters/UtilityAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ public void onAdFailedToLoad(LoadAdError errorCode) {
}

private void CreateTextRow(DataObjectHolder holder, UtilitiesInfo mUtilitiesInfo) {
holder.isProtected = mUtilitiesInfo.isProtected();

holder.name.setText(mUtilitiesInfo.getName());
holder.data.setText(context.getString(R.string.data) + ": " + mUtilitiesInfo.getData());
holder.hardware.setText(context.getString(R.string.hardware) + ": " + mUtilitiesInfo.getHardwareName());
Expand Down Expand Up @@ -300,8 +298,6 @@ private void handleLogButtonClick(int idx) {
}

private void CreateDefaultRow(DataObjectHolder holder, UtilitiesInfo mUtilitiesInfo) {
holder.isProtected = mUtilitiesInfo.isProtected();

holder.name.setText(mUtilitiesInfo.getName());
holder.data.setText(context.getString(R.string.data) + ": " + mUtilitiesInfo.getData());
holder.hardware.setText(context.getString(R.string.hardware) + ": " + mUtilitiesInfo.getHardwareName());
Expand Down Expand Up @@ -389,33 +385,13 @@ public void unLiked(LikeButton likeButton) {

private void CreateThermostatRow(DataObjectHolder holder, UtilitiesInfo mUtilitiesInfo, final double setPoint) {
int layoutResourceId;
holder.isProtected = mUtilitiesInfo.isProtected();
if (holder.isProtected)
holder.on_button.setEnabled(false);

holder.on_button.setText(context.getString(R.string.set_temperature));
holder.on_button.setId(mUtilitiesInfo.getIdx());
holder.on_button.setOnClickListener(v -> handleThermostatClick(v.getId()));

int loadMode = mUtilitiesInfo.getModeId();
final ArrayList<String> modes = mUtilitiesInfo.getModes();
if (modes != null && modes.size() > 0) {
holder.spSelector.setId(mUtilitiesInfo.getIdx());
holder.spSelector.setVisibility(View.VISIBLE);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(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) {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if ((holder.spSelector.getId()) == mUtilitiesInfo.getIdx()) {
holder.spSelector.setId(mUtilitiesInfo.getIdx() * 3);
} else {
Expand Down Expand Up @@ -472,8 +448,26 @@ public void onNothingSelected(AdapterView<?> arg0) {}

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);

int loadMode = mUtilitiesInfo.getModeId();
final ArrayList<String> modes = mUtilitiesInfo.getModes();
if (modes != null && modes.size() > 0) {
holder.spSelector.setId(mUtilitiesInfo.getIdx());
holder.spSelector.setVisibility(View.VISIBLE);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(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);

holder.on_button.setVisibility(View.GONE);
holder.data.setText(context.getString(R.string.set_mode) + ": " + modes.get(loadMode));
}
else{
holder.spSelector.setVisibility(View.GONE);
holder.data.setText(context.getString(R.string.set_point) + ": " + setPoint);
}
}

private void handleSelectorChange(UtilitiesInfo device, int id, String mode) {
Expand Down Expand Up @@ -556,7 +550,6 @@ public void setButtons(DataObjectHolder holder, int button) {
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);
Expand Down Expand Up @@ -627,8 +620,6 @@ public static class DataObjectHolder extends RecyclerView.ViewHolder implements
TextView data;
TextView hardware;
ImageView iconRow;
Boolean isProtected;

Chip dayButton;
Chip monthButton;
Chip yearButton;
Expand Down
61 changes: 61 additions & 0 deletions app/src/main/java/nl/hnogames/domoticz/fragments/Dashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import com.skydoves.colorpickerview.ColorPickerDialog;
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -78,6 +80,8 @@
import nl.hnogames.domoticzapi.Containers.DevicesInfo;
import nl.hnogames.domoticzapi.Containers.PlanInfo;
import nl.hnogames.domoticzapi.Containers.SunRiseInfo;
import nl.hnogames.domoticzapi.Containers.UserInfo;
import nl.hnogames.domoticzapi.Containers.UtilitiesInfo;
import nl.hnogames.domoticzapi.Domoticz;
import nl.hnogames.domoticzapi.DomoticzValues;
import nl.hnogames.domoticzapi.Interfaces.DevicesReceiver;
Expand Down Expand Up @@ -1040,6 +1044,63 @@ public void onCameraFullScreenClick(DevicesInfo device, String name) {
CameraUtil.ProcessImage(mContext, device.getCameraIdx(), name);
}

@Override
public void OnModeChanged(DevicesInfo utility, int mode, String modeName) {
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 " + modeName);
if (utility.isProtected()) {
PasswordDialog passwordDialog = new PasswordDialog(
mContext, StaticHelper.getDomoticz(mContext));
passwordDialog.show();
passwordDialog.onDismissListener(new PasswordDialog.DismissListener() {
@Override
public void onDismiss(String password) {
SetThermostatMode(utility, mode, password);
}

@Override
public void onCancel() {}
});
} else {
SetThermostatMode(utility, mode, null);
}
}

private void SetThermostatMode(DevicesInfo utility, int mode, String password) {
StaticHelper.getDomoticz(mContext).setAction(utility.getIdx(),
DomoticzValues.Json.Url.Set.THERMOSTAT,
DomoticzValues.Device.Thermostat.Action.TMODE,
mode,
password,
new setCommandReceiver() {
@Override
public void onReceiveResult(String result) {
if (result.contains("WRONG CODE")) {
UsefulBits.showSnackbar(mContext, frameLayout, R.string.security_wrong_code, Snackbar.LENGTH_SHORT);
if (getActivity() instanceof MainActivity)
((MainActivity) getActivity()).Talk(R.string.security_wrong_code);
} else {
successHandling(result, false);
processDashboard();
}
}

@Override
public void onError(Exception error) {
errorHandling(error);
}
});
}

private void setState(final DevicesInfo device, int state, final String password) {
StaticHelper.getDomoticz(mContext).setModalAction(device.getIdx(),
state,
Expand Down
61 changes: 61 additions & 0 deletions app/src/main/java/nl/hnogames/domoticz/fragments/Plan.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import com.skydoves.colorpickerview.ColorPickerDialog;
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -78,6 +80,8 @@
import nl.hnogames.domoticzapi.Containers.DevicesInfo;
import nl.hnogames.domoticzapi.Containers.PlanInfo;
import nl.hnogames.domoticzapi.Containers.SunRiseInfo;
import nl.hnogames.domoticzapi.Containers.UserInfo;
import nl.hnogames.domoticzapi.Containers.UtilitiesInfo;
import nl.hnogames.domoticzapi.Domoticz;
import nl.hnogames.domoticzapi.DomoticzValues;
import nl.hnogames.domoticzapi.Interfaces.DevicesReceiver;
Expand Down Expand Up @@ -1027,6 +1031,63 @@ public void onCameraFullScreenClick(DevicesInfo device, String name) {
CameraUtil.ProcessImage(mContext, device.getCameraIdx(), name);
}

@Override
public void OnModeChanged(DevicesInfo utility, int mode, String modeName) {
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 " + modeName);
if (utility.isProtected()) {
PasswordDialog passwordDialog = new PasswordDialog(
mContext, StaticHelper.getDomoticz(mContext));
passwordDialog.show();
passwordDialog.onDismissListener(new PasswordDialog.DismissListener() {
@Override
public void onDismiss(String password) {
SetThermostatMode(utility, mode, password);
}

@Override
public void onCancel() {}
});
} else {
SetThermostatMode(utility, mode, null);
}
}

private void SetThermostatMode(DevicesInfo utility, int mode, String password) {
StaticHelper.getDomoticz(mContext).setAction(utility.getIdx(),
DomoticzValues.Json.Url.Set.THERMOSTAT,
DomoticzValues.Device.Thermostat.Action.TMODE,
mode,
password,
new setCommandReceiver() {
@Override
public void onReceiveResult(String result) {
if (result.contains("WRONG CODE")) {
UsefulBits.showSnackbar(mContext, frameLayout, R.string.security_wrong_code, Snackbar.LENGTH_SHORT);
if (getActivity() instanceof MainActivity)
((MainActivity) getActivity()).Talk(R.string.security_wrong_code);
} else {
successHandling(result, false);
processDashboard();
}
}

@Override
public void onError(Exception error) {
errorHandling(error);
}
});
}

private void setState(final DevicesInfo device, int state, final String password) {
StaticHelper.getDomoticz(mContext).setModalAction(device.getIdx(),
state,
Expand Down
Loading

0 comments on commit 643c971

Please sign in to comment.