package com.italtel.iota.demo;

import java.util.Date;
import java.util.Map;

import org.eclipse.kura.message.KuraPayload;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SendMeasureJob implements Job {

    public static final Logger s_logger = LoggerFactory.getLogger(SendMeasureJob.class);

    private VirtualGasMeter virtualGasMeter;

    public void setVirtualGasMeter(VirtualGasMeter virtualGasMeter) {
        this.virtualGasMeter = virtualGasMeter;
    }

    public void execute(JobExecutionContext context) throws JobExecutionException {
        synchronized (virtualGasMeter) {
            Map<String, Object> m_properties = virtualGasMeter.getProperties();

            // fetch the publishing configuration from the publishing properties
            String topic = (String) m_properties.get(VirtualGasMeter.PUBLISH_TOPIC_PROP_NAME);
            Integer qos = (Integer) m_properties.get(VirtualGasMeter.PUBLISH_QOS_PROP_NAME);
            Boolean retain = (Boolean) m_properties.get(VirtualGasMeter.PUBLISH_RETAIN_PROP_NAME);

            Date currentDate = new Date();
            int meterNumber = virtualGasMeter.getMeasures().size();
            for (int i = 0; i < meterNumber; i++) {
                String nameMeter = VirtualGasMeter.METER_PREFIX_NAME + i;
                double comsumption = virtualGasMeter.getRandom().nextDouble() * 4;
                double measure = virtualGasMeter.getMeasures().get(nameMeter) + comsumption;
                virtualGasMeter.getMeasures().put(nameMeter, measure);

                String finalTopic = new StringBuilder(nameMeter).append("/").append(topic).toString();

                // Allocate a new payload
                KuraPayload payload = new KuraPayload();

                // Timestamp the message
                payload.setTimestamp(currentDate);

                // Add metric to the payload
                payload.addMetric("value", measure);
                payload.addMetric("meter", nameMeter);

                byte[] body = payload.getBody();
                String meterBody = new String(body);
                s_logger.info("Published meterBody {}", meterBody);

                // Publish the message
                try {
                    virtualGasMeter.getCloudClient().publish(finalTopic, payload, qos, retain);
                    s_logger.info("Published message to {} for {}", finalTopic, nameMeter);
                } catch (Exception e) {
                    s_logger.error("Cannot publish on topic {} for {}: {}", finalTopic, nameMeter, e.getMessage(), e);
                }

            }
        }
    }

}
@@ -80,54 +80,8 @@
 %0D%0A%0D%0A
-import org.eclipse.kura.message.KuraPayload;%0D%0A
 impo
@@ -1160,20 +1160,20 @@
     
-Date
+Long
  current
 Date
@@ -1168,20 +1168,25 @@
  current
-Date
+Timestamp
  = new D
@@ -1190,16 +1190,26 @@
 w Date()
+.getTime()
 ;%0D%0A     
@@ -1348,25 +1348,25 @@
  String 
-nameMeter
+meterName
  = Virtu
@@ -1551,25 +1551,25 @@
 s().get(
-nameMeter
+meterName
 ) + coms
@@ -1628,25 +1628,25 @@
 s().put(
-nameMeter
+meterName
 , measur
@@ -1706,25 +1706,25 @@
 Builder(
-nameMeter
+meterName
 ).append
@@ -1778,518 +1778,260 @@
     
-// Allocate a new payload%0D%0A                KuraPayload payload = new KuraPayload();%0D%0A%0D%0A                // Timestamp the message%0D%0A                payload.setTimestamp(currentDate);%0D%0A%0D%0A                // Add metric to the payload%0D%0A                payload.addMetric(%22value%22, measure);%0D%0A                payload.addMetric(%22meter%22, nameMeter);%0D%0A%0D%0A                byte%5B%5D body = payload.getBody();%0D%0A                String meterBody = new String(body);%0D%0A                s_logger.info(%22Published meterBody %7B%7D%22, meterBody
+String content = new StringBuilder(%22%7B%22).append(%22%5C%22timestamp%5C%22: %22).append(currentTimestamp)%0D%0A                        .append(%22, %5C%22meter%5C%22: %5C%22%22).append(meterName).append(%22%5C%22, %5C%22measure%5C%22: %22).append(measure)%0D%0A                        .append(%22%7D%22).toString(
 );%0D%0A
@@ -2172,15 +2172,26 @@
 ic, 
-payload
+content.getBytes()
 , qo
@@ -2199,16 +2199,19 @@
 , retain
+, 5
 );%0D%0A    
@@ -2286,25 +2286,25 @@
 lTopic, 
-nameMeter
+meterName
 );%0D%0A    
@@ -2432,17 +2432,17 @@
 ic, 
-nameMeter
+meterName
 , e.

Fix json message