package com.ociweb.oe.foglight.api;


import static com.ociweb.iot.grove.GroveTwig.*;

import com.ociweb.iot.maker.*;
import static com.ociweb.iot.maker.Port.*;

import com.ociweb.gl.api.StateChangeListener;

public class StateMachine implements FogApp
{

	static String cGreen = "Green";
	static String cYellow = "Yellow";
	static String cRed = "Red";
	
	public enum StopLight{
		
		Go(cGreen), 
		Caution(cYellow), 
		Stop(cRed);
		
		private String color;
		
		StopLight(String lightColor){
			color = lightColor;
		}
		
		public String getColor(){
			return color;
		}
	}


    @Override
    public void declareConnections(Hardware c) {
    	
    	c.startStateMachineWith(StopLight.Go);
    }

   
    @Override
    public void declareBehavior(FogRuntime runtime) {

        final FogCommandChannel channel1 = runtime.newCommandChannel(DYNAMIC_MESSAGING);
        final FogCommandChannel channel2 = runtime.newCommandChannel(DYNAMIC_MESSAGING);

        
        
        runtime.addStartupListener(()-> {
        	channel2.changeStateTo(StopLight.Caution);
        	System.out.println("first");
        });
        
        
    	StateChangeListener<StopLight> thing = (oldState, newState) -> {
    		
    		if(newState == StopLight.Go){
    			System.out.println("Go! The light is " + StopLight.Go.getColor());
    			if(channel1.changeStateTo(StopLight.Caution)){
    				channel1.block(7000);
    			}
    		}
    		
    		else if(newState == StopLight.Caution){
    			System.out.println("The light is " + StopLight.Caution.getColor());

    			if(channel1.changeStateTo(StopLight.Stop)){
    				channel1.block(7000);
    			}
    		}
    		
    		else{
    			System.out.println("Stop! The light is " + StopLight.Stop.getColor());
    			if(channel1.changeStateTo(StopLight.Go)){
    				channel1.block(7000);
    			}
    		}
    		return true;
    	};
    	
		runtime.addStateChangeListener(thing);
    	
    }
          
}

@@ -111,16 +111,75 @@
 aker.*;%0A
+import com.ociweb.oe.foglight.api.StateMachine.StopLight;%0A%0A
 import s
@@ -629,16 +629,17 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09
 %0A    @Ov
@@ -743,28 +743,88 @@
 ght.
-Go
+Stop
 );%0A    
-%7D%0A
+%09c.setTimerPulseRate(1);
 %0A   
+ %7D%0A
 %0A   
- 
+%0A%09@SuppressWarnings(%22unchecked%22)%0A%09
 @Ove
@@ -896,1146 +896,136 @@
     
-final FogCommandChannel channel1 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A        final FogCommandChannel channel2 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A%0A        %0A        %0A        runtime.addStartupListener(()-%3E %7B%0A        %09channel2.changeStateTo(StopLight.Caution);%0A        %09System.out.println(%22first%22);%0A        %7D);%0A        %0A        %0A    %09StateChangeListener%3CStopLight%3E thing = (oldState, newState) -%3E %7B%0A    %09%09%0A    %09%09if(newState == StopLight.Go)%7B%0A    %09%09%09System.out.println(%22Go! The light is %22 + StopLight.Go.getColor());%0A    %09%09%09if(channel1.changeStateTo(StopLight.Caution))%7B%0A    %09%09%09%09channel1.block(7000);%0A    %09%09%09%7D%0A    %09%09%7D%0A    %09%09%0A    %09%09else if(newState == StopLight.Caution)%7B%0A    %09%09%09System.out.println(%22The light is %22 + StopLight.Caution.getColor());%0A%0A    %09%09%09if(channel1.changeStateTo(StopLight.Stop))%7B%0A    %09%09%09%09channel1.block(7000);%0A    %09%09%09%7D%0A    %09%09%7D%0A    %09%09%0A    %09%09else%7B%0A    %09%09%09System.out.println(%22Stop! The light is %22 + StopLight.Stop.getColor());%0A    %09%09%09if(channel1.changeStateTo(StopLight.Go))%7B%0A    %09%09%09%09channel1.block(7000);%0A    %09%09%09%7D%0A    %09%09%7D%0A    %09%09return true;%0A    %09%7D;%0A    %09%0A%09%09runtime.addStateChangeListener(thing);%0A    %09
+%0A        runtime.addTimePulseListener(new TimingBehavior(runtime));%0A%09%09runtime.addStateChangeListener(new StateChangeBehavior());
 %0A   

Update StateMachine.java