/* * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. * * This software is dual-licensed to you under the MIT License (MIT) and * the Universal Permissive License (UPL). See the LICENSE file in the root * directory for license terms. You may choose either license, or both. */ import oracle.iot.client.DeviceModel; import oracle.iot.client.device.DirectlyConnectedDevice; import oracle.iot.client.device.VirtualDevice; /** * An example of sending a "Hello World!" message to the * Oracle IoT Cloud Service. This example is meant to illustrate the * minimal Java code for sending data to the cloud service and does not * fully explore the Client Library API. *
* The example uses the concept of a "virtual device", which * can be thought of as a wrapper around a "device model". A * device model is a set of related attributes, actions, and message formats * that can be represented in a real device. For this example, a * model for a fictitious device, the "urn:test:helloworld" device model, is * used. This device model must be uploaded to the server before running this * example. *
* { * "urn": "urn:test:helloworld", * "name": "HelloWorld-DeviceModel", * "description": "Device model for the HelloWorld application", * "attributes": [ * { * "name": "greeting", * "description": "This attribute stores the Hello World message.", * "type": "STRING" * } * ] * } */ public class HelloWorld { // This is the URN of model for our fictitious device. This URN is used // to load the device model from the server into memory. private final static String DEVICE_MODEL_URN = "urn:test:helloworld"; private static final String GREETING_ATTRIBUTE = "greeting"; public static void main(String[] args) throws Exception { // A directly connected device is used for this example. // // The configuration is defined by the trusted asset store. // The location and passphrase of the asset store are specified // using command-line arguments. DirectlyConnectedDevice dcd = new DirectlyConnectedDevice(args[0], args[1]); // Activate the device, if it is not already activated. // Always check if the device is activated before calling activate. // The device model URN is passed into the activate call to tell // the server the device model(s) that are supported by this // directly connected device. if (!dcd.isActivated()) { dcd.activate(DEVICE_MODEL_URN); } // Before creating the virtual device for the device model, // the device model itself is needed. DeviceModel deviceModel = dcd.getDeviceModel(DEVICE_MODEL_URN); // Create the virtual device and associate it with the endpoint id // of the directly connected device. String endpointId = dcd.getEndpointId(); VirtualDevice virtualDevice = dcd.createVirtualDevice(endpointId, deviceModel); // Once the virtual device is created, any of the attributes in // the model can be set. In this example, there is only one // attribute, named "message" in the model. Calling set on this // attribute will cause a data message, "Hello World!" in this // example, to be sent to the server. virtualDevice.set(GREETING_ATTRIBUTE, "Hello World!"); System.out.println("Hello World! message sent."); // Calling close on the directly connected device ensures // communications channels are closed and resources are // cleaned up. dcd.close(); } }