Air conditioner

Go to Git Hub

This example demonstrates how to implement a device plugin for a air conditioner, which has the following capabilities:

aircon

This device plugin is for a directly connected device. For more information, see here.

Prerequisites

  • Install the Atom IDE for Windows.
  • Install the SmartThings SDK Windows plugin for Atom.
  • Install SmartThings app v1.6.05 or higher on:
    • Android 6.0 or higher device
    • iPhone running iOS 10.0 or higher

Run the plugin

You can then control and test the virtual device with the SmartThings app or the device plugin simulator. For more information on testing, see here .


About the sample

index.js

  • init() - gets the OCF device object with the getOCFDevices API
  • getOCFDeviceCB() - uses the subscribe API to listen for data changes on the device with onRepresentCallback(). This callback function is invoked when representation data received from cloud.
  • backAction() - terminates the plugin and returns the user to the SmartThings app.
function init() {
    console.log("-----------init-----------");
    scplugin.manager.getOCFDevices(getOCFDeviceCB);
}

function getOCFDeviceCB(devices) {
    console.log("getOCFDeviceCB : " + devices.length);

    for (var i in devices) {
        console.log("deviceHandle: " + devices[i].deviceHandle);
        console.log("deviceName: " + devices[i].deviceName);
        console.log("deviceType: " + devices[i].deviceType);
        console.log("metadata: " + devices[i].metadata);
    }
    setMainDevice(devices[0]);
    ocfDevice.subscribe(onRepresentCallback);

    for (var i = 0; i < capabilities.length; i++) {
        capabilities[i].update();
    }
}

function onRepresentCallback(result, deviceHandle, uri, rcsJsonString) {
    for (var i = 0; i < capabilities.length; i++) {
        if ( capabilities[i].href == uri) {
            capabilities[i].onRepresentCallback(result, deviceHandle, uri, rcsJsonString);
        }
    }
}

…
function backAction() {
  scplugin.manager.close();
}

capability_xx.js

'update' : function() {
        ocfDevice.getRemoteRepresentation(this.href, this.onRepresentCallback);
},
…
'set' : function(state) {
        scplugin.log.debug(className, arguments.callee.name, "power : " + state);
        var setRcsJson = {};
        setRcsJson["power"] = state;
        ocfDevice.setRemoteRepresentation(this.href, setRcsJson, this.onRepresentCallback);
},