Projektarbeit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainActivity.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package de.edotzlaff.schockwelle;
  2. import androidx.annotation.NonNull;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.fragment.app.FragmentActivity;
  5. import android.app.Dialog;
  6. import android.content.Intent;
  7. import android.hardware.Sensor;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.Toast;
  13. import com.google.android.gms.common.ConnectionResult;
  14. import com.google.android.gms.common.GoogleApiAvailability;
  15. import com.google.firebase.database.DataSnapshot;
  16. import com.google.firebase.database.DatabaseError;
  17. import com.google.firebase.database.DatabaseReference;
  18. import com.google.firebase.database.FirebaseDatabase;
  19. import com.google.firebase.database.ValueEventListener;
  20. public class MainActivity extends FragmentActivity {
  21. private static final String TAG = "MainActivity";
  22. private static final int ERROR_DIALOG_REQUEST = 9001;
  23. private DatabaseReference mDatenbank;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. if (isServiceOK()){
  29. init();
  30. }
  31. deviceInitDataBase(); //TODO Edward: Mit dieser Methode wird beim Starten der App die DB mit, vorerst festen, Werten für alle IDG 1-4 initialiert
  32. }
  33. private void init(){
  34. Button btnEarthquake = (Button) findViewById(R.id.btnEarthquake);
  35. btnEarthquake.setOnClickListener(new View.OnClickListener() {
  36. @Override
  37. public void onClick(View v) {
  38. Intent intent = new Intent(MainActivity.this, EarthquakeMapsActivity.class);
  39. startActivity(intent);
  40. }
  41. });
  42. Button btnSensor = (Button) findViewById(R.id.btnSensor);
  43. btnSensor.setOnClickListener(new View.OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. Intent intent = new Intent(MainActivity.this, SensorMapsActivity.class);
  47. startActivity(intent);
  48. }
  49. });
  50. }
  51. public boolean isServiceOK(){
  52. Log.d(TAG, "isServicesOK(): checking google services version");
  53. int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);
  54. if (available== ConnectionResult.SUCCESS){
  55. Log.d(TAG,"isServicesOK: Google Play Services is working");
  56. return true;
  57. }
  58. else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)){
  59. Log.d(TAG, "isServicesOK(): an error occured but we can fix it");
  60. Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, available, ERROR_DIALOG_REQUEST);
  61. dialog.show();
  62. }
  63. else {
  64. Toast.makeText(this, "You can`t make map request", Toast.LENGTH_SHORT).show();
  65. }
  66. return false;
  67. }
  68. public void deviceInitDataBase()
  69. {
  70. mDatenbank = FirebaseDatabase.getInstance().getReference();
  71. Devices d1 = new Devices("10.00.00.000", false, 1103456789, 49.45470362290784, 11.151032510281498, 1000);
  72. Devices d2 = new Devices("20.00.00.000", false, 1203456789, 40.70064147511923, -73.993767997977787, 200);
  73. Devices d3 = new Devices("30.00.00.000", false, 1303456789, -33.93157129193122, 18.39666975233824, 30);
  74. Devices d4 = new Devices("40.00.00.000", false, 1403456789, -33.40763257191139, -70.61870273653996, 4);
  75. mDatenbank.child("overview").child("IDG1").setValue(d1);
  76. mDatenbank.child("overview").child("IDG2").setValue(d2);
  77. mDatenbank.child("overview").child("IDG3").setValue(d3);
  78. mDatenbank.child("overview").child("IDG4").setValue(d4);
  79. getDataBaseValues();
  80. }
  81. public void getDataBaseValues()
  82. {
  83. mDatenbank.addValueEventListener(new ValueEventListener() {
  84. @Override
  85. public void onDataChange(@NonNull DataSnapshot snapshot) {
  86. processDataBaseValues(snapshot); //Daten - Snapshot, Übergabe an Methode DataChangeProtocol
  87. }
  88. @Override
  89. public void onCancelled(@NonNull DatabaseError error) {
  90. getDataBaseFailure(error);
  91. }
  92. });
  93. }
  94. public void processDataBaseValues (DataSnapshot data)
  95. {
  96. for (int i = 1; i<=4; i++) //Für alle IDG 1-4 werden Werte ausgegeben
  97. {
  98. //####### Auslesen für String-Werte #######:
  99. String ipAdresse = data.child("overview").child("IDG" + i).child("ip").getValue().toString();
  100. //####### Auslesen für boolean-Werte #######:
  101. String vibrationString = data.child("overview").child("IDG" + i).child("vibration").getValue().toString();
  102. boolean vibration;
  103. if(vibrationString == "true"){
  104. vibration = true;
  105. }else{
  106. vibration = false;
  107. }
  108. //####### Auslesen für double-Werte #######:
  109. String breitengradString = data.child("overview").child("IDG" + i).child("breitengrad").getValue().toString();
  110. String laengengradString = data.child("overview").child("IDG" + i).child("laengengrad").getValue().toString();
  111. double breitengrad = Double.parseDouble(breitengradString);
  112. double laengengrad = Double.parseDouble(laengengradString);
  113. //####### Auslesen für long-Werte #######:
  114. String timestampString = data.child("overview").child("IDG" + i).child("timestamp").getValue().toString();
  115. long timestamp = Long.parseLong(timestampString);
  116. //####### Auslesen für ing-Werte #######:
  117. String amplitudeString = data.child("overview").child("IDG" + i).child("amplitude").getValue().toString();
  118. int amplitude = Integer.parseInt(amplitudeString);
  119. //####### Optional zur Kontrolle #######:
  120. /*
  121. System.out.println("IDG" + i + " - IP:" + data.child("overview").child("IDG" + i).child("ip").getValue().toString());
  122. System.out.println("IDG" + i + " - Vibration:" + data.child("overview").child("IDG" + i).child("vibration").getValue().toString());
  123. System.out.println("IDG" + i + " - Zeitstempel:" + data.child("overview").child("IDG" + i).child("timestamp").getValue().toString());
  124. System.out.println("IDG" + i + " - Breitengrad:" + data.child("overview").child("IDG" + i).child("breitengrad").getValue().toString());
  125. System.out.println("IDG" + i + " - Laengengrad:" + data.child("overview").child("IDG" + i).child("laengengrad").getValue().toString());
  126. System.out.println("IDG" + i + " - Amplitude:" + data.child("overview").child("IDG" + i).child("amplitude").getValue().toString());
  127. */
  128. }
  129. }
  130. public void getDataBaseFailure (DatabaseError error)
  131. {
  132. System.out.println("Fehler");
  133. Log.w("Datenbankfehler", error.toException());
  134. }
  135. }