package org.robbins.raspberry.pi.service;

import com.pi4j.io.gpio.*;
import org.robbins.raspberry.pi.exceptions.RaspberryPiAppException;
import org.robbins.raspberry.pi.model.PiAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service("blinkLight")
public class BlinkLightActionImpl implements PiActionService {

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

    @Value("${blink.delay}")
    private long blinkDelay;

    GpioController gpio;
    GpioPinDigitalOutput pin;

    @Override
    public void invokeAction(final PiAction action) throws RaspberryPiAppException {
        LOGGER.debug("Blinking Light service for action: {}", action);

        try {
            final long blinkDuration = calculateBlinkDurationInMilliseconds(Long.parseLong(action.getValue()));

            // create gpio controller
            gpio = GpioFactory.getInstance();

            // provision gpio pin #01 as an output pin
            pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);

            LOGGER.debug("Blinking LED with delay: {}, and duration: {}", blinkDelay, blinkDuration);
            pin.blink(blinkDelay, blinkDuration);
        }
        catch (Exception e) {
            LOGGER.debug("Error while blinking: {}", e.getMessage());
            throw new RaspberryPiAppException(e.getMessage());
        }
        finally {
            // stop all GPIO activity/threads by shutting down the GPIO controller
            // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
            if (gpio != null) {
                LOGGER.debug("Shutting down GPIO");
                gpio.shutdown();
                gpio.unprovisionPin(pin);
            }
            else {
                LOGGER.debug("Unable to shutdown GPIO.  There is no GPIO instance");
            }
        }
    }

    private long calculateBlinkDurationInMilliseconds(final long timeInSeconds) {
        return 1000 * timeInSeconds;
    }
}

@@ -1993,16 +1993,644 @@
 %0A    %7D%0A%0A
+    private void blinkLight(final GpioPinDigitalOutput pin, int blinkCount) throws RaspberryPiAppException %7B%0A%0A        try %7B%0A            for (int i = 0; i %3C blinkCount; i++) %7B%0A                pin.high();%0A                LOGGER.debug(%22--%3E GPIO state should be: ON%22);%0A%0A                Thread.sleep(blinkDelay);%0A%0A                // turn off gpio pin #01%0A                pin.low();%0A                LOGGER.debug(%22--%3E GPIO state should be: OFF%22);%0A%0A                Thread.sleep(blinkDelay);%0A            %7D%0A        %7D%0A        catch (InterruptedException e) %7B%0A            throw new RaspberryPiAppException(e.getMessage());%0A        %7D%0A    %7D%0A%0A
     priv

refactor blinking service