Commit c418c672 authored by Larry Sachs's avatar Larry Sachs

Update for OCF Light Servers

Change-Id: I0a7d686321650869d749232969ee8c546ed6c397
Signed-off-by: Larry Sachs's avatarLarry Sachs <larry.j.sachs@intel.com>
parent a9080081
......@@ -33,7 +33,8 @@ import org.iotivity.base.OcRepresentation;
*/
public class BinarySwitch extends Service {
public static final String UPNP_OIC_TYPE_BINARY_SWITCH = "oic.r.switch.binary";
public static final String OIC_TYPE_BINARY_SWITCH = "oic.r.switch.binary";
public static final String OCF_OIC_URI_PREFIX_BINARY_SWITCH = "/ocf/switch/";
public static final String UPNP_OIC_URI_PREFIX_BINARY_SWITCH = "/upnp/switch/";
public static final String VALUE_KEY = "value";
......
......@@ -33,7 +33,8 @@ import org.iotivity.base.OcRepresentation;
*/
public class Brightness extends Service {
public static final String UPNP_OIC_TYPE_BRIGHTNESS = "oic.r.light.brightness";
public static final String OIC_TYPE_BRIGHTNESS = "oic.r.light.brightness";
public static final String OCF_OIC_URI_PREFIX_BRIGHTNESS = "/ocf/brightness/";
public static final String UPNP_OIC_URI_PREFIX_BRIGHTNESS = "/upnp/brightness/";
public static final String BRIGHTNESS_KEY = "brightness";
......
/*
* //******************************************************************
* //
* // Copyright 2016 Intel Corporation All Rights Reserved.
* //
* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
* //
* // Licensed under the Apache License, Version 2.0 (the "License");
* // you may not use this file except in compliance with the License.
* // You may obtain a copy of the License at
* //
* // http://www.apache.org/licenses/LICENSE-2.0
* //
* // Unless required by applicable law or agreed to in writing, software
* // distributed under the License is distributed on an "AS IS" BASIS,
* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* // See the License for the specific language governing permissions and
* // limitations under the License.
* //
* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
package org.iotivity.base.examples;
import org.iotivity.base.OcException;
import org.iotivity.base.OcRepresentation;
/**
* Configuration
* <p/>
* This class is used by UpnpClientActivity to create an object representation of a remote configuration service
* and update the values depending on the server response
*/
public class Configuration extends Service {
public static final String OIC_TYPE_CONFIG = "oic.wk.con";
public static final String OCF_OIC_URI_PREFIX_CONFIG = "/ocf/light-config/"; // TODO: regex match "/ocf/.*-config/"
public static final String NAME_KEY = "n";
private String mName;
private boolean mIsInitialized;
public Configuration() {
super();
mName = "";
}
public void setOcRepresentation(OcRepresentation rep) throws OcException {
super.setOcRepresentation(rep);
if (rep.hasAttribute(NAME_KEY)) {
Object obj = rep.getValue(NAME_KEY);
if (obj instanceof String) {
mName = (String) obj;
mIsInitialized = true;
}
}
}
public OcRepresentation getOcRepresentation() throws OcException {
OcRepresentation rep = super.getOcRepresentation();
rep.setValue(NAME_KEY, mName);
return rep;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public boolean isInitialized() {
return mIsInitialized;
}
@Override
public String toString() {
return "[" + super.toString() +
", " + "initialized: " + mIsInitialized +
", " + NAME_KEY + ": " + mName + "]";
}
}
......@@ -56,6 +56,7 @@ abstract public class Device extends Resource {
private String mUpc;
private Links mLinks;
private boolean mHasLinksProperty;
public Device() {
super();
......@@ -105,6 +106,7 @@ abstract public class Device extends Resource {
if (rep.hasAttribute(LINKS_KEY)) {
OcRepresentation[] links = rep.getValue(LINKS_KEY);
mLinks.setOcRepresentation(links);
mHasLinksProperty = true;
}
}
......@@ -122,7 +124,9 @@ abstract public class Device extends Resource {
rep.setValue(UPC_KEY, mUpc);
OcRepresentation[] links = mLinks.getOcRepresentation();
rep.setValue(LINKS_KEY, links);
if ((links != null) && (links.length > 0)) {
rep.setValue(LINKS_KEY, links);
}
return rep;
}
......@@ -199,6 +203,10 @@ abstract public class Device extends Resource {
mUpc = upc;
}
public boolean hasLinksProperty() {
return mHasLinksProperty;
}
public Links getLinks() {
return mLinks;
}
......@@ -219,6 +227,7 @@ abstract public class Device extends Resource {
", " + SERIAL_NUMBER_KEY + ": " + mSerialNumber +
", " + PRESENTATION_URL_KEY + ": " + mPresentationUrl +
", " + UPC_KEY + ": " + mUpc +
", " + "hasLinksProp: " + mHasLinksProperty +
", " + LINKS_KEY + mLinks + "]";
}
}
......@@ -33,11 +33,19 @@ import org.iotivity.base.OcRepresentation;
*/
public class Light extends Device {
public static final String UPNP_OIC_TYPE_DEVICE_LIGHT = "oic.d.light";
public static final String OIC_TYPE_DEVICE_LIGHT = "oic.d.light";
public static final String OCF_OIC_URI_PREFIX_LIGHT = "/ocf/light/";
public static final String UPNP_OIC_URI_PREFIX_LIGHT = "/upnp/light/";
public static final String OIC_URI_PREFIX_LIGHT = "/light/";
private BinarySwitch mBinarySwitch;
private Brightness mBrightness;
private Configuration mConfig;
private String mName = "Unknown";
private boolean mState = BinarySwitch.DEFAULT_VALUE;
private int mLightLevel = Brightness.DEFAULT_BRIGHTNESS;
public Light() {
super();
......@@ -45,10 +53,31 @@ public class Light extends Device {
public void setOcRepresentation(OcRepresentation rep) throws OcException {
super.setOcRepresentation(rep);
if (rep.hasAttribute(Configuration.NAME_KEY)) {
Object obj = rep.getValue(Configuration.NAME_KEY);
if (obj instanceof String) {
mName = (String) obj;
}
}
if (rep.hasAttribute(BinarySwitch.VALUE_KEY)) {
Object obj = rep.getValue(BinarySwitch.VALUE_KEY);
if (obj instanceof Boolean) {
mState = (Boolean) obj;
}
}
if (rep.hasAttribute(Brightness.BRIGHTNESS_KEY)) {
Object obj = rep.getValue(Brightness.BRIGHTNESS_KEY);
if (obj instanceof Integer) {
mLightLevel = (Integer) obj;
}
}
}
public OcRepresentation getOcRepresentation() throws OcException {
OcRepresentation rep = super.getOcRepresentation();
rep.setValue(Configuration.NAME_KEY, mName);
rep.setValue(BinarySwitch.VALUE_KEY, mState);
rep.setValue(Brightness.BRIGHTNESS_KEY, mLightLevel);
return rep;
}
......@@ -68,35 +97,62 @@ public class Light extends Device {
mBrightness = brightness;
}
public boolean getState() {
boolean state = BinarySwitch.DEFAULT_VALUE;
public Configuration getConfiguration() {
return mConfig;
}
public void setConfiguration(Configuration config) {
mConfig = config;
}
public String getName() {
if (mConfig != null) {
mName = mConfig.getName();
}
return mName;
}
public void setName(String name) {
if (mConfig != null) {
mConfig.setName(name);
} else {
mName = name;
}
}
public boolean getState() {
if (mBinarySwitch != null) {
state = mBinarySwitch.getValue();
mState = mBinarySwitch.getValue();
}
return state;
return mState;
}
public void setState(boolean state) {
if (mBinarySwitch != null) {
mBinarySwitch.setValue(state);
} else {
mState = state;
}
}
public int getLightLevel() {
int lightLevel = Brightness.DEFAULT_BRIGHTNESS;
if (mBrightness != null) {
lightLevel = mBrightness.getBrightness();
mLightLevel = mBrightness.getBrightness();
}
return lightLevel;
return mLightLevel;
}
public void setLightLevel(int lightLevel) {
if (mBrightness != null) {
mBrightness.setBrightness(lightLevel);
} else {
mLightLevel = lightLevel;
}
}
......@@ -104,8 +160,18 @@ public class Light extends Device {
public String toString() {
String binarySwitchAsString = mBinarySwitch != null ? mBinarySwitch.toString() : "binary switch is null";
String brightnessAsString = mBrightness != null ? mBrightness.toString() : "brightness is null";
return "[" + super.toString() +
", " + binarySwitchAsString +
", " + brightnessAsString + "]";
String configAsString = mConfig != null ? mConfig.toString() : "configuration is null";
if (mConfig != null) {
return "[" + super.toString() +
", " + binarySwitchAsString +
", " + brightnessAsString +
", " + configAsString + "]";
} else {
return "[" + super.toString() +
", " + Configuration.NAME_KEY + ": " + mName +
", " + BinarySwitch.VALUE_KEY + ": " + mState +
", " + Brightness.BRIGHTNESS_KEY + ": " + mLightLevel + "]";
}
}
}
......@@ -39,12 +39,12 @@ public class Link {
private String mHref;
private String mRel;
private String mRt;
private Object mRt; // could be String or String[]
public Link() {
mHref = "";
mRel = "";
mRt = "";
mRt = new Object();
}
public void setOcRepresentation(OcRepresentation ocRep) throws OcException {
......@@ -63,8 +63,14 @@ public class Link {
public OcRepresentation getOcRepresentation() throws OcException {
OcRepresentation ocRep = new OcRepresentation();
ocRep.setValue(HREF_KEY, mHref);
ocRep.setValue(REL_KEY, mHref);
ocRep.setValue(RT_KEY, mHref);
ocRep.setValue(REL_KEY, mRel);
if (mRt instanceof String) {
ocRep.setValue(RT_KEY, (String) mRt);
} else if (mRt instanceof String[]) {
ocRep.setValue(RT_KEY, (String[]) mRt);
} else {
// this is gonna fail...
}
return ocRep;
}
......@@ -84,18 +90,43 @@ public class Link {
mRel = rel;
}
public String getRt() {
public Object getRt() {
return mRt;
}
public void setRt(String rt) {
public void setRt(Object rt) {
mRt = rt;
}
@Override
public String toString() {
String rtAsString = null;
Object rt = getRt();
if (rt instanceof String) {
rtAsString = (String) rt;
} else if (rt instanceof String[]) {
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean first = true;
for (String type : (String[]) rt) {
if (!first) {
sb.append(", ");
}
if (type != null) {
sb.append(type.toString());
}
first = false;
}
sb.append("]");
rtAsString = sb.toString();
} else {
rtAsString = "Unknown rt type of " + rt.getClass().getName();
}
return "[" + HREF_KEY + ": " + getHref() +
", " + REL_KEY + ": " + getRel() +
", " + RT_KEY + ": " + getRt() + "]";
", " + RT_KEY + ": " + rtAsString + "]";
}
}
......@@ -47,6 +47,7 @@ import java.util.Map;
public class OnOffCycleIntentService extends IntentService implements
OcPlatform.OnResourceFoundListener,
OcResource.OnGetListener,
OcResource.OnPutListener,
OcResource.OnPostListener {
private final static String TAG = OnOffCycleIntentService.class.getSimpleName();
......@@ -84,8 +85,8 @@ public class OnOffCycleIntentService extends IntentService implements
mResourceLookup.clear();
mResourceUriToParentDeviceLookup.clear();
Log.i(TAG, "Finding all resources of type " + Light.UPNP_OIC_TYPE_DEVICE_LIGHT);
String requestUri = OcPlatform.WELL_KNOWN_QUERY + "?rt=" + Light.UPNP_OIC_TYPE_DEVICE_LIGHT;
Log.i(TAG, "Finding all resources of type " + Light.OIC_TYPE_DEVICE_LIGHT);
String requestUri = OcPlatform.WELL_KNOWN_QUERY + "?rt=" + Light.OIC_TYPE_DEVICE_LIGHT;
OcPlatform.findResource("", requestUri, EnumSet.of(OcConnectivityType.CT_DEFAULT), this);
} catch (OcException e) {
......@@ -115,7 +116,9 @@ public class OnOffCycleIntentService extends IntentService implements
// We are only interested in light resources
// For now, we are only interested in light resources
if (resourceUri.startsWith(Light.UPNP_OIC_URI_PREFIX_LIGHT)) {
if (resourceUri.startsWith(Light.UPNP_OIC_URI_PREFIX_LIGHT)
|| resourceUri.startsWith(Light.OCF_OIC_URI_PREFIX_LIGHT)
|| resourceUri.startsWith(Light.OIC_URI_PREFIX_LIGHT)) {
if (!mResourceLookup.containsKey(resourceUri)) {
Log.i(TAG, "URI of the new light resource: " + resourceUri);
......@@ -174,7 +177,8 @@ public class OnOffCycleIntentService extends IntentService implements
if (!mResourceLookup.containsKey(resourceUri)) {
Log.i(TAG, "URI of the new linked resource: " + resourceUri);
if (resourceUri.startsWith(BinarySwitch.UPNP_OIC_URI_PREFIX_BINARY_SWITCH)) {
if (resourceUri.startsWith(BinarySwitch.UPNP_OIC_URI_PREFIX_BINARY_SWITCH)
|| resourceUri.startsWith(BinarySwitch.OCF_OIC_URI_PREFIX_BINARY_SWITCH)) {
BinarySwitch binarySwitch = new BinarySwitch();
binarySwitch.setUri(resourceUri);
......@@ -187,7 +191,8 @@ public class OnOffCycleIntentService extends IntentService implements
tracked = true;
}
} else if (resourceUri.startsWith(Brightness.UPNP_OIC_URI_PREFIX_BRIGHTNESS)) {
} else if (resourceUri.startsWith(Brightness.UPNP_OIC_URI_PREFIX_BRIGHTNESS)
|| resourceUri.startsWith(Brightness.OCF_OIC_URI_PREFIX_BRIGHTNESS)) {
Brightness brightness = new Brightness();
brightness.setUri(resourceUri);
......@@ -273,9 +278,28 @@ public class OnOffCycleIntentService extends IntentService implements
Links links = device.getLinks();
for (Link link : links.getLinks()) {
String href = link.getHref();
String rt = link.getRt();
String requestUri = OcPlatform.WELL_KNOWN_QUERY + "?rt=" + rt;
OcPlatform.findResource("", requestUri, EnumSet.of(OcConnectivityType.CT_DEFAULT), new ResourceFoundListener(ocRepUri, href));
// rt could be String or String[]
Object rt = link.getRt();
String rtAsString = null;
if (rt instanceof String) {
rtAsString = (String) rt;
} else if (rt instanceof String[]) {
if (((String[]) rt).length > 0) {
rtAsString = ((String[]) rt)[0];
} else {
Log.e(TAG, "(String[])rt is empty");
}
} else {
Log.e(TAG, "Unknown rt type of " + rt.getClass().getName());
}
if ((rtAsString != null) && (!mResourceLookup.containsKey(href))) {
Log.i(TAG, "Finding all resources of type " + rtAsString);
String requestUri = OcPlatform.WELL_KNOWN_QUERY + "?rt=" + rtAsString;
OcPlatform.findResource("", requestUri, EnumSet.of(OcConnectivityType.CT_DEFAULT), new ResourceFoundListener(ocRepUri, href));
}
}
}
......@@ -291,6 +315,14 @@ public class OnOffCycleIntentService extends IntentService implements
}
}
if (resource instanceof Light && !((Light) resource).hasLinksProperty()) {
// Properties are on the light device
// Call a local method which will internally invoke put API on the light resource
Light light = (Light) resource;
Log.i(TAG, light.getName() + " changing state to " + ((!light.getState()) ? "on" : "off"));
postLightRepresentation(light.getUri(), !light.getState(), light.getLightLevel());
}
} else {
Log.w(TAG, "No resource for uri " + ocRepUri);
}
......@@ -332,88 +364,178 @@ public class OnOffCycleIntentService extends IntentService implements
Light light = (Light) mResourceLookup.get(resourceUri); // TODO: check instanceof before cast
if (light != null) {
// set new values
light.setState(newState);
light.setLightLevel(newLightLevel);
Log.i(TAG, "Posting light representation...");
if (light.hasLinksProperty()) {
BinarySwitch binarySwitch = light.getBinarySwitch();
if ((binarySwitch != null) && (binarySwitch.isInitialized())) {
OcRepresentation binarySwitchRepresentation = null;
try {
binarySwitchRepresentation = binarySwitch.getOcRepresentation();
BinarySwitch binarySwitch = light.getBinarySwitch();
if ((binarySwitch != null) && (binarySwitch.isInitialized())) {
OcRepresentation binarySwitchRepresentation = null;
try {
binarySwitchRepresentation = binarySwitch.getOcRepresentation();
} catch (OcException e) {
Log.e(TAG, "Failed to get OcRepresentation from a binary switch -- " + e.toString(), e);
} catch (OcException e) {
Log.e(TAG, "Failed to get OcRepresentation from a binary switch -- " + e.toString(), e);
}
if (binarySwitchRepresentation != null) {
Map<String, String> queryParams = new HashMap<>();
try {
// Invoke resource's "put" or "post" API with a new representation, query parameters and
// OcResource.OnPostListener event listener implementation
OcResource binarySwitchResource = mIotivityResourceLookup.get(binarySwitch.getUri());
if (binarySwitchResource != null) {
if (binarySwitchResource.getUri().startsWith(BinarySwitch.UPNP_OIC_URI_PREFIX_BINARY_SWITCH)) {
// upnp bridge requires 'post'
binarySwitchResource.post(binarySwitchRepresentation, queryParams, this);
} else {
binarySwitchResource.put(binarySwitchRepresentation, queryParams, this);
}
} else {
Log.e(TAG, "No binary switch resource for light uri " + resourceUri);
}
} catch (OcException e) {
Log.e(TAG, "Error occurred while invoking \"post\" API -- " + e.toString(), e);
}
}
} else {
if (binarySwitch == null) {
Log.e(TAG, "No binary switch for light uri " + resourceUri);
} else {
Log.e(TAG, "Binary switch not initialized: " + binarySwitch);
}
}
if (binarySwitchRepresentation != null) {
Map<String, String> queryParams = new HashMap<>();
Brightness brightness = light.getBrightness();
if ((brightness != null) && (brightness.isInitialized())) {
OcRepresentation brightnessRepresentation = null;
try {
// Invoke resource's "post" API with a new representation, query parameters and
// OcResource.OnPostListener event listener implementation
OcResource binarySwitchResource = mIotivityResourceLookup.get(binarySwitch.getUri());
if (binarySwitchResource != null) {
binarySwitchResource.post(binarySwitchRepresentation, queryParams, this);
} else {
Log.e(TAG, "No binary switch resource for light uri " + resourceUri);
}
brightnessRepresentation = brightness.getOcRepresentation();
} catch (OcException e) {
Log.e(TAG, "Error occurred while invoking \"post\" API -- " + e.toString(), e);
Log.e(TAG, "Failed to get OcRepresentation from a brightness -- " + e.toString(), e);
}
}
} else {
if (binarySwitch == null) {
Log.e(TAG, "No binary switch for light uri " + resourceUri);
if (brightnessRepresentation != null) {
Map<String, String> queryParams = new HashMap<>();
try {
// Invoke resource's "put" or "post" API with a new representation, query parameters and
// OcResource.OnPostListener event listener implementation
OcResource brightnessResource = mIotivityResourceLookup.get(brightness.getUri());
if (brightnessResource != null) {
if (brightnessResource.getUri().startsWith(Brightness.UPNP_OIC_URI_PREFIX_BRIGHTNESS)) {
// upnp bridge requires 'post'
brightnessResource.post(brightnessRepresentation, queryParams, this);
} else {
brightnessResource.put(brightnessRepresentation, queryParams, this);
}
} else {
Log.e(TAG, "No brightness resource for light uri " + resourceUri);
}
} catch (OcException e) {
Log.e(TAG, "Error occurred while invoking \"post\" API -- " + e.toString(), e);
}
}
} else {
Log.e(TAG, "Binary switch not initialized: " + binarySwitch);
if (brightness == null) {
Log.e(TAG, "No brightness for light uri " + resourceUri);
} else {
Log.e(TAG, "Brightness not initialized: " + brightness);
}
}
}
Brightness brightness = light.getBrightness();
if ((brightness != null) && (brightness.isInitialized())) {
OcRepresentation brightnessRepresentation = null;
} else {