From 34a40a199a36d0648e700c3e2e85bf843af7f6ca Mon Sep 17 00:00:00 2001 From: Olivier Girondel Date: Sat, 8 Sep 2018 19:03:42 +0200 Subject: [PATCH] Report temperature (fix #363) --- .../org/traccar/client/DatabaseHelper.java | 9 +++- .../java/org/traccar/client/MainFragment.java | 1 + .../java/org/traccar/client/Position.java | 12 ++++- .../org/traccar/client/PositionProvider.java | 45 ++++++++++++++++++- .../org/traccar/client/ProtocolFormatter.java | 4 ++ .../org/traccar/client/ShortcutActivity.java | 4 +- .../traccar/client/DatabaseHelperTest.java | 2 +- .../traccar/client/ProtocolFormatterTest.java | 6 +-- 8 files changed, 74 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/traccar/client/DatabaseHelper.java b/app/src/main/java/org/traccar/client/DatabaseHelper.java index e7166f59..b3581029 100644 --- a/app/src/main/java/org/traccar/client/DatabaseHelper.java +++ b/app/src/main/java/org/traccar/client/DatabaseHelper.java @@ -27,7 +27,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { - public static final int DATABASE_VERSION = 3; + public static final int DATABASE_VERSION = 4; public static final String DATABASE_NAME = "traccar.db"; public interface DatabaseHandler { @@ -81,6 +81,7 @@ public void onCreate(SQLiteDatabase db) { "course REAL," + "accuracy REAL," + "battery REAL," + + "temperature REAL," + "mock INTEGER)"); } @@ -106,6 +107,9 @@ public void insertPosition(Position position) { values.put("course", position.getCourse()); values.put("accuracy", position.getAccuracy()); values.put("battery", position.getBattery()); + if (position.getTemperature() != null) { + values.put("temperature", position.getTemperature()); + } values.put("mock", position.getMock() ? 1 : 0); db.insertOrThrow("position", null, values); @@ -140,6 +144,9 @@ public Position selectPosition() { position.setCourse(cursor.getDouble(cursor.getColumnIndex("course"))); position.setAccuracy(cursor.getDouble(cursor.getColumnIndex("accuracy"))); position.setBattery(cursor.getDouble(cursor.getColumnIndex("battery"))); + if (!cursor.isNull(cursor.getColumnIndex("temperature"))) { + position.setTemperature(cursor.getFloat(cursor.getColumnIndex("temperature"))); + } position.setMock(cursor.getInt(cursor.getColumnIndex("mock")) > 0); } else { diff --git a/app/src/main/java/org/traccar/client/MainFragment.java b/app/src/main/java/org/traccar/client/MainFragment.java index c78ad78c..9d2d9647 100644 --- a/app/src/main/java/org/traccar/client/MainFragment.java +++ b/app/src/main/java/org/traccar/client/MainFragment.java @@ -60,6 +60,7 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen public static final String KEY_ANGLE = "angle"; public static final String KEY_ACCURACY = "accuracy"; public static final String KEY_STATUS = "status"; + public static final String KEY_TEMPERATURE = "temperature"; private static final int PERMISSIONS_REQUEST_LOCATION = 2; diff --git a/app/src/main/java/org/traccar/client/Position.java b/app/src/main/java/org/traccar/client/Position.java index ca4f2e70..f906e0d5 100644 --- a/app/src/main/java/org/traccar/client/Position.java +++ b/app/src/main/java/org/traccar/client/Position.java @@ -26,7 +26,7 @@ public class Position { public Position() { } - public Position(String deviceId, Location location, double battery) { + public Position(String deviceId, Location location, double battery, Float temperature) { this.deviceId = deviceId; time = new Date(location.getTime()); latitude = location.getLatitude(); @@ -41,6 +41,7 @@ public Position(String deviceId, Location location, double battery) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { this.mock = location.isFromMockProvider(); } + this.temperature = temperature; } private long id; @@ -153,4 +154,13 @@ public void setMock(boolean mock) { this.mock = mock; } + private Float temperature; + + public Float getTemperature() { + return temperature; + } + + public void setTemperature(float temperature) { + this.temperature = temperature; + } } diff --git a/app/src/main/java/org/traccar/client/PositionProvider.java b/app/src/main/java/org/traccar/client/PositionProvider.java index b0c7b98f..cdcd53b8 100644 --- a/app/src/main/java/org/traccar/client/PositionProvider.java +++ b/app/src/main/java/org/traccar/client/PositionProvider.java @@ -16,20 +16,30 @@ package org.traccar.client; import android.annotation.SuppressLint; +import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.location.Criteria; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.BatteryManager; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; import android.widget.Toast; -public class PositionProvider implements LocationListener { +import static android.content.Context.SENSOR_SERVICE; +import static android.hardware.Sensor.REPORTING_MODE_ON_CHANGE; + +public class PositionProvider implements LocationListener, SensorEventListener { private static final String TAG = PositionProvider.class.getSimpleName(); @@ -52,6 +62,10 @@ public interface PositionListener { private Location lastLocation; + private SensorManager sensorManager; + private Sensor temperatureSensor; + private Float temperature; + public PositionProvider(Context context, PositionListener listener) { this.context = context; this.listener = listener; @@ -64,8 +78,17 @@ public PositionProvider(Context context, PositionListener listener) { interval = Long.parseLong(preferences.getString(MainFragment.KEY_INTERVAL, "600")) * 1000; distance = Integer.parseInt(preferences.getString(MainFragment.KEY_DISTANCE, "0")); angle = Integer.parseInt(preferences.getString(MainFragment.KEY_ANGLE, "0")); + if (preferences.contains(MainFragment.KEY_TEMPERATURE)) { + temperature = preferences.getFloat(MainFragment.KEY_TEMPERATURE, 0); + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE); + temperatureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); + } } + @TargetApi(Build.VERSION_CODES.LOLLIPOP) @SuppressLint("MissingPermission") public void startUpdates() { try { @@ -75,6 +98,9 @@ public void startUpdates() { } catch (RuntimeException e) { Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show(); } + if (temperatureSensor != null) { + sensorManager.registerListener(this, temperatureSensor, REPORTING_MODE_ON_CHANGE); + } } public static String getProvider(String accuracy) { @@ -96,7 +122,7 @@ public void onLocationChanged(Location location) { || angle > 0 && Math.abs(location.getBearing() - lastLocation.getBearing()) >= angle)) { Log.i(TAG, "location new"); lastLocation = location; - listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel(context))); + listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel(context), temperature)); } else { Log.i(TAG, location != null ? "location ignored" : "location nil"); } @@ -116,6 +142,9 @@ public void onProviderDisabled(String provider) { public void stopUpdates() { locationManager.removeUpdates(this); + if (sensorManager != null) { + sensorManager.unregisterListener(this); + } } public static double getBatteryLevel(Context context) { @@ -128,4 +157,16 @@ public static double getBatteryLevel(Context context) { return 0; } + @Override + public void onSensorChanged(SensorEvent event) { + if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) { + temperature = event.values[0]; + preferences.edit().putFloat(MainFragment.KEY_TEMPERATURE, temperature).apply(); + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + } + } diff --git a/app/src/main/java/org/traccar/client/ProtocolFormatter.java b/app/src/main/java/org/traccar/client/ProtocolFormatter.java index 2a58531d..30471399 100644 --- a/app/src/main/java/org/traccar/client/ProtocolFormatter.java +++ b/app/src/main/java/org/traccar/client/ProtocolFormatter.java @@ -36,6 +36,10 @@ public static String formatRequest(String url, Position position, String alarm) .appendQueryParameter("accuracy", String.valueOf(position.getAccuracy())) .appendQueryParameter("batt", String.valueOf(position.getBattery())); + if (position.getTemperature() != null) { + builder.appendQueryParameter("temp1", String.valueOf(position.getTemperature())); + } + if (position.getMock()) { builder.appendQueryParameter("mock", String.valueOf(position.getMock())); } diff --git a/app/src/main/java/org/traccar/client/ShortcutActivity.java b/app/src/main/java/org/traccar/client/ShortcutActivity.java index cd2a77cb..4c3e0b05 100644 --- a/app/src/main/java/org/traccar/client/ShortcutActivity.java +++ b/app/src/main/java/org/traccar/client/ShortcutActivity.java @@ -145,7 +145,9 @@ private void sendAlarmLocation(Location location) { Position position = new Position( preferences.getString(MainFragment.KEY_DEVICE, null), - location, PositionProvider.getBatteryLevel(this)); + location, PositionProvider.getBatteryLevel(this), + preferences.contains(MainFragment.KEY_TEMPERATURE) ? + preferences.getFloat(MainFragment.KEY_TEMPERATURE, 0) : null); String request = ProtocolFormatter.formatRequest( preferences.getString(MainFragment.KEY_URL, null), position, ALARM_SOS); diff --git a/app/src/test/java/org/traccar/client/DatabaseHelperTest.java b/app/src/test/java/org/traccar/client/DatabaseHelperTest.java index aea07d87..8984eab1 100644 --- a/app/src/test/java/org/traccar/client/DatabaseHelperTest.java +++ b/app/src/test/java/org/traccar/client/DatabaseHelperTest.java @@ -23,7 +23,7 @@ public void test() throws Exception { DatabaseHelper databaseHelper = new DatabaseHelper(RuntimeEnvironment.application); - Position position = new Position("123456789012345", new Location("gps"), 0); + Position position = new Position("123456789012345", new Location("gps"), 0, null); position.setTime(new Date(0)); assertNull(databaseHelper.selectPosition()); diff --git a/app/src/test/java/org/traccar/client/ProtocolFormatterTest.java b/app/src/test/java/org/traccar/client/ProtocolFormatterTest.java index 3f9f4f1f..cfd712a0 100644 --- a/app/src/test/java/org/traccar/client/ProtocolFormatterTest.java +++ b/app/src/test/java/org/traccar/client/ProtocolFormatterTest.java @@ -19,7 +19,7 @@ public class ProtocolFormatterTest { @Test public void testFormatRequest() throws Exception { - Position position = new Position("123456789012345", new Location("gps"), 0); + Position position = new Position("123456789012345", new Location("gps"), 0, null); position.setTime(new Date(0)); String url = ProtocolFormatter.formatRequest("http://localhost:5055", position); @@ -29,7 +29,7 @@ public void testFormatRequest() throws Exception { @Test public void testFormatPathPortRequest() throws Exception { - Position position = new Position("123456789012345", new Location("gps"), 0); + Position position = new Position("123456789012345", new Location("gps"), 0, null); position.setTime(new Date(0)); String url = ProtocolFormatter.formatRequest("http://localhost:8888/path", position); @@ -39,7 +39,7 @@ public void testFormatPathPortRequest() throws Exception { @Test public void testFormatAlarmRequest() throws Exception { - Position position = new Position("123456789012345", new Location("gps"), 0); + Position position = new Position("123456789012345", new Location("gps"), 0, null); position.setTime(new Date(0)); String url = ProtocolFormatter.formatRequest("http://localhost:5055/path", position, "alert message");