2d1826b51e903af0e5ed76801591a05ae4e8b2ab
[platform/upstream/iotivity.git] /
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
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 package com.example.con_server;
21
22 import java.util.EnumSet;
23 import java.util.Vector;
24
25 import org.iotivity.base.OcException;
26 import org.iotivity.base.OcPlatform;
27 import org.iotivity.base.OcRepresentation;
28 import org.iotivity.base.OcResourceHandle;
29 import org.iotivity.base.ResourceProperty;
30
31 import android.util.Log;
32
33 //For creating/deleting the Diagnostics Resource
34 public class DiagnosticsResource {
35     private final String     LOG_TAG               = "[CON-SERVER]"
36                                                            + this.getClass()
37                                                                    .getSimpleName();
38     // diagnostics members
39     private String           diagnosticsUri;
40     private String           factoryReset;
41     private String           reboot;
42     private String           startCollection;
43     private Vector<String>   diagnosticsTypes      = new Vector<String>();
44     private Vector<String>   diagnosticsInterfaces = new Vector<String>();
45     private OcResourceHandle diagnosticsHandle;
46     private OcRepresentation diagnosticsRep        = new OcRepresentation();
47
48     // constructor
49     public DiagnosticsResource() {
50         Log.i(LOG_TAG, "DiagnosticsCollection: enter");
51
52         factoryReset = ConfigurationDefaultValues.defaultFactoryReset;
53         reboot = ConfigurationDefaultValues.defaultReboot;
54         startCollection = ConfigurationDefaultValues.defaultStartCollection;
55
56         diagnosticsUri = ConfigurationDefaultValues.diagURIPrefix;
57         diagnosticsTypes.add(ConfigurationDefaultValues.diagResourceTypePrefix);
58         diagnosticsInterfaces.add(OcPlatform.DEFAULT_INTERFACE);
59         diagnosticsRep.setValueString("fr", factoryReset);
60         diagnosticsRep.setValueString("rb", reboot);
61         diagnosticsRep.setValueString("ssc", startCollection);
62         diagnosticsRep.setUri(diagnosticsUri);
63         diagnosticsRep.setResourceTypes(diagnosticsTypes);
64         diagnosticsRep.setResourceInterfaces(diagnosticsInterfaces);
65
66     }
67
68     // for creating Diagnostic Resource
69     public void createResource(OcPlatform.EntityHandler listener)
70             throws OcException {
71         Log.i(LOG_TAG, "createResource(Diagnostics): enter");
72         EnumSet<ResourceProperty> propertySet = EnumSet.of(
73                 ResourceProperty.DISCOVERABLE, ResourceProperty.OBSERVABLE);
74         if (null == listener) {
75             Log.i(LOG_TAG, "CallBack Should be binded");
76             return;
77         }
78
79         // Register diagnostic resource
80         diagnosticsHandle = OcPlatform.registerResource(diagnosticsUri,
81                 diagnosticsTypes.get(0), diagnosticsInterfaces.get(0),
82                 listener, propertySet);
83         if (null == diagnosticsHandle) {
84             Log.e(LOG_TAG, "registerResource failed!");
85             return;
86         }
87
88         Thread thread = new Thread() {
89             @Override
90             public void run() {
91                 try {
92                     while (true) {
93                         // Put this thread for sleep for 1 sec.
94                         // Sleep value can be changed as per the developer
95                         // convenience.
96                         Thread.sleep(1000);
97                         if (reboot.equalsIgnoreCase("true")) {
98                             Log.i(LOG_TAG, "Reboot will be soon...");
99                             MainActivity mainActivityObj = MainActivity
100                                     .getMainActivityObject();
101                             if (null == mainActivityObj) {
102                                 Log.e(LOG_TAG,
103                                         "Mainactivity object is invalid!");
104                                 return;
105                             }
106                             try {
107                                 mainActivityObj.runOnUiThread(new Runnable() {
108                                     @Override
109                                     public void run() {
110                                         try {
111                                             MainActivity.reboot();
112                                         } catch (InterruptedException e) {
113                                             e.printStackTrace();
114                                         }
115                                     }
116                                 });
117                             } catch (Exception e) {
118                                 Log.e(LOG_TAG, "InterruptedException occured: "
119                                         + e.toString());
120                                 continue;
121                             }
122                             reboot = ConfigurationDefaultValues.defaultReboot;
123                         }
124                         if (factoryReset.equalsIgnoreCase("true")) {
125                             factoryReset = ConfigurationDefaultValues.defaultFactoryReset;
126                             factoryReset();
127                         }
128                     }
129                 } catch (InterruptedException e) {
130                     e.printStackTrace();
131                 }
132             }
133         };
134         thread.start();
135         Log.i(LOG_TAG, "createResource(Diagnostics): exit");
136     }
137
138     // getters and Setters Methods for diagnostics Resource
139     public void setDiagnosticsRepresentation(OcRepresentation rep) {
140         Log.i(LOG_TAG, "setDiagnosticsRepresentation: enter");
141
142         String fr = rep.getValueString("fr");
143         String rb = rep.getValueString("rb");
144         String ssc = rep.getValueString("ssc");
145
146         if (!(fr.equalsIgnoreCase(""))) {
147             factoryReset = fr;
148             Log.i(LOG_TAG,
149                     "setConfigurationRepresentation: New value(FactoryReset): "
150                             + fr);
151         }
152         if (!(rb.equalsIgnoreCase(""))) {
153             reboot = rb;
154             Log.i(LOG_TAG, "setDiagnosticsRepresentation: new value:(reboot) "
155                     + rb);
156         }
157
158         if (!(ssc.equalsIgnoreCase(""))) {
159             startCollection = ssc;
160             Log.i(LOG_TAG,
161                     "setDiagnosticsRepresentation: new value:(startcollection) "
162                             + ssc);
163         }
164
165         Log.i(LOG_TAG, "setDiagnosticsRepresentation: exit");
166     }
167
168     OcRepresentation getDiagnosticsRepresentation() {
169         diagnosticsRep.setValueString("fr", factoryReset);
170         diagnosticsRep.setValueString("rb", reboot);
171         diagnosticsRep.setValueString("ssc", startCollection);
172         return diagnosticsRep;
173     }
174
175     public String getUri() {
176         return diagnosticsUri;
177     }
178
179     // For Resetting diagnostics Resource attributes to their default values
180     public void factoryReset() {
181         factoryReset = ConfigurationDefaultValues.defaultFactoryReset;
182         reboot = ConfigurationDefaultValues.defaultReboot;
183         startCollection = ConfigurationDefaultValues.defaultStartCollection;
184     }
185
186     // For Deleting diagnostic resource
187     public void deleteResource() {
188         try {
189             if (null != diagnosticsHandle) {
190                 // Unregister the collection resource
191                 OcPlatform.unregisterResource(diagnosticsHandle);
192             }
193         } catch (OcException e) {
194             Log.e(LOG_TAG, "OcException occured! " + e.toString());
195         }
196     }
197 }