1133cc4f4d9eee8619635dd381797741b9341d47
[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                     alarmStatus.setTextColor(Color.BLUE);
65                     //alarmSwitch.setChecked(false);
66                     alarmOn = false;
67                     Log.d("RAHMADI::::", "Set text to OFF and BLUE with alarm value: " + testValStatus);
68                 }
69                 else {// value 1
70                     alarmStatus.setText("ON");
71                     alarmStatus.setTextColor(Color.RED);
72                     //alarmSwitch.setChecked(true);
73                     alarmOn = true;
74                     Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
75                 }
76                 mutex.release();
77
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());
83             }
84
85
86             // Repeat every 2 seconds
87             handler.postDelayed(runnable, 1000);
88         }
89     };
90
91
92     @Override
93     protected void onCreate(Bundle savedInstanceState) {
94
95         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
96         StrictMode.setThreadPolicy(policy);
97
98         super.onCreate(savedInstanceState);
99         setContentView(R.layout.activity_main);
100         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
101         setSupportActionBar(toolbar);
102
103             try {
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");
109
110             } catch (Exception e) {
111
112                 StringWriter sw = new StringWriter();
113                 PrintWriter pw = new PrintWriter(sw);
114                 e.printStackTrace(pw);
115                 Log.e("ALI::::", sw.toString());
116
117         }
118         // TextViews
119         alarmStatus = (TextView) findViewById(R.id.alarmStatus);
120         alarmStatus.setText("OFF");
121         alarmStatus.setTextColor(Color.BLUE);
122         alarmButton = (Button) findViewById(R.id.alarmButton);
123         alarmButton.setOnClickListener(this);
124
125         handler.post(runnable);
126     }
127
128     public void onClick(View v) {
129
130         if (v == alarmButton) {
131             String strAlarm = "alarm";
132             IoTString iotAlarm = new IoTString(strAlarm);
133             String strStatusOn = "1";
134             IoTString iotStatusOn = new IoTString(strStatusOn);
135             String strStatusOff = "0";
136             IoTString iotStatusOff = new IoTString(strStatusOff);
137
138             Log.d("RAHMADI:::::", "Button pressed!");
139
140             try {
141                 mutex.acquire();
142                 if (!alarmOn) {
143
144                     try {
145                         t1.update();
146                         t1.startTransaction();
147                         t1.addKV(iotAlarm, iotStatusOn);
148                         t1.commitTransaction();
149                         alarmOn = true;
150                         alarmButton.setText("ON");
151                     } catch (Exception e) {
152                         StringWriter sw = new StringWriter();
153                         PrintWriter pw = new PrintWriter(sw);
154                         e.printStackTrace(pw);
155                         Log.e("ALI::::", sw.toString());
156                     }
157
158                 } else {
159
160                     try {
161                         t1.update();
162                         t1.startTransaction();
163                         t1.addKV(iotAlarm, iotStatusOff);
164                         t1.commitTransaction();
165                         alarmOn = false;
166                         alarmButton.setText("OFF");
167                     } catch (Exception e) {
168                         StringWriter sw = new StringWriter();
169                         PrintWriter pw = new PrintWriter(sw);
170                         e.printStackTrace(pw);
171                         Log.e("ALI::::", sw.toString());
172                     }
173                 }
174                 mutex.release();
175
176             } catch (Exception e) {
177                 StringWriter sw = new StringWriter();
178                 PrintWriter pw = new PrintWriter(sw);
179                 e.printStackTrace(pw);
180                 Log.e("ALI::::", sw.toString());
181             }
182         }
183     }
184 }