c1fb2086406ff45bc092dd5a36ee10ba86f419f8
[iot2.git] / benchmarks / other / PhoneInterface / Control / app / src / main / java / com / example / ali / control / MainActivity.java
1 package com.example.ali.control;
2
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;
8
9 import android.util.Log;
10 import android.view.View;
11 import android.widget.Button;
12 import android.widget.TextView;
13 import iotcloud.*;
14 import java.io.*;
15 import java.util.concurrent.*;
16 import android.os.Handler;
17
18 /**
19  * This is a simple alarm controller for Android phone based on the code from Ali Younis
20  * @author Rahmadi Trimananda <rtrimana@uci.edu>
21  * @version 1.0
22  */
23 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
24
25     Button alarmButton;
26     TextView alarmStatus;
27
28     Table t1 = null;
29     Semaphore mutex = new Semaphore(1);
30
31     boolean alarmOn = false;
32
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;
38
39     private Runnable runnable = new Runnable() {
40         @Override
41         public void run() {
42
43             String strAlarm = "alarm";
44             IoTString iotAlarm = new IoTString(strAlarm);
45
46             // Insert custom code here
47             try {
48                 Log.e("Ali:::::", "loop............");
49                 mutex.acquire();
50                 //t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
51                 //t1.rebuild();
52                 t1.update();
53                 IoTString testValStatus = t1.getCommitted(iotAlarm);
54                 t1.update();
55
56                 int intStatus = 0;
57                 if(testValStatus != null) {
58                     String strStatus = testValStatus.toString();
59                     intStatus = Integer.parseInt(strStatus);
60                 }
61
62                 if (intStatus == 0) {
63                     alarmStatus.setText("OFF");
64                     alarmButton.setText("ON");
65                     alarmStatus.setTextColor(Color.BLUE);
66                     //alarmSwitch.setChecked(false);
67                     alarmOn = false;
68                     Log.d("RAHMADI::::", "Set text to OFF and BLUE with alarm value: " + testValStatus);
69                 }
70                 else {// value 1
71                     alarmStatus.setText("ON");
72                     alarmButton.setText("OFF");
73                     alarmStatus.setTextColor(Color.RED);
74                     //alarmSwitch.setChecked(true);
75                     alarmOn = true;
76                     Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
77                 }
78                 mutex.release();
79
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());
85             }
86
87
88             // Repeat every 2 seconds
89             handler.postDelayed(runnable, 1000);
90         }
91     };
92
93
94     @Override
95     protected void onCreate(Bundle savedInstanceState) {
96
97         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
98         StrictMode.setThreadPolicy(policy);
99
100         super.onCreate(savedInstanceState);
101         setContentView(R.layout.activity_main);
102         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
103         setSupportActionBar(toolbar);
104
105             try {
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");
111
112             } catch (Exception e) {
113
114                 StringWriter sw = new StringWriter();
115                 PrintWriter pw = new PrintWriter(sw);
116                 e.printStackTrace(pw);
117                 Log.e("ALI::::", sw.toString());
118
119         }
120         // TextViews
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);
126
127         handler.post(runnable);
128     }
129
130     public void onClick(View v) {
131
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);
139
140             Log.d("RAHMADI:::::", "Button pressed!");
141
142             try {
143                 mutex.acquire();
144                 if (!alarmOn) {
145
146                     try {
147                         t1.update();
148                         t1.startTransaction();
149                         t1.addKV(iotAlarm, iotStatusOn);
150                         t1.commitTransaction();
151                         alarmOn = true;
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());
158                     }
159
160                 } else {
161
162                     try {
163                         t1.update();
164                         t1.startTransaction();
165                         t1.addKV(iotAlarm, iotStatusOff);
166                         t1.commitTransaction();
167                         alarmOn = false;
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());
174                     }
175                 }
176                 mutex.release();
177
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());
183             }
184         }
185     }
186 }