Skip to content

Commit

Permalink
Report temperature (fix #363)
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Girondel committed Nov 24, 2018
1 parent d2f6530 commit 34a40a1
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 9 deletions.
9 changes: 8 additions & 1 deletion app/src/main/java/org/traccar/client/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down Expand Up @@ -81,6 +81,7 @@ public void onCreate(SQLiteDatabase db) {
"course REAL," +
"accuracy REAL," +
"battery REAL," +
"temperature REAL," +
"mock INTEGER)");
}

Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/traccar/client/MainFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/org/traccar/client/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
45 changes: 43 additions & 2 deletions app/src/main/java/org/traccar/client/PositionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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");
}
Expand All @@ -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) {
Expand All @@ -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) {
}

}
4 changes: 4 additions & 0 deletions app/src/main/java/org/traccar/client/ProtocolFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/org/traccar/client/ShortcutActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand Down

0 comments on commit 34a40a1

Please sign in to comment.