diff --git a/README.md b/README.md index 0b51d3f..efcab68 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ To make use of this API, the Locaudio server and the RethinkDB database must be y: , spl: , timestamp: , - fingerprint: [: Audio fingerprint] + fingerprint: [: Audio fingerprint] } #### Return structure @@ -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 diff --git a/app/Locabean/AndroidManifest.xml b/app/Locabean/AndroidManifest.xml index d135b9d..6546432 100644 --- a/app/Locabean/AndroidManifest.xml +++ b/app/Locabean/AndroidManifest.xml @@ -9,6 +9,7 @@ android:targetSdkVersion="17" /> + task = new AsyncTask() { + + @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 task = new AsyncTask() { + + @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 task = new AsyncTask() { + + @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; + } + } +} diff --git a/app/Locabean/src/com/locaudio/api/NotifyForm.java b/app/Locabean/src/com/locaudio/api/NotifyForm.java new file mode 100644 index 0000000..94cbafe --- /dev/null +++ b/app/Locabean/src/com/locaudio/api/NotifyForm.java @@ -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 toMap() { + Map map = new HashMap(); + 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; + } + +} diff --git a/app/Locabean/src/com/locaudio/api/NotifyResponse.java b/app/Locabean/src/com/locaudio/api/NotifyResponse.java new file mode 100644 index 0000000..843e9ca --- /dev/null +++ b/app/Locabean/src/com/locaudio/api/NotifyResponse.java @@ -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; +} diff --git a/app/Locabean/src/com/locaudio/api/Point.java b/app/Locabean/src/com/locaudio/api/Point.java new file mode 100644 index 0000000..acaca26 --- /dev/null +++ b/app/Locabean/src/com/locaudio/api/Point.java @@ -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; +} diff --git a/app/Locabean/src/com/locaudio/locabean/NodeActivity.java b/app/Locabean/src/com/locaudio/locabean/NodeActivity.java index 946e05c..48a4dc3 100644 --- a/app/Locabean/src/com/locaudio/locabean/NodeActivity.java +++ b/app/Locabean/src/com/locaudio/locabean/NodeActivity.java @@ -13,6 +13,7 @@ import com.musicg.wave.Wave; import com.locaudio.io.WaveWriter; +import com.locaudio.api.Locaudio; public class NodeActivity extends Activity { @@ -20,6 +21,7 @@ public class NodeActivity extends Activity { private Thread recordingThread = null; private boolean isRecording = false; private TextView fingerprintTextView = null; + private Locaudio locaudio = null; @Override public void onCreate(Bundle savedInstanceState) { @@ -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() { @@ -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; } } diff --git a/app/Locabean/src/com/locaudio/net/Requests.java b/app/Locabean/src/com/locaudio/net/Requests.java index 91b3001..6478dd3 100644 --- a/app/Locabean/src/com/locaudio/net/Requests.java +++ b/app/Locabean/src/com/locaudio/net/Requests.java @@ -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; @@ -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 paramMap) - throws ClientProtocolException, IOException, JSONException { - HttpClient httpclient = new DefaultHttpClient(); - HttpPost httppost = new HttpPost(this.url); + @SuppressWarnings({ "rawtypes", "unchecked" }) + public T post(Class classType, Map 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 params = new ArrayList(); - Iterator> it = paramMap.entrySet().iterator(); + Iterator it = paramMap.entrySet().iterator(); Entry pair = null; while (it.hasNext()) { - pair = (Entry) 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 get(Class 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 entityToObject(HttpEntity entity, Class 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; } + } diff --git a/locaudio/api.py b/locaudio/api.py index 89c1b8b..543968d 100644 --- a/locaudio/api.py +++ b/locaudio/api.py @@ -11,7 +11,7 @@ class Locaudio: def __init__(self, host, port): self.url = "http://" + host + ":" + str(port) - self.pos_url = self.url + "/positions" + self.pos_url = self.url + "/locations" self.notify_url = self.url + "/notify" self.names_url = self.url + "/names" @@ -20,7 +20,7 @@ def make_position_url(self, sound_name): return self.pos_url + "/" + sound_name - def get_sound_positions(self, sound_name): + def get_sound_locations(self, sound_name): req = urllib2.urlopen(self.make_position_url(sound_name)) location_list = json.loads(req.read()) ret_list = list() diff --git a/locaudio/detectionserver.py b/locaudio/detectionserver.py index d0f9bb0..1aacad6 100644 --- a/locaudio/detectionserver.py +++ b/locaudio/detectionserver.py @@ -76,7 +76,7 @@ def post_notify(): return jsonify(error=0, message="No error", name=sound_name) -@config.app.route("/positions/", methods=["GET"]) +@config.app.route("/locations/", methods=["GET"]) def get_sound_positions(sound_name): """ diff --git a/tests/test_server.py b/tests/test_server.py index fec2bb3..71c02f0 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -13,7 +13,7 @@ import locaudio.api as api -server_addr = "localhost" +server_addr = "192.168.1.9" server_port = 8000 test_sound_name = "Cock" @@ -64,7 +64,7 @@ def test_server_notify(self): def test_server_triangulation(self): - pos_list = loc.get_sound_positions(test_sound_name) + pos_list = loc.get_sound_locations(test_sound_name) print "\n=== Server Triangulation === :: {0}\n".format(pos_list)