3 changed files with 7 additions and 227 deletions
-
10src/main/java/com/iflytop/sgs/app/service/device/DeviceInitService.java
-
136src/main/java/com/iflytop/sgs/hardware/service/setup/A8kSubModuleRegInitService.java
-
86src/main/java/com/iflytop/sgs/hardware/service/setup/SubModuleRegInitialValueMgrService.java
@ -1,136 +0,0 @@ |
|||||
package com.iflytop.sgs.hardware.service.setup; |
|
||||
|
|
||||
import com.iflytop.sgs.hardware.comm.can.A8kCanBusService; |
|
||||
import com.iflytop.sgs.hardware.config.A8kSubModuleInitRegConfig; |
|
||||
import com.iflytop.sgs.hardware.drivers.ModuleEnableCtrlDriver; |
|
||||
import com.iflytop.sgs.hardware.exception.HardwareException; |
|
||||
import com.iflytop.sgs.hardware.service.AppEventBusService; |
|
||||
import com.iflytop.sgs.hardware.type.MId; |
|
||||
import com.iflytop.sgs.hardware.type.ModuleType; |
|
||||
import com.iflytop.sgs.hardware.type.RegIndex; |
|
||||
import com.iflytop.sgs.hardware.type.appevent.A8kCanBusOnConnectEvent; |
|
||||
import com.iflytop.sgs.hardware.type.appevent.AppEvent; |
|
||||
import com.iflytop.sgs.hardware.type.db.SubModuleRegInitialValue; |
|
||||
import jakarta.annotation.PostConstruct; |
|
||||
import lombok.RequiredArgsConstructor; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
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 { |
|
||||
// try { |
|
||||
log.info("forceInitA8kModParams"); |
|
||||
dumpAllSubBoardVersion(); |
|
||||
// initA8kModParams(); |
|
||||
isInited = true; |
|
||||
|
|
||||
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("="); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,86 +0,0 @@ |
|||||
package com.iflytop.sgs.hardware.service.setup; |
|
||||
|
|
||||
|
|
||||
import com.iflytop.sgs.hardware.comm.can.A8kCanBusService; |
|
||||
import com.iflytop.sgs.hardware.config.A8kSubModuleInitRegConfig; |
|
||||
import com.iflytop.sgs.hardware.dao.SubModuleRegInitialValueDao; |
|
||||
import com.iflytop.sgs.hardware.exception.HardwareException; |
|
||||
import com.iflytop.sgs.hardware.type.MId; |
|
||||
import com.iflytop.sgs.hardware.type.ModuleType; |
|
||||
import com.iflytop.sgs.hardware.type.RegIndex; |
|
||||
import com.iflytop.sgs.hardware.type.db.SubModuleRegInitialValue; |
|
||||
import lombok.RequiredArgsConstructor; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
|
|
||||
@Component |
|
||||
@Slf4j |
|
||||
@RequiredArgsConstructor |
|
||||
public class SubModuleRegInitialValueMgrService { |
|
||||
|
|
||||
private final SubModuleRegInitialValueDao subModuleRegInitialValueDao; |
|
||||
private final A8kCanBusService canBus; |
|
||||
private final A8kSubModuleInitRegConfig a8kSubModuleInitRegConfig; |
|
||||
|
|
||||
|
|
||||
/** |
|
||||
* 从微控制器同步寄存器值到数据库 |
|
||||
* @throws HardwareException e |
|
||||
*/ |
|
||||
public void syncFromMirco() throws HardwareException { |
|
||||
List<SubModuleRegInitialValue> regcache = List.of(); |
|
||||
for (MId mid : MId.values()) { |
|
||||
if (mid == MId.NotSet) |
|
||||
continue; |
|
||||
|
|
||||
ModuleType moduleType = canBus.moduleReadType(mid); |
|
||||
List<RegIndex> regIndexes = a8kSubModuleInitRegConfig.findRegIndexByModuleType(moduleType); |
|
||||
|
|
||||
for (RegIndex regIndex : regIndexes) { |
|
||||
log.info("read {}({})-{}", mid, mid.index, regIndex); |
|
||||
Integer val = canBus.moduleGetReg(mid, regIndex); |
|
||||
regcache.add(new SubModuleRegInitialValue(mid, regIndex, val)); |
|
||||
} |
|
||||
} |
|
||||
for (SubModuleRegInitialValue reg : regcache) { |
|
||||
log.info(String.format("storage %s(%d) %-40s: %d", reg.mid, reg.mid.index, reg.regIndex, reg.regInitVal)); |
|
||||
subModuleRegInitialValueDao.update(reg.mid, reg.regIndex, reg.regInitVal); |
|
||||
} |
|
||||
|
|
||||
// |
|
||||
// 删除掉不需要的寄存器 |
|
||||
// |
|
||||
var allConfigs = subModuleRegInitialValueDao.getAll(); |
|
||||
for (SubModuleRegInitialValue reg : allConfigs) { |
|
||||
//TODO:模块类型存储到数据库中,而不是从微控制器读取 |
|
||||
ModuleType moduleType = canBus.moduleReadType(reg.mid); |
|
||||
if (!a8kSubModuleInitRegConfig.isNeeded(moduleType, reg.regIndex)) { |
|
||||
subModuleRegInitialValueDao.delete(reg.id); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public void export() { |
|
||||
subModuleRegInitialValueDao.export(); |
|
||||
} |
|
||||
|
|
||||
public String getExportPath() { |
|
||||
return subModuleRegInitialValueDao.getExportPATH(); |
|
||||
} |
|
||||
|
|
||||
public void importFromCSV(String filecontent) { |
|
||||
subModuleRegInitialValueDao.importFromCSV(filecontent, true); |
|
||||
} |
|
||||
|
|
||||
public List<SubModuleRegInitialValue> getAll() { |
|
||||
return subModuleRegInitialValueDao.getAll(); |
|
||||
} |
|
||||
|
|
||||
public SubModuleRegInitialValue findByIDAndRegIndex(MId mid, RegIndex regIndex) { |
|
||||
return subModuleRegInitialValueDao.findByIDAndRegIndex(mid, regIndex); |
|
||||
} |
|
||||
|
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue