Changes
[iotcloud.git] / version2 / src / Control / app / src / main / java / com / example / ali / control / MainActivity.java
1 package com.example.ali.control;
2
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;
12
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;
19 import iotcloud.*;
20 import java.io.*;
21 import java.net.*;
22 import java.util.concurrent.*;
23 import android.os.Handler;
24 import android.content.*;
25
26 public class MainActivity extends AppCompatActivity {
27
28     Switch switchButton1, switchButton2, switchButton3, switchButton4, switchButton5;
29     TextView textView;
30     TextView textView2;
31
32     String switchOn = "Switch is ON";
33     String switchOff = "Switch is OFF";
34
35
36     Table t1 = null;
37     Thread thread = null;
38     Semaphore mutex = new Semaphore(1);
39
40     boolean didCrash = false;
41
42     private Handler handler = new Handler();
43     private Handler handler2 = new Handler();
44
45
46     private Runnable runnable = new Runnable() {
47         @Override
48         public void run() {
49
50             if(didCrash)
51             {
52                 return;
53             }
54
55
56
57             String pingTimerKey = "bulbController";
58             IoTString ipingTimerKey = new IoTString(pingTimerKey);
59
60             String pingTimerKey2 = "wemoController";
61             IoTString ipingTimerKey2 = new IoTString(pingTimerKey2);
62
63             // Insert custom code here
64             try {
65                 Log.e("Ali:::::", "loop............");
66                 mutex.acquire();
67                 t1.update();
68                 IoTString testValA1 = t1.getCommitted(ipingTimerKey);
69                 IoTString testValA2 = t1.getCommitted(ipingTimerKey2);
70                 mutex.release();
71
72                 long l1 = 0;
73                 if(testValA1 != null)
74                 {
75                     String s1 = testValA1.toString();
76                     l1 = Long.parseLong(s1);
77                 }
78
79                 long l2 = 0;
80                 if(testValA2 != null)
81                 {
82                     String s2 = testValA2.toString();
83                     l2 = Long.parseLong(s2);
84                 }
85
86                 Log.e("Ali:::::", "S: " + System.currentTimeMillis() + "   " + l1);
87
88                 if((System.currentTimeMillis() - l1) > 10000)
89                 {
90                     textView.setText("Partition" + (System.currentTimeMillis() - l1) );
91                 }
92                 else if((System.currentTimeMillis() - l2) > 10000)
93                 {
94                     textView.setText("Partition" + (System.currentTimeMillis() - l2) );
95                 }
96                 else
97                 {
98                     textView.setText("All Good");
99                 }
100             } catch (Exception e) {
101
102             }
103
104
105             // Repeat every 2 seconds
106 //            handler.postDelayed(runnable, 1000);
107         }
108     };
109
110
111     int count = 0;
112
113     private Runnable runnable2 = new Runnable() {
114         @Override
115         public void run() {
116
117             if(didCrash)
118             {
119                 return;
120             }
121
122
123             String pingTimerKey = "bulbController";
124             IoTString ipingTimerKey = new IoTString(pingTimerKey);
125
126             String pingTimerKey2 = "wemoController";
127             IoTString ipingTimerKey2 = new IoTString(pingTimerKey2);
128
129             String pingTimerKey3 = "sensorController";
130             IoTString ipingTimerKey3 = new IoTString(pingTimerKey3);
131
132
133             String sen1 = "sensor";
134             IoTString is1 = new IoTString(sen1);
135
136
137             // Insert custom code here
138             try {
139 //                Log.e("Ali:::::", "loop............");
140                 mutex.acquire();
141                 t1.update();
142                 IoTString testValA1 = t1.getCommitted(ipingTimerKey);
143                 IoTString testValA2 = t1.getCommitted(ipingTimerKey2);
144                 IoTString testValA3 = t1.getCommitted(ipingTimerKey3);
145
146                 IoTString testValA4 = t1.getCommitted(is1);
147
148                 mutex.release();
149
150
151                 if(testValA4 == null) {
152                     textView2.setText("No Values");
153                 }
154                 else
155                 {
156                     textView2.setText(testValA4.toString());
157                 }
158
159
160                 long l1 = 0;
161                 if(testValA1 != null)
162                 {
163                     String s1 = testValA1.toString();
164                     l1 = Long.parseLong(s1);
165                 }
166
167                 long l2 = 0;
168                 if(testValA2 != null)
169                 {
170                     String s2 = testValA2.toString();
171                     l2 = Long.parseLong(s2);
172                 }
173
174                 long l3 = 0;
175                 if(testValA3 != null)
176                 {
177                     String s3 = testValA3.toString();
178                     l3 = Long.parseLong(s3);
179                 }
180
181
182
183                 if((System.currentTimeMillis() - l1) > 10000)
184                 {
185                     textView.setText("Partition 1: " + (System.currentTimeMillis() - l1) );
186                 }
187                 else if((System.currentTimeMillis() - l2) > 10000)
188                 {
189                     textView.setText("Partition 2: " + (System.currentTimeMillis() - l2) );
190                 }
191                 else if((System.currentTimeMillis() - l3) > 10000)
192                 {
193                     textView.setText("Partition 3: " + (System.currentTimeMillis() - l3) );
194                 }
195                 else
196                 {
197                     textView.setText("All Good");
198                 }
199
200
201
202
203             } catch (Exception e) {
204
205             }
206
207
208
209             Log.e("Ali::::", "Control Loop: " + count);
210
211
212             count++;
213             count%=3;
214
215             String a1 = "bulb1";
216             String a2 = "bulb2";
217             String a3 = "bulb3";
218             IoTString ia1 = new IoTString(a1);
219             IoTString ia2 = new IoTString(a2);
220             IoTString ia3 = new IoTString(a3);
221
222             String b1 = "wemo1";
223             String b2 = "wemo2";
224             IoTString ib1 = new IoTString(b1);
225             IoTString ib2 = new IoTString(b2);
226
227
228             String valueA = "on";
229             String valueB = "off";
230             IoTString iValueA = new IoTString(valueA);
231             IoTString iValueB = new IoTString(valueB);
232
233
234
235             // Insert custom code here
236             try {
237
238                 if(count == 0)
239                 {
240                     mutex.acquire();
241                     t1.update();
242                     t1.startTransaction();
243                     t1.addKV(ia1, iValueA);
244                     t1.addKV(ia2, iValueB);
245                     t1.addKV(ia3, iValueB);
246                     t1.commitTransaction();
247                     mutex.release();
248                 }
249                 else if(count == 1)
250                 {
251                     mutex.acquire();
252                     t1.update();
253                     t1.startTransaction();
254                     t1.addKV(ia1, iValueB);
255                     t1.addKV(ia2, iValueA);
256                     t1.addKV(ia3, iValueB);
257                     t1.commitTransaction();
258                     mutex.release();
259                 }
260                 else if(count == 2)
261                 {
262                     mutex.acquire();
263                     t1.update();
264                     t1.startTransaction();
265                     t1.addKV(ia1, iValueB);
266                     t1.addKV(ia2, iValueB);
267                     t1.addKV(ia3, iValueA);
268                     t1.commitTransaction();
269                     mutex.release();
270                 }
271
272
273             } catch (Exception e) {
274
275             }
276
277
278
279             handler2.postDelayed(runnable2, 1000);
280         }
281     };
282
283
284
285
286
287
288
289     @Override
290     protected void onCreate(Bundle savedInstanceState) {
291
292         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
293         StrictMode.setThreadPolicy(policy);
294
295
296
297         super.onCreate(savedInstanceState);
298         setContentView(R.layout.activity_main);
299         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
300         setSupportActionBar(toolbar);
301
302         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
303         fab.setOnClickListener(new View.OnClickListener() {
304             @Override
305             public void onClick(View view) {
306                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
307                         .setAction("Action", null).show();
308             }
309         });
310
311
312
313
314
315
316
317             try {
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);
321
322                 Log.e("Ali::::", "Here2");
323                 t1.rebuild(); // update
324                 Log.e("Ali::::", "Here3");
325
326             } catch (Exception e) {
327
328                 StringWriter sw = new StringWriter();
329                 PrintWriter pw = new PrintWriter(sw);
330                 e.printStackTrace(pw);
331                 Log.e("ALI::::", sw.toString());
332
333         }
334
335
336         // Text Box
337         textView = (TextView) findViewById(R.id.textView);
338         textView2 = (TextView) findViewById(R.id.textView4);
339
340
341         if(didCrash)
342         {
343             textView.setText("Error Crashed.....");
344         }
345         else {
346
347
348             // Switches
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);
354
355
356             String a1 = "bulb1";
357             String a2 = "bulb2";
358             String a3 = "bulb3";
359             IoTString ia1 = new IoTString(a1);
360             IoTString ia2 = new IoTString(a2);
361             IoTString ia3 = new IoTString(a3);
362
363             String b1 = "wemo1";
364             String b2 = "wemo2";
365             IoTString ib1 = new IoTString(b1);
366             IoTString ib2 = new IoTString(b2);
367
368
369             String valueA = "on";
370             String valueB = "off";
371             IoTString iValueA = new IoTString(valueA);
372             IoTString iValueB = new IoTString(valueB);
373
374
375             Log.e("Ali::::", "Here");
376
377
378             switchButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
379                 @Override
380                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
381
382                     if(didCrash)
383                     {
384                         return;
385                     }
386
387                     try {
388                         mutex.acquire();
389                         if (bChecked) {
390
391
392                             try {
393                                 t1.update();
394                                 t1.startTransaction();
395                                 t1.addKV(ia1, iValueA);
396                                 t1.commitTransaction();
397                             } catch (Exception e) {
398
399                             }
400
401                         } else {
402
403
404                             try {
405                                 t1.update();
406                                 t1.startTransaction();
407                                 t1.addKV(ia1, iValueB);
408                                 t1.commitTransaction();
409                             } catch (Exception e) {
410
411                             }
412                         }
413                     } catch (Exception e) {
414
415                     }
416
417                     finally {
418                         mutex.release();
419                     }
420                 }
421             });
422
423             switchButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
424                 @Override
425                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
426
427                     if(didCrash)
428                     {
429                         return;
430                     }
431
432
433
434                     try {
435                         mutex.acquire();
436                         if (bChecked) {
437
438                             try {
439                                 t1.update();
440                                 t1.startTransaction();
441                                 t1.addKV(ia2, iValueA);
442                                 t1.commitTransaction();
443                             } catch (Exception e) {
444
445                             }
446
447                         } else {
448
449                             try {
450                                 t1.update();
451                                 t1.startTransaction();
452                                 t1.addKV(ia2, iValueB);
453                                 t1.commitTransaction();
454                             } catch (Exception e) {
455
456                             }
457                         }
458                     } catch (Exception e) {
459
460                     }
461
462                     finally {
463                         mutex.release();
464                     }
465                 }
466             });
467
468             switchButton3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
469                 @Override
470                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
471
472                     if(didCrash)
473                     {
474                         return;
475                     }
476
477                     try {
478                         mutex.acquire();
479                         if (bChecked) {
480
481                             try {
482                                 t1.update();
483                                 t1.startTransaction();
484                                 t1.addKV(ia3, iValueA);
485                                 t1.commitTransaction();
486                             } catch (Exception e) {
487
488                             }
489
490                         } else {
491
492                             try {
493                                 t1.update();
494                                 t1.startTransaction();
495                                 t1.addKV(ia3, iValueB);
496                                 t1.commitTransaction();
497                             } catch (Exception e) {
498
499                             }
500                         }
501                     } catch (Exception e) {
502
503                     }
504
505                     finally {
506                         mutex.release();
507                     }
508                 }
509             });
510
511             switchButton4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
512                 @Override
513                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
514
515                     if(didCrash)
516                     {
517                         return;
518                     }
519
520                     try {
521                         mutex.acquire();
522                         if (bChecked) {
523
524                             try {
525                                 t1.update();
526                                 t1.startTransaction();
527                                 t1.addKV(ib1, iValueA);
528                                 t1.commitTransaction();
529                             } catch (Exception e) {
530
531                             }
532
533                         } else {
534
535                             try {
536                                 t1.update();
537                                 t1.startTransaction();
538                                 t1.addKV(ib1, iValueB);
539                                 t1.commitTransaction();
540                             } catch (Exception e) {
541
542                             }
543                         }
544                     } catch (Exception e) {
545
546                     }
547
548                     finally {
549                         mutex.release();
550                     }
551                 }
552             });
553
554             switchButton5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
555                 @Override
556                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
557
558                     if(didCrash)
559                     {
560                         return;
561                     }
562
563                     try {
564                         mutex.acquire();
565
566                         if (bChecked) {
567
568                             try {
569                                 t1.update();
570                                 t1.startTransaction();
571                                 t1.addKV(ib2, iValueA);
572                                 t1.commitTransaction();
573                             } catch (Exception e) {
574
575                             }
576
577                         } else {
578
579                             try {
580                                 t1.update();
581                                 t1.startTransaction();
582                                 t1.addKV(ib2, iValueB);
583                                 t1.commitTransaction();
584                             } catch (Exception e) {
585
586                             }
587                         }
588                     } catch (Exception e) {
589
590                     }
591
592                     finally {
593                         mutex.release();
594                     }
595                 }
596             });
597
598 //            handler.post(runnable);
599             handler2.post(runnable2);
600         }
601
602     }
603
604     @Override
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);
608         return true;
609     }
610
611     @Override
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();
617
618         //noinspection SimplifiableIfStatement
619         if (id == R.id.action_settings) {
620             return true;
621         }
622
623         return super.onOptionsItemSelected(item);
624     }
625
626
627 }