<commit_before>/**
 * Licensed to the Rhiot under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.rhiot.component.deviceio.gpio;

import java.io.IOException;

import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultProducer;

import io.rhiot.component.deviceio.DeviceIOConstants;
import jdk.dio.ClosedDeviceException;
import jdk.dio.UnavailableDeviceException;
import jdk.dio.gpio.GPIOPin;

/**
 * The DeviceIO GPIO producer.
 */
public class GPIOProducer extends DefaultProducer {

    private GPIOEndpoint endpoint;
    private GPIOPin pin = null;
    private GPIOAction action;

    public GPIOProducer(GPIOEndpoint endpoint, GPIOPin pin) {
        super(endpoint);
        this.endpoint = endpoint;
        this.pin = pin;
        this.action = endpoint.getAction();

    }

    /**
     * Process the message
     */
    @Override
    public void process(Exchange exchange) throws Exception {
        log.debug(exchange.toString());

        if (pin instanceof GPIOPin) {

            GPIOAction headerAction = exchange.getIn().getHeader(DeviceIOConstants.CAMEL_DEVICE_IO_ACTION,
                    GPIOAction.class);

            if (headerAction != null) {
                action = headerAction;
            }

            if (action != null) {
                switch (action) {
                case HIGH:
                    setValue(true);
                    break;
                case LOW:
                    setValue(false);
                    break;
                case TOGGLE:
                    setValue(!pin.getValue());
                    break;
                }
            } else {
                setValue(exchange.getIn().getBody(Boolean.class));<commit_msg>[rhiot/#295] GPIOProducer shouldn't overwrite the "action" on each msg  <commit_after>/**
 * Licensed to the Rhiot under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.rhiot.component.deviceio.gpio;

import io.rhiot.component.deviceio.DeviceIOConstants;

import java.io.IOException;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.impl.DefaultProducer;

import jdk.dio.ClosedDeviceException;
import jdk.dio.UnavailableDeviceException;
import jdk.dio.gpio.GPIOPin;

/**
 * The DeviceIO GPIO producer.
 */
public class GPIOProducer extends DefaultProducer {

    private GPIOEndpoint endpoint;
    private GPIOPin pin = null;
    private GPIOAction action;

    public GPIOProducer(GPIOEndpoint endpoint, GPIOPin pin) {
        super(endpoint);
        this.endpoint = endpoint;
        this.pin = pin;
        this.action = endpoint.getAction();

    }

    protected GPIOAction resolveAction(Message message) {
        if (message.getHeaders().containsKey(DeviceIOConstants.CAMEL_DEVICE_IO_ACTION)) {
            // Exchange Action
            return message.getHeader(DeviceIOConstants.CAMEL_DEVICE_IO_ACTION, GPIOAction.class);
        } else {
            return action; // Endpoint Action
        }
    }

    /**
     * Process the message
     */
    @Override
    public void process(Exchange exchange) throws Exception {
        log.debug(exchange.toString());

        if (pin instanceof GPIOPin) {

            GPIOAction messageAction = resolveAction(exchange.getIn());

            if (messageAction != null) {
                switch (messageAction) {
                case HIGH:
                    setValue(true);
                    break;
                case LOW:
                    setValue(false);
                    break;
                case TOGGLE:
                    setValue(!pin.getValue());
                    break;
                }
            } else {
                setValue(exchange.getIn().getBody(Boolean.class));
{'fraction_non_alphanumeric': 0.08036064288514308, 'fraction_numerical': 0.002156017248137985, 'mean_word_length': 2.3550295857988166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3877606', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1272, 'n_words': 418}