Commit 6e7b5305 authored by Ishankha K.C's avatar Ishankha K.C

baby wet checker

parent 92ce955b
package com.kaluwa.enterprises.babycare.activities; package com.kaluwa.enterprises.babycare.activities;
import static com.kaluwa.enterprises.babycare.config.TokenSaver.clearToken; import static com.kaluwa.enterprises.babycare.config.TokenSaver.clearToken;
import static com.kaluwa.enterprises.babycare.config.TokenSaver.getToken;
import static com.kaluwa.enterprises.babycare.constants.LogTypes.C_WET;
import static com.kaluwa.enterprises.babycare.utils.Utils.animationChanger; import static com.kaluwa.enterprises.babycare.utils.Utils.animationChanger;
import android.content.Intent; import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
...@@ -17,12 +23,35 @@ import androidx.appcompat.app.AppCompatActivity; ...@@ -17,12 +23,35 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.kaluwa.enterprises.babycare.MainActivity; import com.kaluwa.enterprises.babycare.MainActivity;
import com.kaluwa.enterprises.babycare.R; import com.kaluwa.enterprises.babycare.R;
import com.kaluwa.enterprises.babycare.config.ApiConfig;
import com.kaluwa.enterprises.babycare.dto.ActivityLogDto;
import com.kaluwa.enterprises.babycare.dto.BabyDto;
import com.kaluwa.enterprises.babycare.dto.responseDto.AuthenticationDto;
import com.kaluwa.enterprises.babycare.error.ErrorDto;
import com.kaluwa.enterprises.babycare.service.ActivityLogsApiService;
import com.kaluwa.enterprises.babycare.service.BabyApiService;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class DashboardActivity extends AppCompatActivity { public class DashboardActivity extends AppCompatActivity {
LinearLayout btn1, btn2, btn3, btn4, btn5, btn6; private static final String TAG = "DashboardActivity";
private static final long CHECK_INTERVAL_MS = 5000; // 5 seconds interval
private LinearLayout btn1, btn2, btn3, btn4, btn5, btn6;
private AuthenticationDto authDto;
private BabyApiService babyApiService;
private Handler handler = new Handler(Looper.getMainLooper());
private MediaPlayer warningSoundPlayer;
private boolean isWarningPlaying = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -32,6 +61,15 @@ public class DashboardActivity extends AppCompatActivity { ...@@ -32,6 +61,15 @@ public class DashboardActivity extends AppCompatActivity {
// define actionbar // define actionbar
defineActionbar(); defineActionbar();
// initialize user api service
try {
authDto = getToken(getApplicationContext());
} catch (JsonProcessingException e) {
Log.e(TAG, "Error: "+e.getMessage());
Toast.makeText(this, "Error getting token, Please refresh", Toast.LENGTH_SHORT).show();
}
babyApiService = ApiConfig.getInstance().getBabyApi(authDto.getTokenDto().getToken());
btn1 = findViewById(R.id.btn_camera); btn1 = findViewById(R.id.btn_camera);
btn2 = findViewById(R.id.btn_vocal); btn2 = findViewById(R.id.btn_vocal);
btn3 = findViewById(R.id.btn_notifications); btn3 = findViewById(R.id.btn_notifications);
...@@ -69,6 +107,89 @@ public class DashboardActivity extends AppCompatActivity { ...@@ -69,6 +107,89 @@ public class DashboardActivity extends AppCompatActivity {
startActivity(intent); startActivity(intent);
animationChanger(this); animationChanger(this);
}); });
// Start periodic check for wet status
startCheckingWetStatus();
}
private void startCheckingWetStatus() {
handler.post(checkWetStatusRunnable);
}
private final Runnable checkWetStatusRunnable = new Runnable() {
@Override
public void run() {
// Call the API to get the list of babies and check wet status
babyApiService.getAllBabiesByUserId(authDto.getUserId()).enqueue(new Callback<List<BabyDto>>() {
@Override
public void onResponse(Call<List<BabyDto>> call, Response<List<BabyDto>> response) {
if (response.isSuccessful() && response.body() != null) {
List<BabyDto> babies = response.body();
boolean anyBabyWet = false;
for (BabyDto baby : babies) {
if (baby.isWet() && !baby.getDeviceUid().isEmpty()) {
Log.i(TAG, "Baby " + baby.getFirstName() + " is wet.");
Toast.makeText(DashboardActivity.this, "Baby " + baby.getFirstName() + " is wet.", Toast.LENGTH_SHORT).show();
anyBabyWet = true;
}
}
if (anyBabyWet) {
startWarningSound();
} else {
stopWarningSound();
}
} else {
try {
Gson gson = new Gson();
assert response.errorBody() != null;
String errorBodyString = response.errorBody().string();
// Check if the error body is in JSON format
if (errorBodyString.startsWith("{")) {
ErrorDto errorDto = gson.fromJson(errorBodyString, ErrorDto.class);
Toast.makeText(DashboardActivity.this, errorDto.getMessage(), Toast.LENGTH_LONG).show();
} else {
// If the error body is not in JSON format, display a generic error message
Log.e(TAG, errorBodyString);
Toast.makeText(DashboardActivity.this, "An unexpected error occurred", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.e(TAG, "else-error: " + e.getMessage());
}
}
}
@Override
public void onFailure(Call<List<BabyDto>> call, Throwable t) {
Log.e(TAG, "API call failed: " + t.getMessage());
}
});
// Schedule the next check
handler.postDelayed(this, CHECK_INTERVAL_MS);
}
};
private void startWarningSound() {
if (!isWarningPlaying) {
if (warningSoundPlayer == null) {
warningSoundPlayer = MediaPlayer.create(this, R.raw.wet_warning); // Replace with your sound file
warningSoundPlayer.setLooping(true); // Set to loop
}
warningSoundPlayer.start();
isWarningPlaying = true;
}
}
private void stopWarningSound() {
if (isWarningPlaying) {
warningSoundPlayer.stop();
warningSoundPlayer.release();
warningSoundPlayer = null;
isWarningPlaying = false;
}
} }
private void defineActionbar() { private void defineActionbar() {
...@@ -135,4 +256,11 @@ public class DashboardActivity extends AppCompatActivity { ...@@ -135,4 +256,11 @@ public class DashboardActivity extends AppCompatActivity {
super.onBackPressed(); super.onBackPressed();
animationChanger(this); animationChanger(this);
} }
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(checkWetStatusRunnable); // Stop status checking when activity is destroyed
stopWarningSound(); // Ensure sound stops if activity is closed
}
} }
\ No newline at end of file
...@@ -51,6 +51,7 @@ public class BabyDto { ...@@ -51,6 +51,7 @@ public class BabyDto {
private Long userId; private Long userId;
private Long documentId; private Long documentId;
private byte[] imageData; private byte[] imageData;
private boolean wet;
@JsonIgnore @JsonIgnore
private boolean sys_validated; private boolean sys_validated;
......
...@@ -8,6 +8,7 @@ import java.util.List; ...@@ -8,6 +8,7 @@ import java.util.List;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.DELETE; import retrofit2.http.DELETE;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.Query;
public interface ActivityLogsApiService { public interface ActivityLogsApiService {
...@@ -17,4 +18,7 @@ public interface ActivityLogsApiService { ...@@ -17,4 +18,7 @@ public interface ActivityLogsApiService {
@DELETE("activity-logs") @DELETE("activity-logs")
Call<ResponseDto> deleteAllActivityLogs(); Call<ResponseDto> deleteAllActivityLogs();
@GET("activity-logs")
Call<List<ActivityLogDto>> getTop5ActivityLogsByType(@Query("activityLogType") String activityLogType);
} }
...@@ -10,6 +10,7 @@ import retrofit2.http.GET; ...@@ -10,6 +10,7 @@ import retrofit2.http.GET;
import retrofit2.http.POST; import retrofit2.http.POST;
import retrofit2.http.PUT; import retrofit2.http.PUT;
import retrofit2.http.Path; import retrofit2.http.Path;
import retrofit2.http.Query;
public interface BabyApiService { public interface BabyApiService {
...@@ -22,4 +23,7 @@ public interface BabyApiService { ...@@ -22,4 +23,7 @@ public interface BabyApiService {
@PUT("baby/{babyId}") @PUT("baby/{babyId}")
Call<BabyDto> updateBaby(@Path("babyId") Long babyId, @Body BabyDto baby); Call<BabyDto> updateBaby(@Path("babyId") Long babyId, @Body BabyDto baby);
@GET("baby")
Call<List<BabyDto>> getAllBabiesByUserId(@Query("userId") Long userId);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment