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.*;
* @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() {
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) {
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();
// Repeat every 2 seconds
handler.postDelayed(runnable, 1000);
- //handler.post(runnable);
}
};
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());
+ }
+ }
}
}
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="375">
- <file leaf-file-name="MainActivity.java" pinned="false" current-in-tab="false">
+ <file leaf-file-name="MainActivity.java" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/main/java/com/example/lede2/MainActivity.java">
<provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="1350">
- <caret line="101" column="21" selection-start-line="100" selection-start-column="12" selection-end-line="101" selection-end-column="21" />
- <folding>
- <element signature="imports" expanded="false" />
- </folding>
+ <state relative-caret-position="366">
+ <caret line="63" column="8" selection-start-line="63" selection-start-column="8" selection-end-line="63" selection-end-column="46" />
+ <folding />
</state>
</provider>
</entry>
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="525">
<caret line="51" column="12" selection-start-line="51" selection-start-column="8" selection-end-line="51" selection-end-column="12" />
- <folding>
- <element signature="imports" expanded="false" />
- </folding>
+ <folding />
</state>
</provider>
</entry>
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1590">
<caret line="124" column="30" selection-start-line="124" selection-start-column="30" selection-end-line="124" selection-end-column="30" />
- <folding>
- <element signature="e#0#121#0" expanded="false" />
- <element signature="imports" expanded="false" />
- </folding>
+ <folding />
</state>
</provider>
</entry>
<file leaf-file-name="ListActivity.java" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/main/java/com/example/lede2/ListActivity.java">
<provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="15">
+ <state relative-caret-position="495">
<caret line="47" column="8" selection-start-line="47" selection-start-column="8" selection-end-line="47" selection-end-column="8" />
<folding />
</state>
</provider>
</entry>
</file>
- <file leaf-file-name="activity_enroll_device.xml" pinned="false" current-in-tab="true">
+ <file leaf-file-name="activity_enroll_device.xml" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/main/res/layout/activity_enroll_device.xml">
<provider editor-type-id="text-editor">
<state relative-caret-position="120">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
- <pane id="Scope" />
- <pane id="Scratches" />
<pane id="AndroidView">
<subPane />
</pane>
+ <pane id="Scope" />
<pane id="PackagesPane" />
+ <pane id="Scratches" />
<pane id="ProjectPane">
<subPane>
<PATH>
</state>
</provider>
</entry>
- <entry file="file://$PROJECT_DIR$/src/main/java/com/example/lede2/MainActivity.java">
- <provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="1350">
- <caret line="101" column="21" selection-start-line="100" selection-start-column="12" selection-end-line="101" selection-end-column="21" />
- <folding>
- <element signature="imports" expanded="false" />
- </folding>
- </state>
- </provider>
- </entry>
<entry file="file://$PROJECT_DIR$/src/main/java/com/example/lede2/EnrollDeviceActivity.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="525">
<caret line="51" column="12" selection-start-line="51" selection-start-column="8" selection-end-line="51" selection-end-column="12" />
- <folding>
- <element signature="imports" expanded="false" />
- </folding>
+ <folding />
</state>
</provider>
</entry>
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1590">
<caret line="124" column="30" selection-start-line="124" selection-start-column="30" selection-end-line="124" selection-end-column="30" />
- <folding>
- <element signature="e#0#121#0" expanded="false" />
- <element signature="imports" expanded="false" />
- </folding>
+ <folding />
</state>
</provider>
</entry>
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="315">
<caret line="21" column="104" selection-start-line="21" selection-start-column="104" selection-end-line="21" selection-end-column="104" />
- <folding />
</state>
</provider>
</entry>
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="105">
<caret line="7" column="80" selection-start-line="7" selection-start-column="80" selection-end-line="7" selection-end-column="80" />
- <folding />
</state>
</provider>
</entry>
</entry>
<entry file="file://$PROJECT_DIR$/src/main/java/com/example/lede2/ListActivity.java">
<provider selected="true" editor-type-id="text-editor">
- <state relative-caret-position="15">
+ <state relative-caret-position="495">
<caret line="47" column="8" selection-start-line="47" selection-start-column="8" selection-end-line="47" selection-end-column="8" />
<folding />
</state>
<state />
</provider>
</entry>
+ <entry file="file://$PROJECT_DIR$/src/main/java/com/example/lede2/MainActivity.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state relative-caret-position="366">
+ <caret line="63" column="8" selection-start-line="63" selection-start-column="8" selection-end-line="63" selection-end-column="46" />
+ <folding />
+ </state>
+ </provider>
+ </entry>
</component>
</project>
\ No newline at end of file