Zendure SolarFlow: parse firmware version, allow setting maximum charge

This commit is contained in:
Andreas Shimokawa
2025-12-25 20:15:03 +01:00
parent 897647175c
commit db92b58083
6 changed files with 75 additions and 44 deletions
@@ -677,6 +677,7 @@ public class DeviceSettingsPreferenceConst {
public static final String PREF_BATTERY_DISCHARGE_INTERVALS_SET = "battery_discharge_intervals_set"; public static final String PREF_BATTERY_DISCHARGE_INTERVALS_SET = "battery_discharge_intervals_set";
public static final String PREF_BATTERY_DISCHARGE_MANUAL = "battery_discharge_manual"; public static final String PREF_BATTERY_DISCHARGE_MANUAL = "battery_discharge_manual";
public static final String PREF_BATTERY_MINIMUM_CHARGE = "battery_minimum_charge"; public static final String PREF_BATTERY_MINIMUM_CHARGE = "battery_minimum_charge";
public static final String PREF_BATTERY_MAXIMUM_CHARGE = "battery_maximum_charge";
public static final String PREF_BATTERY_ALLOW_PASS_THROUGH = "battery_allow_pass_through"; public static final String PREF_BATTERY_ALLOW_PASS_THROUGH = "battery_allow_pass_through";
public static final String PREF_DISPLAY_ENABLED = "display_enabled"; public static final String PREF_DISPLAY_ENABLED = "display_enabled";
@@ -1001,6 +1001,7 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
addPreferenceHandlerFor("lock"); addPreferenceHandlerFor("lock");
addPreferenceHandlerFor(PREF_BATTERY_MINIMUM_CHARGE); addPreferenceHandlerFor(PREF_BATTERY_MINIMUM_CHARGE);
addPreferenceHandlerFor(PREF_BATTERY_MAXIMUM_CHARGE);
addPreferenceHandlerFor(PREF_BATTERY_ALLOW_PASS_THROUGH); addPreferenceHandlerFor(PREF_BATTERY_ALLOW_PASS_THROUGH);
addPreferenceHandlerFor(PREF_DISPLAY_ENABLED); addPreferenceHandlerFor(PREF_DISPLAY_ENABLED);
@@ -90,6 +90,7 @@ public class SolarFlowDeviceCoordinator extends AbstractBLEDeviceCoordinator {
public int[] getSupportedDeviceSpecificSettings(GBDevice device) { public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
return new int[]{ return new int[]{
R.xml.devicesettings_battery_minimum_charge, R.xml.devicesettings_battery_minimum_charge,
R.xml.devicesettings_battery_maximum_charge,
}; };
} }
@@ -16,6 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */ along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.zendure; package nodomain.freeyourgadget.gadgetbridge.service.devices.zendure;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_BATTERY_MAXIMUM_CHARGE;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_BATTERY_MINIMUM_CHARGE; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_BATTERY_MINIMUM_CHARGE;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
@@ -59,6 +60,7 @@ public class SolarFlowDeviceSupport extends AbstractBTLESingleDeviceSupport {
private int hyperTmp; private int hyperTmp;
private int electricLevel = -1; private int electricLevel = -1;
private String deviceId; private String deviceId;
private int firmwareVersion = -1;
public SolarFlowDeviceSupport() { public SolarFlowDeviceSupport() {
super(LOG); super(LOG);
@@ -74,47 +76,49 @@ public class SolarFlowDeviceSupport extends AbstractBTLESingleDeviceSupport {
if (jsonMessage.has("method")) { if (jsonMessage.has("method")) {
String method = jsonMessage.getString("method"); String method = jsonMessage.getString("method");
if (method.equals("BLESPP")) { switch (method) {
deviceId = jsonMessage.getString("deviceId"); case "BLESPP" -> {
JSONObject sendMessage = new JSONObject() deviceId = jsonMessage.getString("deviceId");
.put("messageId", messageId++) JSONObject sendMessage = new JSONObject()
.put("method", "BLESPP_OK"); .put("messageId", messageId++)
.put("method", "BLESPP_OK");
sendMessage(sendMessage); sendMessage(sendMessage);
JSONObject sendMessageGetInfo = new JSONObject() JSONObject sendMessageGetInfo = new JSONObject()
.put("messageId", messageId++) .put("messageId", messageId++)
.put("method", "getInfo") .put("method", "getInfo")
.put("timestamp", System.currentTimeMillis() / 1000); .put("timestamp", System.currentTimeMillis() / 1000);
sendMessage(sendMessageGetInfo); sendMessage(sendMessageGetInfo);
}
JSONObject sendMessageGetAllInfos = new JSONObject() case "getInfo-rsp" -> {
.put("messageId", messageId++) JSONObject sendMessageGetAllInfos = new JSONObject()
.put("deviceId", deviceId) .put("messageId", messageId++)
.put("method", "read") .put("deviceId", deviceId)
.put("properties", new JSONArray().put("getAll")) .put("method", "read")
.put("timestamp", System.currentTimeMillis() / 1000); .put("properties", new JSONArray().put("getAll"))
sendMessage(sendMessageGetAllInfos); .put("timestamp", System.currentTimeMillis() / 1000);
} else if (method.equals("report")) { sendMessage(sendMessageGetAllInfos);
long messageIdReport = jsonMessage.getLong("messageId"); }
if (messageIdReport != this.messageIdReport) { case "report" -> {
if (this.messageIdReport != -1) { long messageIdReport = jsonMessage.getLong("messageId");
reportToStatusActivity(); if (messageIdReport != this.messageIdReport) {
} else { if (this.messageIdReport != -1) {
TransactionBuilder builder = createTransactionBuilder("setInitialized"); reportToStatusActivity();
builder.setDeviceState(GBDevice.State.INITIALIZED); } else {
builder.queue(); TransactionBuilder builder = createTransactionBuilder("setInitialized");
builder.setDeviceState(GBDevice.State.INITIALIZED);
builder.queue();
}
this.messageIdReport = messageIdReport;
}
if (jsonMessage.has("properties")) {
return handleReportProperties(jsonMessage.getJSONObject("properties"));
}
if (jsonMessage.has("packData")) {
return handleReportPackData(jsonMessage.getJSONArray("packData"));
} }
this.messageIdReport = messageIdReport;
} }
if (jsonMessage.has("properties")) {
return handleReportProperties(jsonMessage.getJSONObject("properties"));
}
if (jsonMessage.has("packData")) {
return handleReportPackData(jsonMessage.getJSONArray("packData"));
}
} }
} }
} catch (JSONException e) { } catch (JSONException e) {
@@ -144,14 +148,19 @@ public class SolarFlowDeviceSupport extends AbstractBTLESingleDeviceSupport {
public void onSendConfiguration(final String config) { public void onSendConfiguration(final String config) {
Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress())); Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()));
switch (config) { switch (config) {
case PREF_BATTERY_MINIMUM_CHARGE: case PREF_BATTERY_MINIMUM_CHARGE -> {
int minSoc = devicePrefs.getInt(PREF_BATTERY_MINIMUM_CHARGE, 10); int minSoc = devicePrefs.getInt(PREF_BATTERY_MINIMUM_CHARGE, 10);
if (minSoc < 0) minSoc = 0; if (minSoc < 0) minSoc = 0;
if (minSoc > 50) minSoc = 50; if (minSoc > 50) minSoc = 50;
sendWriteProperty("minSoc", minSoc*10); sendWriteProperty("minSoc", minSoc * 10);
return; }
default: case PREF_BATTERY_MAXIMUM_CHARGE -> {
LOG.warn("Unknown config changed: {}", config); int socSet = devicePrefs.getInt(PREF_BATTERY_MAXIMUM_CHARGE, 100);
if (socSet < 70) socSet = 70;
if (socSet > 100) socSet = 100;
sendWriteProperty("socSet", socSet * 10);
}
default -> LOG.warn("Unknown config changed: {}", config);
} }
} }
@@ -199,13 +208,21 @@ public class SolarFlowDeviceSupport extends AbstractBTLESingleDeviceSupport {
int electricLevel = properties.getInt("electricLevel"); int electricLevel = properties.getInt("electricLevel");
if (electricLevel != this.electricLevel) { if (electricLevel != this.electricLevel) {
this.electricLevel = electricLevel; this.electricLevel = electricLevel;
getDevice().setBatteryLevel(electricLevel); getDevice().setBatteryLevel(electricLevel); // update event will be sent when firmware gets read
getDevice().sendDeviceUpdateIntent(getContext());
} }
} }
if (properties.has("outputHomePower")) { if (properties.has("outputHomePower")) {
outputHomePower = properties.getInt("outputHomePower"); outputHomePower = properties.getInt("outputHomePower");
} }
if (properties.has("MASTER")) {
int firmwareVersion = properties.getInt("MASTER");
if (firmwareVersion != this.firmwareVersion) {
this.firmwareVersion = firmwareVersion;
String firmwareString = ((firmwareVersion & 0xf000) >> 12) + "." + ((firmwareVersion & 0x0f00) >> 8) + "." + (firmwareVersion & 0x00ff);
getDevice().setFirmwareVersion(firmwareString);
getDevice().sendDeviceUpdateIntent(getContext());
}
}
} catch (JSONException e) { } catch (JSONException e) {
LOG.error("JSON error while parsing report: {}", e.getMessage()); LOG.error("JSON error while parsing report: {}", e.getMessage());
+1
View File
@@ -4137,6 +4137,7 @@
<string name="manual_discharge">Manual discharge intervals</string> <string name="manual_discharge">Manual discharge intervals</string>
<string name="summary_battery_discharge_intervals_set">Send configuration below to device</string> <string name="summary_battery_discharge_intervals_set">Send configuration below to device</string>
<string name="battery_minimum_charge">Minimum battery charge in %</string> <string name="battery_minimum_charge">Minimum battery charge in %</string>
<string name="battery_maximum_charge">Maximum battery charge in %</string>
<string name="battery_allow_pass_though_summary">When enabled, the battery can be charged while discharging</string> <string name="battery_allow_pass_though_summary">When enabled, the battery can be charged while discharging</string>
<string name="battery_allow_pass_through">Allow battery pass-through</string> <string name="battery_allow_pass_through">Allow battery pass-through</string>
<string name="canned_replies_not_empty">There should be at least one canned reply.</string> <string name="canned_replies_not_empty">There should be at least one canned reply.</string>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditTextPreference
android:defaultValue="100"
android:inputType="number"
android:key="battery_maximum_charge"
android:title="@string/battery_maximum_charge"
app:useSimpleSummaryProvider="true" />
</androidx.preference.PreferenceScreen>