/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2015,2016 
*/
package quarks.samples.connectors.iotf;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.google.gson.JsonObject;

import quarks.connectors.iot.IotDevice;
import quarks.connectors.iot.QoS;
import quarks.connectors.iotf.IotfDevice;
import quarks.providers.direct.DirectProvider;
import quarks.topology.TStream;
import quarks.topology.Topology;

/**
 * IBM Watson IoT Platform Quickstart sample.
 * Submits a JSON device event every second using the
 * same format as the IoTF device simulator,
 * with keys {@code temp}, {@code humidity}  and {@code objectTemp}
 * and random values.
 * 
 * The device type is {@code iotsamples-quarks} and a random
 * device identifier is generated. Both are printed out when
 * the application starts.
 */
public class IotfQuickstart {

    public static void main(String[] args) {

        DirectProvider tp = new DirectProvider();
        Topology topology = tp.newTopology("IotfQuickstart");
        
        // Declare a connection to IoTF Quickstart service
        String deviceId = "qs" + Long.toHexString(new Random().nextLong());
        IotDevice device = IotfDevice.quickstart(topology, deviceId);
        
        System.out.println("Quickstart device type:" + IotfDevice.QUICKSTART_DEVICE_TYPE);
        System.out.println("Quickstart device id  :" + deviceId);
             
        Random r = new Random();
        TStream<double[]> raw = topology.poll(() -> {
            double[]  v = new double[3];
            
            v[0] = r.nextGaussian() * 10.0 + 40.0;
            v[1] = r.nextGaussian() * 10.0 + 50.0;
            v[2] = r.nextGaussian() * 10.0 + 60.0;
            
            return v;
        }, 1, TimeUnit.SECONDS);
        
        TStream<JsonObject> json = raw.map(v -> {
            JsonObject j = new JsonObject();
            j.addProperty("temp", v[0]);
            j.addProperty("humidity", v[1]);
            j.addProperty("objectTemp", v[2]);
            return j;
        });
        
        device.events(json, "sensors", QoS.FIRE_AND_FORGET);
        

        tp.submit(topology);
    }
}

@@ -567,20 +567,26 @@
  as the 
-IoTF
+Quickstart
  device 
@@ -689,16 +689,19 @@
 ues.%0A * 
+%3CP%3E
 %0A * The 
@@ -842,16 +842,126 @@
 starts.%0A
+ * %3C/P%3E%0A * A URL is also printed that allows viewing of the data%0A * as it received by the Quickstart service.%0A
  */%0Apubl
@@ -1517,32 +1517,146 @@
 :%22 + deviceId);%0A
+        System.out.println(%22https://quickstart.internetofthings.ibmcloud.com/#/device/%22%0A             + deviceId);%0A
              %0A  
@@ -2287,32 +2287,32 @@
    %7D);%0A        %0A
+
         device.e
@@ -2355,25 +2355,16 @@
 FORGET);
-%0A        
 %0A%0A      

Print URL to use for IotfQuickstart