From: rtrimana Date: Fri, 29 Sep 2017 22:51:55 +0000 (-0700) Subject: Using a button instead of a switch for the app (avoiding race condition) X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=bba1273366946a5e9b801200611865e50d21e31f;p=iot2.git Using a button instead of a switch for the app (avoiding race condition) --- diff --git a/benchmarks/other/PhoneInterface/Control/app/src/main/java/com/example/ali/control/MainActivity.java b/benchmarks/other/PhoneInterface/Control/app/src/main/java/com/example/ali/control/MainActivity.java index 7b57ef1..1133cc4 100644 --- a/benchmarks/other/PhoneInterface/Control/app/src/main/java/com/example/ali/control/MainActivity.java +++ b/benchmarks/other/PhoneInterface/Control/app/src/main/java/com/example/ali/control/MainActivity.java @@ -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 * @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()); + } + } } } diff --git a/benchmarks/other/PhoneInterface/Control/app/src/main/res/layout/content_main.xml b/benchmarks/other/PhoneInterface/Control/app/src/main/res/layout/content_main.xml index 83963bb..3da3345 100644 --- a/benchmarks/other/PhoneInterface/Control/app/src/main/res/layout/content_main.xml +++ b/benchmarks/other/PhoneInterface/Control/app/src/main/res/layout/content_main.xml @@ -42,14 +42,12 @@ android:textSize="36sp" android:textStyle="bold" /> - + android:layout_centerHorizontal="true" /> diff --git a/others/lede-gui/.idea/workspace.xml b/others/lede-gui/.idea/workspace.xml index c0a6a8c..49130a1 100644 --- a/others/lede-gui/.idea/workspace.xml +++ b/others/lede-gui/.idea/workspace.xml @@ -47,14 +47,12 @@ - + - - - - - + + + @@ -64,9 +62,7 @@ - - - + @@ -76,10 +72,7 @@ - - - - + @@ -87,7 +80,7 @@ - + @@ -107,7 +100,7 @@ - + @@ -1239,12 +1232,12 @@ - - + + @@ -2279,23 +2272,11 @@ - - - - - - - - - - - - - + @@ -2303,10 +2284,7 @@ - - - - + @@ -2314,7 +2292,6 @@ - @@ -2332,7 +2309,6 @@ - @@ -2349,7 +2325,7 @@ - + @@ -2377,5 +2353,13 @@ + + + + + + + + \ No newline at end of file