1 package com.example.ali.control;
3 import android.os.Bundle;
4 import android.support.design.widget.FloatingActionButton;
5 import android.support.design.widget.Snackbar;
6 import android.support.v7.app.AppCompatActivity;
7 import android.support.v7.widget.Toolbar;
8 import android.view.View;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.os.StrictMode;
13 import android.util.Log;
14 import android.os.Bundle;
15 import android.support.v7.app.AppCompatActivity;
16 import android.widget.CompoundButton;
17 import android.widget.Switch;
18 import android.widget.TextView;
22 import java.util.concurrent.*;
23 import android.os.Handler;
24 import android.content.*;
26 public class MainActivity extends AppCompatActivity {
28 Switch switchButton1, switchButton2, switchButton3, switchButton4, switchButton5;
32 String switchOn = "Switch is ON";
33 String switchOff = "Switch is OFF";
38 Semaphore mutex = new Semaphore(1);
40 boolean didCrash = false;
42 private Handler handler = new Handler();
43 private Handler handler2 = new Handler();
46 private Runnable runnable = new Runnable() {
57 String pingTimerKey = "bulbController";
58 IoTString ipingTimerKey = new IoTString(pingTimerKey);
60 String pingTimerKey2 = "wemoController";
61 IoTString ipingTimerKey2 = new IoTString(pingTimerKey2);
63 // Insert custom code here
65 Log.e("Ali:::::", "loop............");
68 IoTString testValA1 = t1.getCommitted(ipingTimerKey);
69 IoTString testValA2 = t1.getCommitted(ipingTimerKey2);
75 String s1 = testValA1.toString();
76 l1 = Long.parseLong(s1);
82 String s2 = testValA2.toString();
83 l2 = Long.parseLong(s2);
86 Log.e("Ali:::::", "S: " + System.currentTimeMillis() + " " + l1);
88 if((System.currentTimeMillis() - l1) > 10000)
90 textView.setText("Partition" + (System.currentTimeMillis() - l1) );
92 else if((System.currentTimeMillis() - l2) > 10000)
94 textView.setText("Partition" + (System.currentTimeMillis() - l2) );
98 textView.setText("All Good");
100 } catch (Exception e) {
105 // Repeat every 2 seconds
106 // handler.postDelayed(runnable, 1000);
113 private Runnable runnable2 = new Runnable() {
123 String pingTimerKey = "bulbController";
124 IoTString ipingTimerKey = new IoTString(pingTimerKey);
126 String pingTimerKey2 = "wemoController";
127 IoTString ipingTimerKey2 = new IoTString(pingTimerKey2);
129 String pingTimerKey3 = "sensorController";
130 IoTString ipingTimerKey3 = new IoTString(pingTimerKey3);
133 String sen1 = "sensor";
134 IoTString is1 = new IoTString(sen1);
137 // Insert custom code here
139 // Log.e("Ali:::::", "loop............");
142 IoTString testValA1 = t1.getCommitted(ipingTimerKey);
143 IoTString testValA2 = t1.getCommitted(ipingTimerKey2);
144 IoTString testValA3 = t1.getCommitted(ipingTimerKey3);
146 IoTString testValA4 = t1.getCommitted(is1);
151 if(testValA4 == null) {
152 textView2.setText("No Values");
156 textView2.setText(testValA4.toString());
161 if(testValA1 != null)
163 String s1 = testValA1.toString();
164 l1 = Long.parseLong(s1);
168 if(testValA2 != null)
170 String s2 = testValA2.toString();
171 l2 = Long.parseLong(s2);
175 if(testValA3 != null)
177 String s3 = testValA3.toString();
178 l3 = Long.parseLong(s3);
183 if((System.currentTimeMillis() - l1) > 10000)
185 textView.setText("Partition 1: " + (System.currentTimeMillis() - l1) );
187 else if((System.currentTimeMillis() - l2) > 10000)
189 textView.setText("Partition 2: " + (System.currentTimeMillis() - l2) );
191 else if((System.currentTimeMillis() - l3) > 10000)
193 textView.setText("Partition 3: " + (System.currentTimeMillis() - l3) );
197 textView.setText("All Good");
203 } catch (Exception e) {
209 Log.e("Ali::::", "Control Loop: " + count);
218 IoTString ia1 = new IoTString(a1);
219 IoTString ia2 = new IoTString(a2);
220 IoTString ia3 = new IoTString(a3);
224 IoTString ib1 = new IoTString(b1);
225 IoTString ib2 = new IoTString(b2);
228 String valueA = "on";
229 String valueB = "off";
230 IoTString iValueA = new IoTString(valueA);
231 IoTString iValueB = new IoTString(valueB);
235 // Insert custom code here
242 t1.startTransaction();
243 t1.addKV(ia1, iValueA);
244 t1.addKV(ia2, iValueB);
245 t1.addKV(ia3, iValueB);
246 t1.commitTransaction();
253 t1.startTransaction();
254 t1.addKV(ia1, iValueB);
255 t1.addKV(ia2, iValueA);
256 t1.addKV(ia3, iValueB);
257 t1.commitTransaction();
264 t1.startTransaction();
265 t1.addKV(ia1, iValueB);
266 t1.addKV(ia2, iValueB);
267 t1.addKV(ia3, iValueA);
268 t1.commitTransaction();
273 } catch (Exception e) {
279 handler2.postDelayed(runnable2, 1000);
290 protected void onCreate(Bundle savedInstanceState) {
292 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
293 StrictMode.setThreadPolicy(policy);
297 super.onCreate(savedInstanceState);
298 setContentView(R.layout.activity_main);
299 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
300 setSupportActionBar(toolbar);
302 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
303 fab.setOnClickListener(new View.OnClickListener() {
305 public void onClick(View view) {
306 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
307 .setAction("Action", null).show();
318 Log.e("Ali::::", "Here1");
319 t1 = new Table("http://dc-6.calit2.uci.edu/test.iotcloud/", "reallysecret", 1000, -1, MainActivity.this);
320 Log.e("Ali::::", "Here2");
321 t1.rebuild(); // update
322 Log.e("Ali::::", "Here3");
324 } catch (Exception e) {
326 StringWriter sw = new StringWriter();
327 PrintWriter pw = new PrintWriter(sw);
328 e.printStackTrace(pw);
329 Log.e("ALI::::", sw.toString());
335 textView = (TextView) findViewById(R.id.textView);
336 textView2 = (TextView) findViewById(R.id.textView4);
341 textView.setText("Error Crashed.....");
347 switchButton1 = (Switch) findViewById(R.id.switch1);
348 switchButton2 = (Switch) findViewById(R.id.switch2);
349 switchButton3 = (Switch) findViewById(R.id.switch3);
350 switchButton4 = (Switch) findViewById(R.id.switch4);
351 switchButton5 = (Switch) findViewById(R.id.switch5);
357 IoTString ia1 = new IoTString(a1);
358 IoTString ia2 = new IoTString(a2);
359 IoTString ia3 = new IoTString(a3);
363 IoTString ib1 = new IoTString(b1);
364 IoTString ib2 = new IoTString(b2);
367 String valueA = "on";
368 String valueB = "off";
369 IoTString iValueA = new IoTString(valueA);
370 IoTString iValueB = new IoTString(valueB);
373 Log.e("Ali::::", "Here");
376 switchButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
378 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
392 t1.startTransaction();
393 t1.addKV(ia1, iValueA);
394 t1.commitTransaction();
395 } catch (Exception e) {
404 t1.startTransaction();
405 t1.addKV(ia1, iValueB);
406 t1.commitTransaction();
407 } catch (Exception e) {
411 } catch (Exception e) {
421 switchButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
423 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
438 t1.startTransaction();
439 t1.addKV(ia2, iValueA);
440 t1.commitTransaction();
441 } catch (Exception e) {
449 t1.startTransaction();
450 t1.addKV(ia2, iValueB);
451 t1.commitTransaction();
452 } catch (Exception e) {
456 } catch (Exception e) {
466 switchButton3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
468 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
481 t1.startTransaction();
482 t1.addKV(ia3, iValueA);
483 t1.commitTransaction();
484 } catch (Exception e) {
492 t1.startTransaction();
493 t1.addKV(ia3, iValueB);
494 t1.commitTransaction();
495 } catch (Exception e) {
499 } catch (Exception e) {
509 switchButton4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
511 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
524 t1.startTransaction();
525 t1.addKV(ib1, iValueA);
526 t1.commitTransaction();
527 } catch (Exception e) {
535 t1.startTransaction();
536 t1.addKV(ib1, iValueB);
537 t1.commitTransaction();
538 } catch (Exception e) {
542 } catch (Exception e) {
552 switchButton5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
554 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
568 t1.startTransaction();
569 t1.addKV(ib2, iValueA);
570 t1.commitTransaction();
571 } catch (Exception e) {
579 t1.startTransaction();
580 t1.addKV(ib2, iValueB);
581 t1.commitTransaction();
582 } catch (Exception e) {
586 } catch (Exception e) {
596 // handler.post(runnable);
597 handler2.post(runnable2);
603 public boolean onCreateOptionsMenu(Menu menu) {
604 // Inflate the menu; this adds items to the action bar if it is present.
605 getMenuInflater().inflate(R.menu.menu_main, menu);
610 public boolean onOptionsItemSelected(MenuItem item) {
611 // Handle action bar item clicks here. The action bar will
612 // automatically handle clicks on the Home/Up button, so long
613 // as you specify a parent activity in AndroidManifest.xml.
614 int id = item.getItemId();
616 //noinspection SimplifiableIfStatement
617 if (id == R.id.action_settings) {
621 return super.onOptionsItemSelected(item);