Phone app and iotcloud addition into the fourth benchmark and a few minor stuff
[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.widget.CompoundButton;
11 import android.widget.Switch;
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 {
24
25     Switch alarmSwitch;
26     TextView alarmStatus;
27
28     Table t1 = null;
29     Thread thread = null;
30     Semaphore mutex = new Semaphore(1);
31
32     boolean didCrash = false;
33
34     private Handler handler = new Handler();
35     private static final String CLOUD_SERVER = "http://dc-6.calit2.uci.edu/test.iotcloud/";
36     private static final String PASSWORD = "reallysecret";
37     private static final int LOCAL_MACHINE_ID = 399;
38     private static final int LISTENING_PORT = -1;
39
40     private Runnable runnable = new Runnable() {
41         @Override
42         public void run() {
43
44             String strAlarm = "alarm";
45             IoTString iotAlarm = new IoTString(strAlarm);
46
47             // Insert custom code here
48             try {
49                 Log.e("Ali:::::", "loop............");
50                 mutex.acquire();
51                 t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
52                 t1.rebuild();
53                 //t1.update();
54                 IoTString testValStatus = t1.getCommitted(iotAlarm);
55                 t1.update();
56                 mutex.release();
57
58                 int intStatus = 0;
59                 if(testValStatus != null) {
60                     String strStatus = testValStatus.toString();
61                     intStatus = Integer.parseInt(strStatus);
62                 }
63
64                 if (intStatus == 0) {
65                     alarmStatus.setText("OFF");
66                     alarmStatus.setTextColor(Color.BLUE);
67                     alarmSwitch.setChecked(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                     alarmStatus.setTextColor(Color.RED);
73                     alarmSwitch.setChecked(true);
74                     Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
75                 }
76
77             } catch (Exception e) {
78                 StringWriter sw = new StringWriter();
79                 PrintWriter pw = new PrintWriter(sw);
80                 e.printStackTrace(pw);
81                 Log.e("ALI::::", sw.toString());
82             }
83
84
85             // Repeat every 2 seconds
86             handler.postDelayed(runnable, 1000);
87             //handler.post(runnable);
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         alarmSwitch = (Switch) findViewById(R.id.alarmSwitch);
123
124         alarmSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
125             @Override
126             public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
127
128                 String strAlarm = "alarm";
129                 IoTString iotAlarm = new IoTString(strAlarm);
130                 String strStatusOn = "1";
131                 IoTString iotStatusOn = new IoTString(strStatusOn);
132                 String strStatusOff = "0";
133                 IoTString iotStatusOff = new IoTString(strStatusOff);
134
135                 try {
136                     if (bChecked) {
137
138                         try {
139                             t1.update();
140                             t1.startTransaction();
141                             t1.addKV(iotAlarm, iotStatusOn);
142                             t1.commitTransaction();
143                         } catch (Exception e) {
144                             StringWriter sw = new StringWriter();
145                             PrintWriter pw = new PrintWriter(sw);
146                             e.printStackTrace(pw);
147                             Log.e("ALI::::", sw.toString());
148                         }
149
150                     } else {
151
152                         try {
153                             t1.update();
154                             t1.startTransaction();
155                             t1.addKV(iotAlarm, iotStatusOff);
156                             t1.commitTransaction();
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());
162                         }
163                     }
164
165                 } catch (Exception e) {
166                     StringWriter sw = new StringWriter();
167                     PrintWriter pw = new PrintWriter(sw);
168                     e.printStackTrace(pw);
169                     Log.e("ALI::::", sw.toString());
170                 }
171             }
172         });
173
174         handler.post(runnable);
175     }
176 }