package com.ociweb.oe.foglight.api;


import static com.ociweb.iot.grove.GroveTwig.Button;
import static com.ociweb.iot.grove.GroveTwig.LED;
import static com.ociweb.iot.maker.Port.D2;
import static com.ociweb.iot.maker.Port.D3;

import com.ociweb.gl.impl.stage.ReactiveListenerStage;
import com.ociweb.iot.maker.FogApp;
import com.ociweb.iot.maker.FogCommandChannel;
import com.ociweb.iot.maker.FogRuntime;
import com.ociweb.iot.maker.Hardware;
import com.ociweb.iot.maker.Port;

public class Shutdown implements FogApp
{	
	private static final Port LED_PORT = D2;
	private static final Port BUTTON_PORT = D3;
	private static boolean statusOfLED = false;

    @Override
    public void declareConnections(Hardware c) {
        
    	c.connect(LED, D2);
    	c.connect(Button, D3);

    }


    @Override
    public void declareBehavior(final FogRuntime runtime) {
        
    	final FogCommandChannel channel1 = runtime.newCommandChannel(DYNAMIC_MESSAGING);
    	final FogCommandChannel channel2 = runtime.newCommandChannel(DYNAMIC_MESSAGING);
    	final FogCommandChannel channel3 = runtime.newCommandChannel(DYNAMIC_MESSAGING);
    	final FogCommandChannel channel4 = runtime.newCommandChannel(DYNAMIC_MESSAGING);
    	
    	runtime.addStartupListener(()->{
    		channel1.setValue(LED_PORT, true);
    		statusOfLED = true;
    		System.out.println("The Light is on");
    	});   	

    	
    	runtime.addDigitalListener((port, connection, time, value)-> {
    		if(value == 1){
    			System.out.println("Starting the shutdown process");
    			//1
    			runtime.shutdownRuntime();
    			
    			//2
    			ReactiveListenerStage.requestSystemShutdown(new Runnable(){
					@Override
					public void run() {
						// TODO Auto-generated method stub
					}
    			});
    			
//    			//3
//    			ReactiveListenerStage.requestSystemShutdown(shutdownRunnable);
//    			
//    			//4
//    			runtime.requestSystemShutdown(shutdownRunnable);
    		}
    	});    	
    	
    	
    	runtime.addShutdownListener(()->{
    		//check if light is on, if it is, return false and turn it off, if not, return true
    		return true;
    	});
    	
    	runtime.addPubSubListener((topic, payload)->{
    		return true;
    	});
    	
    }
          
}

@@ -30,31 +30,23 @@
 t.api;%0A%0A
-%0A
 import 
-static 
 com.ociw
@@ -56,80 +56,15 @@
 iot.
-grove.GroveTwig.Button;%0Aimport static com.ociweb.iot.grove.GroveTwig.LED
+maker.*
 ;%0Aim
@@ -101,18 +101,17 @@
 er.Port.
-D2
+*
 ;%0Aimport
@@ -137,273 +137,35 @@
 iot.
-maker.Port.D3;%0A%0Aimport com.ociweb.gl.impl.stage.ReactiveListenerStage;%0Aimport com.ociweb.iot.maker.FogApp;%0Aimport com.ociweb.iot.maker.FogCommandChannel;%0Aimport com.ociweb.iot.maker.FogRuntime;%0Aimport com.ociweb.iot.maker.Hardware;%0Aimport com.ociweb.iot.maker.Port;
+grove.AnalogDigitalTwig.*;%0A
 %0A%0Apu
@@ -297,52 +297,8 @@
 3;%0A%09
-private static boolean statusOfLED = false;%0A
 %0A   
@@ -385,15 +385,27 @@
 ect(
-LED, D2
+Button, BUTTON_PORT
 );%0A 
@@ -422,18 +422,21 @@
 ect(
-Button, D3
+LED, LED_PORT
 );%0A%0A
@@ -441,17 +441,18 @@
 %0A%0A    %7D%0A
-%0A
+  
 %0A    @Ov
@@ -526,1374 +526,111 @@
     
-    %0A    %09final FogCommandChannel channel1 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A    %09final FogCommandChannel channel2 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A    %09final FogCommandChannel channel3 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A    %09final FogCommandChannel channel4 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A    %09%0A    %09runtime.addStartupListener(()-%3E%7B%0A    %09%09channel1.setValue(LED_PORT, true);%0A    %09%09statusOfLED = true;%0A    %09%09System.out.println(%22The Light is on%22);%0A    %09%7D);   %09%0A%0A    %09%0A    %09runtime.addDigitalListener((port, connection, time, value)-%3E %7B%0A    %09%09if(value == 1)%7B%0A    %09%09%09System.out.println(%22Starting the shutdown process%22);%0A    %09%09%09//1%0A    %09%09%09runtime.shutdownRuntime();%0A    %09%09%09%0A    %09%09%09//2%0A    %09%09%09ReactiveListenerStage.requestSystemShutdown(new Runnable()%7B%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public void run() %7B%0A%09%09%09%09%09%09// TODO Auto-generated method stub%0A%09%09%09%09%09%7D%0A    %09%09%09%7D);%0A    %09%09%09%0A//    %09%09%09//3%0A//    %09%09%09ReactiveListenerStage.requestSystemShutdown(shutdownRunnable);%0A//    %09%09%09%0A//    %09%09%09//4%0A//    %09%09%09runtime.requestSystemShutdown(shutdownRunnable);%0A    %09%09%7D%0A    %09%7D);    %09%0A    %09%0A    %09%0A    %09runtime.addShutdownListener(()-%3E%7B%0A    %09%09//check if light is on, if it is, return false and turn it off, if not, return true%0A    %09%09return true;%0A    %09%7D);%0A    %09%0A    %09runtime.addPubSubListener((topic, payload)-%3E%7B%0A    %09%09return true;%0A    %09%7D);%0A    %09%0A    %7D
+%09runtime.registerListener(new ShutdownBehavior(runtime)).addSubscription(%22LED%22);%0A    %09%0A    %7D          %0A
 %0A   

Update Shutdown.java