2 //******************************************************************
4 // Copyright 2016 Samsung Electronics All Rights Reserved.
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.service.ns.sample.provider;
25 import android.content.Context;
27 import android.util.Log;
28 import android.widget.Toast;
30 import org.iotivity.base.ModeType;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcResourceHandle;
33 import org.iotivity.base.PlatformConfig;
34 import org.iotivity.base.QualityOfService;
35 import org.iotivity.base.ServiceType;
36 import org.iotivity.service.ns.common.Message;
37 import org.iotivity.service.ns.provider.*;
38 import org.iotivity.service.ns.common.*;
40 import java.util.HashMap;
42 public class ProviderSample
43 implements ProviderService.OnConsumerSubscribedListener,
44 ProviderService.OnMessageSynchronizedListener {
46 private static final String TAG = "NS_PROVIDER_SAMPLE";
48 private Context mContext = null;
49 private OcResourceHandle mResourceHandle;
50 private ProviderService ioTNotification = null;
51 private HashMap<String, Integer> msgMap;
53 private Handler mHandler = null;
55 private static final int CONSUMER_SUBSCRIBED = 1;
56 private static final int MESSAGE_SYNC = 2;
58 private static final int SYNC_READ = 0;
59 private static final int SYNC_DISMISS = 1;
60 private static final int SYNC_UNREAD = 2;
61 private boolean gAcceptor;
62 private Consumer gConsumer;
64 public ProviderSample(Context context) {
65 Log.i(TAG, "Create providerSample Instance");
67 this.msgMap = new HashMap<>();
68 this.mContext = context;
69 ioTNotification = ProviderService.getInstance();
72 public void setHandler(Handler handler) {
73 this.mHandler = handler;
76 private void configurePlatform() {
78 PlatformConfig platformConfig = new PlatformConfig(mContext,
79 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0",
80 0, // Uses randomly available port
81 QualityOfService.LOW);
83 Log.i(TAG, "Configuring platform.");
84 OcPlatform.Configure(platformConfig);
86 OcPlatform.stopPresence(); // Initialize OcPlatform
87 } catch (Exception e) {
88 Log.e(TAG, "Exception: stopping presence when configuration step: "
91 Log.i(TAG, "Configuration done Successfully");
94 public void start(boolean policy) {
95 Log.i(TAG, "Start ProviderService -IN");
99 ioTNotification.start(this, this, policy, "Info", false);
100 } catch (Exception e) {
101 Log.e(TAG, "Exception: start : " + e);
103 Log.i(TAG, "Start ProviderService - OUT");
106 public void registerTopic() {
107 Log.i(TAG, "Register Topics -IN");
109 ioTNotification.registerTopic("OCF_TOPIC1");
110 ioTNotification.registerTopic("OCF_TOPIC2");
111 ioTNotification.registerTopic("OCF_TOPIC3");
112 ioTNotification.registerTopic("OCF_TOPIC4");
113 } catch (Exception e) {
114 Log.e(TAG, "Exception: registerTopic : "+ e);
117 Log.i(TAG, "Start ProviderService - OUT");
120 public void setTopic() {
121 Log.i(TAG, "Set Topic -IN");
122 if (gConsumer == null) {
126 gConsumer.setTopic("OCF_TOPIC1");
127 gConsumer.setTopic("OCF_TOPIC2");
128 gConsumer.setTopic("OCF_TOPIC3");
129 gConsumer.setTopic("OCF_TOPIC4");
130 } catch (Exception e) {
131 Log.e(TAG, "Exception: setTopic : " + e);
134 Log.i(TAG, "setTopic ProviderService - OUT");
139 Log.i(TAG, "Stop ProviderService - IN");
141 OcPlatform.stopPresence();
142 } catch (Exception e) {
143 Log.e(TAG,"Exception: stopping presence when terminating NS server: "+ e);
146 ioTNotification.stop();
147 } catch (Exception e) {
148 Log.e(TAG, "Exception: stop : " + e);
151 Log.i(TAG, "Stop ProviderService - OUT");
154 public Message createMessage() {
155 Log.i(TAG, "createMessage ProviderService - IN");
156 Message message = null;
158 message = ioTNotification.createMessage();
159 } catch (Exception e) {
160 Log.e(TAG, "Exception: createMessage : " + e);
162 Log.i(TAG, "createMessage ProviderService - OUT");
166 public void sendMessage(Message notiMessage) {
167 Log.i(TAG, "SendMessage ProviderService - IN");
170 ioTNotification.sendMessage(notiMessage);
171 } catch (Exception e) {
172 Log.e(TAG, "Exception: sendMessage : " + e);
175 Log.i(TAG, "SendMessage ProviderService - OUT");
176 mHandler.post(new Runnable() {
179 Toast.makeText(mContext, "Notification sent",
180 Toast.LENGTH_SHORT).show();
185 public void sendSyncInfo(long messageId, SyncInfo.SyncType syncType) {
186 Log.i(TAG, "SendSyncInfo ProviderService - IN");
187 if (msgMap.containsKey(messageId)) {
188 if (msgMap.get(messageId) == SYNC_UNREAD) {
190 ioTNotification.sendSyncInfo(messageId, syncType);
191 Log.i(TAG, "Notification Sync ");
192 } catch (Exception e) {
193 Log.e(TAG, "Exception: sendSyncInfo : " + e);
195 Log.i(TAG, "SendSyncInfo ProviderService - OUT");
196 msgMap.put("ID: " + messageId, SYNC_READ);
201 public void enableRemoteService(String servAdd) {
202 Log.i(TAG, "EnableRemoteService ProviderService - IN");
204 ioTNotification.enableRemoteService(servAdd);
205 } catch (Exception e) {
206 Log.e(TAG, "Exception: enableRemoteService : " + e);
208 Log.i(TAG, "EnableRemoteService ProviderService - OUT");
212 public void disableRemoteService(String servAdd) {
213 Log.i(TAG, "DisableRemoteService ProviderService - IN");
215 ioTNotification.disableRemoteService(servAdd);
216 } catch (Exception e) {
217 Log.e(TAG, "Exception: disableRemoteService : " + e);
219 Log.i(TAG, "DisableRemoteService ProviderService - OUT");
223 public void subscribeMQService(String servAdd, String topicName) {
224 Log.i(TAG, "SubscribeMQService ProviderService - IN");
226 ioTNotification.subscribeMQService(servAdd, topicName);
227 } catch (Exception e) {
228 Log.e(TAG, "Exception: subscribeMQService : " + e);
230 Log.i(TAG, "SubscribeMQService ProviderService - OUT");
234 public void acceptSubscription(Consumer consumer, boolean accepted) {
235 Log.i(TAG, "AcceptSubscription ProviderService - IN");
237 consumer.acceptSubscription(accepted);
238 } catch (Exception e) {
239 Log.e(TAG, "Exception: acceptSubscription : " + e);
241 Log.i(TAG, "AcceptSubscription ProviderService - OUT");
245 public void onConsumerSubscribed(Consumer consumer) {
246 Log.i(TAG, "onConsumerSubscribed - IN");
247 gConsumer = consumer;
248 acceptSubscription(consumer, true);
249 android.os.Message msg = mHandler.obtainMessage(CONSUMER_SUBSCRIBED,
250 "Consumer Id: " + consumer.getConsumerId());
251 mHandler.sendMessage(msg);
252 Log.i(TAG, "onConsumerSubscribed - OUT");
256 public void onMessageSynchronized(SyncInfo syncInfo) {
258 "Received SyncInfo with messageID: " + syncInfo.getMessageId());
259 android.os.Message msg = mHandler.obtainMessage(MESSAGE_SYNC,
260 "Message Id: " + syncInfo.getMessageId());
261 mHandler.sendMessage(msg);
264 public HashMap<String, Integer> getMsgMap() {