Commit 745f6a73 authored by Larry Sachs's avatar Larry Sachs

Beginnings of OCF AV Data Models

Change-Id: I6ffbf3c9fd7cc6f8fe09e0621a1b02e8a6986bdd
Signed-off-by: Larry Sachs's avatarLarry Sachs <larry.j.sachs@intel.com>
parent 009b2015
/*
* //******************************************************************
* //
* // Copyright 2018 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;
/**
* Action
*
* This class is used by UpnpAvClientActivity to create an object representation of a media control action
* and update the values depending on the server response
*/
public class Action {
public static final String HREF_KEY = "href";
public static final String REL_KEY = "rel";
public static final String ACTION_KEY = "action";
public static final String ALLOWED_VALUES_KEY = "allowed-values";
public static final String ALLOWED_RANGE_KEY = "allowed-range";
public static final String RANGE_STEP_KEY = "range-step";
private String mHref;
private String[] mRel = {"self"};
private String mAction = "stop";
private Double[] mAllowedValues;
private Double[] mAllowedRange;
private Double mRangeStep;
public Action() {
}
public void setOcRepresentation(OcRepresentation ocRep) throws OcException {
if (ocRep.hasAttribute(HREF_KEY)) {
mHref = ocRep.getValue(HREF_KEY);
}
if (ocRep.hasAttribute(REL_KEY)) {
mRel = ocRep.getValue(REL_KEY);
}
if (ocRep.hasAttribute(ACTION_KEY)) {
mAction = ocRep.getValue(ACTION_KEY);
}
if (ocRep.hasAttribute(ALLOWED_VALUES_KEY)) {
Object[] objects = ocRep.getValue(ALLOWED_VALUES_KEY);
int i = 0;
for (Object obj : objects) {
if (obj instanceof Double) {
mAllowedValues[i] = ((Double) obj);
}
++i;
}
}
if (ocRep.hasAttribute(ALLOWED_RANGE_KEY)) {
Object[] objects = ocRep.getValue(ALLOWED_RANGE_KEY);
int i = 0;
for (Object obj : objects) {
if (obj instanceof Double) {
mAllowedRange[i] = ((Double) obj);
}
++i;
}
}
if (ocRep.hasAttribute(RANGE_STEP_KEY)) {
Object obj = ocRep.getValue(RANGE_STEP_KEY);
if (obj instanceof Double) {
mRangeStep = ((Double) obj);
}
}
}
public OcRepresentation getOcRepresentation() throws OcException {
OcRepresentation ocRep = new OcRepresentation();
if (mHref != null) {
ocRep.setValue(HREF_KEY, mHref);
}
ocRep.setValue(REL_KEY, mRel);
ocRep.setValue(ACTION_KEY, mAction);
if (mAllowedValues != null) {
double[] valuesAsDouble = new double[mAllowedValues.length];
for (int i = 0; i < mAllowedValues.length; ++i) {
valuesAsDouble[i] = mAllowedValues[i];
}
ocRep.setValue(ALLOWED_VALUES_KEY, valuesAsDouble);
}
if (mAllowedRange != null) {
double[] rangeAsDouble = new double[mAllowedRange.length];
for (int i = 0; i < mAllowedRange.length; ++i) {
rangeAsDouble[i] = mAllowedRange[i];
}
ocRep.setValue(ALLOWED_RANGE_KEY, rangeAsDouble);
}
if (mRangeStep != null) {
ocRep.setValue(RANGE_STEP_KEY, mRangeStep);
}
return ocRep;
}
public String getHref() {
return mHref;
}
public void setHref(String href) {
mHref = href;
}
public String[] getRel() {
return mRel;
}
public void setRel(String rel[]) {
if ((rel != null) && (rel.length > 0)) {
mRel = rel;
}
}
public String getAction() {
return mAction;
}
public void setAction(String action) {
if (action != null) {
mAction = action;
}
}
public Double[] getAllowedValues() {
return mAllowedValues;
}
public void setAllowedValues(Double[] allowedValues) {
if ((allowedValues != null) && (allowedValues.length > 0)) {
mAllowedValues = allowedValues;
}
}
public Double[] getAllowedRange() {
return mAllowedRange;
}
public void setAllowedRange(Double[] allowedRange) {
if ((allowedRange != null) && (allowedRange.length == 2)) {
mAllowedRange = allowedRange;
}
}
public Double getRangeStep() {
return mRangeStep;
}
public void setRangeStep(Double rangeStep) {
mRangeStep = rangeStep;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean first = true;
for (String type : getRel()) {
if (!first) {
sb.append(", ");
}
if (type != null) {
sb.append(type.toString());
}
first = false;
}
sb.append("]");
String relAsString = sb.toString();
String allowedValuesAsString = null;
if (getAllowedValues() != null) {
sb = new StringBuilder();
sb.append("[");
first = true;
for (Double value : getAllowedValues()) {
if (!first) {
sb.append(", ");
}
if (value != null) {
sb.append(value.toString());
}
first = false;
}
sb.append("]");
allowedValuesAsString = sb.toString();
}
String allowedRangeAsString = null;
if (getAllowedRange() != null) {
sb = new StringBuilder();
sb.append("[");
first = true;
for (Double range : getAllowedRange()) {
if (!first) {
sb.append(", ");
}
if (range != null) {
sb.append(range.toString());
}
first = false;
}
sb.append("]");
allowedRangeAsString = sb.toString();
}
return "[" + HREF_KEY + ": " + getHref() +
", " + REL_KEY + ": " + relAsString +
", " + ACTION_KEY + ": " + getAction() +
", " + ALLOWED_VALUES_KEY + ": " + allowedValuesAsString +
", " + ALLOWED_RANGE_KEY + ": " + allowedRangeAsString +
", " + RANGE_STEP_KEY + ": " + getRangeStep() + "]";
}
}
/*
* //******************************************************************
* //
* // Copyright 2018 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;
/**
* Actions
*
* This class is used by UpnpAvClientActivity to create an object representation of the media control actions
* and update the values depending on the server response
*/
public class Actions {
public static final String ACTIONS_KEY = "actions";
private Action[] mActions;
public Actions() {
mActions = new Action[0];
}
public void setOcRepresentation(OcRepresentation[] ocRepActions) throws OcException {
if ((ocRepActions != null) && (ocRepActions.length > 0)) {
mActions = new Action[ocRepActions.length];
int index = 0;
for (OcRepresentation ocRepAction : ocRepActions) {
mActions[index] = new Action();
mActions[index].setOcRepresentation(ocRepAction);
++index;
}
}
}
public OcRepresentation[] getOcRepresentation() throws OcException {
OcRepresentation[] ocRepActions = new OcRepresentation[mActions.length];
int index = 0;
for (Action action : mActions) {
OcRepresentation ocRepAction = action.getOcRepresentation();
ocRepActions[index] = ocRepAction;
++index;
}
return ocRepActions;
}
public Action[] getActions() {
return mActions;
}
public void setActions(Action[] actions) {
mActions = (actions != null) ? actions : new Action[0];
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean first = true;
for (Action action : mActions) {
if (!first) {
sb.append(", ");
}
if (action != null) {
sb.append(action.toString());
}
first = false;
}
sb.append("]");
return sb.toString();
}
}
/*
* //******************************************************************
* //
* // Copyright 2018 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;
/**
* Audio
*
* This class is used by UpnpAvClientActivity to create an object representation of a remote audio resource
* and update the values depending on the server response
*/
public class Audio extends Service {
public static final String OIC_TYPE_AUDIO = "oic.r.audio";
public static final String OCF_OIC_URI_PREFIX_AUDIO = "/ocf/audio/";
public static final String UPNP_OIC_URI_PREFIX_AUDIO = "/upnp/audio/";
public static final String MUTE_KEY = "mute";
public static final boolean DEFAULT_MUTE = false;
public static final String VOLUME_KEY = "volume";
public static final int DEFAULT_VOLUME = 0;
private boolean mMute;
private int mVolume;
private boolean mIsInitialized;
public Audio() {
super();
mMute = DEFAULT_MUTE;
mVolume = DEFAULT_VOLUME;
}
public void setOcRepresentation(OcRepresentation rep) throws OcException {
super.setOcRepresentation(rep);
boolean muteIsInitialized = false;
boolean volumeIsInitialized = false;
if (rep.hasAttribute(MUTE_KEY)) {
Object obj = rep.getValue(MUTE_KEY);
if (obj instanceof Boolean) {
mMute = (Boolean) obj;
muteIsInitialized = true;
}
}
if (rep.hasAttribute(VOLUME_KEY)) {
Object obj = rep.getValue(VOLUME_KEY);
if (obj instanceof Integer) {
mVolume = (Integer) obj;
volumeIsInitialized = true;
}
}
mIsInitialized = (muteIsInitialized && volumeIsInitialized);
}
public OcRepresentation getOcRepresentation() throws OcException {
OcRepresentation rep = super.getOcRepresentation();
rep.setValue(MUTE_KEY, mMute);
rep.setValue(VOLUME_KEY, mVolume);
return rep;
}
public boolean getMute() {
return mMute;
}
public void setMute(boolean mute) {
mMute = mute;
}
public int getVolume() {
return mVolume;
}
public void setVolume(int volume) {
mVolume = volume;
}
public boolean isInitialized() {
return mIsInitialized;
}
@Override
public String toString() {
return "[" + super.toString() +
", " + "initialized: " + mIsInitialized +
", " + MUTE_KEY + ": " + mMute +
", " + VOLUME_KEY + ": " + mVolume + "]";
}
}
/*
* //******************************************************************
* //
* // Copyright 2016 Intel Corporation All Rights Reserved.
* // Copyright 2016-2018 Intel Corporation All Rights Reserved.
* //
* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
* //
......@@ -60,6 +60,7 @@ abstract public class Device extends Resource {
private String mUpc;
private Links mLinks;
private boolean mHasLinksProperty;
private Bitmap mIconBitmap;
......@@ -115,6 +116,7 @@ abstract public class Device extends Resource {
if (rep.hasAttribute(LINKS_KEY)) {
OcRepresentation[] links = rep.getValue(LINKS_KEY);
mLinks.setOcRepresentation(links);
mHasLinksProperty = true;
}
}
......@@ -133,7 +135,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;
}
......@@ -218,6 +222,10 @@ abstract public class Device extends Resource {
mUpc = upc;
}
public boolean hasLinksProperty() {
return mHasLinksProperty;
}
public Links getLinks() {
return mLinks;
}
......@@ -247,6 +255,7 @@ abstract public class Device extends Resource {
", " + PRESENTATION_URL_KEY + ": " + mPresentationUrl +
", " + ICON_URL_KEY + ": " + mIconUrl +
", " + UPC_KEY + ": " + mUpc +
", " + "hasLinksProp: " + mHasLinksProperty +
", " + LINKS_KEY + mLinks + "]";
}
}
/*
* //******************************************************************
* //
* // Copyright 2016 Intel Corporation All Rights Reserved.
* // Copyright 2016-2018 Intel Corporation All Rights Reserved.
* //
* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
* //
......@@ -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 {
......@@ -64,7 +64,13 @@ public class Link {
OcRepresentation ocRep = new OcRepresentation();
ocRep.setValue(HREF_KEY, mHref);
ocRep.setValue(REL_KEY, mRel);
ocRep.setValue(RT_KEY, mRt);
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 + "]";
}
}
/*
* //******************************************************************
* //
* // Copyright 2018 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.