1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 ******************************************************************/
20 package com.example.con_server;
22 import java.util.EnumSet;
23 import java.util.Vector;
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;
31 import android.util.Log;
33 //For creating/deleting the Diagnostics Resource
34 public class DiagnosticsResource {
35 private final String LOG_TAG = "[CON-SERVER]"
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();
49 public DiagnosticsResource() {
50 Log.i(LOG_TAG, "DiagnosticsCollection: enter");
52 factoryReset = ConfigurationDefaultValues.defaultFactoryReset;
53 reboot = ConfigurationDefaultValues.defaultReboot;
54 startCollection = ConfigurationDefaultValues.defaultStartCollection;
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);
68 // for creating Diagnostic Resource
69 public void createResource(OcPlatform.EntityHandler listener)
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");
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!");
88 Thread thread = new Thread() {
93 // Put this thread for sleep for 1 sec.
94 // Sleep value can be changed as per the developer
97 if (reboot.equalsIgnoreCase("true")) {
98 Log.i(LOG_TAG, "Reboot will be soon...");
99 MainActivity mainActivityObj = MainActivity
100 .getMainActivityObject();
101 if (null == mainActivityObj) {
103 "Mainactivity object is invalid!");
107 mainActivityObj.runOnUiThread(new Runnable() {
111 MainActivity.reboot();
112 } catch (InterruptedException e) {
117 } catch (Exception e) {
118 Log.e(LOG_TAG, "InterruptedException occured: "
122 reboot = ConfigurationDefaultValues.defaultReboot;
124 if (factoryReset.equalsIgnoreCase("true")) {
125 factoryReset = ConfigurationDefaultValues.defaultFactoryReset;
129 } catch (InterruptedException e) {
135 Log.i(LOG_TAG, "createResource(Diagnostics): exit");
138 // getters and Setters Methods for diagnostics Resource
139 public void setDiagnosticsRepresentation(OcRepresentation rep) {
140 Log.i(LOG_TAG, "setDiagnosticsRepresentation: enter");
142 String fr = rep.getValueString("fr");
143 String rb = rep.getValueString("rb");
144 String ssc = rep.getValueString("ssc");
146 if (!(fr.equalsIgnoreCase(""))) {
149 "setConfigurationRepresentation: New value(FactoryReset): "
152 if (!(rb.equalsIgnoreCase(""))) {
154 Log.i(LOG_TAG, "setDiagnosticsRepresentation: new value:(reboot) "
158 if (!(ssc.equalsIgnoreCase(""))) {
159 startCollection = ssc;
161 "setDiagnosticsRepresentation: new value:(startcollection) "
165 Log.i(LOG_TAG, "setDiagnosticsRepresentation: exit");
168 OcRepresentation getDiagnosticsRepresentation() {
169 diagnosticsRep.setValueString("fr", factoryReset);
170 diagnosticsRep.setValueString("rb", reboot);
171 diagnosticsRep.setValueString("ssc", startCollection);
172 return diagnosticsRep;
175 public String getUri() {
176 return diagnosticsUri;
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;
186 // For Deleting diagnostic resource
187 public void deleteResource() {
189 if (null != diagnosticsHandle) {
190 // Unregister the collection resource
191 OcPlatform.unregisterResource(diagnosticsHandle);
193 } catch (OcException e) {
194 Log.e(LOG_TAG, "OcException occured! " + e.toString());