1 package org.iotivity.service.ssm.sample;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.util.ArrayList;
8 import java.util.Iterator;
10 import java.util.UUID;
12 import org.iotivity.base.ModeType;
13 import org.iotivity.base.OcPlatform;
14 import org.iotivity.base.PlatformConfig;
15 import org.iotivity.base.QualityOfService;
16 import org.iotivity.base.ServiceType;
17 import org.iotivity.service.ssm.DataReader;
18 import org.iotivity.service.ssm.IQueryEngineEvent;
19 import org.iotivity.service.ssm.ModelData;
20 import org.iotivity.service.ssm.R;
21 import org.iotivity.service.ssm.SSMInterface;
23 import android.app.Activity;
24 import android.app.AlertDialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.content.DialogInterface.OnClickListener;
28 import android.content.SharedPreferences;
29 import android.content.SharedPreferences.Editor;
30 import android.content.res.AssetManager;
31 import android.net.ConnectivityManager;
32 import android.net.NetworkInfo;
33 import android.os.Bundle;
34 import android.os.Handler;
35 import android.os.Message;
36 import android.util.Log;
37 import android.view.View;
38 import android.widget.EditText;
39 import android.widget.ScrollView;
40 import android.widget.TextView;
42 public class MainActivity extends Activity {
44 private SSMInterface SoftSensorManager = null;
45 private ArrayList<Integer> mRunningQueries = new ArrayList<Integer>();
47 private IQueryEngineEvent mQueryEngineEventListener = null;
48 private final String LOG_TAG = this.getClass().getSimpleName();
49 private static MainActivity activityObj;
51 void PrintLog(String log) {
52 Message msg = new Message();
53 Bundle data = new Bundle();
54 data.putString("Log", log);
56 logHandler.sendMessage(msg);
59 private Handler logHandler = new Handler() {
61 public void handleMessage(
63 tvLog.append(msg.getData()
66 svLog.fullScroll(ScrollView.FOCUS_DOWN);
70 private TextView tvLog = null;
71 private ScrollView svLog = null;
72 private EditText edtQuery = null;
73 private EditText edtUnregisterQuery = null;
75 View.OnClickListener clickHandler = null;
77 View.OnClickListener textAddHandler = null;
80 public void onCreate(Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
82 setContentView(R.layout.activity_main);
84 tvLog = (TextView) findViewById(R.id.txtLog);
85 svLog = (ScrollView) findViewById(R.id.sclLog);
86 edtQuery = (EditText) findViewById(R.id.editQuery);
87 edtUnregisterQuery = (EditText) findViewById(R.id.editUnregisterQuery);
89 // calling the method to check the Wi-fi connectivity and configuring
93 mQueryEngineEventListener = new IQueryEngineEvent() {
95 public void onQueryEngineEvent(int cqid, DataReader result) {
96 Log.i("[SSM]", "event received! cqid=" + cqid);
97 PrintLog("Event from cqid " + cqid + " has received");
99 List<String> models = result.getAffectedModels();
101 for (String modelName : models) {
102 PrintLog("Model: " + modelName);
105 int dataCount = result.getModelDataCount(modelName);
107 for (int i = 0; i < dataCount; i++) {
108 ModelData modelData = result.getModelData(
111 for (int j = 0; j < modelData.getPropertyCount(); j++) {
113 + modelData.getPropertyName(j)
115 + modelData.getPropertyValue(j));
120 } catch (Exception e) {
122 PrintLog("Receiving Event from cqid " + cqid
129 clickHandler = new View.OnClickListener() {
131 public void onClick(View v) {
133 case R.id.btnRegisterQuery:
136 cqid = SoftSensorManager.registerQuery(edtQuery
137 .getText().toString(),
138 mQueryEngineEventListener);
139 mRunningQueries.add(cqid);
140 PrintLog(edtQuery.getText().toString()
141 + " has executed, cqid=" + cqid);
142 } catch (Exception e) {
143 PrintLog("Register Query failed");
148 case R.id.btnUnregisterQuery:
149 Iterator<Integer> it = mRunningQueries.iterator();
151 while (it.hasNext()) {
152 if (it.next() == Integer
153 .parseInt(edtUnregisterQuery.getText()
156 SoftSensorManager.unregisterQuery(Integer
157 .parseInt(edtUnregisterQuery
158 .getText().toString()));
159 PrintLog("Unregister Query has executed, cqid="
161 .parseInt(edtUnregisterQuery
165 } catch (NumberFormatException e) {
166 PrintLog("Invalid Query Id");
167 } catch (Exception e) {
168 PrintLog("UnRegister Query failed");
179 queryNum = Integer.parseInt(edtUnregisterQuery
180 .getText().toString()) + 1;
181 edtUnregisterQuery.setText(queryNum + "");
182 } catch (NumberFormatException e) {
183 PrintLog("Invalid Query Id");
189 queryNum = Integer.parseInt(edtUnregisterQuery
190 .getText().toString()) - 1;
191 edtUnregisterQuery.setText(queryNum + "");
192 } catch (NumberFormatException e) {
193 PrintLog("Invalid Query Id");
200 textAddHandler = new View.OnClickListener() {
202 public void onClick(View v) {
205 edtQuery.setText("");
208 case R.id.btLogClear:
212 case R.id.btFullDevice:
213 edtQuery.setText("subscribe Device if Device.dataId != 0");
216 case R.id.btDiscomfortIndex:
217 edtQuery.setText("subscribe Device.DiscomfortIndexSensor if Device.DiscomfortIndexSensor.discomfortIndex > 0");
225 SoftSensorManager = new SSMInterface();
227 String initConfig = "<SSMCore>" + "<Device>" + "<UDN>" + getUUID()
228 + "</UDN>" + "<Name>MyMobile</Name>" + "<Type>Mobile</Type>"
229 + "</Device>" + "<Config>"
230 + "<SoftSensorRepository>/data/data/" + getPackageName()
231 + "/files/</SoftSensorRepository>"
232 + "<SoftSensorDescription>/data/data/" + getPackageName()
233 + "/files/SoftSensorDescription.xml</SoftSensorDescription>"
234 + "</Config>" + "</SSMCore>";
237 SoftSensorManager.startSSMCore(initConfig);
238 } catch (Exception e) {
242 findViewById(R.id.btnRegisterQuery).setOnClickListener(clickHandler);
243 findViewById(R.id.btnUnregisterQuery).setOnClickListener(clickHandler);
244 findViewById(R.id.btFullDevice).setOnClickListener(textAddHandler);
245 findViewById(R.id.btDiscomfortIndex).setOnClickListener(textAddHandler);
246 findViewById(R.id.btPlus).setOnClickListener(clickHandler);
247 findViewById(R.id.btMinus).setOnClickListener(clickHandler);
248 findViewById(R.id.btClear).setOnClickListener(textAddHandler);
249 findViewById(R.id.btLogClear).setOnClickListener(textAddHandler);
253 protected void onDestroy() {
255 SoftSensorManager.stopSSMCore();
256 } catch (Exception e) {
263 private String getUUID() {
264 String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
265 SharedPreferences sharedPrefs = getSharedPreferences(PREF_UNIQUE_ID,
266 Context.MODE_PRIVATE);
267 String uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
269 if (uniqueID == null) {
270 uniqueID = UUID.randomUUID().toString();
271 Editor editor = sharedPrefs.edit();
272 editor.putString(PREF_UNIQUE_ID, uniqueID);
279 private void copyFiles(String path) {
280 AssetManager assetManager = getAssets();
281 String assets[] = null;
284 assets = assetManager.list(path);
286 if (assets.length == 0) {
289 String fullPath = "/data/data/"
290 + this.getClass().getPackage().toString() + "/" + path;
291 File dir = new File(fullPath);
295 for (int i = 0; i < assets.length; ++i) {
296 copyFiles(path + "/" + assets[i]);
299 } catch (IOException ex) {
300 Log.e("tag", "I/O Exception", ex);
304 private void copyFile(String filename) {
305 AssetManager assetManager = getAssets();
306 InputStream in = null;
307 OutputStream out = null;
310 in = assetManager.open(filename);
311 out = openFileOutput(filename.split("/")[1], Context.MODE_PRIVATE);
313 byte[] buffer = new byte[1024];
316 while ((read = in.read(buffer)) != -1) {
317 out.write(buffer, 0, read);
325 } catch (Exception e) {
326 Log.e("tag", e.getMessage());
330 private void configurePlatform() {
332 ConnectivityManager connManager;
335 PlatformConfig platformConfigObj;
337 // Check the wifi connectivity
338 connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
339 wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
340 if (false == wifi.isConnected()) {
341 // WiFi is not connected close the application
342 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
343 dialogBuilder.setTitle("Error");
345 .setMessage("WiFi is not enabled/connected! Please connect the WiFi and start application again...");
346 dialogBuilder.setCancelable(false);
347 dialogBuilder.setPositiveButton("OK", new OnClickListener() {
349 public void onClick(DialogInterface dialog, int which) {
350 // Closing the application
351 activityObj.finish();
355 dialog = dialogBuilder.create();
359 // If wifi is connected calling the configure method for configuring the
361 platformConfigObj = new PlatformConfig(this,ServiceType.IN_PROC,
362 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
364 Log.i(LOG_TAG, "Before Calling Configure of ocPlatform");
365 OcPlatform.Configure(platformConfigObj);
366 Log.i(LOG_TAG, "Configuration done Successfully");