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, 6000, MainActivity.this);
320 t1.addLocalCommunication(353211, "192.168.2.192", 6000);
322 Log.e("Ali::::", "Here2");
323 t1.rebuild(); // update
324 Log.e("Ali::::", "Here3");
326 } catch (Exception e) {
328 StringWriter sw = new StringWriter();
329 PrintWriter pw = new PrintWriter(sw);
330 e.printStackTrace(pw);
331 Log.e("ALI::::", sw.toString());
337 textView = (TextView) findViewById(R.id.textView);
338 textView2 = (TextView) findViewById(R.id.textView4);
343 textView.setText("Error Crashed.....");
349 switchButton1 = (Switch) findViewById(R.id.switch1);
350 switchButton2 = (Switch) findViewById(R.id.switch2);
351 switchButton3 = (Switch) findViewById(R.id.switch3);
352 switchButton4 = (Switch) findViewById(R.id.switch4);
353 switchButton5 = (Switch) findViewById(R.id.switch5);
359 IoTString ia1 = new IoTString(a1);
360 IoTString ia2 = new IoTString(a2);
361 IoTString ia3 = new IoTString(a3);
365 IoTString ib1 = new IoTString(b1);
366 IoTString ib2 = new IoTString(b2);
369 String valueA = "on";
370 String valueB = "off";
371 IoTString iValueA = new IoTString(valueA);
372 IoTString iValueB = new IoTString(valueB);
375 Log.e("Ali::::", "Here");
378 switchButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
380 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
394 t1.startTransaction();
395 t1.addKV(ia1, iValueA);
396 t1.commitTransaction();
397 } catch (Exception e) {
406 t1.startTransaction();
407 t1.addKV(ia1, iValueB);
408 t1.commitTransaction();
409 } catch (Exception e) {
413 } catch (Exception e) {
423 switchButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
425 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
440 t1.startTransaction();
441 t1.addKV(ia2, iValueA);
442 t1.commitTransaction();
443 } catch (Exception e) {
451 t1.startTransaction();
452 t1.addKV(ia2, iValueB);
453 t1.commitTransaction();
454 } catch (Exception e) {
458 } catch (Exception e) {
468 switchButton3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
470 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
483 t1.startTransaction();
484 t1.addKV(ia3, iValueA);
485 t1.commitTransaction();
486 } catch (Exception e) {
494 t1.startTransaction();
495 t1.addKV(ia3, iValueB);
496 t1.commitTransaction();
497 } catch (Exception e) {
501 } catch (Exception e) {
511 switchButton4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
513 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
526 t1.startTransaction();
527 t1.addKV(ib1, iValueA);
528 t1.commitTransaction();
529 } catch (Exception e) {
537 t1.startTransaction();
538 t1.addKV(ib1, iValueB);
539 t1.commitTransaction();
540 } catch (Exception e) {
544 } catch (Exception e) {
554 switchButton5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
556 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
570 t1.startTransaction();
571 t1.addKV(ib2, iValueA);
572 t1.commitTransaction();
573 } catch (Exception e) {
581 t1.startTransaction();
582 t1.addKV(ib2, iValueB);
583 t1.commitTransaction();
584 } catch (Exception e) {
588 } catch (Exception e) {
598 // handler.post(runnable);
599 handler2.post(runnable2);
605 public boolean onCreateOptionsMenu(Menu menu) {
606 // Inflate the menu; this adds items to the action bar if it is present.
607 getMenuInflater().inflate(R.menu.menu_main, menu);
612 public boolean onOptionsItemSelected(MenuItem item) {
613 // Handle action bar item clicks here. The action bar will
614 // automatically handle clicks on the Home/Up button, so long
615 // as you specify a parent activity in AndroidManifest.xml.
616 int id = item.getItemId();
618 //noinspection SimplifiableIfStatement
619 if (id == R.id.action_settings) {
623 return super.onOptionsItemSelected(item);