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.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;
	
	public static void main(String[] args){
		FogRuntime.run(new Shutdown());
	}
	
	
    @Override
    public void declareConnections(Hardware c) {
        
    	c.connect(LED, D2);
    	c.connect(Button, D3);
    	c.enableTelemetry(false);
    }


    @Override
    public void declareBehavior(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");
    			channel2.block(100);
    			channel2.publishTopic("Shutdown", writable->{});
    		}
    	});    	
    	
    	
    	runtime.addPubSubListener((topic, payload)->{
			if(statusOfLED){
				System.out.println("The light is still on, it must be turned off before shutdown");
				channel3.block(100);
				channel3.publishTopic("LED", writable->{});
			}
			else{
				System.out.println("Shutting down now");
				runtime.shutdownRuntime();
			}
			return true;
    	}).addSubscription("Shutdown");

    	    	
    	runtime.addPubSubListener((topic, payload)->{
    		System.out.println("Turning off the light");
    		channel4.setValue(LED_PORT, false);
    		statusOfLED = false;
    		channel4.block(100);
    		channel4.publishTopic("Shutdown", writable->{});
    		return true;
    	}).addSubscription("LED");
    }
          
}

@@ -80,73 +80,19 @@
 wig.
-Button;%0Aimport static com.ociweb.iot.grove.GroveTwig.LED;
+*;%0A
 %0Aimport 
 stat
@@ -79,39 +79,32 @@
 Twig.*;%0A%0Aimport 
-static 
 com.ociweb.iot.m
@@ -112,15 +112,9 @@
 ker.
-Port.D2
+*
 ;%0Aim
@@ -151,18 +151,17 @@
 er.Port.
-D3
+*
 ;%0A%0Aimpor
@@ -177,183 +177,43 @@
 web.
-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
+gl.impl.stage.ReactiveListenerStage
 ;%0A%0Ap
@@ -389,16 +389,18 @@
 lse;%0A%09%0A%09
+/*
 public s
@@ -469,16 +469,18 @@
 ));%0A%09%7D%0A%09
+*/
 %0A%09%0A    @
@@ -1396,82 +1396,35 @@
  %09%09%09
-channel2.block(100);%0A    %09%09%09channel2.publishTopic(%22Shutdown%22, writable-%3E%7B%7D
+//1%0A    %09%09%09requestShutdown(
 );%0A 
@@ -1432,422 +1432,379 @@
   %09%09
-%7D
+%09
 %0A    %09
-%7D);    %09%0A    %09%0A    %09%0A    %09runtime.addPubSubListener((topic, payload)-%3E%7B%0A%09%09%09if(statusOfLED)%7B%0A%09%09%09%09System.out.println(%22The light is still on, it must be turned off before shutdown%22);%0A%09%09%09%09channel3.block(100);%0A%09%09%09%09channel3.publishTopic(%22LED%22, writable-%3E%7B%7D);%0A%09%09%09%7D%0A%09%09%09else%7B%0A%09%09%09%09System.out.println(%22Shutting down now%22);%0A%09%09%09%09runtime.shutdownRuntime();%0A%09%09%09%7D%0A%09%09%09return true;%0A    %09%7D).addSubscription(%22Shutdown%22);%0A
+%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
     
@@ -1822,17 +1822,16 @@
 ime.
-addPubSub
+Shutdown
 List
@@ -1840,228 +1840,187 @@
 er((
-topic, payload)-%3E%7B%0A    %09%09System.out.println(%22Turning off the light%22);%0A    %09%09channel4.setValue(LED_PORT, false);%0A    %09%09statusOfLED = false;%0A    %09%09channel4.block(100);%0A    %09%09channel4.publishTopic(%22Shutdown%22, writable-%3E%7B%7D);
+)-%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   
@@ -2046,32 +2046,15 @@
  %09%7D)
-.addSubscription(%22LED%22);
+;%0A    %09
 %0A   

Update Shutdown.java