Add new protocol formatter
Этот коммит содержится в:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 - 2014 Anton Tananaev (anton.tananaev@gmail.com)
|
||||
* Copyright 2012 - 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.
|
||||
@@ -21,12 +21,26 @@ import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import android.location.Location;
|
||||
import android.net.Uri;
|
||||
|
||||
/**
|
||||
* Protocol formatting
|
||||
*/
|
||||
public class Protocol {
|
||||
|
||||
public static String formatRequest(String address, int port, String id, Location l, double battery) {
|
||||
|
||||
Uri.Builder builder = new Uri.Builder();
|
||||
builder.scheme("http").encodedAuthority(address + ':' + port)
|
||||
.appendQueryParameter("id", id)
|
||||
.appendQueryParameter("timestamp", String.valueOf(l.getTime()))
|
||||
.appendQueryParameter("lat", String.valueOf(l.getLatitude()))
|
||||
.appendQueryParameter("lon", String.valueOf(l.getLongitude()))
|
||||
.appendQueryParameter("speed", String.valueOf(l.getSpeed() * 1.943844))
|
||||
.appendQueryParameter("bearing", String.valueOf(l.getBearing()))
|
||||
.appendQueryParameter("altitude", String.valueOf(l.getAltitude()))
|
||||
.appendQueryParameter("batt", String.valueOf(battery));
|
||||
|
||||
return builder.build().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format device id message
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
package org.traccar.client;
|
||||
|
||||
import android.location.Location;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricGradleTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(RobolectricGradleTestRunner.class)
|
||||
@Config(constants = BuildConfig.class, sdk = 21)
|
||||
public class ProtocolTest {
|
||||
|
||||
@Test
|
||||
public void testFormatRequest() throws Exception {
|
||||
|
||||
Location location = new Location("gps");
|
||||
location.setTime(0);
|
||||
|
||||
String url = Protocol.formatRequest("localhost", 5055, "123456789012345", location, 0);
|
||||
|
||||
assertEquals("http://localhost:5055?id=123456789012345×tamp=0&lat=0.0&lon=0.0&speed=0.0&bearing=0.0&altitude=0.0&batt=0.0", url);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Ссылка в новой задаче
Block a user