refactor(ft546): register shuttle runtimes from configuration

This commit is contained in:
zgw
2026-08-11 11:58:53 +08:00
parent 11a21e0688
commit 46302c0de1
6 changed files with 263 additions and 50 deletions

View File

@@ -0,0 +1,101 @@
package com.ruoyi.project.ft546.device;
import com.ruoyi.common.domain.PlcConnectInfo;
import com.ruoyi.device.srgv.domain.DeviceSrgv;
import com.ruoyi.project.ft546.config.Ft546DeviceProfile;
import com.ruoyi.project.ft546.device.shuttle.Ft546SrgvRuntimeFactory;
import com.ruoyi.schedule.plcRead.srgv.PlcSrgvRead;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
class Ft546DeviceRuntimeRegistryTest {
@Test
void registersAnyConfiguredNumberOfShuttleRuntimes() {
Ft546DeviceRuntimeRegistry registry = new Ft546DeviceRuntimeRegistry();
PlcSrgvRead first = runtime("1");
PlcSrgvRead second = runtime("2");
PlcSrgvRead ninth = runtime("9");
registry.register("1", first);
registry.register("2", second);
registry.register("9", ninth);
assertEquals(3, registry.allSrgv().size());
assertSame(ninth, registry.requireSrgv("9"));
}
@Test
void duplicateDeviceCannotSilentlyReplaceAnActiveRuntime() {
Ft546DeviceRuntimeRegistry registry = new Ft546DeviceRuntimeRegistry();
registry.register("1", runtime("1"));
assertThrows(IllegalStateException.class,
() -> registry.register("1", runtime("1")));
}
@Test
void factoryRejectsAProfileForAnotherDevice() {
DeviceSrgv device = device("1");
Ft546DeviceProfile profile = new Ft546DeviceProfile();
profile.setDeviceId("2");
profile.setDeviceType("SHUTTLE");
profile.setAdapterType("FT546_LEGACY_SRGV");
assertThrows(IllegalArgumentException.class,
() -> new Ft546SrgvRuntimeFactory().create(device, profile));
}
@Test
void schedulingAndLegacySendPathsDoNotSelectThreeFixedReaders()
throws Exception {
String schedule = readSource(
"com/ruoyi/schedule/ScheduleThread.java");
String legacy = readSource("com/ruoyi/schedule/scheduleTask/srgv/"
+ "BaseSRgvThread.java");
assertFalse(schedule.contains("plcSrgvReadCustomThread1"));
assertFalse(schedule.contains("case 1:"));
assertFalse(legacy.contains("plcSrgvReadCustomThread1"));
assertFalse(legacy.contains("switch (deviceId)"));
}
private static PlcSrgvRead runtime(String id) {
DeviceSrgv device = device(id);
return new PlcSrgvRead(device,
new PlcConnectInfo(device.getIp(), device.getPort()));
}
private static DeviceSrgv device(String id) {
DeviceSrgv device = new DeviceSrgv();
device.setDeviceId(Integer.valueOf(id));
device.setIp("127.0.0." + id);
device.setPort(502);
return device;
}
private static String readSource(String relative) throws Exception {
Path current = Paths.get(System.getProperty(
"maven.multiModuleProjectDirectory", "."))
.toAbsolutePath().normalize();
while (current != null) {
Path source = current.resolve(Paths.get("ruoyi-admin", "src",
"main", "java", relative));
if (Files.isRegularFile(source)) {
return new String(Files.readAllBytes(source),
StandardCharsets.UTF_8);
}
current = current.getParent();
}
throw new java.nio.file.NoSuchFileException(relative);
}
}