Implement database for buffer
Этот коммит содержится в:
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -21,11 +21,10 @@ import java.util.Date;
|
|||||||
|
|
||||||
public class Position {
|
public class Position {
|
||||||
|
|
||||||
public Location location; // DELME
|
public Position() {
|
||||||
|
}
|
||||||
|
|
||||||
public Position(String deviceId, Location location, double battery) {
|
public Position(String deviceId, Location location, double battery) {
|
||||||
this.location = location; // DELME
|
|
||||||
|
|
||||||
this.deviceId = deviceId;
|
this.deviceId = deviceId;
|
||||||
time = new Date(location.getTime());
|
time = new Date(location.getTime());
|
||||||
latitude = location.getLatitude();
|
latitude = location.getLatitude();
|
||||||
@@ -36,6 +35,10 @@ public class Position {
|
|||||||
this.battery = battery;
|
this.battery = battery;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
public long getId() { return id; }
|
||||||
|
public void setId(long id) { this.id = id; }
|
||||||
|
|
||||||
private String deviceId;
|
private String deviceId;
|
||||||
public String getDeviceId() { return deviceId; }
|
public String getDeviceId() { return deviceId; }
|
||||||
public void setDeviceId(String deviceId) { this.deviceId = deviceId; }
|
public void setDeviceId(String deviceId) { this.deviceId = deviceId; }
|
||||||
|
|||||||
@@ -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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user