package com.ociweb.iot.gasPumpSimulator;


import static com.ociweb.iot.grove.GroveTwig.AngleSensor;
import static com.ociweb.iot.grove.GroveTwig.Button;
import static com.ociweb.iot.grove.GroveTwig.UltrasonicRanger;
import static com.ociweb.iot.maker.Port.A0;
import static com.ociweb.iot.maker.Port.A2;
import static com.ociweb.iot.maker.Port.D7;

import com.ociweb.iot.maker.DeviceRuntime;
import com.ociweb.iot.maker.Hardware;
import com.ociweb.iot.maker.IoTSetup;

public class IoTApp implements IoTSetup
{
	
	private static String serverURI;
	private static String clientId;
	private static int    tankDepth;
	private static String fuelName;
	private static int    centsPerGallon; 
	
	private final static int BUTTON_RATE_MS = 200; //5 per second per second
	private final static int RANGER_RATE_MS = 200; //5 per second per second
	
	private final static boolean RETURN_EVERY_SAMPLE = true; //by default only returns changes
	private final static String TOPIC_TANK  = "tank";
	private final static String TOPIC_PUMP  = "pump";
	private final static String TOPIC_TOTAL = "total";
	
	
	
    public static void main( String[] args ) {

    	fuelName =                        DeviceRuntime.getOptArg("--fuelName",  "-fn", args, "diesel");
    	centsPerGallon = Integer.parseInt(DeviceRuntime.getOptArg("--fuelPrice", "-fp", args, "215"));
    	serverURI =                       DeviceRuntime.getOptArg("--brokerURI", "-br", args, "tcp://127.0.0.1:1883");
    	clientId =                        DeviceRuntime.getOptArg("--clientId",  "-id", args, "unknownStation");
    	tankDepth =      Integer.parseInt(DeviceRuntime.getOptArg("--tankDepth", "-td", args, "13"));

        DeviceRuntime.run(new IoTApp());
        
    }
        
    @Override
    public void declareConnections(Hardware c) {
        c.connect(Button, D7, BUTTON_RATE_MS, RETURN_EVERY_SAMPLE);
        c.connect(AngleSensor, A0);
        c.connect(UltrasonicRanger, A2, RANGER_RATE_MS, RETURN_EVERY_SAMPLE); //default rate 5x per second
        
        c.startStateMachineWith(PumpState.Idle);
    }


    @Override
    public void declareBehavior(DeviceRuntime runtime) {
        
    	//Continuous monitoring of the tank and values published to TOPIC_TANK    	
    	runtime.registerListener(new TankMonitor(runtime, TOPIC_TANK, tankDepth, fuelName))
    	                              .includePorts(A2);
    	
    	runtime.registerListener(new ModeSelector(runtime, AngleSensor.range()))
    	                              .includePorts(A0);
    	  	    	
    	runtime.registerListener(new PumpSimulator(runtime, TOPIC_PUMP, TOPIC_TOTAL, fuelName, centsPerGallon))
    	                              .includePorts(D7);
    	     	    	
    	runtime.registerListener(new DisplayController(runtime, TOPIC_TANK, TOPIC_PUMP, TOPIC_TOTAL))
    	                              .addSubscription(TOPIC_TANK)
    	                              .addSubscription(TOPIC_PUMP)
    	                              .addSubscription(TOPIC_TOTAL);
    	
    	runtime.addPubSubListener(new PublishDataMQTT(serverURI,clientId))
   										.addSubscription(TOPIC_TOTAL)
   										.addSubscription(TOPIC_TANK);
    	
    }

  
}

@@ -461,16 +461,60 @@
 oTSetup;
+%0Aimport com.ociweb.iot.maker.ListenerFilter;
 %0A%0Apublic
@@ -725,16 +725,57 @@
 allon; %0A
+%09private static boolean publishTankData;%0A
 %09%0A%09priva
@@ -1249,32 +1249,37 @@
                 
+     
 DeviceRuntime.ge
@@ -1299,16 +1299,23 @@
 elName%22,
+       
   %22-fn%22,
@@ -1363,32 +1363,37 @@
 nteger.parseInt(
+     
 DeviceRuntime.ge
@@ -1414,16 +1414,23 @@
 lPrice%22,
+       
  %22-fp%22, 
@@ -1460,16 +1460,21 @@
 verURI =
+     
         
@@ -1526,16 +1526,23 @@
 kerURI%22,
+       
  %22-br%22, 
@@ -1603,32 +1603,37 @@
                 
+     
 DeviceRuntime.ge
@@ -1653,16 +1653,23 @@
 ientId%22,
+       
   %22-id%22,
@@ -1733,16 +1733,21 @@
 arseInt(
+     
 DeviceRu
@@ -1776,16 +1776,23 @@
 kDepth%22,
+       
  %22-td%22, 
@@ -1805,16 +1805,128 @@
 %2213%22));%0A
+    %09publishTankData = Boolean.parseBoolean(DeviceRuntime.getOptArg(%22--publishTankData%22,  %22-pt%22, args, %22true%22));
 %0A       
@@ -3255,32 +3255,60 @@
 AL);%0A    %09%0A    %09
+ListenerFilter mqttFilter = 
 runtime.addPubSu
@@ -3400,30 +3400,62 @@
 C_TOTAL)
-%0A
+;%0A 
    %09
-%09%09%09%09%09%09%09%09%09
+if (publishTankData) %7B%0A    %09%09mqttFilter
 .addSubs
@@ -3476,16 +3476,37 @@
 _TANK);%0A
+    %09%7D%0A    %09%09%09%09%09%09%09%09%09%0A
     %09%0A  

switch to turn off and on the pump messages.