Browse Source

change device

dev
MTing 3 weeks ago
parent
commit
18d2c84244
  1. 58
      app/src/main/java/com/ouxuan/oxface/MainActivity.java
  2. 99
      app/src/main/java/com/ouxuan/oxface/device/RelayController.java

58
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.DeviceSelectDataManager;
import com.ouxuan.oxface.data.LoginDataManager; import com.ouxuan.oxface.data.LoginDataManager;
import com.ouxuan.oxface.device.DeviceUtils; 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.PadApiService;
import com.ouxuan.oxface.network.api.UserApiService; import com.ouxuan.oxface.network.api.UserApiService;
import com.ouxuan.oxface.network.callback.CompleteApiResponseCallback; import com.ouxuan.oxface.network.callback.CompleteApiResponseCallback;
@ -60,6 +61,7 @@ public class MainActivity extends AppCompatActivity {
private DeviceSelectDataManager deviceSelectDataManager; // 设备选择数据管理器 private DeviceSelectDataManager deviceSelectDataManager; // 设备选择数据管理器
private KeepAliveManager keepAliveManager; // 保持活跃管理器 private KeepAliveManager keepAliveManager; // 保持活跃管理器
private AutoStartManager autoStartManager; // 自启动管理器 private AutoStartManager autoStartManager; // 自启动管理器
private RelayController relayController; // 继电器控制器
private Dialog currentDialog; // 用于跟踪当前显示的Dialog防止WindowLeaked错误 private Dialog currentDialog; // 用于跟踪当前显示的Dialog防止WindowLeaked错误
// 添加设备ID和版本号TextView引用 // 添加设备ID和版本号TextView引用
@ -94,6 +96,9 @@ public class MainActivity extends AppCompatActivity {
// 初始化设备选择数据管理器 // 初始化设备选择数据管理器
deviceSelectDataManager = DeviceSelectDataManager.getInstance(this); deviceSelectDataManager = DeviceSelectDataManager.getInstance(this);
// 初始化继电器控制器
relayController = RelayController.getInstance();
// 初始化保持活跃管理器并启动用于24小时无人值守 // 初始化保持活跃管理器并启动用于24小时无人值守
keepAliveManager = KeepAliveManager.getInstance(this); keepAliveManager = KeepAliveManager.getInstance(this);
keepAliveManager.startKeepAlive(this); keepAliveManager.startKeepAlive(this);
@ -895,13 +900,10 @@ public class MainActivity extends AppCompatActivity {
", Hardware ID: " + hardwareId + ", Hardware ID: " + hardwareId +
", API Code: " + apiResponse.getCode()); ", API Code: " + apiResponse.getCode());
// 动态获取设备类型并切换相机配置
// 动态获取设备类型并统一初始化相关模块
int deviceType = deviceSelectDataManager.getDeviceType(); int deviceType = deviceSelectDataManager.getDeviceType();
Log.d(TAG, "选择设备后获取设备类型: " + deviceType); 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 @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();

99
app/src/main/java/com/ouxuan/oxface/device/RelayController.java

@ -32,6 +32,10 @@ public class RelayController {
private static final String PWM_FLASH_OUT_2 = "/sys/class/gpio/gpio123/direction"; 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"; 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操作命令 // GPIO操作命令
private static final byte[] OPEN_RELAY = {'1'}; private static final byte[] OPEN_RELAY = {'1'};
private static final byte[] CLOSE_RELAY = {'0'}; private static final byte[] CLOSE_RELAY = {'0'};
@ -52,6 +56,9 @@ public class RelayController {
// 第七批设备管理器 // 第七批设备管理器
private DeviceType7Manager deviceType7Manager; private DeviceType7Manager deviceType7Manager;
// 当前设备类型5=第6批6=第7批等
private int deviceType = -1;
private RelayController() { private RelayController() {
handler = new Handler(Looper.getMainLooper()); 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 设备类型 * @return 设备类型
*/ */
private String getCurrentPadType() { 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值 * 写入GPIO值
* @param value 要写入的值 * @param value 要写入的值
* @param padType 设备类型 * @param padType 设备类型
@ -211,38 +261,19 @@ public class RelayController {
return success; return success;
} else { } 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; 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 是否成功 * @return 是否成功
*/ */
public boolean openRelay() { public boolean openRelay() {
return openRelay(PAD_TYPE_DEFAULT);
return openRelay(getCurrentPadType());
} }
/** /**
@ -275,7 +306,7 @@ public class RelayController {
* @return 是否成功 * @return 是否成功
*/ */
public boolean closeRelay() { public boolean closeRelay() {
return closeRelay(PAD_TYPE_DEFAULT);
return closeRelay(getCurrentPadType());
} }
/** /**

Loading…
Cancel
Save