Skip to content

Commit

Permalink
deprecation of getCommandParams, new getWhatParams. Fixes #30.
Browse files Browse the repository at this point in the history
  • Loading branch information
mvalla committed Mar 5, 2022
1 parent 435bd0e commit 4b60f43
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 74 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 32 additions & 4 deletions src/main/java/org/openwebnet4j/message/BaseOpenMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -420,16 +431,33 @@ 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();
}
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
Expand Down Expand Up @@ -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();
Expand Down
46 changes: 14 additions & 32 deletions src/main/java/org/openwebnet4j/message/CENPlusScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.HashMap;
import java.util.Map;

import org.openwebnet4j.OpenDeviceType;

/**
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -210,31 +196,28 @@ 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;
}
}

@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());
Expand All @@ -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;
Expand Down
48 changes: 16 additions & 32 deletions src/main/java/org/openwebnet4j/message/CENScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.HashMap;
import java.util.Map;

import org.openwebnet4j.OpenDeviceType;

/**
Expand Down Expand Up @@ -120,9 +121,6 @@ public static CENPressure fromValue(int i) {
return mapping.get(i);
}

// public Integer value() {
// return value;
// }
}

@Override
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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
Expand All @@ -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);
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/openwebnet4j/test/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
Expand Down

0 comments on commit 4b60f43

Please sign in to comment.