Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / android / examples / guiclient / src / main / java / org / iotivity / guiclient / OcAttributeInfo.java
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 package org.iotivity.guiclient;
22
23 import android.util.Log;
24
25 import java.io.Serializable;
26
27 /**
28  * Created by nathanhs on 12/28/15.
29  */
30 public class OcAttributeInfo implements Serializable {
31     /**
32      * Hardcoded TAG... if project never uses proguard then
33      * MyOcClient.class.getName() is the better way.
34      */
35     private static final String TAG = "OcAttributeInfo";
36
37     private static final boolean LOCAL_LOGV = true; // set to false to compile out verbose logging
38
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;
45
46     /**
47      * These are the resource types supported by OcResourceInfo.
48      */
49     public enum OC_ATTRIBUTE_TYPE {
50         AMBIENT_LIGHT_SENSOR_READING,
51         LIGHT_DIMMER,
52         LIGHT_SWITCH,
53         PLATFORM_LED_SWITCH,
54         ROOM_TEMPERATURE_SENSOR_READING;
55
56         private static final OC_ATTRIBUTE_TYPE[] values = OC_ATTRIBUTE_TYPE.values();
57
58         public static OC_ATTRIBUTE_TYPE fromInt(int i) {
59             return values[i];
60         }
61     }
62
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;
74
75     public OcAttributeInfo(OC_ATTRIBUTE_TYPE type, OcResourceInfo parent) {
76         if (LOCAL_LOGV) Log.v(TAG, "OcAttributeInfo() constructor");
77
78         this.mId = OcAttributeInfo.mIdInitializer++; // give a unique Id from other OcResourceInfos
79         this.mParentResource = parent;
80         this.mReadOnly = this.getReadOnlyBasedOnType(type);
81         this.mType = type;
82         this.mValueBool = false;
83         this.mValueInt = 0;
84         this.mValueString = "error";
85         this.mValueMax = this.getMaxAttributeValueBasedOnType(type);
86     }
87
88     public int getId() {
89         return mId;
90     }
91
92     public OC_ATTRIBUTE_TYPE getType() {
93         return mType;
94     }
95
96     public boolean getValueBool() {
97         return this.mValueBool;
98     }
99
100     public int getValueInt() {
101         return this.mValueInt;
102     }
103
104     public int getValueMax() {
105         return mValueMax;
106     }
107
108     public String getValueString() {
109         return this.mValueString;
110     }
111
112     public int getValueAsPercentOfMax() {
113         return (this.mValueInt*100)/this.mValueMax;
114     }
115
116     public boolean isCurrentlyObserved() {
117         return this.mCurrentlyObserved;
118     }
119
120     public boolean isObservable() {
121         return this.mObservable;
122     }
123
124     public boolean isReadOnly() {
125         return mReadOnly;
126     }
127
128     public void setCurrentlyObserved(boolean currentlyObserved) {
129         this.mCurrentlyObserved = currentlyObserved;
130     }
131
132     public void setValueBool(boolean value) {
133         this.mValueBool = value;
134     }
135
136     public void setValueInt(int value) {
137         this.mValueInt = value;
138     }
139
140     public void setValueString(String value) {
141         this.mValueString = value;
142     }
143
144     private int getMaxAttributeValueBasedOnType(OC_ATTRIBUTE_TYPE type) {
145         switch(type) {
146             case AMBIENT_LIGHT_SENSOR_READING:
147                 return AMBIENT_LIGHT_SENSOR_READING_MAX;
148             case LIGHT_DIMMER:
149                 return LIGHT_DIMMER_MAX;
150             case LIGHT_SWITCH:
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;
156             default:
157                 Log.w(TAG, "getMaxAttributeValueBasedOnType(): unrecognized attribute type.");
158                 return 0;
159         }
160     }
161
162     private boolean getReadOnlyBasedOnType(OC_ATTRIBUTE_TYPE type) {
163         switch(type) {
164             case AMBIENT_LIGHT_SENSOR_READING:
165             case ROOM_TEMPERATURE_SENSOR_READING:
166                 return true;
167             case LIGHT_DIMMER:
168             case LIGHT_SWITCH:
169             case PLATFORM_LED_SWITCH:
170                 return false;
171             default:
172                 Log.w(TAG, "getReadOnlyBasedOnType(): unrecognized attribute type.");
173                 return false;
174         }
175     }
176 }