1 /******************************************************************
2 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ******************************************************************/
19 package org.iotivity.service.ns.sample.consumer;
21 import android.content.Context;
22 import android.os.Handler;
23 import android.os.Message;
24 import android.util.Log;
26 import org.iotivity.base.ModeType;
27 import org.iotivity.base.OcPlatform;
28 import org.iotivity.base.PlatformConfig;
29 import org.iotivity.base.QualityOfService;
30 import org.iotivity.base.ServiceType;
31 import org.iotivity.service.ns.common.NSException;
32 import org.iotivity.service.ns.common.SyncInfo;
33 import org.iotivity.service.ns.common.TopicsList;
34 import org.iotivity.service.ns.common.Topic;
35 import org.iotivity.service.ns.consumer.ConsumerService;
36 import org.iotivity.service.ns.consumer.Provider;
38 public class ConsumerSample
39 implements ConsumerService.OnProviderDiscoveredListener,
40 Provider.OnProviderStateListener, Provider.OnMessageReceivedListener,
41 Provider.OnSyncInfoReceivedListener {
42 private static final String TAG = "NS_CONSUMER_SAMPLE";
44 private Context mContext = null;
45 private ConsumerService consumerService = null;
46 private boolean mAcceptor = true;
47 private Provider mProvider = null;
49 private Handler mHandler = null;
51 private static final int PROVIDER_DISCOVERED = 1;
52 private static final int STATE_CHANGED = 2;
53 private static final int MESSAGE_RECEIVED = 3;
54 private static final int SYNCINFO_RECEIVED = 4;
55 private static final int TOPICS_RECEIVED = 5;
57 public ConsumerSample(Context context) {
58 Log.i(TAG, "Create consumer sample Instance");
60 this.mContext = context;
61 consumerService = new ConsumerService();
64 public void setHandler(Handler handler) {
65 this.mHandler = handler;
68 public boolean getAcceptor() {
72 private void configurePlatform() {
74 PlatformConfig platformConfig = new PlatformConfig(mContext,
75 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0",
76 0, // Uses randomly available port
77 QualityOfService.LOW);
79 Log.i(TAG, "Configuring platform.");
80 OcPlatform.Configure(platformConfig);
82 OcPlatform.stopPresence(); // Initialize OcPlatform
83 } catch (Exception e) {
84 Log.e(TAG, "Exception: stopping presence when configuration step: "
87 Log.i(TAG, "Configuration done Successfully");
90 public void startNotificationConsumer() {
93 consumerService.start(this);
94 } catch (Exception e) {
95 Log.e(TAG, "Exception: startNotificationConsumer : " + e);
99 public void stopNotificationConsumer() {
101 consumerService.stop();
103 } catch (Exception e) {
104 Log.e(TAG, "Exception: stopNotificationConsumer : " + e);
108 public void enableRemoteService(String serverAddress) {
110 consumerService.enableRemoteService(serverAddress);
111 } catch (NSException e) {
112 Log.e(TAG, "NSException: enableRemoteService : " + e);
116 public void subscribeMQService(String servAdd, String topicName) {
117 Log.i(TAG, "SubscribeMQService - IN");
119 consumerService.subscribeMQService(servAdd, topicName);
120 } catch (Exception e) {
121 Log.e(TAG, "Exception: subscribeMQService : " + e);
123 Log.i(TAG, "SubscribeMQService - OUT");
127 public void rescanProvider() {
129 consumerService.rescanProvider();
130 } catch (Exception e) {
131 Log.e(TAG, "Exception: rescanProvider : " + e);
135 public void subscribe() {
137 mProvider.subscribe();
138 Log.i(TAG, "Notification consumer subscribe: " );
139 } catch (Exception e) {
140 Log.e(TAG, "Exception: subscribe : " + e);
144 public void unsubscribe() {
146 mProvider.unsubscribe();
147 Log.i(TAG, "Notification consumer unsubscribe: ");
148 } catch (Exception e) {
149 Log.e(TAG, "Exception: unsubscribe : " + e);
152 public void getTopicsList() {
153 if (mProvider != null) {
155 TopicsList topicsList = mProvider.getTopicList();
156 StringBuilder stringBuilder = new StringBuilder();
157 stringBuilder.append("TopicList Received :");
158 for (Topic topic : topicsList.getTopicsList()) {
159 Log.i(TAG, "Topic Name : " + topic.getTopicName());
160 Log.i(TAG, "Topic State : " + topic.getState());
162 .append("\nTopic Name : " + topic.getTopicName());
163 stringBuilder.append("\nTopic State : " + topic.getState());
165 Message msg = mHandler.obtainMessage(TOPICS_RECEIVED,
166 stringBuilder.toString());
167 mHandler.sendMessage(msg);
168 } catch (Exception e) {
169 Log.e(TAG, "Exception: getTopicsList : " + e);
172 Log.e(TAG, "getTopicsList Provider NULL");
176 public void updateTopicList(TopicsList topicsList) {
177 if (mProvider != null) {
179 mProvider.updateTopicList(topicsList);
180 } catch (Exception e) {
181 Log.e(TAG, "Exception: updateTopicList : " + e);
184 Log.e(TAG, "updateTopicList Provider NULL");
189 public void onProviderDiscovered(Provider provider) {
190 Log.i(TAG, "onProviderDiscovered");
191 if (provider == null) {
192 Log.e(TAG, "providerID is Null ");
195 mProvider = provider;
196 Log.i(TAG, "Provider ID: " + provider.getProviderId());
197 Message msg = mHandler.obtainMessage(PROVIDER_DISCOVERED,
198 "Provider Discovered Id: " + provider.getProviderId());
199 mHandler.sendMessage(msg);
201 Log.i(TAG, "setListeners to Discovered Provider");
202 provider.setListener(this, this, this);
203 Log.i(TAG, "setListeners done");
204 if (!provider.isSubscribed()) {
205 Log.i(TAG, "Provider not subscribed. Acceptor is Consumer");
207 provider.subscribe();
210 "Provider is already subscribed. Acceptor is Provider");
213 } catch (Exception e) {
214 Log.e(TAG, "Exception : " + e);
219 public void onProviderStateReceived(Provider.ProviderState state) {
220 Log.i(TAG, "onProviderStateReceived");
222 Log.i(TAG, "ProviderState Received : " + state);
223 Message msg = mHandler.obtainMessage(STATE_CHANGED,
224 "ProviderState Received : " + state);
225 mHandler.sendMessage(msg);
229 public void onMessageReceived(
230 org.iotivity.service.ns.common.Message message) {
231 Log.i(TAG, "onMessageReceived");
233 Log.i(TAG, "Message Id: " + message.getMessageId());
234 Log.i(TAG, "Message title: " + message.getTitle());
235 Log.i(TAG, "Message Content: " + message.getContentText());
236 Log.i(TAG, "Message Topic: " + message.getTopic());
237 Log.i(TAG, "Message Source: " + message.getSourceName());
239 Message msg = mHandler.obtainMessage(MESSAGE_RECEIVED,
240 "Message Id: " + message.getMessageId() + "\n"
241 + "Message title: " + message.getTitle() + "\n"
242 + "Message Content: " + message.getContentText() + "\n"
243 + "Message Topic: " + message.getTopic() + "\n"
244 + "Message Source: " + message.getSourceName());
245 mHandler.sendMessage(msg);
247 Log.i(TAG, "send READ syncInfo");
248 mProvider.sendSyncInfo(message.getMessageId(),
249 SyncInfo.SyncType.READ);
250 } catch (Exception e) {
251 Log.e(TAG, "Exception : " + e);
256 public void onSyncInfoReceived(SyncInfo sync) {
257 Log.i(TAG, "onSyncInfoReceived");
259 Log.i(TAG, "Sync Id: " + sync.getMessageId());
260 Log.i(TAG, "Sync STATE: " + sync.getState());
261 Message msg = mHandler.obtainMessage(SYNCINFO_RECEIVED,
262 "Sync Id: " + sync.getMessageId() + "\n" + "Sync STATE: "
264 mHandler.sendMessage(msg);