<commit_before>package ev3dev.hardware;

import ev3dev.utils.Sysfs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.Properties;

/**
 * This class been designed to discover if the library is used in:
 *
 * - EV3 Brick
 * - Raspberry Pi 1 + PiStorms
 * - Raspberry Pi 1 + BrickPi
 * 
 * @author <NAME>
 *
 * At the moment, the class extends from Device, but close method doesn´ close any real resource.
 */


public abstract class EV3DevDevice {

    private static final Logger LOGGER = LoggerFactory.getLogger(EV3DevDevice.class);

    protected final Properties ev3DevProperties;
    protected final EV3DevPlatform CURRENT_PLATFORM;

    protected static final String LEGO_PORT = "lego-port";
    protected static final String ADDRESS = "address";
    protected static final String LEGO_SENSOR = "lego-sensor";
    protected static final String MODE = "mode";
    protected static final String DEVICE = "set_device";

    protected File PATH_DEVICE = null;

    public EV3DevDevice() {

        final EV3DevPropertyLoader ev3DevPropertyLoader = new EV3DevPropertyLoader();
        ev3DevProperties = ev3DevPropertyLoader.getEV3DevProperties();
        final EV3DevPlatforms ev3DevPlatforms = new EV3DevPlatforms();
        CURRENT_PLATFORM = ev3DevPlatforms.getPlatform();
    }

    //TODO Rename method to detect
    /**
     * This method matches a input with the internal position in EV3Dev.
     * @param type type
     * @param portName port
     */
    protected void detect(final String type, final String portName) {
        final String devicePath = EV3DevFileSystem.getRootPath() + "/" + type;
        if(LOGGER.isTraceEnabled())
            LOGGER.trace("Retrieving devices in path: ", devicePath);
        final List<File> deviceAvailables = Sysfs.getElements(devicePath);
        boolean connected = false;
        if(LOGGER.isTraceEnabled())
            LOGGER.trace("Checking devices on: {}", devicePath);
        String pathDeviceName;
        int deviceCounter = 1;
        for (File deviceAvailable : deviceAvailables) {
            PATH_DEVICE = deviceAvailable;
            pathDeviceName = PATH_DEVICE + "/" + ADDRESS;
            if(LOGGER.isTraceEnabled())
                LOGGER.trace("Device {}:", deviceCounter);
            String result = Sysfs.readString(pathDeviceName);
            if(LOGGER.isTraceEnabled())
                LOGGER.trace("Port expected: {}, actual: {}.", portName, result);
            //TODO Review to use equals. It is more strict
            if (result.contains(portName)) {
                if(LOGGER.isDebugEnabled())
                    LOGGER.debug("Detected device on path: {}, {}", pathDeviceName, result);
                connected = true;
                break;
            } else {
                if(LOGGER.isTraceEnabled())
                    LOGGER.trace("Skipped device");
            }
            deviceCounter++;
        }

        if(!connected){
            throw new RuntimeException("The device was not detected in: " + portName);
        }
    }

    /**
     * Returns the value of an attribute supported for a Device
     *
     * @param attribute attribute
     * @return value
     */
    protected String getStringAttribute(final String attribute){
        return Sysfs.readString(PATH_DEVICE + "/" +  attribute);
    }

    /**
     * Returns the value of an attribute supported for a Device
     *
     * @param attribute attribute
     * @return value
     */
    protected int getIntegerAttribute(final String attribute){
        return Integer.parseInt(getStringAttribute(attribute));
    }

    /**
     * Returns the value of an attribute supported for a Device
     *
     * @param attribute attribute
     * @return value
     */
    protected float getFloatAttribute(final String attribute) {
        return Float.parseFloat(getStringAttribute(attribute));
    }

    /**
     * Reads the specified value from the sensor.
     *
     * @param index Index of the returned value, e.g. value&lt;index&gt; will be read.
     * @return Measured value.
     */
    public float readValue(final int index) {
        return getFloatAttribute("value" + index);
    }

    /**
     * Set a value on an attribute
     *
     * @param attribute attribute
     * @param value value
     */
    protected void setStringAttribute(final String attribute, final String value){
        final boolean result = Sysfs.writeString(this.PATH_DEVICE + "/" +  attribute, value);
        if(!result){
            throw new RuntimeException("Operation not executed: " + this.PATH_DEVICE + "/" +  attribute + " with value " + value);
        }
    }

    /**<commit_msg>Using the methods from SysFs<commit_after>package ev3dev.hardware;

import ev3dev.utils.Sysfs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.Properties;

/**
 * This class been designed to discover if the library is used in:
 *
 * - EV3 Brick
 * - Raspberry Pi 1 + PiStorms
 * - Raspberry Pi 1 + BrickPi
 * 
 * @author <NAME>
 *
 * At the moment, the class extends from Device, but close method doesn´ close any real resource.
 */


public abstract class EV3DevDevice {

    private static final Logger LOGGER = LoggerFactory.getLogger(EV3DevDevice.class);

    protected final Properties ev3DevProperties;
    protected final EV3DevPlatform CURRENT_PLATFORM;

    protected static final String LEGO_PORT = "lego-port";
    protected static final String ADDRESS = "address";
    protected static final String LEGO_SENSOR = "lego-sensor";
    protected static final String MODE = "mode";
    protected static final String DEVICE = "set_device";

    protected File PATH_DEVICE = null;

    public EV3DevDevice() {

        final EV3DevPropertyLoader ev3DevPropertyLoader = new EV3DevPropertyLoader();
        ev3DevProperties = ev3DevPropertyLoader.getEV3DevProperties();
        final EV3DevPlatforms ev3DevPlatforms = new EV3DevPlatforms();
        CURRENT_PLATFORM = ev3DevPlatforms.getPlatform();
    }

    //TODO Rename method to detect
    /**
     * This method matches a input with the internal position in EV3Dev.
     * @param type type
     * @param portName port
     */
    protected void detect(final String type, final String portName) {
        final String devicePath = EV3DevFileSystem.getRootPath() + "/" + type;
        if(LOGGER.isTraceEnabled())
            LOGGER.trace("Retrieving devices in path: ", devicePath);
        final List<File> deviceAvailables = Sysfs.getElements(devicePath);
        boolean connected = false;
        if(LOGGER.isTraceEnabled())
            LOGGER.trace("Checking devices on: {}", devicePath);
        String pathDeviceName;
        int deviceCounter = 1;
        for (File deviceAvailable : deviceAvailables) {
            PATH_DEVICE = deviceAvailable;
            pathDeviceName = PATH_DEVICE + "/" + ADDRESS;
            if(LOGGER.isTraceEnabled())
                LOGGER.trace("Device {}:", deviceCounter);
            String result = Sysfs.readString(pathDeviceName);
            if(LOGGER.isTraceEnabled())
                LOGGER.trace("Port expected: {}, actual: {}.", portName, result);
            //TODO Review to use equals. It is more strict
            if (result.contains(portName)) {
                if(LOGGER.isDebugEnabled())
                    LOGGER.debug("Detected device on path: {}, {}", pathDeviceName, result);
                connected = true;
                break;
            } else {
                if(LOGGER.isTraceEnabled())
                    LOGGER.trace("Skipped device");
            }
            deviceCounter++;
        }

        if(!connected){
            throw new RuntimeException("The device was not detected in: " + portName);
        }
    }

    /**
     * Returns the value of an attribute supported for a Device
     *
     * @param attribute attribute
     * @return value
     */
    protected String getStringAttribute(final String attribute){
        return Sysfs.readString(PATH_DEVICE + "/" +  attribute);
    }

    /**
     * Returns the value of an attribute supported for a Device
     *
     * @param attribute attribute
     * @return value
     */
    protected int getIntegerAttribute(final String attribute){
        return Sysfs.readInteger(PATH_DEVICE + "/" +  attribute);
    }

    /**
     * Returns the value of an attribute supported for a Device
     *
     * @param attribute attribute
     * @return value
     */
    protected float getFloatAttribute(final String attribute) {
        return Sysfs.readFloat(PATH_DEVICE + "/" +  attribute);
    }

    /**
     * Reads the specified value from the sensor.
     *
     * @param index Index of the returned value, e.g. value&lt;index&gt; will be read.
     * @return Measured value.
     */
    public float readValue(final int index) {
        return getFloatAttribute("value" + index);
    }

    /**
     * Set a value on an attribute
     *
     * @param attribute attribute
     * @param value value
     */
    protected void setStringAttribute(final String attribute, final String value){
        final boolean result = Sysfs.writeString(this.PATH_DEVICE + "/" +  attribute, value);
        if(!result){
            throw new RuntimeException("Operation not executed: " + this.PATH_DEVICE + "/" +  attribute + " with value " + value);
        }
    }

    /**
{'fraction_non_alphanumeric': 0.09358090185676393, 'fraction_numerical': 0.005305039787798408, 'mean_word_length': 2.461623209695189, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5518953', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2503, 'n_words': 748}