From 18d2c842445ce0d51e5cdf643dfb47cb3bef89da Mon Sep 17 00:00:00 2001 From: MTing Date: Thu, 25 Sep 2025 15:54:42 +0800 Subject: [PATCH] change device --- .../main/java/com/ouxuan/oxface/MainActivity.java | 58 +++++++++++-- .../com/ouxuan/oxface/device/RelayController.java | 99 ++++++++++++++-------- 2 files changed, 118 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/ouxuan/oxface/MainActivity.java b/app/src/main/java/com/ouxuan/oxface/MainActivity.java index 6306b91..f78c74a 100644 --- a/app/src/main/java/com/ouxuan/oxface/MainActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/MainActivity.java @@ -31,6 +31,7 @@ import com.google.gson.Gson; import com.ouxuan.oxface.data.DeviceSelectDataManager; import com.ouxuan.oxface.data.LoginDataManager; import com.ouxuan.oxface.device.DeviceUtils; +import com.ouxuan.oxface.device.RelayController; import com.ouxuan.oxface.network.api.PadApiService; import com.ouxuan.oxface.network.api.UserApiService; import com.ouxuan.oxface.network.callback.CompleteApiResponseCallback; @@ -60,6 +61,7 @@ public class MainActivity extends AppCompatActivity { private DeviceSelectDataManager deviceSelectDataManager; // 设备选择数据管理器 private KeepAliveManager keepAliveManager; // 保持活跃管理器 private AutoStartManager autoStartManager; // 自启动管理器 + private RelayController relayController; // 继电器控制器 private Dialog currentDialog; // 用于跟踪当前显示的Dialog,防止WindowLeaked错误 // 添加设备ID和版本号TextView引用 @@ -94,6 +96,9 @@ public class MainActivity extends AppCompatActivity { // 初始化设备选择数据管理器 deviceSelectDataManager = DeviceSelectDataManager.getInstance(this); + // 初始化继电器控制器 + relayController = RelayController.getInstance(); + // 初始化保持活跃管理器并启动(用于24小时无人值守) keepAliveManager = KeepAliveManager.getInstance(this); keepAliveManager.startKeepAlive(this); @@ -895,13 +900,10 @@ public class MainActivity extends AppCompatActivity { ", Hardware ID: " + hardwareId + ", API Code: " + apiResponse.getCode()); - // 动态获取设备类型并切换相机配置 + // 动态获取设备类型并统一初始化相关模块 int deviceType = deviceSelectDataManager.getDeviceType(); Log.d(TAG, "选择设备后获取设备类型: " + deviceType); - switchCameraByDeviceType(deviceType); - - // 根据设备类型切换485串口路径 - com.ouxuan.oxface.device.Ox485.switch485ByDeviceType(deviceType); + setDeviceInitByType(deviceType); // 新增:调用获取小程序码接口和上传人脸小程序码接口 // 使用计数器确保两个请求都完成后再进入人脸识别界面 @@ -1355,6 +1357,52 @@ public class MainActivity extends AppCompatActivity { } } + /** + * 根据设备类型统一初始化所有相关模块 + * @param deviceType 设备类型(5=第6批,6=第7批等) + */ + private void setDeviceInitByType(int deviceType) { + try { + LogManager.logInfo("MainActivity", "开始根据设备类型(" + deviceType + ")初始化相关模块"); + + // 1. 切换相机配置 + switchCameraByDeviceType(deviceType); + + // 2. 根据设备类型切换485串口路径 + com.ouxuan.oxface.device.Ox485.switch485ByDeviceType(deviceType); + + // 3. 设置继电器控制器的设备类型 + if (relayController != null) { + relayController.setDeviceType(deviceType); + LogManager.logInfo("MainActivity", "继电器控制器设备类型已设置为: " + deviceType); + } else { + LogManager.logError("MainActivity", "继电器控制器未初始化"); + } + + // 4. 初始化第七批设备管理器(如果设备类型为6或7) + if (deviceType == 6 || deviceType == 7) { + if (relayController != null) { + boolean initSuccess = relayController.initDeviceType7Manager(this); + if (initSuccess) { + LogManager.logInfo("MainActivity", "第七批设备管理器初始化成功"); + } else { + LogManager.logError("MainActivity", "第七批设备管理器初始化失败"); + } + } + } + + // TODO: 5. 初始化状态栏开关管理模块(待实现) + // StatusBarManager.initByDeviceType(deviceType); + + LogManager.logInfo("MainActivity", "设备类型相关模块初始化完成"); + + } catch (Exception e) { + String errorMessage = "设备类型初始化失败: " + e.getMessage(); + android.util.Log.e("MainActivity", errorMessage, e); + LogManager.logError("MainActivity", errorMessage, e); + } + } + @Override protected void onResume() { super.onResume(); diff --git a/app/src/main/java/com/ouxuan/oxface/device/RelayController.java b/app/src/main/java/com/ouxuan/oxface/device/RelayController.java index 9638957..21147e9 100644 --- a/app/src/main/java/com/ouxuan/oxface/device/RelayController.java +++ b/app/src/main/java/com/ouxuan/oxface/device/RelayController.java @@ -31,6 +31,10 @@ public class RelayController { // 第2批设备GPIO路径 private static final String PWM_FLASH_OUT_2 = "/sys/class/gpio/gpio123/direction"; private static final String PWM_FLASH_2 = "/sys/class/gpio/gpio123/value"; + + // 第7批设备GPIO路径 + private static final String PWM_FLASH_OUT_7 = "/sys/class/gpio/gpio88/direction"; + private static final String PWM_FLASH_7 = "/sys/class/gpio/gpio88/value"; // GPIO操作命令 private static final byte[] OPEN_RELAY = {'1'}; @@ -52,6 +56,9 @@ public class RelayController { // 第七批设备管理器 private DeviceType7Manager deviceType7Manager; + // 当前设备类型(5=第6批,6=第7批等) + private int deviceType = -1; + private RelayController() { handler = new Handler(Looper.getMainLooper()); } @@ -73,12 +80,28 @@ public class RelayController { } /** + * 设置设备类型 + * @param deviceType 设备类型(5=第6批,6=第7批等) + */ + public void setDeviceType(int deviceType) { + this.deviceType = deviceType; + LogManager.logInfo(TAG, "设置设备类型: " + deviceType); + } + + /** * 获取当前设备类型(需要根据实际情况实现) * @return 设备类型 */ private String getCurrentPadType() { - // TODO: 根据设备信息返回正确的类型,这里暂时返回默认 - return PAD_TYPE_DEFAULT; + // 根据设备类型返回对应的padType + if (deviceType == 6) { + return PAD_TYPE_SEVEN; // deviceType=6表示第7批设备,使用DeviceType7Manager + } else if (deviceType == 5) { + return PAD_TYPE_TWO; // deviceType=5表示第6批设备,使用第2批的GPIO路径 + } else { + // 其他情况返回默认 + return PAD_TYPE_DEFAULT; + } } /** @@ -193,6 +216,33 @@ public class RelayController { } /** + * 直接写入GPIO值到文件 + * @param value 要写入的值 + * @param valuePath GPIO值文件路径 + * @return 是否成功 + */ + private boolean writeGpioValueDirect(byte[] value, String valuePath) { + FileOutputStream fos = null; + try { + fos = new FileOutputStream(valuePath); + fos.write(value); + LogManager.logInfo(TAG, "GPIO值写入成功: " + valuePath + " -> " + new String(value)); + return true; + } catch (IOException e) { + LogManager.logError(TAG, "GPIO值写入失败: " + valuePath + ", 错误: " + e.getMessage(), e); + return false; + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + LogManager.logError(TAG, "关闭FileOutputStream失败", e); + } + } + } + } + + /** * 写入GPIO值 * @param value 要写入的值 * @param padType 设备类型 @@ -211,38 +261,19 @@ public class RelayController { return success; } else { // 其他设备使用文件操作 - FileOutputStream fos = null; - try { - String valuePath = PAD_TYPE_TWO.equals(padType) ? PWM_FLASH_2 : PWM_FLASH; - fos = new FileOutputStream(valuePath); - fos.write(value); + String valuePath = PAD_TYPE_TWO.equals(padType) ? PWM_FLASH_2 : PWM_FLASH; + + // 第一次尝试 + if (writeGpioValueDirect(value, valuePath)) { return true; - } catch (Exception e) { - if (e instanceof IOException) { - // 如果写入失败,尝试设置GPIO方向后重试 - LogManager.logWarning(TAG, "GPIO写入失败,尝试设置方向后重试"); - setGpioDirection(padType); - try { - if (fos != null) { - fos.write(value); - return true; - } - } catch (IOException ioException) { - LogManager.logError(TAG, "重试写入GPIO值失败", ioException); - } - } else { - LogManager.logError(TAG, "写入GPIO值时发生意外异常", e); - } - return false; - } finally { - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - LogManager.logError(TAG, "关闭FileOutputStream失败", e); - } - } } + + // 如果失败,尝试设置GPIO方向后重试 + LogManager.logWarning(TAG, "GPIO写入失败,尝试设置方向后重试"); + setGpioDirection(padType); + + // 第二次尝试 + return writeGpioValueDirect(value, valuePath); } } @@ -251,7 +282,7 @@ public class RelayController { * @return 是否成功 */ public boolean openRelay() { - return openRelay(PAD_TYPE_DEFAULT); + return openRelay(getCurrentPadType()); } /** @@ -275,7 +306,7 @@ public class RelayController { * @return 是否成功 */ public boolean closeRelay() { - return closeRelay(PAD_TYPE_DEFAULT); + return closeRelay(getCurrentPadType()); } /**