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 = 399;
37 private static final int LISTENING_PORT = 7000;
39 private Runnable runnable = new Runnable() {
43 String strBulb = "bulb";
44 IoTString iotBulb = new IoTString(strBulb);
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(iotBulb);
55 if(testValStatus == null)
58 String strStatus = testValStatus.toString();
59 Log.d("RAHMADI::::", "Getting bulb status: " + strStatus);
60 if (strStatus.equals("off")) {
61 alarmStatus.setText("OFF");
62 alarmButton.setText("ON");
63 alarmStatus.setTextColor(Color.BLUE);
64 //alarmSwitch.setChecked(false);
66 Log.d("RAHMADI::::", "Set text to OFF and BLUE with alarm value: " + testValStatus);
69 alarmStatus.setText("ON");
70 alarmButton.setText("OFF");
71 alarmStatus.setTextColor(Color.RED);
72 //alarmSwitch.setChecked(true);
74 Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
78 } catch (Exception e) {
79 StringWriter sw = new StringWriter();
80 PrintWriter pw = new PrintWriter(sw);
81 e.printStackTrace(pw);
82 Log.e("ALI::::", sw.toString());
85 // Repeat every 2 seconds
86 handler.postDelayed(runnable, 1000);
92 protected void onCreate(Bundle savedInstanceState) {
94 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
95 StrictMode.setThreadPolicy(policy);
97 super.onCreate(savedInstanceState);
98 setContentView(R.layout.activity_main);
99 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
100 setSupportActionBar(toolbar);
103 Log.e("Ali::::", "Here1");
104 t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
105 Log.e("Ali::::", "Here2");
107 t1.rebuild(); // update
108 t1.addLocalCommunication(400, "192.168.1.192", 6000);
109 Log.e("Ali::::", "Here3");
111 } catch (Exception e) {
113 StringWriter sw = new StringWriter();
114 PrintWriter pw = new PrintWriter(sw);
115 e.printStackTrace(pw);
116 Log.e("ALI::::", sw.toString());
120 alarmStatus = (TextView) findViewById(R.id.alarmStatus);
121 alarmStatus.setText("OFF");
122 alarmStatus.setTextColor(Color.BLUE);
123 alarmButton = (Button) findViewById(R.id.alarmButton);
124 alarmButton.setOnClickListener(this);
126 handler.post(runnable);
129 public void onClick(View v) {
131 if (v == alarmButton) {
132 String strBulb = "bulb";
133 IoTString iotBulb = new IoTString(strBulb);
134 String strStatusOn = "on";
135 IoTString iotStatusOn = new IoTString(strStatusOn);
136 String strStatusOff = "off";
137 IoTString iotStatusOff = new IoTString(strStatusOff);
139 Log.d("RAHMADI:::::", "Button pressed!");
147 t1.startTransaction();
148 t1.addKV(iotBulb, iotStatusOn);
149 t1.commitTransaction();
151 alarmButton.setText("ON");
152 } catch (Exception e) {
153 StringWriter sw = new StringWriter();
154 PrintWriter pw = new PrintWriter(sw);
155 e.printStackTrace(pw);
156 Log.e("ALI::::", sw.toString());
163 t1.startTransaction();
164 t1.addKV(iotBulb, iotStatusOff);
165 t1.commitTransaction();
167 alarmButton.setText("OFF");
168 } catch (Exception e) {
169 StringWriter sw = new StringWriter();
170 PrintWriter pw = new PrintWriter(sw);
171 e.printStackTrace(pw);
172 Log.e("ALI::::", sw.toString());
177 } catch (Exception e) {
178 StringWriter sw = new StringWriter();
179 PrintWriter pw = new PrintWriter(sw);
180 e.printStackTrace(pw);
181 Log.e("ALI::::", sw.toString());