<commit_before>/**
 * Copyright (c) 2010-2016, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.binding.plcbus.internal.protocol;

import java.util.ArrayList;
import java.util.List;

/**
 * DataFrame of PLCBus Protocol
 *
 * @author <NAME>
 * @since 1.0.0
 */
public class DataFrame {

    private int userCode;
    private int home;
    private int unit;
    private CommandFrame commandFrame;

    /**
     * Create a new DataFrame
     */
    public DataFrame() {

    }

    /**
     * Creates a new DataFrame with CommandFrame
     * 
     * @param commandFrame
     *            for DataFrame
     */
    public DataFrame(CommandFrame commandFrame) {
        this.commandFrame = commandFrame;
    }

    /**
     * Sets the UserCode to DataFrame
     * 
     * @param userCode
     *            for DataFrame
     */
    public void setUserCode(String userCode) {
        this.userCode = Convert.toByte(userCode, 16);
    }

    /**
     * Sets address to DataFrame
     * 
     * @param address
     *            for DataFrame
     */
    public void SetAddress(String address) {

        this.home = getHomeFrom(address.charAt(0));
        this.unit = getUnitFrom(address.charAt(1));
    }

    private int getHomeFrom(char home) {
        switch (home) {
            case 'A':
                return 0;
            case 'B':
                return 1;
            case 'C':
                return 2;
            case 'D':
                return 3;
            case 'E':
                return 4;
            case 'F':
                return 5;
            case 'G':
                return 6;
            case 'H':
                return 7;
            case 'I':
                return 8;
            case 'J':
                return 9;
            case 'K':
                return 10;
        }

        return 0;
    }

    private int getUnitFrom(char unit) {
        switch (unit) {
            case '1':
                return 0;
            case '2':
                return 1;
            case '3':
                return 2;
            case '4':
                return 3;
            case '5':
                return 4;
            case '6':
                return 5;
            case '7':
                return 6;
            case '8':
                return 7;
            case '9':
                return 8;
        }

        return 0;
    }

    /**
     * Returns the Bytes of DataFrame
     * 
     * @return bytes of DataFrame
     */
    public List<Byte> getBytes() {
        List<Byte> result = new ArrayList<Byte>();

        result.add(Convert.toByte(userCode));
        result.add(Convert.toByte(home | unit));

        if (commandFrame != null) {
            result.addAll(commandFrame.getBytes());
        }

        return result;
    }

    /**
     * Initializes DataFrame from byte array
     * 
     * @param data
     *            byte of DataFrame
     */
    public void parse(byte[] data) {
        userCode = data[0];
        home = data[1] & 0xF0;
        unit = data[1] & 0x0F;

        commandFrame = new CommandFrame();
        commandFrame.parse(new byte[] { data[2], data[3], data[4] });
    }

    /**
     * Gets the first parameter of CommandFrame
     * 
     * @return first parameter
     */
    public int getFirstParameter() {
        return commandFrame.getFirstParameter();
    }

    /**
     * Gets the second parameter of CommandFrame
     * 
     * @return second parameter
     */
    public int getSecondParameter() {
        return commandFrame.getSecondParameter();
    }

    /**
     * Gets the Command of CommandFrame
     * 
     * @return Command of CommandFrame
     */
    public Command getCommand() {
        return commandFrame.getCommand();
    }

}
<commit_msg>bugfix in the way house codes and unit codes are packed into a single byte. The 4 bit house code has to be packed into the 4 most significant bits of the address byte. The previous version of the code only works for devices in house code A.<commit_after>/**
 * Copyright (c) 2010-2016, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.binding.plcbus.internal.protocol;

import java.util.ArrayList;
import java.util.List;

/**
 * DataFrame of PLCBus Protocol
 *
 * @author <NAME>
 * @since 1.0.0
 */
public class DataFrame {

    private int userCode;
    private int home;
    private int unit;
    private CommandFrame commandFrame;

    /**
     * Create a new DataFrame
     */
    public DataFrame() {

    }

    /**
     * Creates a new DataFrame with CommandFrame
     *
     * @param commandFrame
     *            for DataFrame
     */
    public DataFrame(CommandFrame commandFrame) {
        this.commandFrame = commandFrame;
    }

    /**
     * Sets the UserCode to DataFrame
     *
     * @param userCode
     *            for DataFrame
     */
    public void setUserCode(String userCode) {
        this.userCode = Convert.toByte(userCode, 16);
    }

    /**
     * Sets address to DataFrame
     *
     * @param address
     *            for DataFrame
     */
    public void SetAddress(String address) {

        this.home = getHomeFrom(address.charAt(0));
        this.unit = getUnitFrom(address.charAt(1));
    }

    private int getHomeFrom(char home) {
        switch (home) {
            case 'A':
                return 0;
            case 'B':
                return 1;
            case 'C':
                return 2;
            case 'D':
                return 3;
            case 'E':
                return 4;
            case 'F':
                return 5;
            case 'G':
                return 6;
            case 'H':
                return 7;
            case 'I':
                return 8;
            case 'J':
                return 9;
            case 'K':
                return 10;
        }

        return 0;
    }

    private int getUnitFrom(char unit) {
        switch (unit) {
            case '1':
                return 0;
            case '2':
                return 1;
            case '3':
                return 2;
            case '4':
                return 3;
            case '5':
                return 4;
            case '6':
                return 5;
            case '7':
                return 6;
            case '8':
                return 7;
            case '9':
                return 8;
        }

        return 0;
    }

    /**
     * Returns the Bytes of DataFrame
     *
     * @return bytes of DataFrame
     */
    public List<Byte> getBytes() {
        List<Byte> result = new ArrayList<Byte>();

        result.add(Convert.toByte(userCode));
        result.add(Convert.toByte((home << 4) | unit));

        if (commandFrame != null) {
            result.addAll(commandFrame.getBytes());
        }

        return result;
    }

    /**
     * Initializes DataFrame from byte array
     *
     * @param data
     *            byte of DataFrame
     */
    public void parse(byte[] data) {
        userCode = data[0];
        home = data[1] >>> 4;
        unit = data[1] & 0x0F;

        commandFrame = new CommandFrame();
        commandFrame.parse(new byte[] { data[2], data[3], data[4] });
    }

    /**
     * Gets the first parameter of CommandFrame
     *
     * @return first parameter
     */
    public int getFirstParameter() {
        return commandFrame.getFirstParameter();
    }

    /**
     * Gets the second parameter of CommandFrame
     *
     * @return second parameter
     */
    public int getSecondParameter() {
        return commandFrame.getSecondParameter();
    }

    /**
     * Gets the Command of CommandFrame
     *
     * @return Command of CommandFrame
     */
    public Command getCommand() {
        return commandFrame.getCommand();
    }

}

{'fraction_non_alphanumeric': 0.09723069415639868, 'fraction_numerical': 0.015127485665487373, 'mean_word_length': 1.434808434808435, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5273357', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2197, 'n_words': 672}