/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
*/

package quarks.apps.iot;

import com.google.gson.JsonObject;

import quarks.connectors.iot.IotDevice;
import quarks.connectors.pubsub.PublishSubscribe;
import quarks.topology.TStream;
import quarks.topology.TopologyElement;

/**
 * Application sharing an {@code IotDevice} through publish-subscribe. <BR>
 * This application shares an {@link IotDevice} across multiple running
 * jobs. This allows a single connection to a back-end message hub to be shared
 * across multiple independent applications, without having to build a single
 * topology.
 * <P>
 * Applications coded to {@link IotDevice} obtain a topology specific
 * {@code IotDevice} using {@link #addIotDevice(TopologyElement)}. This returned
 * device will route events and commands to/from the actual message hub
 * {@code IotDevice} through publish-subscribe.
 * <P>
 * An instance of this application is created by first creating a new topology and
 * then creating a {@link IotDevice} specific to the desired message hub. Then
 * the application is created by calling {@link #createApplication(IotDevice)}
 * passing the {@code IotDevice}. <BR>
 * Then additional independent applications (topologies) can be created and they
 * create a proxy {@code IotDevice} for their topology using
 * {@link #addIotDevice(TopologyElement)}. This proxy {@code IotDevice} is then
 * used to send device events and receive device commands in that topology. <BR>
 * Once all the topologies have been declared they can be submitted.
 * </P>
 * 
 * @see PublishSubscribe
 */
public class IotDevicePubSub {
    
    /**
     * IotDevicePubSub application name.
     */
    public static final String APP_NAME = "QuarksIotDevicePubSub";
    
    /**
     * Events published to topic {@value} are sent as device events using the
     * actual message hub {@link IotDevice}. <BR>
     * it is recommended applications use the {@code IotDevice} returned by
     * {@link #addIotDevice(TopologyElement)} to send events rather than
     * publishing streams to this topic.
     */
    public static final String EVENTS_TOPIC = PublishSubscribe.RESERVED_TOPIC_PREFIX + "iot/events";
    
    /**
     * Device commands are published to {@value} by
     * this application. <BR>
     * it is recommended applications use the {@code IotDevice} returned by
     * {@link #addIotDevice(TopologyElement)} to receive commands rather than
     * subscribing to streams with this topic prefix.
     */
    public static final String COMMANDS_TOPIC = PublishSubscribe.RESERVED_TOPIC_PREFIX + "iot/commands";

    /**
     * Create an instance of this application using {@code device} as the device
     * connection to a message hub.
     */
    public static void createApplication(IotDevice device) {

        TStream<JsonObject> events = PublishSubscribe.subscribe(device, EVENTS_TOPIC, JsonObject.class);

        device.events(events, ew -> ew.getAsJsonPrimitive("eventId").getAsString(), ew -> ew.getAsJsonObject("event"),
                ew -> ew.getAsJsonPrimitive("qos").getAsInt());
        
        PublishSubscribe.publish(device.commands(), COMMANDS_TOPIC, JsonObject.class);
    }

    /**
     * Add a proxy {@code IotDevice} for the topology containing {@code te}.
     * <P>
     * Any events sent through the returned device are sent onto the message hub
     * device through publish-subscribe. <BR>
     * Subscribing to commands using the returned device will subscribe to
     * commands received by the message hub device.
     * </P>
     * 
     * @param te
     *            Topology the returned device is contained in.
     * @return Proxy device.
     */
    public static IotDevice addIotDevice(TopologyElement te) {
        return new PubSubIotDevice(te.topology());
    }
}

@@ -846,25 +846,96 @@
 ors.iot.
-IotDevice
+Events;%0Aimport quarks.connectors.iot.IotDevice;%0Aimport quarks.connectors.iot.QoS
 ;%0Aimport
@@ -2319,16 +2319,360 @@
  * %3C/P%3E%0A
+ * %3CP%3E%0A * At startup this application sends a single device event with%0A * identifier %7B@link Events#IOT_START%7D. This performs two functions:%0A * %3CUL%3E%0A * %3CLI%3EInitiates a connection to the message hub.%3C/LI%3E%0A * %3CLI%3EAllows external applications to be notified (by subscribing to device events)%0A * when a Quarks provider starts.%3C/LI%3E%0A * %3C/UL%3E%0A * %3C/P%3E%0A
  * %0A * @
@@ -4202,24 +4202,24 @@
 );%0A        %0A
-
         Publ
@@ -4293,16 +4293,212 @@
 class);%0A
+        %0A        // Publish a single event at startup%0A        TStream%3CJsonObject%3E start = device.topology().of(new JsonObject());%0A        device.events(start, Events.IOT_START, QoS.AT_MOST_ONCE);%0A
     %7D%0A%0A 

Send event at startup