Tweaked phone app for testbed experiment; tweaked testbed files
[iotcloud.git] / version2 / src / Control-2bulbs / 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             String strBulb = "bulb";
46             IoTString iotBulb = new IoTString(strBulb);
47
48             // Insert custom code here
49             try {
50                 Log.e("Ali:::::", "loop............");
51                 mutex.acquire();
52                 //t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
53                 //t1.rebuild();
54                 t1.update();
55                 IoTString testValStatus = t1.getCommitted(iotBulb);
56                 t1.update();
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                     alarmButton.setText("ON");
67                     alarmStatus.setTextColor(Color.BLUE);
68                     //alarmSwitch.setChecked(false);
69                     alarmOn = false;
70                     Log.d("RAHMADI::::", "Set text to OFF and BLUE with alarm value: " + testValStatus);
71                 }
72                 else {// value 1
73                     alarmStatus.setText("ON");
74                     alarmButton.setText("OFF");
75                     alarmStatus.setTextColor(Color.RED);
76                     //alarmSwitch.setChecked(true);
77                     alarmOn = true;
78                     Log.d("RAHMADI::::", "Set text to ON and RED with alarm value: " + testValStatus);
79                 }
80                 mutex.release();
81
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());
87             }
88
89
90             // Repeat every 2 seconds
91             handler.postDelayed(runnable, 1000);
92         }
93     };
94
95
96     @Override
97     protected void onCreate(Bundle savedInstanceState) {
98
99         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
100         StrictMode.setThreadPolicy(policy);
101
102         super.onCreate(savedInstanceState);
103         setContentView(R.layout.activity_main);
104         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
105         setSupportActionBar(toolbar);
106
107         try {
108             Log.e("Ali::::", "Here1");
109             t1 = new Table(CLOUD_SERVER, PASSWORD, LOCAL_MACHINE_ID, LISTENING_PORT, MainActivity.this);
110             Log.e("Ali::::", "Here2");
111             //t1.initTable();
112             t1.rebuild(); // update
113             t1.addLocalCommunication(260, "192.168.1.192", 6000);
114             Log.e("Ali::::", "Here3");
115
116         } catch (Exception e) {
117
118             StringWriter sw = new StringWriter();
119             PrintWriter pw = new PrintWriter(sw);
120             e.printStackTrace(pw);
121             Log.e("ALI::::", sw.toString());
122
123         }
124         // TextViews
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);
130
131         //handler.post(runnable);
132     }
133
134     public void onClick(View v) {
135
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);
143
144             Log.d("RAHMADI:::::", "Button pressed!");
145
146             try {
147                 mutex.acquire();
148                 if (!alarmOn) {
149
150                     try {
151                         t1.update();
152                         t1.startTransaction();
153                         t1.addKV(iotBulb, iotStatusOn);
154                         t1.commitTransaction();
155                         alarmOn = true;
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());
162                     }
163
164                 } else {
165
166                     try {
167                         t1.update();
168                         t1.startTransaction();
169                         t1.addKV(iotBulb, iotStatusOff);
170                         t1.commitTransaction();
171                         alarmOn = false;
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());
178                     }
179                 }
180                 mutex.release();
181
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());
187             }
188         }
189     }
190 }