/* * Copyright © 2016, 2019, 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 UIKit // Import the DeviceLib library import DeviceLib class ViewController: UIViewController { var dcd:DirectlyConnectedDevice? override func viewDidLoad() { super.viewDidLoad() // The device model URN let URN: String = "urn:test:helloworld" // Change the value to the actual provisioning file name. let CONFIG_FILE: String = "MyFile.conf" // Change the value to the actual passphrase let PASSPHRASE: String = "Changeit0" // The custom attribute name let MESSAGE_ATTRIBUTE: String = "greeting" do { // The configuration is defined by the trusted asset store. // The location and passphrase of the asset store can be specified // in a property list or other data file. For simplicity we have // hard coded an example file name and passphrase. In this example // the file is located in the top level bundle directory. dcd = try DirectlyConnectedDevice( path: Bundle.main.bundlePath + "/" + CONFIG_FILE, password: PASSPHRASE) // This closure will be called if the device is activated // It sends a single message to the server. let greet: (_ endpointId: String?, _ error: ClientError?) -> () = { endpointId, error in // If there was an error handle the error if let error = error { print ("\(error)") return } do { // Set up a virtual device based on your device model try self.dcd?.getDeviceModel(deviceModelUrn: URN, callback: {(deviceModel, error) in if let error = error { print("\(error)") return } // It is not an error if the device model does not exist. guard let deviceModel = deviceModel else { print("Device Model \(URN) not found") return } do { let virtualDevice = try self.dcd?.createVirtualDevice(deviceId: (self.dcd?.getEndpointId())!, deviceModel: deviceModel) // Update an attribute in your virtual device. A // change in the value of an attribute triggers a // message to the Cloud Service with the updated // attribute value. _ = try virtualDevice?.set(attributeName: MESSAGE_ATTRIBUTE, attributeValue: "Hello World!" as AnyObject); print("Virtual device created.") } catch { print(error) } }) } catch { print(error) } } // Activate the device if it not activated (so that you can run the // code more than once!). If it is activated call the greet // closure directly. If it is not activated, activate will call // the greet closure after it is activated or if there is an error // during the activation attempt. if !(dcd?.isActivated())! { try dcd?.activate(deviceModels: [URN], callback: greet) } else { greet(dcd?.getEndpointId(), nil) } } catch { print(error) } } override func viewDidDisappear(_ animated: Bool) { // If not supporting running in the background close the device // If there is support for running in the background, then use // notifications to be notified when the application is about to be // closed and perform any resource cleanup in the notification handler. // See .UIApplicationWillTerminate do { try dcd?.close() } catch { print("dcd.close() threw exception \(error)") } } }