Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Updated logging in Android console

Rich Turner6038a2e2176b
diff --git a/console/android/src/main/java/org/openremote/android/ORFirebaseInstanceIDService.java b/console/android/src/main/java/org/openremote/android/ORFirebaseInstanceIDService.java
index 0d0a86fe9..ea7c97ff8 100644
--- a/console/android/src/main/java/org/openremote/android/ORFirebaseInstanceIDService.java
+++ b/console/android/src/main/java/org/openremote/android/ORFirebaseInstanceIDService.java
@@ -1,23 +1,7 @@
-/**
- * Copyright 2016 Google Inc. All Rights Reserved.
- * <p>
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
package org.openremote.android;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
-
import org.openremote.android.service.TokenService;
import java.util.logging.Logger;
diff --git a/console/android/src/main/java/org/openremote/android/service/GeofenceBootCompleteReceiver.kt b/console/android/src/main/java/org/openremote/android/service/GeofenceBootCompleteReceiver.kt
index ed91f6df8..e72fdbcfb 100644
--- a/console/android/src/main/java/org/openremote/android/service/GeofenceBootCompleteReceiver.kt
+++ b/console/android/src/main/java/org/openremote/android/service/GeofenceBootCompleteReceiver.kt
@@ -25,6 +25,7 @@ import android.content.Context
import android.content.Intent
class GeofenceBootCompleteReceiver : BroadcastReceiver() {
+
override fun onReceive(context: Context?, intent: Intent?) {
if (context != null) {
GeofenceProvider.startGeofences(context)
diff --git a/console/android/src/main/java/org/openremote/android/service/GeofenceProvider.kt b/console/android/src/main/java/org/openremote/android/service/GeofenceProvider.kt
index 2e004dfe4..5dc4f85ff 100644
--- a/console/android/src/main/java/org/openremote/android/service/GeofenceProvider.kt
+++ b/console/android/src/main/java/org/openremote/android/service/GeofenceProvider.kt
@@ -9,7 +9,6 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
-import android.util.Log
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
@@ -17,6 +16,8 @@ import com.google.android.gms.location.GeofencingRequest
import com.google.android.gms.location.LocationServices
import org.openremote.android.R
import java.net.URL
+import java.util.logging.Level
+import java.util.logging.Logger
class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissionsResultCallback {
@@ -119,25 +120,31 @@ class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissio
val geofencesKey = "geofences"
var locationReponseCode = 101
var JSON = ObjectMapper()
+ val LOG = Logger.getLogger(GeofenceProvider::class.java.name)
fun getGeofences(context: Context): Array<GeofenceDefinition> {
val sharedPreferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE)
val existingGeofencesJson = sharedPreferences.getString(geofencesKey, null)
+ var geofences : Array<GeofenceDefinition> = arrayOf()
if (existingGeofencesJson != null) {
- return JSON.readValue(existingGeofencesJson, Array<GeofenceDefinition>::class.java)
- } else {
- return arrayOf()
+ geofences = JSON.readValue(existingGeofencesJson, Array<GeofenceDefinition>::class.java)
}
+
+ LOG.info("Found geofences=${geofences.size}")
+ return geofences
}
fun startGeofences(context: Context) {
+
+ LOG.info("Starting geofences")
+
val geofences = getGeofences(context)
val baseUrl = getBaseUrl(context)
if (geofences.isEmpty() || baseUrl == null) {
return
}
- Log.i("GeofenceProvider", "Starting geofences count=${geofences.size}")
+
val intent = getGeofencePendingIntent(context, baseUrl)
val geofencingClient = LocationServices.getGeofencingClient(context)
@@ -146,9 +153,11 @@ class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissio
}
}
-
@SuppressLint("MissingPermission")
private fun addGeofence(geofencingClient: GeofencingClient, geofencePendingIntent: PendingIntent, geofenceDefinition: GeofenceDefinition) {
+
+ LOG.info("Adding geofence: id=${geofenceDefinition.id} lat=${geofenceDefinition.lat} lng=${geofenceDefinition.lng}, radius=${geofenceDefinition.radius}")
+
val geofence = Geofence.Builder()
.setRequestId(geofenceDefinition.id)
.setCircularRegion(geofenceDefinition.lat, geofenceDefinition.lng, geofenceDefinition.radius)
@@ -163,11 +172,11 @@ class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissio
geofencingClient.addGeofences(geofencingRequest, geofencePendingIntent).run {
addOnSuccessListener {
- Log.i("GeofenceProvider", "Geofence added: lat${geofenceDefinition.lat}/lng${geofenceDefinition.lng}/rad${geofenceDefinition.radius}")
+ LOG.info("Added geofence")
}
addOnFailureListener { exception ->
- Log.e("GeofenceProvider", "Failed to add geofence", exception)
+ LOG.log(Level.SEVERE, "Add geofence failed", exception)
}
}
}
@@ -244,13 +253,18 @@ class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissio
}
fun refreshGeofences() {
- Log.i("GeofenceProvider", "refreshGeofences")
+ LOG.info("Refresh geofences")
onEnable(null)
}
private fun onEnable(callback: EnableCallback?) {
+
+ LOG.info("Enabling geofence provider")
+
Thread {
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
+ LOG.info("Has permission so fetching geofences")
+
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// context.startForegroundService(Intent(context, LocationService::class.java))
// } else {
@@ -270,6 +284,7 @@ class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissio
}
private fun registerPermissions(activity: Activity) {
+ LOG.info("Requesting geofence permissions")
val permissions = arrayOf(ACCESS_FINE_LOCATION)
ActivityCompat.requestPermissions(activity, permissions, locationReponseCode)
}
@@ -283,11 +298,15 @@ class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissio
private fun fetchGeofences() {
val sharedPreferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE)
- val geofencesJson = URL("${getBaseUrl(context)}/$geofenceFetchEndpoint${getConsoleId(context)}").readText()
+ val url = "${getBaseUrl(context)}/$geofenceFetchEndpoint${getConsoleId(context)}"
+ LOG.info("Fetching geofences from server: ${url}")
+
+ val geofencesJson = URL(url).readText()
val geofences = JSON.readValue(geofencesJson, Array<GeofenceDefinition>::class.java)
- removeGeofences(getGeofences(context))
- Log.i("GeofenceProvider", "fetchGeofences: count=${geofences.size}")
+ LOG.info("Fetched geofences=${geofences.size}")
+
+ removeGeofences(getGeofences(context))
geofences.forEach {
addGeofence(geofencingClient, getGeofencePendingIntent(context, getBaseUrl(context).orEmpty()), it)
@@ -301,11 +320,11 @@ class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissio
private fun removeGeofence(id: String) {
geofencingClient.removeGeofences(listOf(id)).run {
addOnSuccessListener {
- Log.i("GeofenceProvider", "Geofence removed: $id")
+ LOG.info("Removed geofence: $id")
}
addOnFailureListener { exception ->
- Log.e("GeofenceProvider", "Failed to remove geofence", exception)
+ LOG.log(Level.SEVERE, "Failed to remove geofence", exception)
}
}
}
@@ -319,13 +338,15 @@ class GeofenceProvider(val context: Context) : ActivityCompat.OnRequestPermissio
return
}
+ LOG.info("Removing existing geofences: $ids")
+
geofencingClient.removeGeofences(ids).run {
addOnSuccessListener {
- Log.i("GeofenceProvider", "Geofences removed")
+ LOG.info("Geofences removed")
}
addOnFailureListener { exception ->
- Log.e("GeofenceProvider", exception.message, exception)
+ LOG.log(Level.SEVERE, "Geofences remove failed", exception)
}
}
}
diff --git a/console/android/src/main/java/org/openremote/android/service/GeofenceTransitionsIntentService.kt b/console/android/src/main/java/org/openremote/android/service/GeofenceTransitionsIntentService.kt
index 6b1ff73a8..ca9cf5b0c 100644
--- a/console/android/src/main/java/org/openremote/android/service/GeofenceTransitionsIntentService.kt
+++ b/console/android/src/main/java/org/openremote/android/service/GeofenceTransitionsIntentService.kt
@@ -3,7 +3,6 @@ package org.openremote.android.service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
-import android.util.Log
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingEvent
@@ -17,17 +16,16 @@ class GeofenceTransitionsIntentService : BroadcastReceiver() {
private val LOG = Logger.getLogger(GeofenceTransitionsIntentService::class.java.name)
override fun onReceive(context: Context?, intent: Intent?) {
+
val geofencingEvent = GeofencingEvent.fromIntent(intent)
+ LOG.info("Geofence event received: transition=${geofencingEvent.geofenceTransition}")
+
if (geofencingEvent.hasError()) {
- LOG.warning("Error handling geofence event")
+ LOG.warning("Geofence event error : ${geofencingEvent.errorCode}")
return
}
- LOG.fine("Geofence '" + geofencingEvent.geofenceTransition + "' occurred")
-
- val baseUrl = intent!!.getStringExtra(GeofenceProvider.baseUrlKey)
- val geofenceTransition = geofencingEvent.geofenceTransition
val geofenceDefinitions = GeofenceProvider.getGeofences(context!!)
if (geofenceDefinitions.isEmpty()) {
@@ -35,11 +33,17 @@ class GeofenceTransitionsIntentService : BroadcastReceiver() {
return
}
+ val baseUrl = intent!!.getStringExtra(GeofenceProvider.baseUrlKey)
+ val geofenceTransition = geofencingEvent.geofenceTransition
+
geofencingEvent.triggeringGeofences.forEach {
geofence ->
val geofenceDefinition = geofenceDefinitions.firstOrNull { it.id == geofence.requestId }
if (geofenceDefinition != null) {
+
+ LOG.info("Triggered geofence: id=${geofenceDefinition.id}")
+
val url = URL("$baseUrl${geofenceDefinition.url}")
val connection = url.openConnection() as HttpURLConnection
connection.setRequestProperty("Content-Type", "application/json")
@@ -49,12 +53,14 @@ class GeofenceTransitionsIntentService : BroadcastReceiver() {
val postJson = when (geofenceTransition) {
Geofence.GEOFENCE_TRANSITION_ENTER -> {
+ LOG.info("Sending location 'lat=${geofenceDefinition.lat}/lng=${geofenceDefinition.lng}' to server: HTTP ${geofenceDefinition.httpMethod} $url")
hashMapOf(
"type" to "Point",
"coordinates" to arrayOf(geofencingEvent.triggeringLocation.longitude, geofencingEvent.triggeringLocation.latitude)
)
}
else -> {
+ LOG.info("Sending location 'null' to server: HTTP ${geofenceDefinition.httpMethod} $url")
null
}
}
@@ -68,10 +74,10 @@ class GeofenceTransitionsIntentService : BroadcastReceiver() {
connection.outputStream.flush()
val responseCode = connection.responseCode
- LOG.info("Location posted to server: response=" + responseCode)
+ LOG.info("Send location success: response=$responseCode")
} catch (exception: Exception) {
- LOG.log(Level.SEVERE, exception.message, exception)
+ LOG.log(Level.SEVERE, "Send location failed", exception)
print(exception)
} finally {
connection.disconnect()
diff --git a/console/android/src/main/java/org/openremote/android/service/TokenService.java b/console/android/src/main/java/org/openremote/android/service/TokenService.java
index e1f6e8ad2..f8fc8a1a6 100644
--- a/console/android/src/main/java/org/openremote/android/service/TokenService.java
+++ b/console/android/src/main/java/org/openremote/android/service/TokenService.java
@@ -113,6 +113,8 @@ public class TokenService {
}
public void sendOrStoreFCMToken(String fcmToken, String deviceId) {
+ LOG.info("Storing FCM token: " + fcmToken);
+ LOG.info("Storing Device ID: " + deviceId);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(fcmTokenKey, fcmToken);
editor.putString(deviceIdKey, deviceId);
@@ -120,13 +122,14 @@ public class TokenService {
}
public void clearToken() {
+ LOG.info("Clearing auth refresh token");
SharedPreferences.Editor editor = sharedPref.edit();
editor.remove(refreshTokenKey);
editor.commit();
}
// TODO: Support other content types
- public void executeRequest(String httpMethod, String url, String body) {
+ public void executeRequest(final String httpMethod, String url, String body) {
// If relative URL assume it is relative to base URL
if (!url.toLowerCase(Locale.ROOT).startsWith("http")) {
@@ -170,6 +173,8 @@ public class TokenService {
throw new IllegalArgumentException("Unsupported HTTP Method: " + httpMethod);
}
+ LOG.info("Executing silent request: HTTP " + httpMethod + " " + url);
+
if (useAuth) {
withAccessToken(new TokenCallback() {
@Override
@@ -223,21 +228,25 @@ public class TokenService {
// }
public void notificationDelivered(final Long notificationId, final String fcmToken) {
+ LOG.info("Notification status update 'delivered': " + notificationId);
+
restApiResource.notificationDelivered(realm, notificationId, fcmToken).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
-
+ LOG.info("Notification status update success");
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
-
+ LOG.log(Level.SEVERE, "Notification status update failed", t);
}
});
}
public void notificationAcknowledged(final Long notificationId, final String fcmToken, String acknowledgement) {
+ LOG.info("Notification status update 'acknowledged' with value '" + acknowledgement + "': " + notificationId);
+
if (TextUtils.isEmpty(acknowledgement)) {
acknowledgement = "";
}
@@ -245,12 +254,12 @@ public class TokenService {
restApiResource.notificationAcknowledged(realm, notificationId, fcmToken, acknowledgement).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
-
+ LOG.info("Notification status update success");
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
-
+ LOG.log(Level.SEVERE, "Notification status update failed", t);
}
});
}