diff --git a/app/src/main/java/com/ouxuan/oxface/device/DeviceType7Manager.java b/app/src/main/java/com/ouxuan/oxface/device/DeviceType7Manager.java new file mode 100644 index 0000000..29b37af --- /dev/null +++ b/app/src/main/java/com/ouxuan/oxface/device/DeviceType7Manager.java @@ -0,0 +1,687 @@ +package com.ouxuan.oxface.device; + +import android.content.Context; +import android.os.RemoteException; +import android.util.Log; + +import com.ouxuan.oxface.utils.LogManager; + +import android.os.yx.IYxGpioListener; +import android.os.yx.YxDeviceManager; + +/** + * 第七批设备管理器 + * 封装YxDeviceManager的所有功能,包括APK管理、系统控制、硬件控制等 + * + * @author Oxuan + * @version 1.0 + * @date 2025/09/25 + */ +public class DeviceType7Manager { + + private static final String TAG = "DeviceType7Manager"; + + // YxDeviceManager实例 + private YxDeviceManager yxApiInstance; + private Context context; + + // GPIO常量定义 + public static final class YxGpioConstants { + public static final int GPIO_RELAY = 88; + public static final int GPIO_WB = 4; // 微波 + public static final int GPIO_MC = 114; // 门磁 + public static final int GPIO_EXIT_SWITCH = 115; // 退出开关 + public static final int GPIO_IO1 = 114; + public static final int GPIO_IO2 = 115; + + public static final int GPIO_DIRECTION_OUTPUT = 0; + public static final int GPIO_DIRECTION_INPUT = 1; + + public static final class GpioState { + public static final int LOW = 0; + public static final int HIGH = 1; + } + } + + // 补光灯控制常量 + public static final class LightConstants { + public static final String LIGHT_RED_ON = "lightron"; + public static final String LIGHT_RED_OFF = "lightroff"; + public static final String LIGHT_GREEN_ON = "lightgon"; + public static final String LIGHT_GREEN_OFF = "lightgoff"; + public static final String LIGHT_WHITE_ON = "lightbon"; + public static final String LIGHT_WHITE_OFF = "lightboff"; + } + + // 单例实例 + private static volatile DeviceType7Manager instance; + + private DeviceType7Manager() { + } + + /** + * 获取DeviceType7Manager单例实例 + * @return DeviceType7Manager实例 + */ + public static DeviceType7Manager getInstance() { + if (instance == null) { + synchronized (DeviceType7Manager.class) { + if (instance == null) { + instance = new DeviceType7Manager(); + } + } + } + return instance; + } + + /** + * 初始化YxDeviceManager + * @param context Android上下文 + * @return 是否初始化成功 + */ + public boolean init(Context context) { + this.context = context; + try { + yxApiInstance = YxDeviceManager.getInstance(context); + LogManager.logInfo(TAG, "YxDeviceManager初始化成功"); + return true; + } catch (Exception e) { + LogManager.logError(TAG, "YxDeviceManager初始化失败", e); + return false; + } + } + + /** + * 检查是否已初始化 + * @return 是否已初始化 + */ + private boolean isInitialized() { + return yxApiInstance != null && context != null; + } + + // ==================== APK管理功能 ==================== + + /** + * 静默安装APK + * @param apkPath APK文件绝对路径 + * @param start 安装完成后是否自动启动 + * @return 是否成功 + */ + public boolean silentInstallApk(String apkPath, boolean start) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + yxApiInstance.silentInstallApk(apkPath, start); + LogManager.logInfo(TAG, "APK静默安装成功: " + apkPath + ", 自动启动: " + start); + return true; + } catch (Exception e) { + LogManager.logError(TAG, "APK静默安装失败: " + apkPath, e); + return false; + } + } + + /** + * 静默卸载APK + * @param packageName APK包名 + * @return 是否成功 + */ + public boolean unInstallApk(String packageName) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + yxApiInstance.unInstallApk(packageName); + LogManager.logInfo(TAG, "APK静默卸载成功: " + packageName); + return true; + } catch (Exception e) { + LogManager.logError(TAG, "APK静默卸载失败: " + packageName, e); + return false; + } + } + + // ==================== 系统功能 ==================== + + /** + * 写入设备SN号 + * @param sn SN号 + * @return 是否成功 + */ + public boolean setDeviceSerialno(String sn) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + int result = yxApiInstance.setDeviceSerialno(sn); + boolean success = (result != -1); + if (success) { + LogManager.logInfo(TAG, "设备SN写入成功: " + sn); + } else { + LogManager.logError(TAG, "设备SN写入失败: " + sn); + } + return success; + } catch (Exception e) { + LogManager.logError(TAG, "设备SN写入异常: " + sn, e); + return false; + } + } + + /** + * 写入设备Mac地址 + * @param mac Mac地址 + * @return 是否成功 + */ + public boolean setDeviceMacaddress(String mac) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + int result = yxApiInstance.setDeviceMacaddress(mac); + boolean success = (result != -1); + if (success) { + LogManager.logInfo(TAG, "设备Mac地址写入成功: " + mac); + } else { + LogManager.logError(TAG, "设备Mac地址写入失败: " + mac); + } + return success; + } catch (Exception e) { + LogManager.logError(TAG, "设备Mac地址写入异常: " + mac, e); + return false; + } + } + + /** + * 获取设备SN号 + * @return SN号,失败返回null + */ + public String getSerialno() { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return null; + } + + try { + String sn = yxApiInstance.getSerialno(); + LogManager.logInfo(TAG, "获取设备SN成功: " + sn); + return sn; + } catch (Exception e) { + LogManager.logError(TAG, "获取设备SN失败", e); + return null; + } + } + + /** + * 关机 + * @return 是否成功 + */ + public boolean shutDownNow() { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + int result = yxApiInstance.shutDownNow(); + boolean success = (result != -1); + if (success) { + LogManager.logInfo(TAG, "设备关机成功"); + } else { + LogManager.logError(TAG, "设备关机失败"); + } + return success; + } catch (Exception e) { + LogManager.logError(TAG, "设备关机异常", e); + return false; + } + } + + /** + * 重启 + * @return 是否成功 + */ + public boolean rebootNow() { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + int result = yxApiInstance.rebootNow(); + boolean success = (result != -1); + if (success) { + LogManager.logInfo(TAG, "设备重启成功"); + } else { + LogManager.logError(TAG, "设备重启失败"); + } + return success; + } catch (Exception e) { + LogManager.logError(TAG, "设备重启异常", e); + return false; + } + } + + /** + * 设置系统时间 + * @param timestamp 时间戳 + */ + public void setSystemTime(long timestamp) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return; + } + + try { + yxApiInstance.setSystemTime(timestamp); + LogManager.logInfo(TAG, "系统时间设置成功: " + timestamp); + } catch (Exception e) { + LogManager.logError(TAG, "系统时间设置失败: " + timestamp, e); + } + } + + /** + * 启用看门狗 + */ + public void enableWatchdog() { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return; + } + + try { + yxApiInstance.enableWatchdog(); + LogManager.logInfo(TAG, "看门狗启用成功"); + } catch (Exception e) { + LogManager.logError(TAG, "看门狗启用失败", e); + } + } + + /** + * 应用层喂狗 + */ + public void feedWatchdog() { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return; + } + + try { + yxApiInstance.feedWatchdog(); + LogManager.logInfo(TAG, "喂狗成功"); + } catch (Exception e) { + LogManager.logError(TAG, "喂狗失败", e); + } + } + + /** + * 停用看门狗 + */ + public void disableWatchdog() { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return; + } + + try { + yxApiInstance.disableWatchdog(); + LogManager.logInfo(TAG, "看门狗停用成功"); + } catch (Exception e) { + LogManager.logError(TAG, "看门狗停用失败", e); + } + } + + /** + * 获取设备IMEI号 + * @return IMEI号,失败返回null + */ + public String getTelephonyImei() { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return null; + } + + try { + String imei = yxApiInstance.getTelephonyImei(); + LogManager.logInfo(TAG, "获取设备IMEI成功: " + imei); + return imei; + } catch (Exception e) { + LogManager.logError(TAG, "获取设备IMEI失败", e); + return null; + } + } + + /** + * 获取sim卡iccid + * @return ICCID,失败返回null + */ + public String getSimSerialNumber() { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return null; + } + + try { + String iccid = yxApiInstance.getSimSerialNumber(); + LogManager.logInfo(TAG, "获取sim卡ICCID成功: " + iccid); + return iccid; + } catch (Exception e) { + LogManager.logError(TAG, "获取sim卡ICCID失败", e); + return null; + } + } + + /** + * 设置状态栏显示隐藏 + * @param show true显示,false隐藏 + */ + public void setStaBar(boolean show) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return; + } + + try { + yxApiInstance.setStaBar(show); + LogManager.logInfo(TAG, "状态栏设置成功: " + (show ? "显示" : "隐藏")); + } catch (Exception e) { + LogManager.logError(TAG, "状态栏设置失败: " + (show ? "显示" : "隐藏"), e); + } + } + + /** + * 设置导航栏显示隐藏 + * @param show true显示,false隐藏 + */ + public void setNavBar(boolean show) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return; + } + + try { + yxApiInstance.setNavBar(show); + LogManager.logInfo(TAG, "导航栏设置成功: " + (show ? "显示" : "隐藏")); + } catch (Exception e) { + LogManager.logError(TAG, "导航栏设置失败: " + (show ? "显示" : "隐藏"), e); + } + } + + // ==================== 硬件功能 - GPIO ==================== + + /** + * 设置GPIO方向 + * @param gpio GPIO编号 + * @param direction 方向(0输出,1输入) + * @return 是否成功 + */ + public boolean setGpioDirection(int gpio, int direction) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + boolean success = yxApiInstance.setGpioDirection(gpio, direction); + if (success) { + LogManager.logInfo(TAG, "GPIO方向设置成功 - GPIO: " + gpio + ", 方向: " + (direction == 0 ? "输出" : "输入")); + } else { + LogManager.logError(TAG, "GPIO方向设置失败 - GPIO: " + gpio); + } + return success; + } catch (Exception e) { + LogManager.logError(TAG, "GPIO方向设置异常 - GPIO: " + gpio, e); + return false; + } + } + + /** + * 获取GPIO方向 + * @param gpio GPIO编号 + * @return 方向字符串,失败返回null + */ + public String getGpioDirection(int gpio) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return null; + } + + try { + String direction = yxApiInstance.getGpioDirection(gpio); + LogManager.logInfo(TAG, "获取GPIO方向成功 - GPIO: " + gpio + ", 方向: " + direction); + return direction; + } catch (Exception e) { + LogManager.logError(TAG, "获取GPIO方向失败 - GPIO: " + gpio, e); + return null; + } + } + + /** + * 获取GPIO值 + * @param gpio GPIO编号 + * @return GPIO值(-1失败,0低,1高) + */ + public int getGpioValue(int gpio) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return -1; + } + + try { + int value = yxApiInstance.getGpioValue(gpio); + LogManager.logInfo(TAG, "获取GPIO值成功 - GPIO: " + gpio + ", 值: " + value); + return value; + } catch (Exception e) { + LogManager.logError(TAG, "获取GPIO值失败 - GPIO: " + gpio, e); + return -1; + } + } + + /** + * 设置GPIO值 + * @param gpio GPIO编号 + * @param value 值(0低,1高) + * @return 是否成功 + */ + public boolean setGpioValue(int gpio, int value) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + int result = yxApiInstance.setGpioValue(gpio, value); + boolean success = (result != -1); + if (success) { + LogManager.logInfo(TAG, "设置GPIO值成功 - GPIO: " + gpio + ", 值: " + value); + } else { + LogManager.logError(TAG, "设置GPIO值失败 - GPIO: " + gpio); + } + return success; + } catch (Exception e) { + LogManager.logError(TAG, "设置GPIO值异常 - GPIO: " + gpio, e); + return false; + } + } + + /** + * 注册GPIO监听器 + * @param listener 监听器 + * @param gpio GPIO编号 + * @return 是否成功 + */ + public boolean registerGpioListener(IYxGpioListener listener, int gpio) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return false; + } + + try { + yxApiInstance.register(listener, gpio); + LogManager.logInfo(TAG, "GPIO监听器注册成功 - GPIO: " + gpio); + return true; + } catch (Exception e) { + LogManager.logError(TAG, "GPIO监听器注册失败 - GPIO: " + gpio, e); + return false; + } + } + + // ==================== 硬件功能 - 补光灯 ==================== + + /** + * 控制补光灯 + * @param command 控制命令 + */ + public void controlLight(String command) { + if (!isInitialized()) { + LogManager.logError(TAG, "YxDeviceManager未初始化"); + return; + } + + try { + yxApiInstance.setOemFunc(command); + LogManager.logInfo(TAG, "补光灯控制成功: " + command); + } catch (Exception e) { + LogManager.logError(TAG, "补光灯控制失败: " + command, e); + } + } + + /** + * 红灯亮 + */ + public void turnOnRedLight() { + controlLight(LightConstants.LIGHT_RED_ON); + } + + /** + * 红灯灭 + */ + public void turnOffRedLight() { + controlLight(LightConstants.LIGHT_RED_OFF); + } + + /** + * 绿灯亮 + */ + public void turnOnGreenLight() { + controlLight(LightConstants.LIGHT_GREEN_ON); + } + + /** + * 绿灯灭 + */ + public void turnOffGreenLight() { + controlLight(LightConstants.LIGHT_GREEN_OFF); + } + + /** + * 白灯亮 + */ + public void turnOnWhiteLight() { + controlLight(LightConstants.LIGHT_WHITE_ON); + } + + /** + * 白灯灭 + */ + public void turnOffWhiteLight() { + controlLight(LightConstants.LIGHT_WHITE_OFF); + } + + // ==================== 硬件功能 - 继电器 ==================== + + /** + * 开启继电器 + * @return 是否成功 + */ + public boolean openRelay() { + // 设置GPIO为输出模式 + if (!setGpioDirection(YxGpioConstants.GPIO_RELAY, YxGpioConstants.GPIO_DIRECTION_OUTPUT)) { + return false; + } + + // 设置为高电平 + return setGpioValue(YxGpioConstants.GPIO_RELAY, YxGpioConstants.GpioState.HIGH); + } + + /** + * 关闭继电器 + * @return 是否成功 + */ + public boolean closeRelay() { + // 设置GPIO为输出模式 + if (!setGpioDirection(YxGpioConstants.GPIO_RELAY, YxGpioConstants.GPIO_DIRECTION_OUTPUT)) { + return false; + } + + // 设置为低电平 + return setGpioValue(YxGpioConstants.GPIO_RELAY, YxGpioConstants.GpioState.LOW); + } + + /** + * 获取继电器状态 + * @return true开启,false关闭,null失败 + */ + public Boolean getRelayStatus() { + int value = getGpioValue(YxGpioConstants.GPIO_RELAY); + if (value == -1) { + return null; + } + return value == YxGpioConstants.GpioState.HIGH; + } + + // ==================== 硬件功能 - 传感器 ==================== + + /** + * 获取微波传感器状态 + * @return true有人,false无人,null失败 + */ + public Boolean getMicrowaveStatus() { + int value = getGpioValue(YxGpioConstants.GPIO_WB); + if (value == -1) { + return null; + } + return value == YxGpioConstants.GpioState.HIGH; + } + + /** + * 获取门磁传感器状态 + * @return true开门,false关门,null失败 + */ + public Boolean getDoorMagnetStatus() { + int value = getGpioValue(YxGpioConstants.GPIO_MC); + if (value == -1) { + return null; + } + return value == YxGpioConstants.GpioState.HIGH; + } + + /** + * 获取退出开关状态 + * @return true按下,false未按下,null失败 + */ + public Boolean getExitSwitchStatus() { + int value = getGpioValue(YxGpioConstants.GPIO_EXIT_SWITCH); + if (value == -1) { + return null; + } + return value == YxGpioConstants.GpioState.HIGH; + } + + // ==================== 释放资源 ==================== + + /** + * 释放资源 + */ + public void release() { + yxApiInstance = null; + context = null; + LogManager.logInfo(TAG, "DeviceType7Manager资源已释放"); + } +} \ No newline at end of file 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 cdef846..9638957 100644 --- a/app/src/main/java/com/ouxuan/oxface/device/RelayController.java +++ b/app/src/main/java/com/ouxuan/oxface/device/RelayController.java @@ -1,5 +1,6 @@ package com.ouxuan.oxface.device; +import android.content.Context; import android.os.Handler; import android.os.Looper; import android.util.Log; @@ -40,6 +41,7 @@ public class RelayController { // 设备类型 public static final String PAD_TYPE_DEFAULT = "pad_default"; public static final String PAD_TYPE_TWO = "pad_two"; + public static final String PAD_TYPE_SEVEN = "pad_seven"; // 单例实例 private static volatile RelayController instance; @@ -47,11 +49,39 @@ public class RelayController { // Handler用于延时操作 private Handler handler; + // 第七批设备管理器 + private DeviceType7Manager deviceType7Manager; + private RelayController() { handler = new Handler(Looper.getMainLooper()); } /** + * 初始化第七批设备管理器 + * @param context Android上下文 + * @return 是否初始化成功 + */ + public boolean initDeviceType7Manager(Context context) { + deviceType7Manager = DeviceType7Manager.getInstance(); + boolean success = deviceType7Manager.init(context); + if (success) { + LogManager.logInfo(TAG, "第七批设备管理器初始化成功"); + } else { + LogManager.logError(TAG, "第七批设备管理器初始化失败"); + } + return success; + } + + /** + * 获取当前设备类型(需要根据实际情况实现) + * @return 设备类型 + */ + private String getCurrentPadType() { + // TODO: 根据设备信息返回正确的类型,这里暂时返回默认 + return PAD_TYPE_DEFAULT; + } + + /** * 获取RelayController单例实例 * @return RelayController实例 */ @@ -71,20 +101,27 @@ public class RelayController { * @param padType 设备类型 */ private void setGpioDirection(String padType) { - FileOutputStream fos = null; - try { - String directionPath = PAD_TYPE_TWO.equals(padType) ? PWM_FLASH_OUT_2 : PWM_FLASH_OUT; - fos = new FileOutputStream(directionPath); - fos.write(GPIO_OUT); - LogManager.logInfo(TAG, "GPIO方向设置为输出模式成功, 设备类型: " + padType); - } catch (Exception e) { - LogManager.logError(TAG, "设置GPIO方向失败", e); - } finally { - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - LogManager.logError(TAG, "关闭FileOutputStream失败", e); + if (PAD_TYPE_SEVEN.equals(padType) && deviceType7Manager != null) { + // 第七批设备使用DeviceType7Manager + deviceType7Manager.setGpioDirection(DeviceType7Manager.YxGpioConstants.GPIO_RELAY, + DeviceType7Manager.YxGpioConstants.GPIO_DIRECTION_OUTPUT); + } else { + // 其他设备使用文件操作 + FileOutputStream fos = null; + try { + String directionPath = PAD_TYPE_TWO.equals(padType) ? PWM_FLASH_OUT_2 : PWM_FLASH_OUT; + fos = new FileOutputStream(directionPath); + fos.write(GPIO_OUT); + LogManager.logInfo(TAG, "GPIO方向设置为输出模式成功, 设备类型: " + padType); + } catch (Exception e) { + LogManager.logError(TAG, "设置GPIO方向失败", e); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + LogManager.logError(TAG, "关闭FileOutputStream失败", e); + } } } } @@ -120,22 +157,36 @@ public class RelayController { * @return GPIO值字符串,失败返回null */ public String readGpioValue(String padType) { - BufferedReader reader = null; - try { - String valuePath = PAD_TYPE_TWO.equals(padType) ? PWM_FLASH_2 : PWM_FLASH; - reader = new BufferedReader(new FileReader(valuePath)); - String value = reader.readLine(); - LogManager.logInfo(TAG, "读取GPIO值成功: " + value + ", 设备类型: " + padType); - return value; - } catch (IOException e) { - LogManager.logError(TAG, "读取GPIO值失败", e); - return null; - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - LogManager.logError(TAG, "关闭BufferedReader失败", e); + if (PAD_TYPE_SEVEN.equals(padType) && deviceType7Manager != null) { + // 第七批设备使用DeviceType7Manager + int value = deviceType7Manager.getGpioValue(DeviceType7Manager.YxGpioConstants.GPIO_RELAY); + if (value == -1) { + LogManager.logError(TAG, "DeviceType7Manager读取GPIO值失败"); + return null; + } else { + String strValue = String.valueOf(value); + LogManager.logInfo(TAG, "DeviceType7Manager读取GPIO值成功: " + strValue); + return strValue; + } + } else { + // 其他设备使用文件操作 + BufferedReader reader = null; + try { + String valuePath = PAD_TYPE_TWO.equals(padType) ? PWM_FLASH_2 : PWM_FLASH; + reader = new BufferedReader(new FileReader(valuePath)); + String value = reader.readLine(); + LogManager.logInfo(TAG, "读取GPIO值成功: " + value + ", 设备类型: " + padType); + return value; + } catch (IOException e) { + LogManager.logError(TAG, "读取GPIO值失败", e); + return null; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + LogManager.logError(TAG, "关闭BufferedReader失败", e); + } } } } @@ -148,35 +199,48 @@ public class RelayController { * @return 是否成功 */ private boolean writeGpioValue(byte[] value, String padType) { - FileOutputStream fos = null; - try { - String valuePath = PAD_TYPE_TWO.equals(padType) ? PWM_FLASH_2 : PWM_FLASH; - fos = new FileOutputStream(valuePath); - fos.write(value); - 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); - } + if (PAD_TYPE_SEVEN.equals(padType) && deviceType7Manager != null) { + // 第七批设备使用DeviceType7Manager + int gpioValue = (value[0] == '1') ? DeviceType7Manager.YxGpioConstants.GpioState.HIGH : DeviceType7Manager.YxGpioConstants.GpioState.LOW; + boolean success = deviceType7Manager.setGpioValue(DeviceType7Manager.YxGpioConstants.GPIO_RELAY, gpioValue); + if (success) { + LogManager.logInfo(TAG, "DeviceType7Manager写入GPIO值成功: " + gpioValue); } else { - LogManager.logError(TAG, "写入GPIO值时发生意外异常", e); + LogManager.logError(TAG, "DeviceType7Manager写入GPIO值失败"); } - return false; - } finally { - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - LogManager.logError(TAG, "关闭FileOutputStream失败", e); + 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); + 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); + } } } } @@ -313,11 +377,11 @@ public class RelayController { } /** - * 获取继电器当前状态(默认设备) - * @return true为开启状态,false为关闭状态,null为读取失败 + * 获取第七批设备管理器实例 + * @return DeviceType7Manager实例 */ - public Boolean getRelayStatus() { - return getRelayStatus(PAD_TYPE_DEFAULT); + public DeviceType7Manager getDeviceType7Manager() { + return deviceType7Manager; } /** @@ -327,6 +391,10 @@ public class RelayController { if (handler != null) { handler.removeCallbacksAndMessages(null); } + if (deviceType7Manager != null) { + deviceType7Manager.release(); + deviceType7Manager = null; + } LogManager.logInfo(TAG, "RelayController资源已释放"); } } \ No newline at end of file