com.adobe.cq.screens.impl.aemio.ScreensDeviceModel Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
************************************************************************/
package com.adobe.cq.screens.impl.aemio;
import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceManager;
import com.adobe.granite.haf.annotations.ApiAction;
import com.adobe.granite.haf.annotations.ApiModel;
import com.adobe.granite.haf.annotations.HttpFormParam;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import javax.inject.Inject;
import javax.jcr.RepositoryException;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_ACT_SEND_COMMAND;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_ACT_SEND_COMMAND_WITH_ACK;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_DEVICE_COMMAND;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_DEVICE_COMMAND_ACK;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_DEVICE_COMMAND_PAYLOAD;
/**
* Models a device.
*
* Note this is currently not used, because the devices can't be mapped into the tree.
*/
@Model(adaptables = Resource.class)
@ApiModel(category = ScreensModelLookup.CATEGORY, type={"aem-io/screens/device"}, modelLookup = ScreensModelLookup.class)
public class ScreensDeviceModel {
@Inject
@Self
protected Resource baseResource;
@ApiAction(method = "POST", name = REST_ACT_SEND_COMMAND)
public void executeCommand(
@HttpFormParam(REST_DEVICE_COMMAND) String msg,
@HttpFormParam(value = REST_DEVICE_COMMAND_PAYLOAD, optional = true) String msgPayload
) throws RepositoryException, JSONException {
executeCommand(msg, msgPayload, false);
}
@ApiAction(method = "POST", name = REST_ACT_SEND_COMMAND_WITH_ACK)
public void executeCommand(
@HttpFormParam(REST_DEVICE_COMMAND) String msg,
@HttpFormParam(value = REST_DEVICE_COMMAND_PAYLOAD, optional = true) String msgPayload,
@HttpFormParam(value = REST_DEVICE_COMMAND_ACK, optional = true) Boolean requiresAck
) throws RepositoryException, JSONException {
DeviceManager dm = baseResource.getResourceResolver().adaptTo(DeviceManager.class);
if (dm == null) {
return;
}
Device device = baseResource.adaptTo(Device.class);
if (device == null) {
return;
}
JSONObject payload = null;
if (msgPayload != null) {
payload = new JSONObject(msgPayload);
}
dm.executeCommand(device, msg, payload, requiresAck != null && requiresAck);
}
}