Skip to content

Commit

Permalink
WIP enable byte array for request.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kreutzer committed May 27, 2020
1 parent 637e949 commit 698c10f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public Map<String, Object> setTime(Request timeRequest) throws StiebelHeatPumpEx
*/
public Map<String, Object> readData(Request request) throws StiebelHeatPumpException {
Map<String, Object> data = new HashMap<>();
String requestStr = String.format("%02X", request.getRequestByte());
String requestStr = DataParser.bytesToHex(request.getRequestByte(), false);
logger.debug("RequestByte -> {}", requestStr);
byte[] responseAvailable;
byte[] requestMessage = createRequestMessage(request.getRequestByte());
Expand Down Expand Up @@ -503,20 +503,36 @@ private byte[] receiveData() {
* message
* @return request message byte[]
*/
private byte[] createRequestMessage(byte requestByte) {
private byte[] createRequestMessage(byte[] requestytes) {
short checkSum;
byte[] requestMessage = new byte[] { DataParser.HEADERSTART, DataParser.GET, (byte) 0x00, requestByte,
DataParser.ESCAPE, DataParser.END };
byte[] requestMessage = concat(new byte[] { DataParser.HEADERSTART, DataParser.GET, (byte) 0x00 }, requestytes,
new byte[] { DataParser.ESCAPE, DataParser.END });
try {
// prepare request message
checkSum = parser.calculateChecksum(requestMessage);
requestMessage[2] = parser.shortToByte(checkSum)[0];
requestMessage = parser.addDuplicatedBytes(requestMessage);
} catch (StiebelHeatPumpException e) {
String requestStr = String.format("%02X", requestByte);
String requestStr = String.format("%02X", requestytes);
logger.error("Could not create request [{}] message !", requestStr, e.toString());
}
return requestMessage;
}

byte[] concat(byte[]... arrays) {
// Determine the length of the result array
int totalLength = 0;
for (int i = 0; i < arrays.length; i++) {
totalLength += arrays[i].length;
}
// create the result array
byte[] result = new byte[totalLength];
// copy the source arrays into the result array
int currentIndex = 0;
for (int i = 0; i < arrays.length; i++) {
System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length);
currentIndex += arrays[i].length;
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.stiebelheatpump.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.bind.DatatypeConverter;
Expand Down Expand Up @@ -72,7 +73,7 @@ public Requests getRequests() {
Requests requests = new Requests();

for (Record record : records) {
byte requestByte = DatatypeConverter.parseHexBinary(record.getRequestByte())[0];
byte[] requestByte = DatatypeConverter.parseHexBinary(record.getRequestByte());

RecordDefinition recordDefinition = new RecordDefinition();
recordDefinition.setChannelid(record.getChannelid());
Expand Down Expand Up @@ -101,7 +102,7 @@ public Requests getRequests() {

boolean found = false;
for (Request request : requests.getRequests()) {
if (request.getRequestByte() == requestByte) {
if (Arrays.equals(request.getRequestByte(), requestByte)) {
request.getRecordDefinitions().add(recordDefinition);
found = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class StiebelHeatPumpBindingConstants {
// public static final String CHANNELGROUP_VERSION = "channelGroupTypeVersion";
public static final String CHANNELID_VERSION = "version";
public static final String CHANNEL_VERSION = "version" + CHANNELGROUPSEPERATOR + CHANNELID_VERSION;
public static final byte REQUEST_VERSION = (byte) 0xFD;
public static final byte REQUEST_TIME = (byte) 0xFC;
public static final byte[] REQUEST_VERSION = { (byte) 0xFD };
public static final byte[] REQUEST_TIME = { (byte) 0xFC };
public static final String CHANNELID_TIME = "time";
public static final String CHANNEL_SETTIME = "time" + CHANNELGROUPSEPERATOR + "setTime";
public static final String CHANNEL_DUMPRESPONSE = "version" + CHANNELGROUPSEPERATOR + "dumpResponse";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.io.transport.serial.SerialPortIdentifier;
import org.eclipse.smarthome.io.transport.serial.SerialPortManager;
import org.openhab.binding.stiebelheatpump.protocol.DataParser;
import org.openhab.binding.stiebelheatpump.protocol.RecordDefinition;
import org.openhab.binding.stiebelheatpump.protocol.RecordDefinition.Type;
import org.openhab.binding.stiebelheatpump.protocol.Request;
Expand Down Expand Up @@ -128,12 +130,13 @@ public void handleCommand(ChannelUID channelUID, Command command) {
break;
case CHANNEL_DUMPRESPONSE:
for (byte requestByte : DEBUGBYTES) {
Request request = heatPumpConfiguration.getRequestByByte(requestByte);
byte[] debugBytes = new byte[] { requestByte };
Request request = heatPumpConfiguration.getRequestByByte(debugBytes);
if (request == null) {
String requestStr = String.format("%02X", requestByte);
String requestStr = DataParser.bytesToHex(debugBytes);
logger.debug("Could not find request for {} in the thingtype definition.", requestStr);
request = new Request();
request.setRequestByte(requestByte);
request.setRequestByte(debugBytes);
}
communicationService.dumpResponse(request);
Thread.sleep(config.waitingTime);
Expand Down Expand Up @@ -195,7 +198,7 @@ public void channelLinked(ChannelUID channelUID) {
logger.debug("Could not find valid record definitionrequest in channel for: {}", channelId);
return;
}
String requestStr = String.format("%02X", request.getRequestByte());
String requestStr = DataParser.bytesToHex(request.getRequestByte(), true);
logger.debug("Found valid record definition in request {} with ChannelID:{}", requestStr, channelId);
RecordDefinition record = request.getRecordDefinitionByChannelId(channelId);
if (record == null) {
Expand Down Expand Up @@ -632,16 +635,16 @@ private void updateDateChannel(String dateString, ChannelUID channelUID) {
*/
private boolean categorizeHeatPumpConfiguration() {
for (Request request : heatPumpConfiguration.getRequests()) {
String requestByte = String.format("%02X", request.getRequestByte());
logger.debug("Request : RequestByte -> {}", requestByte);
String requestStr = DataParser.bytesToHex(request.getRequestByte());
logger.debug("Request : RequestByte -> {}", requestStr);

if (request.getRequestByte() == REQUEST_VERSION) {
if (Arrays.equals(request.getRequestByte(), REQUEST_VERSION)) {
versionRequest = request;
logger.debug("set version request : {}", requestByte);
logger.debug("set version request : {}", requestStr);
}
if (timeRequest == null && request.getRequestByte() == REQUEST_TIME) {
if (timeRequest == null && Arrays.equals(request.getRequestByte(), REQUEST_TIME)) {
timeRequest = request;
logger.debug("set time request : {}", requestByte);
logger.debug("set time request : {}", requestStr);
}

// group requests in different categories by investigating data type in first record
Expand Down Expand Up @@ -681,7 +684,7 @@ private void updateRefreshRequests() {
String channelId = parts[parts.length - 1];
Request request = heatPumpConfiguration.getRequestByChannelId(channelId);
if (request != null) {
String requestbyte = String.format("%02X", request.getRequestByte());
String requestStr = DataParser.bytesToHex(request.getRequestByte());
RecordDefinition record = request.getRecordDefinitionByChannelId(channelId);
if (record == null) {
logger.warn("Could not find valid record definition for {}, please verify thing definition.",
Expand All @@ -691,12 +694,12 @@ private void updateRefreshRequests() {
record.setChannelid(channelUID.getId());
if (record.getDataType() == Type.Settings && !heatPumpSettingRefresh.getRequests().contains(request)) {
heatPumpSettingRefresh.getRequests().add(request);
logger.info("Request {} added to setting refresh scheduler.", requestbyte);
logger.info("Request {} added to setting refresh scheduler.", requestStr);
}
if (record.getDataType() != Type.Settings
&& !heatPumpSensorStatusRefresh.getRequests().contains(request)) {
heatPumpSensorStatusRefresh.getRequests().add(request);
logger.info("Request {} added to sensor/status refresh scheduler.", requestbyte);
logger.info("Request {} added to sensor/status refresh scheduler.", requestStr);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public DataParser() {
public Map<String, Object> parseRecords(final byte[] response, Request request) throws StiebelHeatPumpException {

Map<String, Object> map = new HashMap<>();
String bytes = bytesToHex(response);
String bytes = bytesToHex(response, true);
logger.debug("Parse bytes: {}", bytes);

if (response.length < 2) {
Expand Down Expand Up @@ -105,7 +105,7 @@ public Map<String, Object> parseRecords(final byte[] response, Request request)
* @throws StiebelHeatPumpException
*/
public Object parseRecord(byte[] response, RecordDefinition recordDefinition) throws StiebelHeatPumpException {
String responseStr = bytesToHex(response);
String responseStr = bytesToHex(response, true);
try {
if (response.length < 2) {
logger.error("response does not have a valid length of bytes: {}", responseStr);
Expand Down Expand Up @@ -248,7 +248,7 @@ public boolean dataAvailable(byte[] response) throws StiebelHeatPumpException {
throw new StiebelHeatPumpException("invalid response length on request of data " + bytesToHex(response));
}
if (response[0] != ESCAPE) {
throw new StiebelHeatPumpException("invalid response on request of data " + bytesToHex(response));
throw new StiebelHeatPumpException("invalid response on request of data " + bytesToHex(response, true));
}
if (response.length == 2 && response[1] == DATAAVAILABLE[1]) {
return true;
Expand All @@ -266,21 +266,22 @@ public boolean dataAvailable(byte[] response) throws StiebelHeatPumpException {
public void verifyHeader(byte[] response) throws StiebelHeatPumpException {

if (response.length < 4) {
throw new StiebelHeatPumpException("invalide response length on request of data " + bytesToHex(response));
throw new StiebelHeatPumpException(
"invalide response length on request of data " + bytesToHex(response, true));
}

if (response[0] != HEADERSTART) {
throw new StiebelHeatPumpException(
"invalid response on request of data, found no header start: " + bytesToHex(response));
"invalid response on request of data, found no header start: " + bytesToHex(response, true));
}

if (response[1] != GET & response[1] != SET) {
throw new StiebelHeatPumpException(
"invalid response on request of data, response is neither get nor set: " + bytesToHex(response));
throw new StiebelHeatPumpException("invalid response on request of data, response is neither get nor set: "
+ bytesToHex(response, true));
}

if (response[2] != calculateChecksum(response)) {
throw new StiebelHeatPumpException("invalid checksum on request of data " + bytesToHex(response));
throw new StiebelHeatPumpException("invalid checksum on request of data " + bytesToHex(response, true));
}
}

Expand Down Expand Up @@ -583,13 +584,13 @@ private byte[] setBit(byte[] data, int position, boolean value) {
* to be converted
* @return string representing the bytes
*/
public static String bytesToHex(byte[] bytes) {
public static String bytesToHex(byte[] bytes, boolean format) {
int dwords = bytes.length / 4 + 1;
char[] hexChars = new char[bytes.length * 3 + dwords * 4];
int position = 0;
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
if (j % 4 == 0) {
if (format && j % 4 == 0) {
String str = "(" + String.format("%02d", j) + ")";
char[] charArray = str.toCharArray();
for (char character : charArray) {
Expand All @@ -606,4 +607,9 @@ public static String bytesToHex(byte[] bytes) {
}
return new String(hexChars);
}

public static String bytesToHex(byte[] bytes) {
return bytesToHex(bytes, false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static enum Type {

private String channelid;

private byte requestByte;
private byte[] requestByte;

private Type dataType;

Expand Down Expand Up @@ -67,7 +67,7 @@ public RecordDefinition() {
* @param unit
* of the value
*/
public RecordDefinition(String channelid, byte requestByte, int position, int lenght, double scale, Type dataType,
public RecordDefinition(String channelid, byte[] requestByte, int position, int lenght, double scale, Type dataType,
String unit) {
this.channelid = channelid;
this.requestByte = requestByte;
Expand Down Expand Up @@ -103,7 +103,7 @@ public RecordDefinition(String channelid, byte requestByte, int position, int le
* @param unit
* of the value
*/
public RecordDefinition(String channelid, byte requestByte, int position, int lenght, double scale, Type dataType,
public RecordDefinition(String channelid, byte[] requestByte, int position, int lenght, double scale, Type dataType,
int min, int max, double step, int bitPosition, String unit) {
this.channelid = channelid;
this.requestByte = requestByte;
Expand Down Expand Up @@ -140,7 +140,7 @@ public RecordDefinition(String channelid, byte requestByte, int position, int le
* @param unit
* of the value
*/
public RecordDefinition(String channelid, byte requestByte, int position, int lenght, double scale, Type dataType,
public RecordDefinition(String channelid, byte[] requestByte, int position, int lenght, double scale, Type dataType,
int min, int max, double step, String unit) {
this.channelid = channelid;
this.requestByte = requestByte;
Expand All @@ -162,11 +162,11 @@ public void setChannelid(String channelid) {
this.channelid = channelid;
}

public byte getRequestByte() {
public byte[] getRequestByte() {
return requestByte;
}

public void setRequestByte(byte requestByte) {
public void setRequestByte(byte[] requestByte) {
this.requestByte = requestByte;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public class Request {
private Logger logger = LoggerFactory.getLogger(Request.class);
private String name;
private String description;
private Byte requestByte;
private byte[] requestByte;
private List<RecordDefinition> recordDefinitionList;

public Request() {
this.recordDefinitionList = new ArrayList<>();
}

public Request(String name, String description, byte requestByte) {
public Request(String name, String description, byte[] requestByte) {
this.name = name;
this.description = description;
this.requestByte = requestByte;
Expand All @@ -58,11 +58,11 @@ public void setDescription(String description) {
this.description = description;
}

public byte getRequestByte() {
public byte[] getRequestByte() {
return requestByte;
}

public void setRequestByte(byte requestByte) {
public void setRequestByte(byte[] requestByte) {
this.requestByte = requestByte;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public RecordDefinition getRecordDefinitionByChannelId(String channelId) {
return null;
}

public Request getRequestByByte(byte requestByte) {
public Request getRequestByByte(byte[] requestByte) {
for (Request request : requestList) {
if (request.getRequestByte() == requestByte) {
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void reset() {
@Override
public void write(byte[] data) throws StiebelHeatPumpException {
try {
String dataStr = DataParser.bytesToHex(data);
String dataStr = DataParser.bytesToHex(data, true);
logger.debug("Send request message : {}", dataStr);
out.write(data);
out.flush();
Expand Down

0 comments on commit 698c10f

Please sign in to comment.