Using a button instead of a switch for the app (avoiding race condition)
[iot2.git] / benchmarks / other / PhoneInterface / Control / app / src / main / java / com / example / ali / control / MainActivity.java
index 7b57ef1..1133cc4 100644 (file)
@@ -7,8 +7,8 @@ import android.support.v7.widget.Toolbar;
 import android.os.StrictMode;
 
 import android.util.Log;
-import android.widget.CompoundButton;
-import android.widget.Switch;
+import android.view.View;
+import android.widget.Button;
 import android.widget.TextView;
 import iotcloud.*;
 import java.io.*;
@@ -20,21 +20,20 @@ import android.os.Handler;
  * @author Rahmadi Trimananda <rtrimana@uci.edu>
  * @version 1.0
  */
-public class MainActivity extends AppCompatActivity {
+public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
-    Switch alarmSwitch;
+    Button alarmButton;
     TextView alarmStatus;
 
     Table t1 = null;
-    Thread thread = null;
     Semaphore mutex = new Semaphore(1);
 
-    boolean didCrash = false;
+    boolean alarmOn = false;
 
     private Handler handler = new Handler();
     private static final String CLOUD_SERVER = "http://dc-6.calit2.uci.edu/test.iotcloud/";
     private static final String PASSWORD = "reallysecret";
-    private static final int LOCAL_MACHINE_ID = 399;
+    private static final int LOCAL_MACHINE_ID = 400;
     private static final int LISTENING_PORT = -1;
 
     private Runnable runnable = new Runnable() {
@@ -48,12 +47,11 @@ public class MainActivity extends AppCompatActivity {
             try {
                 Log.e("Ali:::::", "loop............");
                 mutex.acquire();
-                t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
-                t1.rebuild();
-                //t1.update();
+                //t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
+                //t1.rebuild();
+                t1.update();
                 IoTString testValStatus = t1.getCommitted(iotAlarm);
                 t1.update();
-                mutex.release();
 
                 int intStatus = 0;
                 if(testValStatus != null) {
@@ -64,15 +62,18 @@ public class MainActivity extends AppCompatActivity {
                 if (intStatus == 0) {
                     alarmStatus.setText("OFF");
                     alarmStatus.setTextColor(Color.BLUE);
-                    alarmSwitch.setChecked(false);
+                    //alarmSwitch.setChecked(false);
+                    alarmOn = false;
                     Log.d("RAHMADI::::", "Set text to OFF and BLUE with alarm value: " + testValStatus);
                 }
                 else {// value 1
                     alarmStatus.setText("ON");
                     alarmStatus.setTextColor(Color.RED);
-                    alarmSwitch.setChecked(true);
+                    //alarmSwitch.setChecked(true);
+                    alarmOn = true;
                     Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
                 }
+                mutex.release();
 
             } catch (Exception e) {
                 StringWriter sw = new StringWriter();
@@ -84,7 +85,6 @@ public class MainActivity extends AppCompatActivity {
 
             // Repeat every 2 seconds
             handler.postDelayed(runnable, 1000);
-            //handler.post(runnable);
         }
     };
 
@@ -119,58 +119,66 @@ public class MainActivity extends AppCompatActivity {
         alarmStatus = (TextView) findViewById(R.id.alarmStatus);
         alarmStatus.setText("OFF");
         alarmStatus.setTextColor(Color.BLUE);
-        alarmSwitch = (Switch) findViewById(R.id.alarmSwitch);
-
-        alarmSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-            @Override
-            public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
-
-                String strAlarm = "alarm";
-                IoTString iotAlarm = new IoTString(strAlarm);
-                String strStatusOn = "1";
-                IoTString iotStatusOn = new IoTString(strStatusOn);
-                String strStatusOff = "0";
-                IoTString iotStatusOff = new IoTString(strStatusOff);
-
-                try {
-                    if (bChecked) {
-
-                        try {
-                            t1.update();
-                            t1.startTransaction();
-                            t1.addKV(iotAlarm, iotStatusOn);
-                            t1.commitTransaction();
-                        } catch (Exception e) {
-                            StringWriter sw = new StringWriter();
-                            PrintWriter pw = new PrintWriter(sw);
-                            e.printStackTrace(pw);
-                            Log.e("ALI::::", sw.toString());
-                        }
-
-                    } else {
-
-                        try {
-                            t1.update();
-                            t1.startTransaction();
-                            t1.addKV(iotAlarm, iotStatusOff);
-                            t1.commitTransaction();
-                        } catch (Exception e) {
-                            StringWriter sw = new StringWriter();
-                            PrintWriter pw = new PrintWriter(sw);
-                            e.printStackTrace(pw);
-                            Log.e("ALI::::", sw.toString());
-                        }
+        alarmButton = (Button) findViewById(R.id.alarmButton);
+        alarmButton.setOnClickListener(this);
+
+        handler.post(runnable);
+    }
+
+    public void onClick(View v) {
+
+        if (v == alarmButton) {
+            String strAlarm = "alarm";
+            IoTString iotAlarm = new IoTString(strAlarm);
+            String strStatusOn = "1";
+            IoTString iotStatusOn = new IoTString(strStatusOn);
+            String strStatusOff = "0";
+            IoTString iotStatusOff = new IoTString(strStatusOff);
+
+            Log.d("RAHMADI:::::", "Button pressed!");
+
+            try {
+                mutex.acquire();
+                if (!alarmOn) {
+
+                    try {
+                        t1.update();
+                        t1.startTransaction();
+                        t1.addKV(iotAlarm, iotStatusOn);
+                        t1.commitTransaction();
+                        alarmOn = true;
+                        alarmButton.setText("ON");
+                    } catch (Exception e) {
+                        StringWriter sw = new StringWriter();
+                        PrintWriter pw = new PrintWriter(sw);
+                        e.printStackTrace(pw);
+                        Log.e("ALI::::", sw.toString());
                     }
 
-                } catch (Exception e) {
-                    StringWriter sw = new StringWriter();
-                    PrintWriter pw = new PrintWriter(sw);
-                    e.printStackTrace(pw);
-                    Log.e("ALI::::", sw.toString());
+                } else {
+
+                    try {
+                        t1.update();
+                        t1.startTransaction();
+                        t1.addKV(iotAlarm, iotStatusOff);
+                        t1.commitTransaction();
+                        alarmOn = false;
+                        alarmButton.setText("OFF");
+                    } catch (Exception e) {
+                        StringWriter sw = new StringWriter();
+                        PrintWriter pw = new PrintWriter(sw);
+                        e.printStackTrace(pw);
+                        Log.e("ALI::::", sw.toString());
+                    }
                 }
-            }
-        });
+                mutex.release();
 
-        handler.post(runnable);
+            } catch (Exception e) {
+                StringWriter sw = new StringWriter();
+                PrintWriter pw = new PrintWriter(sw);
+                e.printStackTrace(pw);
+                Log.e("ALI::::", sw.toString());
+            }
+        }
     }
 }