1 package com.example.ali.control;
3 import android.graphics.Color;
4 import android.os.Bundle;
5 import android.support.v7.app.AppCompatActivity;
6 import android.support.v7.widget.Toolbar;
7 import android.os.StrictMode;
9 import android.util.Log;
10 import android.widget.CompoundButton;
11 import android.widget.Switch;
12 import android.widget.TextView;
15 import java.util.concurrent.*;
16 import android.os.Handler;
19 * This is a simple alarm controller for Android phone based on the code from Ali Younis
20 * @author Rahmadi Trimananda <rtrimana@uci.edu>
23 public class MainActivity extends AppCompatActivity {
30 Semaphore mutex = new Semaphore(1);
32 boolean didCrash = false;
34 private Handler handler = new Handler();
35 private static final String CLOUD_SERVER = "http://dc-6.calit2.uci.edu/test.iotcloud/";
36 private static final String PASSWORD = "reallysecret";
37 private static final int LOCAL_MACHINE_ID = 399;
38 private static final int LISTENING_PORT = -1;
40 private Runnable runnable = new Runnable() {
44 String strAlarm = "alarm";
45 IoTString iotAlarm = new IoTString(strAlarm);
47 // Insert custom code here
49 Log.e("Ali:::::", "loop............");
51 t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
54 IoTString testValStatus = t1.getCommitted(iotAlarm);
59 if(testValStatus != null) {
60 String strStatus = testValStatus.toString();
61 intStatus = Integer.parseInt(strStatus);
65 alarmStatus.setText("OFF");
66 alarmStatus.setTextColor(Color.BLUE);
67 alarmSwitch.setChecked(false);
68 Log.d("RAHMADI::::", "Set text to OFF and BLUE with alarm value: " + testValStatus);
71 alarmStatus.setText("ON");
72 alarmStatus.setTextColor(Color.RED);
73 alarmSwitch.setChecked(true);
74 Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
77 } catch (Exception e) {
78 StringWriter sw = new StringWriter();
79 PrintWriter pw = new PrintWriter(sw);
80 e.printStackTrace(pw);
81 Log.e("ALI::::", sw.toString());
85 // Repeat every 2 seconds
86 handler.postDelayed(runnable, 1000);
87 //handler.post(runnable);
93 protected void onCreate(Bundle savedInstanceState) {
95 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
96 StrictMode.setThreadPolicy(policy);
98 super.onCreate(savedInstanceState);
99 setContentView(R.layout.activity_main);
100 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
101 setSupportActionBar(toolbar);
104 Log.e("Ali::::", "Here1");
105 t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
106 Log.e("Ali::::", "Here2");
107 t1.rebuild(); // update
108 Log.e("Ali::::", "Here3");
110 } catch (Exception e) {
112 StringWriter sw = new StringWriter();
113 PrintWriter pw = new PrintWriter(sw);
114 e.printStackTrace(pw);
115 Log.e("ALI::::", sw.toString());
119 alarmStatus = (TextView) findViewById(R.id.alarmStatus);
120 alarmStatus.setText("OFF");
121 alarmStatus.setTextColor(Color.BLUE);
122 alarmSwitch = (Switch) findViewById(R.id.alarmSwitch);
124 alarmSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
126 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
128 String strAlarm = "alarm";
129 IoTString iotAlarm = new IoTString(strAlarm);
130 String strStatusOn = "1";
131 IoTString iotStatusOn = new IoTString(strStatusOn);
132 String strStatusOff = "0";
133 IoTString iotStatusOff = new IoTString(strStatusOff);
140 t1.startTransaction();
141 t1.addKV(iotAlarm, iotStatusOn);
142 t1.commitTransaction();
143 } catch (Exception e) {
144 StringWriter sw = new StringWriter();
145 PrintWriter pw = new PrintWriter(sw);
146 e.printStackTrace(pw);
147 Log.e("ALI::::", sw.toString());
154 t1.startTransaction();
155 t1.addKV(iotAlarm, iotStatusOff);
156 t1.commitTransaction();
157 } catch (Exception e) {
158 StringWriter sw = new StringWriter();
159 PrintWriter pw = new PrintWriter(sw);
160 e.printStackTrace(pw);
161 Log.e("ALI::::", sw.toString());
165 } catch (Exception e) {
166 StringWriter sw = new StringWriter();
167 PrintWriter pw = new PrintWriter(sw);
168 e.printStackTrace(pw);
169 Log.e("ALI::::", sw.toString());
174 handler.post(runnable);