Skip to content

Commit

Permalink
Added awesome RestfulAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander James Wallar committed Jan 20, 2014
1 parent 02e8172 commit 4080336
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To make use of this API, the Locaudio server and the RethinkDB database must be
y: <Float: Y position>,
spl: <Float: Sound pressure level>,
timestamp: <Float: Unix time in seconds>,
fingerprint: [<Float>: Audio fingerprint]
fingerprint: [<Int>: Audio fingerprint]
}

#### Return structure
Expand All @@ -36,7 +36,7 @@ To make use of this API, the Locaudio server and the RethinkDB database must be

### Getting positions of sounds

GET /positions/:sound_name
GET /location/:sound_name


#### Parameters
Expand Down
1 change: 1 addition & 0 deletions app/Locabean/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
Expand Down
Binary file added app/Locabean/libs/gson-2.2.4.jar
Binary file not shown.
11 changes: 11 additions & 0 deletions app/Locabean/src/com/locaudio/api/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.locaudio.api;

import com.google.gson.annotations.SerializedName;

public class Location {
@SerializedName("position")
public Point position;

@SerializedName("confidence")
public float confidence;
}
113 changes: 113 additions & 0 deletions app/Locabean/src/com/locaudio/api/Locaudio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.locaudio.api;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import org.apache.http.client.ClientProtocolException;

import android.os.AsyncTask;

import com.locaudio.net.Requests;

public class Locaudio extends Requests {
private static final String NAMES_ROUTE = "names";
private static final String NOTIFY_ROUTE = "notify";
private static final String LOCATIONS_ROUTE = "locations";

public Locaudio(String ipAddress, int port) {
super(ipAddress, port);
}

public Location[] getSoundLocations(final String soundName) {

AsyncTask<Requests, Integer, Location[]> task = new AsyncTask<Requests, Integer, Location[]>() {

@Override
protected Location[] doInBackground(Requests... reqs) {
try {
return reqs[0].get(Location[].class, LOCATIONS_ROUTE,
soundName);
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

};

try {
task.execute(this);
return task.get();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} catch (ExecutionException e) {
e.printStackTrace();
return null;
}
}

public String[] getNames() throws ClientProtocolException, IOException {
AsyncTask<Requests, Integer, String[]> task = new AsyncTask<Requests, Integer, String[]>() {

@Override
protected String[] doInBackground(Requests... reqs) {
try {
return reqs[0].get(String[].class, NAMES_ROUTE);
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

};

try {
task.execute(this);
return task.get();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} catch (ExecutionException e) {
e.printStackTrace();
return null;
}
}

public NotifyResponse notifyEvent(final NotifyForm event)
throws ClientProtocolException, IOException {
AsyncTask<Requests, Integer, NotifyResponse> task = new AsyncTask<Requests, Integer, NotifyResponse>() {

@Override
protected NotifyResponse doInBackground(Requests... reqs) {
try {
return reqs[0].post(NotifyResponse.class, event.toMap(), NOTIFY_ROUTE);
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

};

try {
task.execute(this);
return task.get();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} catch (ExecutionException e) {
e.printStackTrace();
return null;
}
}
}
62 changes: 62 additions & 0 deletions app/Locabean/src/com/locaudio/api/NotifyForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.locaudio.api;

import java.util.HashMap;
import java.util.Map;

public class NotifyForm {

private float x;
private float y;
private float soundPressureLevel;
private float timestamp;
private byte[] fingerprint;

public NotifyForm() {

}

public NotifyForm(float x, float y, float soundPressureLevel,
float timestamp, byte[] fingerprint) {
this.x = x;
this.y = y;
this.soundPressureLevel = soundPressureLevel;
this.timestamp = timestamp;
this.fingerprint = fingerprint;
}

public Map<String, ?> toMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x", this.x);
map.put("y", this.y);
map.put("spl", this.soundPressureLevel);
map.put("timestamp", this.timestamp);
map.put("fingerprint", this.fingerprint);
return map;
}

public NotifyForm setX(float x) {
this.x = x;
return this;
}

public NotifyForm setY(float y) {
this.y = y;
return this;
}

public NotifyForm setSoundPressureLevel(float soundPressureLevel) {
this.soundPressureLevel = soundPressureLevel;
return this;
}

public NotifyForm setTimestamp(float timestamp) {
this.timestamp = timestamp;
return this;
}

public NotifyForm setFingerprint(byte[] fingerprint) {
this.fingerprint = fingerprint;
return this;
}

}
14 changes: 14 additions & 0 deletions app/Locabean/src/com/locaudio/api/NotifyResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.locaudio.api;

import com.google.gson.annotations.SerializedName;

public class NotifyResponse {
@SerializedName("error")
public int error;

@SerializedName("message")
public String message;

@SerializedName("name")
public String name;
}
12 changes: 12 additions & 0 deletions app/Locabean/src/com/locaudio/api/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.locaudio.api;

import com.google.gson.annotations.SerializedName;

public class Point {

@SerializedName("x")
public float x;

@SerializedName("y")
public float y;
}
6 changes: 6 additions & 0 deletions app/Locabean/src/com/locaudio/locabean/NodeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import com.musicg.wave.Wave;

import com.locaudio.io.WaveWriter;
import com.locaudio.api.Locaudio;

public class NodeActivity extends Activity {

private AudioRecord recorder = null;
private Thread recordingThread = null;
private boolean isRecording = false;
private TextView fingerprintTextView = null;
private Locaudio locaudio = null;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -30,6 +32,8 @@ public void onCreate(Bundle savedInstanceState) {
enableButtons(false);
fingerprintTextView = (TextView) findViewById(R.id.fingerprintText);
fingerprintTextView.setMovementMethod(new ScrollingMovementMethod());

locaudio = new Locaudio("192.168.1.9", 8000);
}

private void setButtonHandlers() {
Expand Down Expand Up @@ -105,6 +109,8 @@ public void onClick(View v) {
Wave wave = new Wave(WaveWriter.getFilename());
fingerprintTextView.setText(Arrays.toString(wave
.getFingerprint()));

System.out.println(locaudio.getSoundLocations("Cock")[0].position.x);
break;
}
}
Expand Down
68 changes: 47 additions & 21 deletions app/Locabean/src/com/locaudio/net/Requests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -18,66 +17,93 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import com.google.gson.Gson;

public class Requests {

private String ipAddress = null;
private int port = 80;
private String url = null;
private static final int CHAR_BUFFER_SIZE = 256;

public Requests() {
this.ipAddress = "localhost";
this.port = 8000;
this.url = this.ipAddress + ":" + this.port;
this.url = "http://" + this.ipAddress + ":" + this.port;
}

public Requests(String ipAddress, int port) {
this.ipAddress = ipAddress;
this.port = port;
this.url = this.ipAddress + ":" + this.port;
this.url = "http://" + this.ipAddress + ":" + this.port;
}

public JSONObject post(Map<String, String> paramMap)
throws ClientProtocolException, IOException, JSONException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(this.url);
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> T post(Class<T> classType, Map<String, ?> paramMap,
String... urlParams) throws ClientProtocolException, IOException {

String requestUrl = this.concatWithUrl(urlParams);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(requestUrl);

// Request parameters and other properties.
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();

Iterator<Entry<String, String>> it = paramMap.entrySet().iterator();
Iterator<?> it = paramMap.entrySet().iterator();
Entry<String, String> pair = null;

while (it.hasNext()) {
pair = (Entry<String, String>) it.next();
pair = (Entry) it.next();
params.add(new BasicNameValuePair(pair.getKey(), pair.getValue()));
}

httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

// Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();

return entityToObject(entity, classType);
}

public <T> T get(Class<T> classType, String... urlParams)
throws ClientProtocolException, IOException {

String requestUrl = this.concatWithUrl(urlParams);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(requestUrl);
HttpContext localContext = new BasicHttpContext();

HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();

return entityToObject(entity, classType);
}

protected static <T> T entityToObject(HttpEntity entity, Class<T> classType)
throws IllegalStateException, IOException {
if (entity != null) {
InputStreamReader reader = new InputStreamReader(
entity.getContent());
try {
CharBuffer target = CharBuffer.allocate(CHAR_BUFFER_SIZE);
reader.read(target);
return new JSONObject(target.toString());
Gson gson = new Gson();
return gson.fromJson(reader, classType);
} finally {
reader.close();
}
} else {
return new JSONObject();
return null;
}
}

public JSONObject get(String... params) {
return new JSONObject();

protected String concatWithUrl(String... urlParams) {
String retString = this.url;
for (String urlParam : urlParams) {
retString += "/" + urlParam;
}
return retString;
}

}
Loading

0 comments on commit 4080336

Please sign in to comment.