Added Android App
[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, -1, MainActivity.this);
320                 Log.e("Ali::::", "Here2");
321                 t1.rebuild(); // update
322                 Log.e("Ali::::", "Here3");
323
324             } catch (Exception e) {
325
326                 StringWriter sw = new StringWriter();
327                 PrintWriter pw = new PrintWriter(sw);
328                 e.printStackTrace(pw);
329                 Log.e("ALI::::", sw.toString());
330
331         }
332
333
334         // Text Box
335         textView = (TextView) findViewById(R.id.textView);
336         textView2 = (TextView) findViewById(R.id.textView4);
337
338
339         if(didCrash)
340         {
341             textView.setText("Error Crashed.....");
342         }
343         else {
344
345
346             // Switches
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);
352
353
354             String a1 = "bulb1";
355             String a2 = "bulb2";
356             String a3 = "bulb3";
357             IoTString ia1 = new IoTString(a1);
358             IoTString ia2 = new IoTString(a2);
359             IoTString ia3 = new IoTString(a3);
360
361             String b1 = "wemo1";
362             String b2 = "wemo2";
363             IoTString ib1 = new IoTString(b1);
364             IoTString ib2 = new IoTString(b2);
365
366
367             String valueA = "on";
368             String valueB = "off";
369             IoTString iValueA = new IoTString(valueA);
370             IoTString iValueB = new IoTString(valueB);
371
372
373             Log.e("Ali::::", "Here");
374
375
376             switchButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
377                 @Override
378                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
379
380                     if(didCrash)
381                     {
382                         return;
383                     }
384
385                     try {
386                         mutex.acquire();
387                         if (bChecked) {
388
389
390                             try {
391                                 t1.update();
392                                 t1.startTransaction();
393                                 t1.addKV(ia1, iValueA);
394                                 t1.commitTransaction();
395                             } catch (Exception e) {
396
397                             }
398
399                         } else {
400
401
402                             try {
403                                 t1.update();
404                                 t1.startTransaction();
405                                 t1.addKV(ia1, iValueB);
406                                 t1.commitTransaction();
407                             } catch (Exception e) {
408
409                             }
410                         }
411                     } catch (Exception e) {
412
413                     }
414
415                     finally {
416                         mutex.release();
417                     }
418                 }
419             });
420
421             switchButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
422                 @Override
423                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
424
425                     if(didCrash)
426                     {
427                         return;
428                     }
429
430
431
432                     try {
433                         mutex.acquire();
434                         if (bChecked) {
435
436                             try {
437                                 t1.update();
438                                 t1.startTransaction();
439                                 t1.addKV(ia2, iValueA);
440                                 t1.commitTransaction();
441                             } catch (Exception e) {
442
443                             }
444
445                         } else {
446
447                             try {
448                                 t1.update();
449                                 t1.startTransaction();
450                                 t1.addKV(ia2, iValueB);
451                                 t1.commitTransaction();
452                             } catch (Exception e) {
453
454                             }
455                         }
456                     } catch (Exception e) {
457
458                     }
459
460                     finally {
461                         mutex.release();
462                     }
463                 }
464             });
465
466             switchButton3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
467                 @Override
468                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
469
470                     if(didCrash)
471                     {
472                         return;
473                     }
474
475                     try {
476                         mutex.acquire();
477                         if (bChecked) {
478
479                             try {
480                                 t1.update();
481                                 t1.startTransaction();
482                                 t1.addKV(ia3, iValueA);
483                                 t1.commitTransaction();
484                             } catch (Exception e) {
485
486                             }
487
488                         } else {
489
490                             try {
491                                 t1.update();
492                                 t1.startTransaction();
493                                 t1.addKV(ia3, iValueB);
494                                 t1.commitTransaction();
495                             } catch (Exception e) {
496
497                             }
498                         }
499                     } catch (Exception e) {
500
501                     }
502
503                     finally {
504                         mutex.release();
505                     }
506                 }
507             });
508
509             switchButton4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
510                 @Override
511                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
512
513                     if(didCrash)
514                     {
515                         return;
516                     }
517
518                     try {
519                         mutex.acquire();
520                         if (bChecked) {
521
522                             try {
523                                 t1.update();
524                                 t1.startTransaction();
525                                 t1.addKV(ib1, iValueA);
526                                 t1.commitTransaction();
527                             } catch (Exception e) {
528
529                             }
530
531                         } else {
532
533                             try {
534                                 t1.update();
535                                 t1.startTransaction();
536                                 t1.addKV(ib1, iValueB);
537                                 t1.commitTransaction();
538                             } catch (Exception e) {
539
540                             }
541                         }
542                     } catch (Exception e) {
543
544                     }
545
546                     finally {
547                         mutex.release();
548                     }
549                 }
550             });
551
552             switchButton5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
553                 @Override
554                 public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
555
556                     if(didCrash)
557                     {
558                         return;
559                     }
560
561                     try {
562                         mutex.acquire();
563
564                         if (bChecked) {
565
566                             try {
567                                 t1.update();
568                                 t1.startTransaction();
569                                 t1.addKV(ib2, iValueA);
570                                 t1.commitTransaction();
571                             } catch (Exception e) {
572
573                             }
574
575                         } else {
576
577                             try {
578                                 t1.update();
579                                 t1.startTransaction();
580                                 t1.addKV(ib2, iValueB);
581                                 t1.commitTransaction();
582                             } catch (Exception e) {
583
584                             }
585                         }
586                     } catch (Exception e) {
587
588                     }
589
590                     finally {
591                         mutex.release();
592                     }
593                 }
594             });
595
596 //            handler.post(runnable);
597             handler2.post(runnable2);
598         }
599
600     }
601
602     @Override
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);
606         return true;
607     }
608
609     @Override
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();
615
616         //noinspection SimplifiableIfStatement
617         if (id == R.id.action_settings) {
618             return true;
619         }
620
621         return super.onOptionsItemSelected(item);
622     }
623
624
625 }