From d3e1feda5b538dffb63e0b225ddba7803ae21234 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 22 May 2017 16:31:24 +0300 Subject: [PATCH] some fixes and tests --- numass-control/control-room/build.gradle | 9 +++--- .../kotlin/inr/numass/control/ServerView.kt | 3 +- .../inr/numass/readvac/MeradatVacDevice.java | 32 +++++++++++++++---- .../vac/src/main/resources/config/devices.xml | 2 +- .../inr/numass/readvac/MeradatVacTest.groovy | 17 ++++++++++ .../inr/numass/server/NumassRootHandler.java | 3 +- .../java/inr/numass/server/TestServer.java | 3 +- 7 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 numass-control/vac/src/test/java/inr/numass/readvac/MeradatVacTest.groovy diff --git a/numass-control/control-room/build.gradle b/numass-control/control-room/build.gradle index e41363e2..d699b932 100644 --- a/numass-control/control-room/build.gradle +++ b/numass-control/control-room/build.gradle @@ -1,10 +1,7 @@ plugins{ id "org.jetbrains.kotlin.jvm" version '1.1.2' id "application" -} - -repositories { - mavenCentral() + id 'com.github.johnrengelman.shadow' version '2.0.0' } if (!hasProperty('mainClass')) { @@ -30,6 +27,8 @@ dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:1.1.2" } -apply plugin: 'kotlin' +shadowJar{ + mergeServiceFiles() +} diff --git a/numass-control/control-room/src/main/kotlin/inr/numass/control/ServerView.kt b/numass-control/control-room/src/main/kotlin/inr/numass/control/ServerView.kt index 44bf3056..dc22b486 100644 --- a/numass-control/control-room/src/main/kotlin/inr/numass/control/ServerView.kt +++ b/numass-control/control-room/src/main/kotlin/inr/numass/control/ServerView.kt @@ -4,6 +4,7 @@ import hep.dataforge.context.Context import hep.dataforge.exceptions.StorageException import hep.dataforge.meta.Meta import hep.dataforge.server.ServerManager +import hep.dataforge.server.storage.StorageServeUtils import hep.dataforge.storage.commons.StorageFactory import inr.numass.client.ClientUtils import javafx.beans.property.SimpleObjectProperty @@ -77,7 +78,7 @@ class ServerView() : View("Numass server controller") { } } - serverManager.addStorage("numass", storage); + StorageServeUtils.addStorage(serverManager,storage,"numass-storage") } } } diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/MeradatVacDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/MeradatVacDevice.java index 0f7cd980..ff3e8b04 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/MeradatVacDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/MeradatVacDevice.java @@ -15,6 +15,7 @@ import hep.dataforge.exceptions.ControlException; import hep.dataforge.meta.Meta; import java.math.BigDecimal; +import java.math.BigInteger; import java.math.RoundingMode; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,6 +25,7 @@ import java.util.regex.Pattern; */ @ValueDef(name = "address", type = "NUMBER", def = "1", info = "A modbus address") public class MeradatVacDevice extends PortSensor { + private static final String REQUEST = "0300000002"; public MeradatVacDevice() { } @@ -42,7 +44,7 @@ public class MeradatVacDevice extends PortSensor { @Override protected Measurement createMeasurement() { - return new MeradatMeasurement(meta().getInt("address", 1)); + return new MeradatMeasurement(); } @Override @@ -50,6 +52,24 @@ public class MeradatVacDevice extends PortSensor { return meta().getString("type", "Vit vacuumeter"); } + public static String calculateLRC(String inputString) { + /* + * String is Hex String, need to convert in ASCII. + */ + byte[] bytes = new BigInteger(inputString, 16).toByteArray(); + int checksum = 0; + for (byte aByte : bytes) { + checksum += aByte; + } + String val = Integer.toHexString(-checksum); + val = val.substring(val.length()-2).toUpperCase(); + if (val.length() < 2) { + val = "0" + val; + } + + return val; + } + private class MeradatMeasurement extends SimpleMeasurement { @@ -57,9 +77,10 @@ public class MeradatVacDevice extends PortSensor { private final Pattern response; private final String base; - public MeradatMeasurement(int address) { - base = String.format(":%02d", address); - query = base + "0300000002FA\r\n"; + public MeradatMeasurement() { + base = String.format(":%02d", meta().getInt("address", 1)); + String dataStr = base.substring(1) + REQUEST; + query = base + REQUEST + calculateLRC(dataStr) + "\r\n"; response = Pattern.compile(base + "0304(\\w{4})(\\w{4})..\r\n"); } @@ -86,8 +107,7 @@ public class MeradatVacDevice extends PortSensor { this.progressUpdate("OK"); updateState(CONNECTED_STATE, true); return res.doubleValue(); - } - else { + } else { this.progressUpdate("Wrong answer: " + answer); updateState(CONNECTED_STATE, false); return null; diff --git a/numass-control/vac/src/main/resources/config/devices.xml b/numass-control/vac/src/main/resources/config/devices.xml index c5c1677b..60afc049 100644 --- a/numass-control/vac/src/main/resources/config/devices.xml +++ b/numass-control/vac/src/main/resources/config/devices.xml @@ -7,6 +7,6 @@ - + diff --git a/numass-control/vac/src/test/java/inr/numass/readvac/MeradatVacTest.groovy b/numass-control/vac/src/test/java/inr/numass/readvac/MeradatVacTest.groovy new file mode 100644 index 00000000..9f255e6f --- /dev/null +++ b/numass-control/vac/src/test/java/inr/numass/readvac/MeradatVacTest.groovy @@ -0,0 +1,17 @@ +package inr.numass.readvac + +import spock.lang.Specification + +/** + * Created by darksnake on 22-May-17. + */ +class MeradatVacTest extends Specification { + def "CalculateLRC"() { + given: + def str = "020300000002"; + when: + def lrc = MeradatVacDevice.calculateLRC(str).toUpperCase(); + then: + lrc == "F9" + } +} diff --git a/numass-server/src/main/java/inr/numass/server/NumassRootHandler.java b/numass-server/src/main/java/inr/numass/server/NumassRootHandler.java index 9d6e8224..f8a6abaf 100644 --- a/numass-server/src/main/java/inr/numass/server/NumassRootHandler.java +++ b/numass-server/src/main/java/inr/numass/server/NumassRootHandler.java @@ -20,7 +20,6 @@ import java.io.StringWriter; import java.util.HashMap; import java.util.Map; -import static hep.dataforge.server.storage.StorageRenderer.renderStorage; import static inr.numass.server.HandlerUtils.renderStates; /** @@ -68,7 +67,7 @@ public class NumassRootHandler implements Handler { StringBuilder b = new StringBuilder(); Storage rootStorage = server.getRun().getStorage(); rootStorage.refresh(); - renderStorage(b,manager.resolveObject(rootStorage), rootStorage); + //renderStorage(b, manager.resolveObject(rootStorage), rootStorage); data.put("storageContent", b.toString()); } catch (Exception ex) { data.put("storageContent", ex.toString()); diff --git a/numass-server/src/main/java/inr/numass/server/TestServer.java b/numass-server/src/main/java/inr/numass/server/TestServer.java index 98f5b3fd..be14e0e9 100644 --- a/numass-server/src/main/java/inr/numass/server/TestServer.java +++ b/numass-server/src/main/java/inr/numass/server/TestServer.java @@ -8,6 +8,7 @@ package inr.numass.server; import hep.dataforge.context.Context; import hep.dataforge.context.Global; import hep.dataforge.server.ServerManager; +import hep.dataforge.server.storage.StorageServeUtils; import hep.dataforge.storage.commons.StorageManager; import hep.dataforge.storage.filestorage.FileStorageFactory; import inr.numass.storage.NumassStorage; @@ -35,7 +36,7 @@ public class TestServer { context.getLogger().info("Starting test numass storage servlet in '{}'", path); NumassStorage storage = new NumassStorage(context, FileStorageFactory.buildStorageMeta(path, true, true)); - serverManager.bind("numass", storage, NumassStorageHandler::new); + StorageServeUtils.addStorage(serverManager,storage,"numass-storage"); serverManager.startServer();