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