Skip to content

Commit

Permalink
Add driver (driverUniqueId) support
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Girondel committed May 7, 2018
1 parent f148e4d commit bdf33cb
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 10 deletions.
5 changes: 4 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 @@ -73,6 +73,7 @@ public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE position (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"deviceId TEXT," +
"driverUniqueId TEXT," +
"time INTEGER," +
"latitude REAL," +
"longitude REAL," +
Expand All @@ -93,6 +94,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public void insertPosition(Position position) {
ContentValues values = new ContentValues();
values.put("deviceId", position.getDeviceId());
values.put("driverUniqueId", position.getDriverUniqueId());
values.put("time", position.getTime().getTime());
values.put("latitude", position.getLatitude());
values.put("longitude", position.getLongitude());
Expand Down Expand Up @@ -127,6 +129,7 @@ public Position selectPosition() {

position.setId(cursor.getLong(cursor.getColumnIndex("id")));
position.setDeviceId(cursor.getString(cursor.getColumnIndex("deviceId")));
position.setDriverUniqueId(cursor.getString(cursor.getColumnIndex("driverUniqueId")));
position.setTime(new Date(cursor.getLong(cursor.getColumnIndex("time"))));
position.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude")));
position.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude")));
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/org/traccar/client/MainFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen
private static final int ALARM_MANAGER_INTERVAL = 15000;

public static final String KEY_DEVICE = "id";
public static final String KEY_DRIVER_UNIQUE_ID = "driverUniqueId";
public static final String KEY_URL = "url";
public static final String KEY_INTERVAL = "interval";
public static final String KEY_DISTANCE = "distance";
Expand Down Expand Up @@ -87,6 +88,12 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
return newValue != null && !newValue.equals("");
}
});
findPreference(KEY_DRIVER_UNIQUE_ID).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
return newValue != null && !newValue.equals("");
}
});
findPreference(KEY_URL).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Expand Down Expand Up @@ -164,6 +171,7 @@ public void onPause() {

private void setPreferencesEnabled(boolean enabled) {
findPreference(KEY_DEVICE).setEnabled(enabled);
findPreference(KEY_DRIVER_UNIQUE_ID).setEnabled(enabled);
findPreference(KEY_URL).setEnabled(enabled);
findPreference(KEY_INTERVAL).setEnabled(enabled);
findPreference(KEY_DISTANCE).setEnabled(enabled);
Expand All @@ -181,6 +189,8 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
}
} else if (key.equals(KEY_DEVICE)) {
findPreference(KEY_DEVICE).setSummary(sharedPreferences.getString(KEY_DEVICE, null));
} else if (key.equals(KEY_DRIVER_UNIQUE_ID)) {
findPreference(KEY_DRIVER_UNIQUE_ID).setSummary(sharedPreferences.getString(KEY_DRIVER_UNIQUE_ID, null));
}
}

Expand Down Expand Up @@ -211,6 +221,13 @@ private void initPreferences() {
((EditTextPreference) findPreference(KEY_DEVICE)).setText(id);
}
findPreference(KEY_DEVICE).setSummary(sharedPreferences.getString(KEY_DEVICE, null));

if (!sharedPreferences.contains(KEY_DRIVER_UNIQUE_ID)) {
String id = String.valueOf(new Random().nextInt(900000) + 100000);
sharedPreferences.edit().putString(KEY_DRIVER_UNIQUE_ID, id).apply();
((EditTextPreference) findPreference(KEY_DRIVER_UNIQUE_ID)).setText(id);
}
findPreference(KEY_DRIVER_UNIQUE_ID).setSummary(sharedPreferences.getString(KEY_DRIVER_UNIQUE_ID, null));
}

private void startTrackingService(boolean checkPermission, boolean permission) {
Expand Down
13 changes: 12 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,8 +26,9 @@ public class Position {
public Position() {
}

public Position(String deviceId, Location location, double battery) {
public Position(String deviceId, String driverUniqueId, Location location, double battery) {
this.deviceId = deviceId;
this.driverUniqueId = driverUniqueId;
time = new Date(location.getTime());
latitude = location.getLatitude();
longitude = location.getLongitude();
Expand Down Expand Up @@ -63,6 +64,16 @@ public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

private String driverUniqueId;

public String getDriverUniqueId() {
return driverUniqueId;
}

public void setDriverUniqueId(String driverUniqueId) {
this.driverUniqueId = driverUniqueId;
}

private Date time;

public Date getTime() {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/org/traccar/client/PositionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public interface PositionListener {
private LostApiClient apiClient;

private String deviceId;
private String driverUniqueId;
private long interval;
private double distance;
private double angle;
Expand All @@ -62,6 +63,7 @@ public PositionProvider(Context context, PositionListener listener) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);

deviceId = preferences.getString(MainFragment.KEY_DEVICE, "undefined");
driverUniqueId = preferences.getString(MainFragment.KEY_DRIVER_UNIQUE_ID, "undefined");
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"));
Expand Down Expand Up @@ -105,7 +107,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, driverUniqueId, location, getBatteryLevel(context)));
} else {
Log.i(TAG, location != null ? "location ignored" : "location nil");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static String formatRequest(String url, Position position, String alarm)
Uri serverUrl = Uri.parse(url);
Uri.Builder builder = serverUrl.buildUpon()
.appendQueryParameter("id", position.getDeviceId())
.appendQueryParameter("driverUniqueId", position.getDriverUniqueId())
.appendQueryParameter("timestamp", String.valueOf(position.getTime().getTime() / 1000))
.appendQueryParameter("lat", String.valueOf(position.getLatitude()))
.appendQueryParameter("lon", String.valueOf(position.getLongitude()))
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/traccar/client/ShortcutActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public void onConnected() {

Position position = new Position(
preferences.getString(MainFragment.KEY_DEVICE, null),
preferences.getString(MainFragment.KEY_DRIVER_UNIQUE_ID, null),
location, PositionProvider.getBatteryLevel(this));

String request = ProtocolFormatter.formatRequest(
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<string name="app_name">Client Traccar</string>
<string name="app_logo">Traccar</string>
<string name="settings_id_title">Identifiant de l\'appareil</string>
<string name="settings_driver_unique_id_title">Identifiant du conducteur</string>
<string name="settings_url_title">URL du serveur</string>
<string name="settings_url_summary">URL du serveur de suivi</string>
<string name="settings_interval_title">Fréquence</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<string name="shortcut_sos">Send SOS</string>
<string name="channel_default">Primary Channel</string>
<string name="settings_id_title">Device identifier</string>
<string name="settings_driver_unique_id_title">Driver identifier</string>
<string name="settings_url_title">Server URL</string>
<string name="settings_url_summary">Tracking server URL</string>
<string name="settings_interval_title">Frequency</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
android:key="id"
android:title="@string/settings_id_title" />

<EditTextPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="driverUniqueId"
android:title="@string/settings_driver_unique_id_title" />

<EditTextPreference
android:defaultValue="@string/settings_url_default_value"
android:key="url"
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", "123456789", new Location("gps"), 0);
position.setTime(new Date(0));

assertNull(databaseHelper.selectPosition());
Expand Down
12 changes: 6 additions & 6 deletions app/src/test/java/org/traccar/client/ProtocolFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@ public class ProtocolFormatterTest {
@Test
public void testFormatRequest() throws Exception {

Position position = new Position("123456789012345", new Location("gps"), 0);
Position position = new Position("123456789012345", "123456789", new Location("gps"), 0);
position.setTime(new Date(0));

String url = ProtocolFormatter.formatRequest("http://localhost:5055", position);
assertEquals("http://localhost:5055?id=123456789012345&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&accuracy=0.0&batt=0.0", url);
assertEquals("http://localhost:5055?id=123456789012345&driverUniqueId=123456789&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&accuracy=0.0&batt=0.0", url);
}

@Test
public void testFormatPathPortRequest() throws Exception {

Position position = new Position("123456789012345", new Location("gps"), 0);
Position position = new Position("123456789012345", "123456789", new Location("gps"), 0);
position.setTime(new Date(0));

String url = ProtocolFormatter.formatRequest("http://localhost:8888/path", position);
assertEquals("http://localhost:8888/path?id=123456789012345&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&accuracy=0.0&batt=0.0", url);
assertEquals("http://localhost:8888/path?id=123456789012345&driverUniqueId=123456789&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&accuracy=0.0&batt=0.0", url);
}

@Test
public void testFormatAlarmRequest() throws Exception {

Position position = new Position("123456789012345", new Location("gps"), 0);
Position position = new Position("123456789012345", "123456789", new Location("gps"), 0);
position.setTime(new Date(0));

String url = ProtocolFormatter.formatRequest("http://localhost:5055/path", position, "alert message");
assertEquals("http://localhost:5055/path?id=123456789012345&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&accuracy=0.0&batt=0.0&alarm=alert%20message", url);
assertEquals("http://localhost:5055/path?id=123456789012345&driverUniqueId=123456789&timestamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&accuracy=0.0&batt=0.0&alarm=alert%20message", url);
}
}

0 comments on commit bdf33cb

Please sign in to comment.