Network audio

Go to Git Hub

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

networkaudio

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.
var ocfDevice;
var className = "NetworkAudio";
var capabilities = [capabilitySwitch, capabilityAudioTrackData, capabilityAudioVolume, capabilityMediaPlayback, capabilityMediaPlaybackRepeat, capabilityMediaPlaybackShuffle, capabilityMediaInputSource, capabilityMediaTrackControl];
var inlineStyle;

window.onload = function () {
    console.log("version : 0.0.1");
    init();

    inlineStyle = document.createElement('style');
    document.body.appendChild(inlineStyle);
};

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 setMainDevice(device) {
    scplugin.log.debug(className, arguments.callee.name, "set ocf device : " + device.deviceName);
    ocfDevice = device;
}

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

function onPowerBtnClicked() {
    capabilitySwitch.powerToggle();
}

function onSelectSource(selectedItem) {
    capabilityMediaInputSource.set(selectedItem.value);
}

function onPrevClicked() {
    capabilityMediaTrackControl.set("previous");
}

function onPlayClicked() {
    capabilityMediaPlayback.toggle();
}

function onNextClicked() {
    capabilityMediaTrackControl.set("next");
}

function onVolumeClicked() {
    capabilityAudioVolume.muteToggle();
}

function InputVolume(rangeId, rangeValue) {
        capabilityAudioVolume.setVolume(parseInt(rangeValue));
}

function onSelectRepeated() {
    document.getElementById("repeat_listbox").classList.toggle("show");
}

function closeListBox(event) {
    var x1 = event.offsetLeft;
    var y1 = event.offsetTop;
    var x2 = event.offsetWidth;
    var y2 = event.offsetHeight;
    var x = event.onmouseout.arguments["0"].clientX;
    var y = event.onmouseout.arguments["0"].clientY;
    if(x < x1 || x > x1+x2)
        capabilityMediaPlaybackRepeat.closeListbox();
    else if(y < y1 || y > y1+y2)
        capabilityMediaPlaybackRepeat.closeListbox();
}

function onSelectOff() {
    capabilityMediaPlaybackRepeat.set("off");
}

function onSelectOne() {
    capabilityMediaPlaybackRepeat.set("one");
}

function onSelectAll() {
    capabilityMediaPlaybackRepeat.set("all");
}

function onClickShuffle(suffleMode) {
    if(suffleMode.checked == true) {
        capabilityMediaPlaybackShuffle.set("enabled");
        suffleMode.checked = false;
    }
    else {
        capabilityMediaPlaybackShuffle.set("disabled");
        suffleMode.checked = true;
    }
}

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);
       },