2 * Copyright (c) 2010-2023 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.modbus.sunspec.internal.parser;
15 import java.nio.charset.StandardCharsets;
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;
26 * Parser for the Common Message block
28 * @author Nagy Attila Gabor - Initial contribution
32 public class CommonModelParser extends AbstractBaseParser implements SunspecParser<CommonModelBlock> {
37 private final Logger logger = LoggerFactory.getLogger(CommonModelParser.class);
40 public CommonModelBlock parse(ModbusRegisterArray raw) {
41 CommonModelBlock block = new CommonModelBlock();
43 block.sunSpecDID = extractUInt16(raw, 0, 0);
45 block.length = extractUInt16(raw, 1, raw.size() - SunSpecConstants.MODEL_HEADER_SIZE);
47 if (block.length + SunSpecConstants.MODEL_HEADER_SIZE != raw.size()) {
48 logger.warn("Short read on common block loding. Expected size: {}, got: {}",
49 block.length + SunSpecConstants.MODEL_HEADER_SIZE, raw.size());
53 // parse manufacturer, model and version
54 block.manufacturer = ModbusBitUtilities.extractStringFromRegisters(raw, 2, 32, StandardCharsets.UTF_8);
55 block.model = ModbusBitUtilities.extractStringFromRegisters(raw, 18, 32, StandardCharsets.UTF_8);
56 block.version = ModbusBitUtilities.extractStringFromRegisters(raw, 42, 16, StandardCharsets.UTF_8);
57 block.serialNumber = ModbusBitUtilities.extractStringFromRegisters(raw, 50, 32, StandardCharsets.UTF_8);
59 block.deviceAddress = extractUInt16(raw, 66, 1);