Browse Source

调整初始化链接逻辑

master
白凤吉 2 months ago
parent
commit
894f2fdadc
  1. 46
      src/main/java/com/iflytop/sgs/app/service/device/DeviceInitService.java
  2. 10
      src/main/java/com/iflytop/sgs/hardware/comm/can/A8kCanBusConnection.java
  3. 200
      src/main/java/com/iflytop/sgs/hardware/service/setup/A8kSubModuleRegInitService.java
  4. 3
      src/main/java/com/iflytop/sgs/hardware/type/error/A8kEcode.java

46
src/main/java/com/iflytop/sgs/app/service/device/DeviceInitService.java

@ -16,6 +16,7 @@ import com.iflytop.sgs.common.enums.HeatModuleCode;
import com.iflytop.sgs.common.enums.cmd.CmdColor;
import com.iflytop.sgs.common.service.CanBusService;
import com.iflytop.sgs.hardware.exception.HardwareException;
import com.iflytop.sgs.hardware.service.setup.A8kSubModuleRegInitService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -38,26 +39,34 @@ public class DeviceInitService {
private final ObjectProvider<HeatModuleState> heatModuleStateProvider;
private final ObjectProvider<SolutionContainerState> solutionContainerStateProvider;
private final DeviceLightSoundService deviceLightSoundService;
private final A8kSubModuleRegInitService a8kSubModuleRegInitService;
@PostConstruct
public void init() {
public void init() throws InterruptedException {
while (!a8kSubModuleRegInitService.getIsInited()) {
Thread.sleep(100);
}
new Thread(() -> {
try {
Thread.sleep(2000);
CompletableFuture.runAsync(() -> {
DeviceCommandBundle deviceCommandBundle = DeviceCommandGenerator.tricolorLightOpen(CmdColor.blue);
deviceCommandService.sendCommand(deviceCommandBundle);
try {
deviceLightSoundService.openColor(CmdColor.blue);
} catch (HardwareException e) {
log.error("设备初始化灯光失败,CmdColor.blue");
}
});
initDeviceState();
initDeviceSetData();
canBusService.initOvertime();
initEnable();
initSensorState();
try {
deviceLightSoundService.openColor(CmdColor.green);
} catch (HardwareException e) {
log.error("设备初始化灯光失败,Color.GREEN");
}
CompletableFuture.runAsync(() -> {
try {
deviceLightSoundService.openColor(CmdColor.green);
} catch (HardwareException e) {
log.error("设备初始化灯光失败,CmdColor.green");
}
});
deviceStateService.getDeviceState().setInitComplete(true);
} catch (Exception e) {
log.error("设备初始化失败", e);
@ -73,7 +82,6 @@ public class DeviceInitService {
if (deviceStateService.getDeviceState().isVirtual() || deviceStateService.getDeviceState().isInitComplete()) {
return;
}
//从数据库中读取数据通过串口进行数据初始化
List<DeviceParamConfig> deviceParamConfigs = deviceParamConfigService.list();
for (DeviceParamConfig deviceParamConfig : deviceParamConfigs) {
DeviceInitializationData data = new DeviceInitializationData();
@ -81,18 +89,20 @@ public class DeviceInitService {
data.setMid(deviceParamConfig.getMid());
data.setRegIndex(deviceParamConfig.getRegIndex());
data.setRegInitVal(deviceParamConfig.getRegVal());
try {
canBusService.moduleSetRegByApp(data.getMid(), data.getRegIndex(), data.getRegInitVal());
} catch (Exception e) {
log.error("设备初始化写入参数失败,2秒后重试", e);
Thread.sleep(2000);
canBusService.moduleSetRegByApp(data.getMid(), data.getRegIndex(), data.getRegInitVal());
//initDeviceSetData();
boolean success = false; // 标记是否执行成功
while (!success) {
try {
canBusService.moduleSetRegByApp(data.getMid(), data.getRegIndex(), data.getRegInitVal());
success = true;
} catch (Exception e) {
log.error("设备初始化写入参数失败,错误: {}", e.getMessage());
Thread.sleep(2000);
}
}
}
}
/**
* 初始化所有设备使能
*/

10
src/main/java/com/iflytop/sgs/hardware/comm/can/A8kCanBusConnection.java

@ -183,7 +183,7 @@ public class A8kCanBusConnection extends WebSocketClient {
//
// PRIVATE
//
@Scheduled(fixedRate = 10000)
@Scheduled(fixedRate = 2000)
private void autoConnect() {
if (!enableCanBus)
return;
@ -191,10 +191,18 @@ public class A8kCanBusConnection extends WebSocketClient {
if (!isOpen()) {
if (getReadyState().equals(ReadyState.NOT_YET_CONNECTED)) {
try {
log.warn("canbus connect ...");
connect();
log.warn("canbus connect success");
} catch (IllegalStateException ignored) {
log.warn("a8k canbus connect failed, maybe already connected");
} catch (Exception e) {
log.error("a8k canbus connect error", e);
// 连接失败等待重试
return;
}
} else if (getReadyState().equals(ReadyState.CLOSED)) {
log.warn("a8k canbus connection closed, try to reconnect");
reconnect();
}
}

200
src/main/java/com/iflytop/sgs/hardware/service/setup/A8kSubModuleRegInitService.java

@ -19,118 +19,118 @@ import org.springframework.stereotype.Component;
import java.util.List;
/**
* 子设备寄存器初始化服务
* 子设备寄存器初始化服务
*/
@Component
@Slf4j
@RequiredArgsConstructor
public class A8kSubModuleRegInitService {
private final A8kCanBusService canBus;
private final ModuleEnableCtrlDriver moduleEnableCtrlDriver;
private final AppEventBusService eventBus;
private final A8kSubModuleInitRegConfig a8kSubModuleInitRegConfig;
private final SubModuleRegInitialValueMgrService subModuleRegInitialValueMgrService;
private Boolean isInited = false;
@PostConstruct
void init() {
eventBus.regListener(this::onAppEvent);
}
public synchronized boolean getIsInited() {
return isInited;
}
private void onAppEvent(AppEvent event) {
if (event instanceof A8kCanBusOnConnectEvent) {
var initThread = new Thread(this::initModuleRegVal);
initThread.setName("initModuleRegVal");
initThread.start();
}
}
private void initModuleRegVal() {
do {
private final A8kCanBusService canBus;
private final ModuleEnableCtrlDriver moduleEnableCtrlDriver;
private final AppEventBusService eventBus;
private final A8kSubModuleInitRegConfig a8kSubModuleInitRegConfig;
private final SubModuleRegInitialValueMgrService subModuleRegInitialValueMgrService;
private Boolean isInited = false;
@PostConstruct
void init() {
eventBus.regListener(this::onAppEvent);
}
public synchronized boolean getIsInited() {
return isInited;
}
private void onAppEvent(AppEvent event) {
if (event instanceof A8kCanBusOnConnectEvent) {
var initThread = new Thread(this::initModuleRegVal);
initThread.setName("initModuleRegVal");
initThread.start();
}
}
private void initModuleRegVal() {
do {
// try {
log.info("forceInitA8kModParams");
dumpAllSubBoardVersion();
log.info("forceInitA8kModParams");
dumpAllSubBoardVersion();
// initA8kModParams();
isInited = true;
isInited = true;
log.info("======================================================");
log.info("= init hardware param success...... =");
log.info("======================================================");
break;
log.info("======================================================");
log.info("= init hardware param success...... =");
log.info("======================================================");
break;
// } catch (HardwareException e) {
// log.error("init hardware param fail......, try init it after 5s", e);
// }
} while (false);
}
private void dumpAllSubBoardVersion() {
log.info("=======================================================");
log.info("= dump all sub board version...... =");
log.info("=");
boolean hasDiffVersion = false;
Integer maxVersion = null;
for (MId mid : MId.values()) {
if (mid == MId.NotSet) {
continue;
}
try {
if (!canBus.ping(mid)) {
log.error(String.format("+ %-40s(%d): offline", mid, mid.index));
continue;
}
Integer ver = canBus.moduleReadVersion(mid);
String moduleType = canBus.moduleReadType(mid).toString();
if (maxVersion != null && !maxVersion.equals(ver)) {
hasDiffVersion = true;
}
if (maxVersion == null || ver > maxVersion) {
maxVersion = ver;
}
log.info(String.format("+ %-40s(%d): type:%-20s version:%s", mid, mid.index, moduleType, ver));
} catch (HardwareException e) {
log.error(String.format("+ %20s(%d): offline", mid, mid.index));
}
}
if (!hasDiffVersion) {
// gStateMgrService.setMcuVersion(String.format("V%04d", maxVersion));
} else {
//版本不一致标记为(*)表示需要升级
// gStateMgrService.setMcuVersion(String.format("V%04d(*)", maxVersion));
}
}
private void initA8kModParams() throws HardwareException {
for (MId mid : MId.values()) {
if (mid == MId.NotSet)
continue;
log.info("============== {}({}) ===============", mid, mid.index);
ModuleType moduleType = canBus.moduleReadType(mid);
List<RegIndex> regIndexes = a8kSubModuleInitRegConfig.findRegIndexByModuleType(moduleType);
for (RegIndex regIndex : regIndexes) {
SubModuleRegInitialValue val = subModuleRegInitialValueMgrService.findByIDAndRegIndex(mid, regIndex);
if (val == null)
continue;
log.info(String.format("= init %s(%d) %-45s: %d", mid, mid.index, regIndex, val.regInitVal));
canBus.moduleSetReg(mid, regIndex, val.regInitVal);
}
log.info("=");
}
}
} while (false);
}
private void dumpAllSubBoardVersion() {
log.info("=======================================================");
log.info("= dump all sub board version...... =");
log.info("=");
boolean hasDiffVersion = false;
Integer maxVersion = null;
for (MId mid : MId.values()) {
if (mid == MId.NotSet) {
continue;
}
try {
if (!canBus.ping(mid)) {
log.error(String.format("+ %-40s(%d): offline", mid, mid.index));
continue;
}
Integer ver = canBus.moduleReadVersion(mid);
String moduleType = canBus.moduleReadType(mid).toString();
if (maxVersion != null && !maxVersion.equals(ver)) {
hasDiffVersion = true;
}
if (maxVersion == null || ver > maxVersion) {
maxVersion = ver;
}
log.info(String.format("+ %-40s(%d): type:%-20s version:%s", mid, mid.index, moduleType, ver));
} catch (HardwareException e) {
log.error(String.format("+ %20s(%d): offline", mid, mid.index));
}
}
if (!hasDiffVersion) {
// gStateMgrService.setMcuVersion(String.format("V%04d", maxVersion));
} else {
//版本不一致标记为(*)表示需要升级
// gStateMgrService.setMcuVersion(String.format("V%04d(*)", maxVersion));
}
}
private void initA8kModParams() throws HardwareException {
for (MId mid : MId.values()) {
if (mid == MId.NotSet)
continue;
log.info("============== {}({}) ===============", mid, mid.index);
ModuleType moduleType = canBus.moduleReadType(mid);
List<RegIndex> regIndexes = a8kSubModuleInitRegConfig.findRegIndexByModuleType(moduleType);
for (RegIndex regIndex : regIndexes) {
SubModuleRegInitialValue val = subModuleRegInitialValueMgrService.findByIDAndRegIndex(mid, regIndex);
if (val == null)
continue;
log.info(String.format("= init %s(%d) %-45s: %d", mid, mid.index, regIndex, val.regInitVal));
canBus.moduleSetReg(mid, regIndex, val.regInitVal);
}
log.info("=");
}
}
}

3
src/main/java/com/iflytop/sgs/hardware/type/error/A8kEcode.java

@ -30,7 +30,8 @@ public enum A8kEcode {
SYS_EXCEPTION(18), //系统异常
DEVICE_NOT_INIT(19), //设备未初始化
APPE_PAUSE_OPERATION_NOT_SUPPORT_IN_ENGINEER_TASK(20), //
APPE_CONTINUE_OPERATION_NOT_SUPPORT_IN_ENGINEER_TASK(21), //
APPE_CONTINUE_OPERATION_NOT_SUPPORT_IN_ENGINEER_TASK(21),
WEBSOCKET_CONNECT_ERROR(22), //websocket连接错误//
//
// 参数错误

Loading…
Cancel
Save