]> git.basschouten.com Git - openhab-addons.git/blob
34a1e0661870987ec81e9a9b1ec75b906b265dab
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.modbus.e3dc.internal.dto;
14
15 import java.nio.ByteBuffer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.modbus.e3dc.internal.modbus.Data;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.util.HexUtils;
22
23 /**
24  * The {@link InfoBlock} Data object for E3DC Info Block
25  *
26  * @author Bernd Weymann - Initial contribution
27  */
28 @NonNullByDefault
29 public class InfoBlock implements Data {
30     private static final StringType EMPTY = new StringType("NULL");
31     public StringType modbusId = EMPTY;
32     public StringType modbusVersion = EMPTY;
33     public DecimalType supportedRegisters = new DecimalType(-1);
34     public StringType manufacturer = EMPTY;
35     public StringType modelName = EMPTY;
36     public StringType serialNumber = EMPTY;
37     public StringType firmware = EMPTY;
38
39     /**
40      * For decoding see Modbus Register Mapping Chapter 3.1.1 page 14
41      *
42      * @param bArray - Modbus Registers as bytes from 40001 to 40067
43      */
44     public InfoBlock(byte[] bArray) {
45         // index handling to calculate the correct start index
46         ByteBuffer wrapper = ByteBuffer.wrap(bArray);
47
48         // first uint16 = 2 bytes - decode magic byte
49         byte[] magicBytes = new byte[2];
50         wrapper.get(magicBytes);
51         this.modbusId = new StringType(HexUtils.bytesToHex(magicBytes));
52         // first uint16 = 2 bytes - decode magic byte
53
54         // unit8 (Modbus Major Version) + uint8 Modbus minor Version
55         String modbusVersion = wrapper.get() + "." + wrapper.get();
56         this.modbusVersion = new StringType(modbusVersion);
57
58         // unit16 - supported registers
59         short supportedRegisters = wrapper.getShort();
60         this.supportedRegisters = new DecimalType(supportedRegisters);
61
62         byte[] buffer = new byte[32];
63         // 16 registers with uint16 = 32 bytes to decode a proper String
64         wrapper.get(buffer);
65         String manufacturer = DataConverter.getString(buffer);
66         this.manufacturer = new StringType(manufacturer);
67
68         // 16 registers with uint16 = 32 bytes to decode a proper String
69         wrapper.get(buffer);
70         String model = DataConverter.getString(buffer);
71         this.modelName = new StringType(model);
72
73         // 16 registers with uint16 = 32 bytes to decode a proper String
74         wrapper.get(buffer);
75         String serialNumber = DataConverter.getString(buffer);
76         this.serialNumber = new StringType(serialNumber);
77
78         // 16 registers with uint16 = 32 bytes to decode a proper String
79         wrapper.get(buffer);
80         String firmware = DataConverter.getString(buffer);
81         this.firmware = new StringType(firmware);
82     }
83 }