1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
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 /// @file EntityHandlerNoti.java
23 package oic.plugin.gear.noti;
25 import java.util.UUID;
27 import oic.plugin.gear.noti.Activator.TemplateTypes;
29 import org.iotivity.base.EntityHandlerResult;
30 import org.iotivity.base.OcException;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcRepresentation;
33 import org.iotivity.base.OcResourceRequest;
34 import org.iotivity.base.OcResourceResponse;
35 import org.iotivity.base.RequestHandlerFlag;
36 import org.iotivity.base.RequestType;
37 import org.osgi.framework.BundleContext;
39 import android.util.Log;
41 public class EntityHandlerNoti implements OcPlatform.EntityHandler {
43 final private static String TAG = "EntityHandler";
45 public static String textNoti;
47 public EntityHandlerResult handleEntity(OcResourceRequest resourcerequest) {
48 Log.e(TAG, "Entity Handler callback. LJH");
50 System.out.println("IN Server CPP entity Handler");
51 System.out.println("gear");
52 // OcResourceResponse response = new OcResourceResponse();
53 // int result = 1; // EH_ERROR
54 if (resourcerequest != null
55 && resourcerequest.getResourceUri().equals("/a/galaxy/gear")) {
56 RequestType requestType = resourcerequest.getRequestType();
57 EnumSet<RequestHandlerFlag> handlerFlagSet = resourcerequest
58 .getRequestHandlerFlagSet();
60 if(handlerFlagSet.contains(RequestHandlerFlag.INIT))
62 Log.e(TAG, "requestFlag : Init");
64 if(handlerFlagSet.contains(RequestHandlerFlag.REQUEST))
66 OcResourceResponse response = new OcResourceResponse();
67 OcRepresentation representation = new OcRepresentation();
68 response.setRequestHandle(resourcerequest
70 response.setResourceHandle(resourcerequest
71 .getResourceHandle());
73 "/******************************************************************************/");
74 Log.d("JUDO", "Name: "
75 + resourcerequest.getResourceRepresentation()
76 .getValueString("name"));
77 Log.d("JUDO", "Name: "
78 + resourcerequest.getResourceRepresentation()
79 .getValueString("power"));
80 Log.d("JUDO", "Name: "
81 + resourcerequest.getResourceRepresentation()
82 .getValueInt("brigthness"));
83 Log.d("JUDO", "Name: "
84 + resourcerequest.getResourceRepresentation()
85 .getValueInt("color"));
87 "/******************************************************************************/");
88 switch (requestType) {
92 textNoti = resourcerequest
93 .getResourceRepresentation()
94 .getValueString("power");
100 response.setErrorCode(200);
101 // representation.setUri("/a/galaxy/gear");
102 representation.setValueString("name",
103 Activator.myNotify.m_name);
104 representation.setValueString("power",
105 Activator.myNotify.m_power);
106 representation.setValueInt("brightness", 0);
107 representation.setValueInt("color", 0);
108 response.setResourceRepresentation(representation);
110 OcPlatform.sendResponse(response);
111 } catch (OcException e) {
112 // TODO Auto-generated catch block
114 return EntityHandlerResult.ERROR;
117 if(handlerFlagSet.contains(RequestHandlerFlag.OBSERVER))
119 Log.e(TAG, "requestFlag : Observer");
121 return EntityHandlerResult.OK;
123 return EntityHandlerResult.ERROR;
125 * if((requestFlag & 1 << 0) != 0 ) // init flag { Log.e(TAG,
126 * "requestFlag : Init"); } if((requestFlag & 1 << 1) != 0)
127 * //RequestFlag { if(requestType.equals("GET")) {
129 * } else if(requestType.equals("PUT")) { textNoti =
130 * resourcerequest.getResourceRepresentation().getValueString("power");
131 * perform(0); } else if(requestType.equals("POST")) { }
132 * OcRepresentation representation = new OcRepresentation();
133 * OcResourceResponse response = new OcResourceResponse();
134 * response.setRequestHandle(resourcerequest.getRequestHandle());
135 * response.setResourceHandle(resourcerequest.getResourceHandle());
136 * representation.setUri("/a/galaxy/gear");
137 * representation.setValueString("name", Activator.myNotify.m_name);
138 * representation.setValueString("power", Activator.myNotify.m_power);
139 * representation.setValueInt("brightness", 0);
140 * representation.setValueInt("color", 0); response.setErrorCode(200);
141 * response.setResourceRepresentation(representation); try {
142 * OcPlatform.sendResponse(response); } catch (OcException e) { // TODO
143 * Auto-generated catch block e.printStackTrace(); return
144 * EntityHandlerResult.ERROR; } } if((requestFlag & 1 << 2) != 0)
145 * //ObserverFlag { Log.e(TAG, "requestFlag : Observer"); } } return
146 * EntityHandlerResult.OK;
151 public void stop(BundleContext bundleContext) throws Exception {
152 Activator.AppContext = null;
153 Activator.mRichNotificationManager.stop();
156 public void perform(int primary) {
157 if (primary < 0 || primary >= TemplateTypes.values().length) {
161 Log.w(TAG, "Sending Notification");
163 switch (TemplateTypes.values()[primary]) {
165 performExample(new SmallHeaderExample(Activator.AppContext));
170 private void performExample(IExample example) {
171 UUID uuid = Activator.mRichNotificationManager.notify(example
173 Log.w(TAG, "Notification ID: " + uuid);