std::string strBMIResult;
if (m_pBMISensor->executeBMISensorLogic(&m_mapInputData, &strBMIResult) != -1)
- setAttribute("BMIresult", RCSResourceAttributes::Value(strBMIResult.c_str()));
+ setAttribute("BMIresult", RCSResourceAttributes::Value(strBMIResult.c_str())), true;
}
void BMISensorResource::onUpdatedInputResource(const std::string attributeName,
m_pDiscomfortIndexSensor->executeDISensorLogic(&m_mapInputData, &strDiscomfortIndex);
- setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str()));
+ setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str()), true);
for (auto it : m_mapInputData)
{
import android.app.Activity;
import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
import android.util.Log;
import android.view.View;
+import android.widget.ListView;
import android.widget.TextView;
import org.iotivity.service.RcsException;
import org.iotivity.service.client.RcsDiscoveryManager;
import org.iotivity.service.client.RcsRemoteResourceObject;
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* It contains the discover resource API for Discovering Container Resource
*/
+
public class ContainerClientActivity extends Activity implements
RcsDiscoveryManager.OnResourceDiscoveredListener {
+ public static final int MSG_ID_ATTRIBUTE_RECEIVED = 0;
+ public static final int MSG_ID_CACHEDATA_RECEIVED = 1;
+
private final String LOG_TAG = "[SampleClient] "
+ this.getClass().getSimpleName();
+ private final String RESOURCE_DISCOVERY_URI_KEYWORD = "discomfort";
+
+ private Handler mHandler;
+ private ResourceListAdapter mResourceListViewAdapter;
+
+ private ListView mResourceListView;
private TextView mLogView;
+ private List<String> mFoundResourceUris;
+ private List<ResourceListItem> mFoundResources;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container_client);
+
+ mFoundResourceUris = new ArrayList<String>();
+ mFoundResources = new ArrayList<ResourceListItem>();
+
+ mHandler = new ContainerClientHandler(this);
+
+ mResourceListViewAdapter = new ResourceListAdapter(this, R.layout.discovered_resource, mFoundResources);
+ mResourceListView = (ListView) findViewById(R.id.ResourceListView);
+ mResourceListView.setAdapter(mResourceListViewAdapter);
+
mLogView = (TextView) findViewById(R.id.log);
}
public void onDiscoverResourceClick(View v) {
try {
- RcsDiscoveryManager.getInstance().discoverResourceByType(
- RcsAddress.multicast(), "oic.r.discomfortindex",
+ RcsDiscoveryManager.getInstance().discoverResource(
+ RcsAddress.multicast(),
ContainerClientActivity.this);
+
+ mFoundResourceUris.clear();
+ mFoundResources.clear();
+ mResourceListViewAdapter.notifyDataSetChanged();
+
mLogView.setText("");
} catch (RcsException e) {
e.printStackTrace();
}
}
+ private boolean IsFoundResource(String resourceUri)
+ {
+ if (mFoundResourceUris.contains(resourceUri))
+ return true;
+
+ return false;
+ }
+
@Override
public void onResourceDiscovered(
final RcsRemoteResourceObject discoveredResource) {
- Log.i(LOG_TAG, "onResourceDiscovered");
runOnUiThread(new Runnable() {
public void run() {
try {
- mLogView.setText(Utils.resourceInfo(discoveredResource));
+ String uri = discoveredResource.getUri();
+ Log.i(LOG_TAG, "onResourceDiscovered -- " + uri);
+
+ if (uri.contains(RESOURCE_DISCOVERY_URI_KEYWORD) && !IsFoundResource(uri)) {
+ mFoundResourceUris.add(uri);
+ mFoundResources.add(new ResourceListItem(uri, discoveredResource, ContainerClientActivity.this.getHandler()));
+ mResourceListViewAdapter.notifyDataSetChanged();
+ }
} catch (RcsException e) {
- Utils.showError(ContainerClientActivity.this, LOG_TAG, e);
+ e.printStackTrace();
}
}
});
}
-}
+
+ private void printLog(String message) {
+ Log.i(LOG_TAG, message);
+ mLogView.setText(message);
+ }
+
+ public Handler getHandler() {
+ return mHandler;
+ }
+
+ private static class ContainerClientHandler extends Handler {
+ private WeakReference<ContainerClientActivity> mActivityRef;
+ private String logMsg;
+
+ private ContainerClientHandler(ContainerClientActivity activity) {
+ mActivityRef = new WeakReference<>(activity);
+ }
+
+ @Override
+ public void handleMessage(Message msg) {
+ super.handleMessage(msg);
+
+ ContainerClientActivity activity = mActivityRef.get();
+ if (activity == null) return;
+
+ switch (msg.what) {
+ case MSG_ID_ATTRIBUTE_RECEIVED:
+ logMsg = "-- ATTRIBUTE_RECEIVED\n";
+ logMsg += msg.obj.toString();
+ break;
+
+ case MSG_ID_CACHEDATA_RECEIVED:
+ logMsg = "-- CACHEDATA_RECEIVED\n";
+ logMsg += msg.obj.toString();
+ break;
+ }
+ activity.printLog(logMsg);
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+/******************************************************************
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ * <p>
+ * <p>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************/
+
+package org.iotivity.service.sample.client;
+
+import android.app.Activity;
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.TextView;
+import android.widget.ToggleButton;
+
+import org.iotivity.service.RcsException;
+
+import java.util.List;
+
+public class ResourceListAdapter extends ArrayAdapter<ResourceListItem> {
+
+ private Context mContext;
+ private int mLayoutId;
+ private List<ResourceListItem> foundResources;
+
+ public ResourceListAdapter(Context context, int layoutId, List<ResourceListItem> foundResources)
+ {
+ super(context, layoutId, foundResources);
+
+ this.mLayoutId = layoutId;
+ this.mContext = context;
+ this.foundResources = foundResources;
+ }
+
+ @Override
+ public View getView(final int position, View convertView, final ViewGroup parent) {
+ if (convertView == null)
+ {
+ LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
+ convertView = inflater.inflate(mLayoutId, parent, false);
+ }
+
+ String resourceUri = foundResources.get(position).mResourceUri;
+
+ TextView textViewUri = (TextView) convertView.findViewById(R.id.textView_uri);
+ textViewUri.setText(resourceUri);
+
+ textViewUri.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View view) {
+ try {
+ TextView textView = (TextView) ((Activity) mContext).findViewById(R.id.log);
+ textView.setText(Utils.resourceInfo(foundResources.get(position).mResource));
+ } catch (RcsException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+
+ Button buttonGet = (Button) convertView.findViewById(R.id.button_get);
+ buttonGet.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View view) {
+ ResourceListItem selectedResource = foundResources.get(position);
+ try {
+ TextView textView = (TextView) ((Activity) mContext).findViewById(R.id.log);
+ textView.setText("Get Remote Attributes from \'" + selectedResource.mResourceUri + "\'");
+ selectedResource.mResource.getRemoteAttributes(selectedResource.mGetListener);
+ } catch (RcsException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+
+ ToggleButton buttonObserve = (ToggleButton) convertView.findViewById(R.id.button_observe);
+ buttonObserve.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View view) {
+ ResourceListItem selectedResource = foundResources.get(position);
+ TextView textView = (TextView) ((Activity) mContext).findViewById(R.id.log);
+
+ try {
+ if (!selectedResource.mCacheStarted) {
+ textView.setText("Start Observe \'" + selectedResource.mResourceUri + "\'");
+ selectedResource.mResource.startCaching(selectedResource.mCacheListener);
+ selectedResource.mCacheStarted = true;
+ ((ToggleButton) view).setChecked(true);
+ } else {
+ textView.setText("Stop Observing \'" + selectedResource.mResourceUri + "\'");
+ selectedResource.mResource.stopCaching();
+ selectedResource.mCacheStarted = false;
+ ((ToggleButton) view).setChecked(false);
+ }
+ } catch (RcsException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+
+ return convertView;
+ }
+}
\ No newline at end of file
--- /dev/null
+/******************************************************************
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ * <p>
+ * <p>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************/
+
+package org.iotivity.service.sample.client;
+
+import android.os.Handler;
+import android.util.Log;
+
+import org.iotivity.service.RcsException;
+import org.iotivity.service.RcsResourceAttributes;
+import org.iotivity.service.client.RcsRemoteResourceObject;
+
+public class ResourceListItem {
+ private final String LOG_TAG = "[RCSampleClient] "
+ + this.getClass().getSimpleName();
+
+ public String mResourceUri;
+ public RcsRemoteResourceObject mResource;
+ public RemoteResourceGetListener mGetListener;
+ public RemoteResourceCacheListener mCacheListener;
+ public boolean mCacheStarted;
+
+ private Handler mHandler;
+
+ public ResourceListItem(String resourceUri, RcsRemoteResourceObject resource, Handler handler)
+ {
+ mResourceUri = resourceUri;
+ mResource = resource;
+
+ mGetListener = new RemoteResourceGetListener();
+ mCacheListener = new RemoteResourceCacheListener();
+ mCacheStarted = false;
+
+ mHandler = handler;
+ }
+
+ private class RemoteResourceGetListener implements RcsRemoteResourceObject.OnRemoteAttributesReceivedListener
+ {
+
+ @Override
+ public void onAttributesReceived(final RcsResourceAttributes rcsResourceAttributes, int eCode) {
+ Log.i(LOG_TAG, "onAttributesReceived -- " + mResourceUri);
+
+ new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ String logMessage;
+ logMessage = "-- " + mResourceUri + "\n";
+ logMessage += Utils.resourceAttributes(rcsResourceAttributes);
+
+ mHandler.obtainMessage(ContainerClientActivity.MSG_ID_ATTRIBUTE_RECEIVED, logMessage).sendToTarget();
+ } catch (RcsException e) {
+ e.printStackTrace();
+ }
+ }
+ }).run();
+ }
+ }
+
+ private class RemoteResourceCacheListener implements RcsRemoteResourceObject.OnCacheUpdatedListener
+ {
+
+ @Override
+ public void onCacheUpdated(final RcsResourceAttributes rcsResourceAttributes) {
+ Log.i(LOG_TAG, "onCacheUpdated -- " + mResourceUri);
+
+ new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ String logMessage;
+ logMessage = "-- " + mResourceUri + "\n";
+ logMessage += Utils.resourceAttributes(rcsResourceAttributes);
+
+ mHandler.obtainMessage(ContainerClientActivity.MSG_ID_CACHEDATA_RECEIVED, logMessage).sendToTarget();
+ } catch (RcsException e) {
+ e.printStackTrace();
+ }
+ }
+ }).run();
+ }
+ }
+}
\ No newline at end of file
-package org.iotivity.service.sample.client;
+/******************************************************************
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ * <p>
+ * <p>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************/
-import android.content.Context;
-import android.util.Log;
-import android.widget.Toast;
+package org.iotivity.service.sample.client;
import org.iotivity.service.RcsException;
+import org.iotivity.service.RcsResourceAttributes;
import org.iotivity.service.client.RcsRemoteResourceObject;
public class Utils {
return sb.toString();
}
- public static void showError(Context ctx, String tag, String msg) {
- Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
- Log.e(tag, msg);
- }
+ public static String resourceAttributes(RcsResourceAttributes attr)
+ throws RcsException {
+ StringBuilder sb = new StringBuilder();
+
+ for (String key : attr.keySet())
+ {
+ sb.append(key + " : " + attr.get(key) + "\n");
+ }
- public static void showError(Context ctx, String tag, Exception e) {
- Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT).show();
- Log.e(tag, e.getMessage(), e);
+ return sb.toString();
}
}
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main"
- android:weightSum="1">
+ android:weightSum="1"
+ android:padding="5dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onDiscoverResourceClick"
android:text="@string/discover_resource"
- android:id="@+id/di" />
+ android:id="@+id/di"
+ android:layout_alignParentTop="true"
+ android:layout_alignParentStart="true" />
<TextView
android:id="@+id/log"
android:layout_height="250dp"
android:ems="10"
android:layout_weight="0.26"
+ android:layout_alignParentStart="true"
+ android:layout_alignParentBottom="true"
+ android:background="#32437200"
+ android:padding="10dp" />
+
+ <ListView
+ android:layout_width="wrap_content"
+ android:layout_height="376dp"
+ android:id="@+id/ResourceListView"
android:layout_below="@+id/di"
- android:layout_alignParentStart="true" />
+ android:layout_alignParentStart="true"
+ android:layout_above="@+id/log" />
</RelativeLayout>
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent" android:layout_height="match_parent">
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="/softsensor/discomfortIndex/1"
+ android:id="@+id/textView_uri"
+ android:layout_alignBaseline="@+id/button_get"
+ android:layout_alignBottom="@+id/button_get"
+ android:layout_alignParentStart="true"
+ android:paddingStart="10dp"
+ android:textStyle="bold" />
+
+ <Button
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Get"
+ android:id="@+id/button_get"
+ android:layout_alignParentTop="true"
+ android:layout_toStartOf="@+id/button_observe" />
+
+ <ToggleButton
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:id="@+id/button_observe"
+ android:layout_alignBottom="@+id/button_get"
+ android:layout_alignParentEnd="true"
+ android:textOff="Observe"
+ android:textOn="Observe" />
+
+</RelativeLayout>
\ No newline at end of file
package org.iotivity.service.sample.container;
import android.content.Context;
-import android.content.pm.ApplicationInfo;
-import android.content.pm.PackageManager;
import android.os.Message;
import android.os.PowerManager;
import java.util.List;
import java.util.Map;
-import dalvik.system.DexFile;
-
/**
* For calling the Resource Container APIs as per user selection on UI and for
* updating the UI
PowerManager pm = null;
PowerManager.WakeLock wl = null;
-
-
// constructor
public ResourceContainer() {
resourceContainerActivityInstance = ResourceContainerActivity
msg = Message.obtain();
msg.what = 1;
resourceContainerActivityInstance.getHandler().sendMessage(msg);
- //containerInstance.registerAndroidResource("testBundle", testResource);
}
}