2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nibeheatpump.internal.message;
15 import static org.junit.Assert.assertEquals;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.openhab.binding.nibeheatpump.internal.protocol.NibeHeatPumpProtocolContext;
25 import org.openhab.binding.nibeheatpump.internal.protocol.NibeHeatPumpProtocolDefaultContext;
26 import org.openhab.core.util.HexUtils;
29 * @author Pauli Anttila - Initial contribution
31 public class NibeHeatPumpProtocolTest {
33 int ackRequestCount = 0;
34 int nakRequestCount = 0;
35 int sendWriteMsgCount = 0;
36 int sendReadMsgCount = 0;
37 List<byte[]> receivedMsgs = null;
39 final NibeHeatPumpProtocolContext mockupContext = new NibeHeatPumpProtocolDefaultContext() {
41 public void sendAck() {
46 public void sendNak() {
51 public void msgReceived(byte[] data) {
52 receivedMsgs.add(Arrays.copyOf(data, data.length));
56 public void sendWriteMsg() {
61 public void sendReadMsg() {
67 public void Before() {
70 sendWriteMsgCount = 0;
72 receivedMsgs = new ArrayList<>();
73 mockupContext.buffer().clear();
74 mockupContext.msg().clear();
80 final String strTestData =
81 // RMU40 message, acknowledge should be send
82 "5C001962189600E1010200000000800000000000020914340001000005B8"
83 // RMU40 message, CRC failure, negative acknowledge should be send
84 + "5C001962189600E1010200000000800000000000020914340001000005B9"
85 // MODBUS40 write request
89 // MODBUS40 read request
93 // MODBUS40 data read out, acknowledge should be send
94 + "5C00206850449C9600489C88014C9C2D014E9CCF004D9CE0014F9C3200509C0400519C8201529C6B02569C3E00C9AF000001A8F600FDA77E02FAA90F0098A9DC27FFFF0000A0A93A04FFFF00009CA9FD19FFFF000081"
97 // MODBUS40 data read out, CRC failure, negative acknowledge should be send
98 + "5C00206850449C9600489C88014C9C2D014E9CCF004D9CE0014F9C3200509C0400519C8201529C6B02569C3E00C9AF000001A8F600FDA77E02FAA90F0098A9DC27FFFF0000A0A93A04FFFF00009CA9FD19FFFF000080"
99 // RMU40 message, acknowledge should be send
100 + "5C001962189600DF01020000000080000000000002091434000100000586"
103 // unknown RMU40 message, acknowledge should be send
105 // MODBUS40 data read out, special len, acknowledge should be send
106 + "5C00206851449C2500489CFC004C9CF1004E9CC7014D9C0B024F9C2500509C3300519C0B01529C5C5C01569C3100C9AF000001A80C01FDA716FAFAA9070098A91B1BFFFF0000A0A9CA02FFFF00009CA99212FFFF0000BE"
107 // MODBUS40 data read out, special len, acknowledge should be send
108 + "5C00206852449C2500489CFE004C9CF2004E9CD4014D9CFB014F9C2500509C3700519C0D01529C5C5C01569C3200C9AF000001A80C01FDA712FAFAA9070098A95C5C1BFFFF0000A0A9D102FFFF00009CA9B412FFFF00007F"
109 // MODBUS40 data read out, special checksum, acknowledge should be send
110 + "5C00206850449C2600489CF6004C9CF1004E9CD6014D9C0C024F9C4500509C3F00519CF100529C0401569CD500C9AF000001A80C01FDA799FAFAA9020098A91A1BFFFF0000A0A9CA02FFFF00009CA99212FFFF0000C5";
113 // create byte data from hex string
114 final byte[] rawData = HexUtils.hexToBytes(strTestData);
116 // put byte data to protocol state machine
117 mockupContext.buffer().put(rawData);
118 mockupContext.buffer().flip();
120 // run protocol state machine to process test data
121 while (mockupContext.state().process(mockupContext)) {
126 assertEquals(7, ackRequestCount);
127 assertEquals(2, nakRequestCount);
128 assertEquals(1, sendWriteMsgCount);
129 assertEquals(1, sendReadMsgCount);
130 assertEquals(7, receivedMsgs.size());
134 expect = "5C001962189600E1010200000000800000000000020914340001000005B8";
135 Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(0));
137 expect = "5C00206850449C9600489C88014C9C2D014E9CCF004D9CE0014F9C3200509C0400519C8201529C6B02569C3E00C9AF000001A8F600FDA77E02FAA90F0098A9DC27FFFF0000A0A93A04FFFF00009CA9FD19FFFF000081";
138 Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(1));
140 expect = "5C001962189600DF01020000000080000000000002091434000100000586";
141 Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(2));
143 expect = "5C0019600079";
144 Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(3));
146 expect = "5C00206851449C2500489CFC004C9CF1004E9CC7014D9C0B024F9C2500509C3300519C0B01529C5C5C01569C3100C9AF000001A80C01FDA716FAFAA9070098A91B1BFFFF0000A0A9CA02FFFF00009CA99212FFFF0000BE";
147 Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(4));
149 expect = "5C00206852449C2500489CFE004C9CF2004E9CD4014D9CFB014F9C2500509C3700519C0D01529C5C5C01569C3200C9AF000001A80C01FDA712FAFAA9070098A95C5C1BFFFF0000A0A9D102FFFF00009CA9B412FFFF00007F";
150 Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(5));
152 expect = "5C00206850449C2600489CF6004C9CF1004E9CD6014D9C0C024F9C4500509C3F00519CF100529C0401569CD500C9AF000001A80C01FDA799FAFAA9020098A91A1BFFFF0000A0A9CA02FFFF00009CA99212FFFF0000C5";
153 Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(6));