package fr.lhuet.home.hardware;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
 * Created by lhuet on 31/12/15.
 */
public class Ds18b20Verticle extends AbstractVerticle {

    private static Logger logger = LoggerFactory.getLogger(Ds18b20Verticle.class.getName());

    private static final int DHWSENSOR = 0;
    private static final int BUFFERSENSOR = 1;

    private String w1DhwFile;
    private String w1BufferFile;

    private float dhwTemp;
    private float bufferTemp;

    @Override
    public void start(Future<Void> fut) throws Exception {

        w1DhwFile = this.config().getString("dhw-system.dhw");
        w1BufferFile = this.config().getString("dhw-system.buffer");

        logger.info("DHW File : " + w1DhwFile);
        logger.info("Buffer File : " + w1BufferFile);

        // Handler to serve the sensors values on the vertx event loop
        MessageConsumer<String> dhwConsumer = vertx.eventBus().consumer("dhw-temp");
        dhwConsumer.handler(event -> {
            switch (event.body()) {
                case "dhw":
                    event.reply(this.dhwTemp);
                    break;
                case "buffer":
                    event.reply(this.bufferTemp);
                    break;
                default:
                    event.reply(Double.valueOf(null));
            }
        });

        // Read the 2 temp sensors "immediately"
        readTemp(event1 -> {
            logger.debug("Temp sensor reading finished");
            fut.complete();
        });
        // .. and continue refreshing the 2 temp. sensors every minute
        vertx.setPeriodic(60000, event -> {
            readTemp(event1 -> {
                logger.debug("Temp sensor reading finished");
            });
        });

    }

    private void readTemp(Handler<AsyncResult> resultHandler) {

        readW1temp(w1DhwFile, DHWSENSOR, event1 -> {
            readW1temp(w1BufferFile, BUFFERSENSOR, event2 -> {
                resultHandler.handle(Future.succeededFuture());
            });
        });
    }

    private void readW1temp(String file, int sensor, Handler<AsyncResult> handlerResult) {
        // w1 file content like :
        //    ce 02 4b 46 7f ff 02 10 0c : crc=0c YES
        //    ce 02 4b 46 7f ff 02 10 0c t=44875
        vertx.fileSystem().readFile(file, (AsyncResult<Buffer> res) -> {
            String content = res.result().toString();
            if (content.contains("YES")) {
                // CRC ok -> extract Temp in deg C
                String[] temp = content.split("t=");
                switch (sensor) {
                    case DHWSENSOR:
                        this.dhwTemp = Float.valueOf(temp[1])/1000;
                        logger.debug("Refreshing DHW Temp sensor value : " + this.dhwTemp);
                        break;
                    case BUFFERSENSOR:
                        this.bufferTemp = Float.valueOf(temp[1])/1000;
                        logger.debug("Refreshing Buffer Temp sensor value : " + this.bufferTemp);
                        break;
                    default:
                        logger.error("readW1temp -> Bad sensor switch");
                        handlerResult.handle(Future.failedFuture("readW1temp -> Bad sensor switch"));
                }
                handlerResult.handle(Future.succeededFuture());
            } else {
                handlerResult.handle(Future.failedFuture("(One Wire bus error) Bad CRC on file " + file));
            }
        });
    }


}

@@ -51,117 +51,9 @@
 ore.
-AbstractVerticle;%0Aimport io.vertx.core.AsyncResult;%0Aimport io.vertx.core.Future;%0Aimport io.vertx.core.Handler
+*
 ;%0Aim
@@ -1542,33 +1542,32 @@
   readTemp(event
-1
  -%3E %7B%0A          
@@ -1799,34 +1799,16 @@
 vent1 -%3E
- %7B%0A               
  logger.
@@ -1844,31 +1844,16 @@
 nished%22)
-;%0A            %7D
 );%0A     
@@ -1928,24 +1928,122 @@
 Handler) %7B%0A%0A
+        Future%3CVoid%3E dhwFut = Future.future();%0A        Future%3CVoid%3E bufferFut = Future.future();%0A%0A
         read
@@ -2075,24 +2075,29 @@
 OR, 
-event1 -%3E %7B%0A    
+dhwFut.completer());%0A
     
@@ -2142,25 +2142,104 @@
 SOR,
+ bufferFut.completer());%0A%0A        CompositeFuture.all(dhwFut, bufferFut).setHandler(
  event
-2
  -%3E %7B%0A
-    
     
@@ -2298,24 +2298,8 @@
 ));%0A
-            %7D);%0A
     
@@ -2302,24 +2302,25 @@
         %7D);%0A
+%0A
     %7D%0A%0A    p
@@ -2390,23 +2390,29 @@
 sult
-%3E handlerResult
+%3CVoid%3E%3E resultHandler
 ) %7B%0A
@@ -3444,37 +3444,37 @@
             
-handlerResult
+resultHandler
 .handle(Futu
@@ -3556,37 +3556,37 @@
             
-handlerResult
+resultHandler
 .handle(Futu
@@ -3649,21 +3649,21 @@
     
-handlerResult
+resultHandler
 .han

add CompositeFuture to sync temperature reading