package fr.lhuet.home.hardware;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
 * Created by lhuet on 31/12/16.
 */
public class WeatherVerticle extends AbstractVerticle{

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

    private String bmp085LinuxVirtualFsDir;
    private float bmp085Temp;
    private float bmp085Pressure;

    @Override
    public void start() throws Exception {

        bmp085LinuxVirtualFsDir = this.config().getString("weather.bmp085Dir");

        if (bmp085LinuxVirtualFsDir != null) {

            vertx.setPeriodic(5000, res -> {
               readBmp085();
            });

        }

    }

    private void readBmp085() {

        Future<Void> startFuture = Future.future();

        Future<Void> fut1 = Future.future();
        vertx.fileSystem().readFile(bmp085LinuxVirtualFsDir+ "/temp0_input", result -> {
            if (result.succeeded()) {
                bmp085Temp = Float.valueOf(result.result().toString())/10;
                logger.info("BMP085 Temp = " + bmp085Temp);
            }
            else {
                logger.error("Error while reading bmp085 temperature", result.cause());
            }
            fut1.complete();
        });

        fut1.compose(event -> {

            vertx.fileSystem().readFile(bmp085LinuxVirtualFsDir+ "/pressure0_input", result -> {
                if (result.succeeded()) {
                    bmp085Pressure = Float.valueOf(result.result().toString())/100;
                    logger.info("BMP085 Pressure = " + bmp085Pressure);
                } else {
                    logger.error("Error while reading bmp085 pressure", result.cause());
                }
            });

        }, startFuture);


    }

}

@@ -86,22 +86,124 @@
 tx.core.
-Future
+AsyncResult;%0Aimport io.vertx.core.Future;%0Aimport io.vertx.core.Handler;%0Aimport io.vertx.core.json.JsonObject
 ;%0Aimport
@@ -278,16 +278,43 @@
 ctory;%0A%0A
+import java.time.Instant;%0A%0A
 /**%0A * C
@@ -402,16 +402,17 @@
 ticle%7B%0A%0A
+%0A
     priv
@@ -483,24 +483,135 @@
 le.class);%0A%0A
+    private static final String ES_TYPE_DOC = %22record%22;%0A    private static final String ES_INDEX = %22weather%22;%0A%0A
     private 
@@ -707,16 +707,19 @@
 ssure;%0A%0A
+%0A%0A%0A
     @Ove
@@ -893,24 +893,183 @@
 != null) %7B%0A%0A
+            // Read sensor on startup%0A            readBmp085(event -%3E %7B%0A                indexToES();%0A            %7D);%0A%0A            // ... and then every minute%0A
             
@@ -1086,17 +1086,18 @@
 eriodic(
-5
+60
 000, res
@@ -1128,16 +1128,75 @@
 dBmp085(
+event -%3E %7B%0A                   indexToES();%0A               %7D
 );%0A     
@@ -1253,16 +1253,39 @@
 dBmp085(
+Handler%3CAsyncResult%3E ar
 ) %7B%0A%0A   
@@ -2321,16 +2321,818 @@
 ture);%0A%0A
+        ar.handle(startFuture);%0A%0A    %7D%0A%0A%0A    private void indexToES() %7B%0A%0A        JsonObject docToindex = new JsonObject()%0A                .put(%22timestamp%22, Instant.now())%0A                .put(%22tempCave%22, this.bmp085Temp)%0A                .put(%22pressure%22, this.bmp085Pressure);%0A        JsonObject message = new JsonObject()%0A                .put(%22index%22, ES_INDEX)%0A                .put(%22type%22, ES_TYPE_DOC)%0A                .put(%22id%22, %22sensor_%22 + Instant.now().toEpochMilli())%0A                .put(%22source%22, docToindex);%0A%0A        vertx.eventBus().send(%22elastic.index%22, message, msg -%3E %7B%0A            if (msg.succeeded()) %7B%0A                logger.info(msg.result().body());%0A            %7D%0A            else %7B%0A                logger.error(%22indexWheaterIntoES Error ...%22, msg.cause());%0A            %7D%0A        %7D);%0A
 %0A    %7D%0A%0A

index bmp85 measures to elasticsearch