Adding config file for sharing.
[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.initTable();
110             t1.rebuild(); // update
111             Log.e("Ali::::", "Here3");
112
113         } catch (Exception e) {
114
115             StringWriter sw = new StringWriter();
116             PrintWriter pw = new PrintWriter(sw);
117             e.printStackTrace(pw);
118             Log.e("ALI::::", sw.toString());
119
120         }
121         // TextViews
122         alarmStatus = (TextView) findViewById(R.id.alarmStatus);
123         alarmStatus.setText("OFF");
124         alarmStatus.setTextColor(Color.BLUE);
125         alarmButton = (Button) findViewById(R.id.alarmButton);
126         alarmButton.setOnClickListener(this);
127
128         handler.post(runnable);
129     }
130
131     public void onClick(View v) {
132
133         if (v == alarmButton) {
134             String strAlarm = "alarm";
135             IoTString iotAlarm = new IoTString(strAlarm);
136             String strStatusOn = "1";
137             IoTString iotStatusOn = new IoTString(strStatusOn);
138             String strStatusOff = "0";
139             IoTString iotStatusOff = new IoTString(strStatusOff);
140
141             Log.d("RAHMADI:::::", "Button pressed!");
142
143             try {
144                 mutex.acquire();
145                 if (!alarmOn) {
146
147                     try {
148                         t1.update();
149                         t1.startTransaction();
150                         t1.addKV(iotAlarm, iotStatusOn);
151                         t1.commitTransaction();
152                         alarmOn = true;
153                         alarmButton.setText("ON");
154                     } catch (Exception e) {
155                         StringWriter sw = new StringWriter();
156                         PrintWriter pw = new PrintWriter(sw);
157                         e.printStackTrace(pw);
158                         Log.e("ALI::::", sw.toString());
159                     }
160
161                 } else {
162
163                     try {
164                         t1.update();
165                         t1.startTransaction();
166                         t1.addKV(iotAlarm, iotStatusOff);
167                         t1.commitTransaction();
168                         alarmOn = false;
169                         alarmButton.setText("OFF");
170                     } catch (Exception e) {
171                         StringWriter sw = new StringWriter();
172                         PrintWriter pw = new PrintWriter(sw);
173                         e.printStackTrace(pw);
174                         Log.e("ALI::::", sw.toString());
175                     }
176                 }
177                 mutex.release();
178
179             } catch (Exception e) {
180                 StringWriter sw = new StringWriter();
181                 PrintWriter pw = new PrintWriter(sw);
182                 e.printStackTrace(pw);
183                 Log.e("ALI::::", sw.toString());
184             }
185         }
186     }
187 }