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.view.View;
11 import android.widget.Button;
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 implements View.OnClickListener {
29 Semaphore mutex = new Semaphore(1);
31 boolean alarmOn = false;
33 private Handler handler = new Handler();
34 private static final String CLOUD_SERVER = "http://dc-6.calit2.uci.edu/test.iotcloud/";
35 private static final String PASSWORD = "reallysecret";
36 private static final int LOCAL_MACHINE_ID = 400;
37 private static final int LISTENING_PORT = -1;
39 private Runnable runnable = new Runnable() {
43 String strAlarm = "alarm";
44 IoTString iotAlarm = new IoTString(strAlarm);
46 // Insert custom code here
48 Log.e("Ali:::::", "loop............");
50 //t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
53 IoTString testValStatus = t1.getCommitted(iotAlarm);
57 if(testValStatus != null) {
58 String strStatus = testValStatus.toString();
59 intStatus = Integer.parseInt(strStatus);
63 alarmStatus.setText("OFF");
64 alarmButton.setText("ON");
65 alarmStatus.setTextColor(Color.BLUE);
66 //alarmSwitch.setChecked(false);
68 Log.d("RAHMADI::::", "Set text to OFF and BLUE with alarm value: " + testValStatus);
71 alarmStatus.setText("ON");
72 alarmButton.setText("OFF");
73 alarmStatus.setTextColor(Color.RED);
74 //alarmSwitch.setChecked(true);
76 Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
80 } catch (Exception e) {
81 StringWriter sw = new StringWriter();
82 PrintWriter pw = new PrintWriter(sw);
83 e.printStackTrace(pw);
84 Log.e("ALI::::", sw.toString());
88 // Repeat every 2 seconds
89 handler.postDelayed(runnable, 1000);
95 protected void onCreate(Bundle savedInstanceState) {
97 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
98 StrictMode.setThreadPolicy(policy);
100 super.onCreate(savedInstanceState);
101 setContentView(R.layout.activity_main);
102 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
103 setSupportActionBar(toolbar);
106 Log.e("Ali::::", "Here1");
107 t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
108 Log.e("Ali::::", "Here2");
109 t1.rebuild(); // update
110 Log.e("Ali::::", "Here3");
112 } catch (Exception e) {
114 StringWriter sw = new StringWriter();
115 PrintWriter pw = new PrintWriter(sw);
116 e.printStackTrace(pw);
117 Log.e("ALI::::", sw.toString());
121 alarmStatus = (TextView) findViewById(R.id.alarmStatus);
122 alarmStatus.setText("OFF");
123 alarmStatus.setTextColor(Color.BLUE);
124 alarmButton = (Button) findViewById(R.id.alarmButton);
125 alarmButton.setOnClickListener(this);
127 handler.post(runnable);
130 public void onClick(View v) {
132 if (v == alarmButton) {
133 String strAlarm = "alarm";
134 IoTString iotAlarm = new IoTString(strAlarm);
135 String strStatusOn = "1";
136 IoTString iotStatusOn = new IoTString(strStatusOn);
137 String strStatusOff = "0";
138 IoTString iotStatusOff = new IoTString(strStatusOff);
140 Log.d("RAHMADI:::::", "Button pressed!");
148 t1.startTransaction();
149 t1.addKV(iotAlarm, iotStatusOn);
150 t1.commitTransaction();
152 alarmButton.setText("ON");
153 } catch (Exception e) {
154 StringWriter sw = new StringWriter();
155 PrintWriter pw = new PrintWriter(sw);
156 e.printStackTrace(pw);
157 Log.e("ALI::::", sw.toString());
164 t1.startTransaction();
165 t1.addKV(iotAlarm, iotStatusOff);
166 t1.commitTransaction();
168 alarmButton.setText("OFF");
169 } catch (Exception e) {
170 StringWriter sw = new StringWriter();
171 PrintWriter pw = new PrintWriter(sw);
172 e.printStackTrace(pw);
173 Log.e("ALI::::", sw.toString());
178 } catch (Exception e) {
179 StringWriter sw = new StringWriter();
180 PrintWriter pw = new PrintWriter(sw);
181 e.printStackTrace(pw);
182 Log.e("ALI::::", sw.toString());