* 设置界面增加电源开关测试

* App 启动时默认不发送开机指令
This commit is contained in:
Zhanghu
2026-03-10 10:45:46 +08:00
parent cac9dd41aa
commit 691d6c15ad
4 changed files with 100 additions and 17 deletions

View File

@@ -9,7 +9,17 @@ author:
2. $ {VERSION_CODE} (去掉空格),会自动替换实际修订号,比如 1.1.4.$ {VERSION_CODE} 2. $ {VERSION_CODE} (去掉空格),会自动替换实际修订号,比如 1.1.4.$ {VERSION_CODE}
--> -->
### [1.0.5.${VERSION_CODE}] - 2026.3.5 ### [1.0.6.$ {VERSION_CODE}] - 2026.3.X
#### 文件下载
* [dashboardclient_1.0.6.apk](dashboardclient_1.0.6.apk)
#### 更新记录
* 设置界面增加电源开关测试
* App 启动时默认不发送开机指令
### [1.0.5.26] - 2026.3.5
#### 文件下载 #### 文件下载

View File

@@ -2,15 +2,21 @@ package cn.ykbox.dashboard.activity;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import com.blankj.utilcode.util.ToastUtils;
import cn.ykbox.dashboard.R; import cn.ykbox.dashboard.R;
import cn.ykbox.dashboard.perferences.PreferenceConfiguration; import cn.ykbox.dashboard.perferences.PreferenceConfiguration;
import cn.ykbox.dashboard.serial.SerialPortDetector;
public class SettingsActivity extends AppCompatActivity { public class SettingsActivity extends AppCompatActivity {
@@ -30,25 +36,82 @@ public class SettingsActivity extends AppCompatActivity {
} }
} }
public static class SettingsFragment extends PreferenceFragmentCompat { public static class SettingsFragment extends PreferenceFragmentCompat
implements Preference.OnPreferenceClickListener {
@Override @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey); setPreferencesFromResource(R.xml.root_preferences, rootKey);
Preference clearDevicePref = findPreference("k_clear_device"); Preference clearDevicePref = findPreference("k_clear_device");
if (clearDevicePref != null) { if (clearDevicePref != null) {
clearDevicePref.setOnPreferenceClickListener(preference -> { clearDevicePref.setOnPreferenceClickListener(this);
new AlertDialog.Builder(requireContext()) }
.setTitle("确认清除")
.setMessage("确定要清除当前串口设备路径吗?") Preference testPowerOnPref = findPreference("k_test_power_on");
.setPositiveButton("确定", (dialog, which) -> { if (testPowerOnPref != null) {
PreferenceConfiguration.setSerialPortPath(requireContext(), ""); testPowerOnPref.setOnPreferenceClickListener(this);
}) }
.setNegativeButton("取消", null)
.show(); Preference testPowerOffPref = findPreference("k_test_power_off");
return true; if (testPowerOffPref != null) {
}); testPowerOffPref.setOnPreferenceClickListener(this);
} }
} }
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
String key = preference.getKey();
if(key.equals("k_clear_device")) {
clearSerialPath();
return true;
} else if(key.equals("k_test_power_on")) {
testPowerOn();
return true;
} else if(key.equals("k_test_power_off")) {
testPowerOff();
return true;
}
return false;
}
private void clearSerialPath() {
new AlertDialog.Builder(requireContext())
.setTitle("确认清除")
.setMessage("确定要清除当前串口设备路径吗?")
.setPositiveButton("确定", (dialog, which) -> {
PreferenceConfiguration.setSerialPortPath(requireContext(), "");
})
.setNegativeButton("取消", null)
.show();
}
private void testPowerOn() {
String portPath = PreferenceConfiguration.getSerialPortPath(getContext());
int baudRate = PreferenceConfiguration.getSerialPortBaudRate(getContext());
if (TextUtils.isEmpty(portPath)) {
ToastUtils.showShort("串口路径未设置");
return;
}
SerialPortDetector detector = new SerialPortDetector(portPath, baudRate);
detector.sendPowerOnCommand();
}
private void testPowerOff() {
String portPath = PreferenceConfiguration.getSerialPortPath(getContext());
int baudRate = PreferenceConfiguration.getSerialPortBaudRate(getContext());
if (TextUtils.isEmpty(portPath)) {
ToastUtils.showShort("串口路径未设置");
return;
}
SerialPortDetector detector = new SerialPortDetector(portPath, baudRate);
detector.sendPowerOffCommand();
}
} }
} }

View File

@@ -33,6 +33,6 @@ public class PreferenceConfiguration {
public static boolean getSendPowerOnCmd(Context context) { public static boolean getSendPowerOnCmd(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean(KEY_SEND_POWER_ON_CMD, true); return prefs.getBoolean(KEY_SEND_POWER_ON_CMD, false);
} }
} }

View File

@@ -14,7 +14,7 @@
app:defaultValue="@string/url_end_point_default_value" /> app:defaultValue="@string/url_end_point_default_value" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="外部设备"> <PreferenceCategory app:title="配置物联网关">
<EditTextPreference <EditTextPreference
app:key="k_serial_port_path" app:key="k_serial_port_path"
app:title="串口设备" app:title="串口设备"
@@ -28,11 +28,21 @@
<CheckBoxPreference <CheckBoxPreference
app:key="k_send_power_on_cmd" app:key="k_send_power_on_cmd"
app:title="App 启动时发送打开电源指令" app:title="App 启动时发送打开电源指令"
app:summary="每次启动 App 时先发送关闭再发送打开电源指令" app:summary="每次启动 App 时先发送关闭电源指令,再发送打开电源指令"
app:defaultValue="true" /> app:defaultValue="false" />
<Preference <Preference
app:key="k_clear_device" app:key="k_clear_device"
app:title="清除设备" app:title="清除设备"
app:summary="清除当前串口设备路径, 下次启动时自动检测设备。" /> app:summary="清除当前串口设备路径, 下次启动时自动检测设备。" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="测试物联网关">
<Preference
app:key="k_test_power_on"
app:title="打开电源"
app:summary="发送命令给物联网关,开启供电" />
<Preference
app:key="k_test_power_off"
app:title="关闭电源"
app:summary="发送命令给物联网关,停止供电" />
</PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>