]> git.basschouten.com Git - openhab-addons.git/blob
52204c9df0e5854b26a1a27bacc784d64b94f868
[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.sunspec.internal.parser;
14
15 import java.nio.charset.StandardCharsets;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.modbus.sunspec.internal.SunSpecConstants;
19 import org.openhab.binding.modbus.sunspec.internal.dto.CommonModelBlock;
20 import org.openhab.core.io.transport.modbus.ModbusBitUtilities;
21 import org.openhab.core.io.transport.modbus.ModbusRegisterArray;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Parser for the Common Message block
27  *
28  * @author Nagy Attila Gabor - Initial contribution
29  *
30  */
31 @NonNullByDefault
32 public class CommonModelParser extends AbstractBaseParser implements SunspecParser<CommonModelBlock> {
33
34     /**
35      * Logger instance
36      */
37     private final Logger logger = LoggerFactory.getLogger(CommonModelParser.class);
38
39     @Override
40     public CommonModelBlock parse(ModbusRegisterArray raw) {
41
42         CommonModelBlock block = new CommonModelBlock();
43
44         block.sunSpecDID = extractUInt16(raw, 0, 0);
45
46         block.length = extractUInt16(raw, 1, raw.size() - SunSpecConstants.MODEL_HEADER_SIZE);
47
48         if (block.length + SunSpecConstants.MODEL_HEADER_SIZE != raw.size()) {
49             logger.warn("Short read on common block loding. Expected size: {}, got: {}",
50                     block.length + SunSpecConstants.MODEL_HEADER_SIZE, raw.size());
51             return block;
52         }
53
54         // parse manufacturer, model and version
55         block.manufacturer = ModbusBitUtilities.extractStringFromRegisters(raw, 2, 32, StandardCharsets.UTF_8);
56         block.model = ModbusBitUtilities.extractStringFromRegisters(raw, 18, 32, StandardCharsets.UTF_8);
57         block.version = ModbusBitUtilities.extractStringFromRegisters(raw, 42, 16, StandardCharsets.UTF_8);
58         block.serialNumber = ModbusBitUtilities.extractStringFromRegisters(raw, 50, 32, StandardCharsets.UTF_8);
59
60         block.deviceAddress = extractUInt16(raw, 66, 1);
61
62         return block;
63     }
64 }