From 4b60f43651e9035ed4ab7b2f0b67e84bd5234953 Mon Sep 17 00:00:00 2001 From: MV Date: Fri, 4 Mar 2022 16:20:57 +0100 Subject: [PATCH] deprecation of getCommandParams, new getWhatParams. Fixes #30. --- CHANGELOG.md | 4 ++ .../openwebnet4j/message/BaseOpenMessage.java | 36 ++++++++++++-- .../openwebnet4j/message/CENPlusScenario.java | 46 ++++++------------ .../org/openwebnet4j/message/CENScenario.java | 48 +++++++------------ .../org/openwebnet4j/test/MessageTest.java | 12 ++--- 5 files changed, 72 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f232e2a91..933653a49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ ### Added - Support for Burglar Alarm (WHO=5) - checks to Where base class constructor +- getWhatParams + +### Changed +- DEPRECATED getCommandParams ## [0.7.1] - 2022-01-14 diff --git a/src/main/java/org/openwebnet4j/message/BaseOpenMessage.java b/src/main/java/org/openwebnet4j/message/BaseOpenMessage.java index 60f875bf2..069233d90 100644 --- a/src/main/java/org/openwebnet4j/message/BaseOpenMessage.java +++ b/src/main/java/org/openwebnet4j/message/BaseOpenMessage.java @@ -62,9 +62,13 @@ public abstract class BaseOpenMessage extends OpenMessage { private String[] dimValues = null; // list of dimension values VAL1...VALn in the frame // *#WHO*WHERE*DIM...*VAL1*...*VALn## + @Deprecated private int[] commandParams = null; // list of command parameters PAR1...PARn in the frame // *WHO*WHAT#PAR1...#PARn*WHERE## + private String[] whatParams = null; // list of What parameters PAR1...PARn in the frame + // *WHO*WHAT#PAR1...#PARn*WHERE## + protected BaseOpenMessage(String frame) { this.frameValue = frame; } @@ -331,16 +335,23 @@ private void parseWhat() throws FrameException { } else { isCommandTranslation = false; } - what = whatFromValue(Integer.parseInt(parts[partsIndex])); commandParams = new int[0]; + whatParams = new String[0]; + what = whatFromValue(Integer.parseInt(parts[partsIndex])); if (what == null) { throw new UnsupportedFrameException("Unsupported WHAT=" + whatStr); } - if (parts.length > 1) { // copy command parameters into commandParams + if (parts.length > 1) { // copy What parameters into whatParams commandParams = new int[parts.length - partsIndex - 1]; for (int i = 0; i < commandParams.length; i++) { commandParams[i] = Integer.parseInt(parts[i + partsIndex + 1]); } + + whatParams = new String[parts.length - partsIndex - 1]; + for (int i = 0; i < whatParams.length; i++) { + whatParams[i] = parts[i + partsIndex + 1]; + } + } } catch (NumberFormatException e) { throw new MalformedFrameException("Invalid integer format in WHAT=" + whatStr); @@ -420,9 +431,12 @@ public boolean isCommandTranslation() throws FrameException { * Returns message command parameters (*WHO*WHAT#Param1#Param2...#ParamN*...), or empty array if * no parameters are present * + * @deprecated Use {@link getWhatParams()} instead. + * * @return int[] of command parameters, or empty array if no parameters are present * @throws FrameException in case of error in frame */ + @Deprecated public int[] getCommandParams() throws FrameException { if (commandParams == null) { getWhat(); @@ -430,6 +444,20 @@ public int[] getCommandParams() throws FrameException { return commandParams; } + /** + * Returns What parameters from a command frame (*WHO*WHAT#Param1#Param2...#ParamN*...), or empty array if + * no parameters are present + * + * @return String[] of command parameters, or empty array if no parameters are present + * @throws FrameException in case of error in frame + */ + public String[] getWhatParams() throws FrameException { + if (whatParams == null) { + getWhat(); + } + return whatParams; + } + /** * Returns an array with DIM parameters PAR1..PARN (*#WHO*DIM#PAR1..#PARN*...##), or empty array * if no parameters are present @@ -494,8 +522,8 @@ public String toStringVerbose() { if (isCommandTranslation()) { verbose += "(translation)"; } - if (getCommandParams().length > 0) { - verbose += ",cmdParams=" + Arrays.toString(getCommandParams()); + if (getWhatParams().length > 0) { + verbose += ",whatParams=" + Arrays.toString(getWhatParams()); } if (getWhere() != null) { verbose += "," + getWhere(); diff --git a/src/main/java/org/openwebnet4j/message/CENPlusScenario.java b/src/main/java/org/openwebnet4j/message/CENPlusScenario.java index 5437a54d6..1805243be 100644 --- a/src/main/java/org/openwebnet4j/message/CENPlusScenario.java +++ b/src/main/java/org/openwebnet4j/message/CENPlusScenario.java @@ -18,6 +18,7 @@ import java.util.HashMap; import java.util.Map; + import org.openwebnet4j.OpenDeviceType; /** @@ -158,12 +159,7 @@ public boolean isOff() throws FrameException { */ public static CENPlusScenario virtualShortPressure(String where, int buttonNumber) { return new CENPlusScenario( - format( - FORMAT_REQUEST_PARAM_STR, - WHO, - WhatCENPlus.SHORT_PRESSURE.value, - buttonNumber, - where)); + format(FORMAT_REQUEST_PARAM_STR, WHO, WhatCENPlus.SHORT_PRESSURE.value, buttonNumber, where)); } /** @@ -175,12 +171,7 @@ public static CENPlusScenario virtualShortPressure(String where, int buttonNumbe */ public static CENPlusScenario virtualStartExtendedPressure(String where, int buttonNumber) { return new CENPlusScenario( - format( - FORMAT_REQUEST_PARAM_STR, - WHO, - WhatCENPlus.START_EXT_PRESSURE.value, - buttonNumber, - where)); + format(FORMAT_REQUEST_PARAM_STR, WHO, WhatCENPlus.START_EXT_PRESSURE.value, buttonNumber, where)); } /** @@ -192,12 +183,7 @@ public static CENPlusScenario virtualStartExtendedPressure(String where, int but */ public static CENPlusScenario virtualExtendedPressure(String where, int buttonNumber) { return new CENPlusScenario( - format( - FORMAT_REQUEST_PARAM_STR, - WHO, - WhatCENPlus.EXT_PRESSURE.value, - buttonNumber, - where)); + format(FORMAT_REQUEST_PARAM_STR, WHO, WhatCENPlus.EXT_PRESSURE.value, buttonNumber, where)); } /** @@ -210,22 +196,20 @@ public static CENPlusScenario virtualExtendedPressure(String where, int buttonNu */ public static CENPlusScenario virtualReleaseExtendedPressure(String where, int buttonNumber) { return new CENPlusScenario( - format( - FORMAT_REQUEST_PARAM_STR, - WHO, - WhatCENPlus.RELEASE_EXT_PRESSURE.value, - buttonNumber, - where)); + format(FORMAT_REQUEST_PARAM_STR, WHO, WhatCENPlus.RELEASE_EXT_PRESSURE.value, buttonNumber, where)); } @Override public Integer getButtonNumber() throws FrameException { - if (getWhat() == WhatCENPlus.OFF_IR_NO_DETECTION - || getWhat() == WhatCENPlus.ON_IR_DETECTION) { + if (getWhat() == WhatCENPlus.OFF_IR_NO_DETECTION || getWhat() == WhatCENPlus.ON_IR_DETECTION) { return null; } - if (getCommandParams() != null) { - return getCommandParams()[0]; + if (getWhatParams() != null) { + try { + return Integer.parseInt(getWhatParams()[0]); + } catch (NumberFormatException nfe) { + throw new FrameException("Frame has wrong WHAT params: " + frameValue); + } } else { return null; } @@ -233,8 +217,7 @@ public Integer getButtonNumber() throws FrameException { @Override public Pressure getButtonPressure() { - if (getWhat() == WhatCENPlus.OFF_IR_NO_DETECTION - || getWhat() == WhatCENPlus.ON_IR_DETECTION) { + if (getWhat() == WhatCENPlus.OFF_IR_NO_DETECTION || getWhat() == WhatCENPlus.ON_IR_DETECTION) { return null; } return CENPlusPressure.fromValue((WhatCENPlus) getWhat()); @@ -246,8 +229,7 @@ public Pressure getButtonPressure() { * @return boolean */ public boolean isDryContactIR() { - if (getWhat() != WhatCENPlus.OFF_IR_NO_DETECTION - && getWhat() != WhatCENPlus.ON_IR_DETECTION) { + if (getWhat() != WhatCENPlus.OFF_IR_NO_DETECTION && getWhat() != WhatCENPlus.ON_IR_DETECTION) { return false; } else { return true; diff --git a/src/main/java/org/openwebnet4j/message/CENScenario.java b/src/main/java/org/openwebnet4j/message/CENScenario.java index f100c2611..da204c09a 100644 --- a/src/main/java/org/openwebnet4j/message/CENScenario.java +++ b/src/main/java/org/openwebnet4j/message/CENScenario.java @@ -18,6 +18,7 @@ import java.util.HashMap; import java.util.Map; + import org.openwebnet4j.OpenDeviceType; /** @@ -120,9 +121,6 @@ public static CENPressure fromValue(int i) { return mapping.get(i); } - // public Integer value() { - // return value; - // } } @Override @@ -149,10 +147,8 @@ protected What whatFromValue(int i) { * @param buttonNumber button number * @return message */ - public static CENScenario virtualStartPressure(String where, int buttonNumber) - throws IllegalArgumentException { - return new CENScenario( - format(FORMAT_REQUEST_WHAT_STR, WHO, whatFromButton(buttonNumber), where)); + public static CENScenario virtualStartPressure(String where, int buttonNumber) throws IllegalArgumentException { + return new CENScenario(format(FORMAT_REQUEST_WHAT_STR, WHO, whatFromButton(buttonNumber), where)); } /** @@ -165,13 +161,8 @@ public static CENScenario virtualStartPressure(String where, int buttonNumber) */ public static CENScenario virtualReleaseShortPressure(String where, int buttonNumber) throws IllegalArgumentException { - return new CENScenario( - format( - FORMAT_REQUEST_PARAM_STR, - WHO, - whatFromButton(buttonNumber), - CENPressure.RELEASE_SHORT_PRESSURE.value, - where)); + return new CENScenario(format(FORMAT_REQUEST_PARAM_STR, WHO, whatFromButton(buttonNumber), + CENPressure.RELEASE_SHORT_PRESSURE.value, where)); } /** @@ -181,15 +172,9 @@ public static CENScenario virtualReleaseShortPressure(String where, int buttonNu * @param buttonNumber button number * @return message */ - public static CENScenario virtualExtendedPressure(String where, int buttonNumber) - throws IllegalArgumentException { - return new CENScenario( - format( - FORMAT_REQUEST_PARAM_STR, - WHO, - whatFromButton(buttonNumber), - CENPressure.EXTENDED_PRESSURE.value, - where)); + public static CENScenario virtualExtendedPressure(String where, int buttonNumber) throws IllegalArgumentException { + return new CENScenario(format(FORMAT_REQUEST_PARAM_STR, WHO, whatFromButton(buttonNumber), + CENPressure.EXTENDED_PRESSURE.value, where)); } /** @@ -202,13 +187,8 @@ public static CENScenario virtualExtendedPressure(String where, int buttonNumber */ public static CENScenario virtualReleaseExtendedPressure(String where, int buttonNumber) throws IllegalArgumentException { - return new CENScenario( - format( - FORMAT_REQUEST_PARAM_STR, - WHO, - whatFromButton(buttonNumber), - CENPressure.RELEASE_EXTENDED_PRESSURE.value, - where)); + return new CENScenario(format(FORMAT_REQUEST_PARAM_STR, WHO, whatFromButton(buttonNumber), + CENPressure.RELEASE_EXTENDED_PRESSURE.value, where)); } @Override @@ -218,10 +198,14 @@ public Integer getButtonNumber() throws FrameException { @Override public Pressure getButtonPressure() throws FrameException { - if (getCommandParams().length == 0) { + if (getWhatParams().length == 0) { return CENPressure.START_PRESSURE; } else { - return CENPressure.fromValue(getCommandParams()[0]); + try { + return CENPressure.fromValue(Integer.parseInt(getWhatParams()[0])); + } catch (NumberFormatException nfe) { + throw new FrameException("Frame has wrong WHAT params: " + frameValue); + } } } diff --git a/src/test/java/org/openwebnet4j/test/MessageTest.java b/src/test/java/org/openwebnet4j/test/MessageTest.java index bed255732..c7b52c23e 100644 --- a/src/test/java/org/openwebnet4j/test/MessageTest.java +++ b/src/test/java/org/openwebnet4j/test/MessageTest.java @@ -85,7 +85,7 @@ public void testLightingOn() { public void testLightingCommandTranslationAndParams() { Lighting lightMsg; try { - lightMsg = (Lighting) BaseOpenMessage.parse("*1*1000#1#1#2#3*0311#4#01##"); + lightMsg = (Lighting) BaseOpenMessage.parse("*1*1000#1#01#2#3*0311#4#01##"); assertNotNull(lightMsg); assertEquals(Who.LIGHTING, lightMsg.getWho()); assertTrue(lightMsg.isCommand()); @@ -95,11 +95,11 @@ public void testLightingCommandTranslationAndParams() { assertEquals(Lighting.WhatLighting.ON, lightMsg.getWhat()); assertTrue(lightMsg.isOn()); assertFalse(lightMsg.isOff()); - assertNotNull(lightMsg.getCommandParams()); - assertEquals(3, lightMsg.getCommandParams().length); - assertEquals(1, lightMsg.getCommandParams()[0]); - assertEquals(2, lightMsg.getCommandParams()[1]); - assertEquals(3, lightMsg.getCommandParams()[2]); + assertNotNull(lightMsg.getWhatParams()); + assertEquals(3, lightMsg.getWhatParams().length); + assertEquals("01", lightMsg.getWhatParams()[0]); + assertEquals("2", lightMsg.getWhatParams()[1]); + assertEquals("3", lightMsg.getWhatParams()[2]); System.out.println(lightMsg.toStringVerbose()); } catch (FrameException e) { Assertions.fail();