1 //******************************************************************
3 // Copyright 2014 Intel Corporation.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 package org.iotivity.guiclient;
23 import android.util.Log;
25 import java.io.Serializable;
28 * Created by nathanhs on 12/28/15.
30 public class OcAttributeInfo implements Serializable {
32 * Hardcoded TAG... if project never uses proguard then
33 * MyOcClient.class.getName() is the better way.
35 private static final String TAG = "OcAttributeInfo";
37 private static final boolean LOCAL_LOGV = true; // set to false to compile out verbose logging
39 private static final int AMBIENT_LIGHT_SENSOR_READING_MAX = 4096;
40 private static final int LIGHT_DIMMER_MAX = 100;
41 private static final int LIGHT_SWITCH_MAX = 1;
42 private static final int PLATFORM_LED_SWITCH_MAX = 1;
43 // TODO: once the temp getValueInt works, figure out how to scale this properly
44 private static final int ROOM_TEMPERATURE_SENSOR_READING_MAX = 4096;
47 * These are the resource types supported by OcResourceInfo.
49 public enum OC_ATTRIBUTE_TYPE {
50 AMBIENT_LIGHT_SENSOR_READING,
54 ROOM_TEMPERATURE_SENSOR_READING;
56 private static final OC_ATTRIBUTE_TYPE[] values = OC_ATTRIBUTE_TYPE.values();
58 public static OC_ATTRIBUTE_TYPE fromInt(int i) {
63 private boolean mCurrentlyObserved;
64 private final int mId;
65 private static int mIdInitializer = 0;
66 private final boolean mObservable = true; //TODO BROKEN fix when implementing observe
67 private final OcResourceInfo mParentResource;
68 private final boolean mReadOnly;
69 private final OC_ATTRIBUTE_TYPE mType;
70 private boolean mValueBool; // used if attribute has a boolean value
71 private int mValueInt; // used if attribute has an int value
72 private String mValueString; // used if attribute has a String value
73 private final int mValueMax;
75 public OcAttributeInfo(OC_ATTRIBUTE_TYPE type, OcResourceInfo parent) {
76 if (LOCAL_LOGV) Log.v(TAG, "OcAttributeInfo() constructor");
78 this.mId = OcAttributeInfo.mIdInitializer++; // give a unique Id from other OcResourceInfos
79 this.mParentResource = parent;
80 this.mReadOnly = this.getReadOnlyBasedOnType(type);
82 this.mValueBool = false;
84 this.mValueString = "error";
85 this.mValueMax = this.getMaxAttributeValueBasedOnType(type);
92 public OC_ATTRIBUTE_TYPE getType() {
96 public boolean getValueBool() {
97 return this.mValueBool;
100 public int getValueInt() {
101 return this.mValueInt;
104 public int getValueMax() {
108 public String getValueString() {
109 return this.mValueString;
112 public int getValueAsPercentOfMax() {
113 return (this.mValueInt*100)/this.mValueMax;
116 public boolean isCurrentlyObserved() {
117 return this.mCurrentlyObserved;
120 public boolean isObservable() {
121 return this.mObservable;
124 public boolean isReadOnly() {
128 public void setCurrentlyObserved(boolean currentlyObserved) {
129 this.mCurrentlyObserved = currentlyObserved;
132 public void setValueBool(boolean value) {
133 this.mValueBool = value;
136 public void setValueInt(int value) {
137 this.mValueInt = value;
140 public void setValueString(String value) {
141 this.mValueString = value;
144 private int getMaxAttributeValueBasedOnType(OC_ATTRIBUTE_TYPE type) {
146 case AMBIENT_LIGHT_SENSOR_READING:
147 return AMBIENT_LIGHT_SENSOR_READING_MAX;
149 return LIGHT_DIMMER_MAX;
151 return LIGHT_SWITCH_MAX;
152 case PLATFORM_LED_SWITCH:
153 return PLATFORM_LED_SWITCH_MAX;
154 case ROOM_TEMPERATURE_SENSOR_READING:
155 return ROOM_TEMPERATURE_SENSOR_READING_MAX;
157 Log.w(TAG, "getMaxAttributeValueBasedOnType(): unrecognized attribute type.");
162 private boolean getReadOnlyBasedOnType(OC_ATTRIBUTE_TYPE type) {
164 case AMBIENT_LIGHT_SENSOR_READING:
165 case ROOM_TEMPERATURE_SENSOR_READING:
169 case PLATFORM_LED_SWITCH:
172 Log.w(TAG, "getReadOnlyBasedOnType(): unrecognized attribute type.");