From 03266a8c785300f9a9cff706caf91554a44a7211 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 13 Aug 2015 11:49:46 +1200 Subject: [PATCH] Implement database for buffer --- .../org/traccar/client/DatabaseHelper.java | 105 ++++++++++++++++++ .../java/org/traccar/client/Position.java | 9 +- .../traccar/client/DatabaseHelperTest.java | 46 ++++++++ 3 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/traccar/client/DatabaseHelper.java create mode 100644 app/src/test/java/org/traccar/client/DatabaseHelperTest.java diff --git a/app/src/main/java/org/traccar/client/DatabaseHelper.java b/app/src/main/java/org/traccar/client/DatabaseHelper.java new file mode 100644 index 0000000..98aa77d --- /dev/null +++ b/app/src/main/java/org/traccar/client/DatabaseHelper.java @@ -0,0 +1,105 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.traccar.client; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.SQLException; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import java.util.Date; + +public class DatabaseHelper extends SQLiteOpenHelper { + + public static final int DATABASE_VERSION = 1; + public static final String DATABASE_NAME = "traccar.db"; + + public DatabaseHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL("CREATE TABLE position (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT," + + "deviceId TEXT," + + "time INTEGER," + + "latitude REAL," + + "longitude REAL," + + "altitude REAL," + + "speed REAL," + + "course REAL," + + "battery REAL)"); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL("DROP TABLE IF EXISTS position;"); + onCreate(db); + } + + public void insertPosition(SQLiteDatabase db, Position position) { + ContentValues values = new ContentValues(); + values.put("deviceId", position.getDeviceId()); + values.put("time", position.getTime().getTime()); + values.put("latitude", position.getLatitude()); + values.put("longitude", position.getLongitude()); + values.put("altitude", position.getAltitude()); + values.put("speed", position.getSpeed()); + values.put("course", position.getCourse()); + values.put("battery", position.getBattery()); + + db.insertOrThrow("position", null, values); + } + + public Position selectPosition(SQLiteDatabase db) { + Position position = new Position(); + + Cursor cursor = db.rawQuery("SELECT * FROM position ORDER BY id LIMIT 1", null); + try { + if (cursor.getCount() > 0) { + + cursor.moveToFirst(); + + position.setId(cursor.getLong(cursor.getColumnIndex("id"))); + position.setDeviceId(cursor.getString(cursor.getColumnIndex("deviceId"))); + position.setTime(new Date(cursor.getLong(cursor.getColumnIndex("time")))); + position.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude"))); + position.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude"))); + position.setAltitude(cursor.getDouble(cursor.getColumnIndex("altitude"))); + position.setSpeed(cursor.getDouble(cursor.getColumnIndex("speed"))); + position.setCourse(cursor.getDouble(cursor.getColumnIndex("course"))); + position.setBattery(cursor.getDouble(cursor.getColumnIndex("battery"))); + + } else { + return null; + } + } finally { + cursor.close(); + } + + return position; + } + + public void deletePosition(SQLiteDatabase db, long id) { + if (db.delete("position", "id = ?", new String[] { String.valueOf(id) }) != 1) { + throw new SQLException(); + } + } + +} diff --git a/app/src/main/java/org/traccar/client/Position.java b/app/src/main/java/org/traccar/client/Position.java index 8a4ab9c..888c620 100644 --- a/app/src/main/java/org/traccar/client/Position.java +++ b/app/src/main/java/org/traccar/client/Position.java @@ -21,11 +21,10 @@ import java.util.Date; public class Position { - public Location location; // DELME + public Position() { + } public Position(String deviceId, Location location, double battery) { - this.location = location; // DELME - this.deviceId = deviceId; time = new Date(location.getTime()); latitude = location.getLatitude(); @@ -36,6 +35,10 @@ public class Position { this.battery = battery; } + private long id; + public long getId() { return id; } + public void setId(long id) { this.id = id; } + private String deviceId; public String getDeviceId() { return deviceId; } public void setDeviceId(String deviceId) { this.deviceId = deviceId; } diff --git a/app/src/test/java/org/traccar/client/DatabaseHelperTest.java b/app/src/test/java/org/traccar/client/DatabaseHelperTest.java new file mode 100644 index 0000000..8851032 --- /dev/null +++ b/app/src/test/java/org/traccar/client/DatabaseHelperTest.java @@ -0,0 +1,46 @@ + +package org.traccar.client; + +import android.database.sqlite.SQLiteDatabase; +import android.location.Location; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.Date; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(constants = BuildConfig.class, sdk = 21) +public class DatabaseHelperTest { + + @Test + public void test() throws Exception { + + DatabaseHelper databaseHelper = new DatabaseHelper(RuntimeEnvironment.application); + + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + + Position position = new Position("123456789012345", new Location("gps"), 0); + position.setTime(new Date(0)); + + assertNull(databaseHelper.selectPosition(db)); + + databaseHelper.insertPosition(db, position); + + position = databaseHelper.selectPosition(db); + + assertNotNull(position); + + databaseHelper.deletePosition(db, position.getId()); + + assertNull(databaseHelper.selectPosition(db)); + + } + +}