package com.gurkensalat.chronos;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

@Component
public class LayerListRunner implements CommandLineRunner, ApplicationListener<ApplicationContextEvent>
{
    private final static Logger LOGGER = LoggerFactory.getLogger(LayerListRunner.class);

    private static Thread thisThread = null;

    private static boolean running = true;


    public void run(String... args) throws Exception
    {
        LOGGER.info("Running my additional runner");

        thisThread = Thread.currentThread();
        while (running)
        {
            LOGGER.info("Doing some work...");

            try
            {
                Thread.sleep(15 * 1000);
            }
            catch (InterruptedException ie)
            {
                // We don't want to continue looping if death was requested
                running = false;
            }
        }

        LOGGER.info("Finished my additional runner");
    }

    public void onApplicationEvent(ApplicationContextEvent event)
    {
        LOGGER.info("Context received event {}", event);
        if (event instanceof ContextStoppedEvent || event instanceof ContextClosedEvent)
        {
            // No clue why onApplicationEvent(ContextStoppedEvent) is not called, fake it here...
            running = false;
            LOGGER.info("Set running to {}", running);
            thisThread.interrupt();
        }
    }
}

@@ -27,16 +27,83 @@
 ronos;%0A%0A
+import opc.OpcClient;%0Aimport opc.OpcDevice;%0Aimport opc.PixelStrip;%0A
 import o
@@ -606,16 +606,59 @@
 mponent;
+%0Aimport org.springframework.util.StopWatch;
 %0A%0A@Compo
@@ -1074,127 +1074,822 @@
     
-thisThread = Thread.currentThread();%0A        while (running)%0A        %7B%0A            LOGGER.info(%22Doing some work...%22
+// TODO make address data configurable%0A        OpcClient server = new OpcClient(%22127.0.0.1%22, 7890);%0A        OpcDevice fadecandy = server.addDevice();%0A        PixelStrip strip = fadecandy.addPixelStrip(0, 60);%0A%0A        LOGGER.info(%22Server config: %7B%7D%22, server.getConfig());%0A%0A        LayerList layers = new LayerList();%0A        layers.add(new ChaseLayer());%0A%0A        thisThread = Thread.currentThread();%0A        while (running)%0A        %7B%0A            try%0A            %7B%0A                StopWatch stopWatch = new StopWatch();%0A                stopWatch.start();%0A                for (LedLayer layer : layers)%0A                %7B%0A                    layer.prepare(strip);%0A                %7D%0A                stopWatch.stop();%0A%0A                LOGGER.debug(%22Preparation of strips took %7B%7D msec%22, stopWatch.getTotalTimeMillis()
 );%0A
-%0A
     
@@ -1896,33 +1896,81 @@
         
-try%0A            %7B
+    server.show();%0A%0A                // Sleep for half a second...
 %0A       
@@ -1995,15 +1995,9 @@
 eep(
-15 * 10
+5
 00);
@@ -2199,24 +2199,153 @@
 %0A        %7D%0A%0A
+        server.clear(); // Set all pixels to black%0A        server.show();  // Show the darkened pixels%0A%0A        server.close();%0A%0A
         LOGG

Prepare layers and send them to the OPC server