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);
45 String strBulb = "bulb";
46 IoTString iotBulb = new IoTString(strBulb);
48 // Insert custom code here
50 Log.e("Ali:::::", "loop............");
52 //t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
55 IoTString testValStatus = t1.getCommitted(iotBulb);
59 if(testValStatus != null) {
60 String strStatus = testValStatus.toString();
61 intStatus = Integer.parseInt(strStatus);
65 alarmStatus.setText("OFF");
66 alarmButton.setText("ON");
67 alarmStatus.setTextColor(Color.BLUE);
68 //alarmSwitch.setChecked(false);
70 Log.d("RAHMADI::::", "Set text to OFF and BLUE with alarm value: " + testValStatus);
73 alarmStatus.setText("ON");
74 alarmButton.setText("OFF");
75 alarmStatus.setTextColor(Color.RED);
76 //alarmSwitch.setChecked(true);
78 Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
82 } catch (Exception e) {
83 StringWriter sw = new StringWriter();
84 PrintWriter pw = new PrintWriter(sw);
85 e.printStackTrace(pw);
86 Log.e("ALI::::", sw.toString());
90 // Repeat every 2 seconds
91 handler.postDelayed(runnable, 1000);
97 protected void onCreate(Bundle savedInstanceState) {
99 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
100 StrictMode.setThreadPolicy(policy);
102 super.onCreate(savedInstanceState);
103 setContentView(R.layout.activity_main);
104 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
105 setSupportActionBar(toolbar);
108 Log.e("Ali::::", "Here1");
109 t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
110 Log.e("Ali::::", "Here2");
112 t1.rebuild(); // update
113 t1.addLocalCommunication(260, "192.168.1.192", 6000);
114 Log.e("Ali::::", "Here3");
116 } catch (Exception e) {
118 StringWriter sw = new StringWriter();
119 PrintWriter pw = new PrintWriter(sw);
120 e.printStackTrace(pw);
121 Log.e("ALI::::", sw.toString());
125 alarmStatus = (TextView) findViewById(R.id.alarmStatus);
126 alarmStatus.setText("OFF");
127 alarmStatus.setTextColor(Color.BLUE);
128 alarmButton = (Button) findViewById(R.id.alarmButton);
129 alarmButton.setOnClickListener(this);
131 //handler.post(runnable);
134 public void onClick(View v) {
136 if (v == alarmButton) {
137 String strBulb = "bulb";
138 IoTString iotBulb = new IoTString(strBulb);
139 String strStatusOn = "on";
140 IoTString iotStatusOn = new IoTString(strStatusOn);
141 String strStatusOff = "off";
142 IoTString iotStatusOff = new IoTString(strStatusOff);
144 Log.d("RAHMADI:::::", "Button pressed!");
152 t1.startTransaction();
153 t1.addKV(iotBulb, iotStatusOn);
154 t1.commitTransaction();
156 alarmButton.setText("ON");
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());
168 t1.startTransaction();
169 t1.addKV(iotBulb, iotStatusOff);
170 t1.commitTransaction();
172 alarmButton.setText("OFF");
173 } catch (Exception e) {
174 StringWriter sw = new StringWriter();
175 PrintWriter pw = new PrintWriter(sw);
176 e.printStackTrace(pw);
177 Log.e("ALI::::", sw.toString());
182 } catch (Exception e) {
183 StringWriter sw = new StringWriter();
184 PrintWriter pw = new PrintWriter(sw);
185 e.printStackTrace(pw);
186 Log.e("ALI::::", sw.toString());