-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I60 Connected Android to server (#74)
* Removed firebase folder * Removed firebase functions deployment from travis.yaml * Random IntelliJ changes * Made initial hello world server * Added more packages * Minor change to README * Minor change to README * Basic implementation of a CRUD server with mongodb * Added Bonjour Publisher and Client * added bonjour * Robot comms teset * WIP makefile * Re-added package-lock.json * Robot turns on when button pressed * Android can now find server via zeroconf * Added test mode for when disconnected from robot * Refactored ServerConnection * Updated server name in android * Added turn off on * Fixed minor things * App can display list of items * Re-added addItem and getItems * Fixed app not connecting to server * Added ability to refresh list of items * Implemented adding order on android * Adding order now removes the items in it from the database
- Loading branch information
Showing
18 changed files
with
478 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
android/app/src/main/java/io/github/assis10t/bobandroid/Application.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
package io.github.assis10t.bobandroid | ||
|
||
import android.app.Application | ||
import timber.log.Timber | ||
|
||
class Application: Application() { | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
ServerConnection.initialize() | ||
|
||
//From: https://github.com/oktay-sen/Coinz | ||
Timber.plant(object : Timber.DebugTree() { | ||
override fun createStackElementTag(element: StackTraceElement): String? { | ||
return super.createStackElementTag(element) + ':' + element.lineNumber | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 111 additions & 23 deletions
134
android/app/src/main/java/io/github/assis10t/bobandroid/ServerConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,148 @@ | ||
package io.github.assis10t.bobandroid | ||
|
||
import android.os.AsyncTask | ||
import android.util.Log | ||
import java.net.Inet4Address | ||
import com.google.gson.Gson | ||
import io.github.assis10t.bobandroid.pojo.GetItemsResponse | ||
import io.github.assis10t.bobandroid.pojo.Item | ||
import io.github.assis10t.bobandroid.pojo.Order | ||
import okhttp3.MediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.RequestBody | ||
import org.jetbrains.anko.doAsync | ||
import org.jetbrains.anko.uiThread | ||
import timber.log.Timber | ||
import java.io.IOException | ||
import java.util.concurrent.TimeUnit | ||
import javax.jmdns.JmDNS | ||
import javax.jmdns.ServiceEvent | ||
import javax.jmdns.ServiceListener | ||
|
||
class ServerConnection { | ||
|
||
companion object { | ||
private val TAG = "ServerConnection" | ||
val SERVER_NAME = "assis10t" | ||
var serverIp: String? = null | ||
var serverAddress: String? = null | ||
val httpClient: OkHttpClient = OkHttpClient() | ||
|
||
val onConnectedListeners: MutableList<(String) -> Unit> = mutableListOf() | ||
val onConnectedListeners: MutableList<(serverAddress: String) -> Unit> = mutableListOf() | ||
|
||
class ConnectTask: AsyncTask<Unit, JmDNS, Unit>() { | ||
override fun doInBackground(vararg params: Unit?) { | ||
Log.d(TAG, "Discovery started") | ||
fun initialize() { | ||
doAsync { | ||
Timber.d("Discovery started") | ||
val mJmDNS = JmDNS.create() | ||
mJmDNS.addServiceListener("_http._tcp.local.", object : ServiceListener { | ||
|
||
override fun serviceResolved(event: ServiceEvent?) { | ||
val info = mJmDNS.getServiceInfo(event!!.type, event.name) | ||
Log.d(TAG, "Service resolved: $info") | ||
Timber.d("Service resolved: $info") | ||
if (info.name.contains(SERVER_NAME)) { | ||
serverIp = "${info.inet4Addresses[0]!!.hostAddress}:${info.port}" | ||
onConnectedListeners.forEach { it(serverIp!!) } | ||
onConnectedListeners.clear() | ||
uiThread { | ||
serverAddress = "http:https://${info.inet4Addresses[0]!!.hostAddress}:${info.port}" | ||
onConnectedListeners.forEach { it(serverAddress!!) } | ||
onConnectedListeners.clear() | ||
} | ||
} | ||
} | ||
|
||
override fun serviceRemoved(event: ServiceEvent?) { | ||
Log.d(TAG, "Service removed") | ||
Timber.d("Service removed") | ||
} | ||
|
||
override fun serviceAdded(event: ServiceEvent?) { | ||
val info = mJmDNS.getServiceInfo(event!!.type, event.name) | ||
Log.d(TAG, "Service added: $info") | ||
Timber.d("Service added: $info") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
fun initialize() { | ||
ConnectTask().execute() | ||
ServerConnection().connect { ip -> | ||
Log.d(TAG, "Server found at $ip") | ||
Timber.d("Server found at $ip") | ||
} | ||
} | ||
} | ||
|
||
fun connect(onConnected: (String) -> Unit) { | ||
if (serverIp != null) | ||
onConnected(serverIp!!) | ||
if (serverAddress != null) | ||
onConnected(serverAddress!!) | ||
else | ||
onConnectedListeners.add(onConnected) | ||
} | ||
|
||
val getRequestFactory = { http: OkHttpClient -> | ||
{ url: String, onGetComplete: (success: Boolean, response: String?) -> Unit -> | ||
doAsync { | ||
Timber.d("Get request to $url") | ||
try { | ||
val request = Request.Builder().url(url).build() | ||
val response = http.newCall(request).execute() | ||
Timber.d("Response received.") | ||
if (!response.isSuccessful) { | ||
Timber.e("Get Request failed: (${response.code()}) ${response.body().toString()}") | ||
uiThread { onGetComplete(false, null) } | ||
} else { | ||
uiThread { onGetComplete(true, response.body()?.string()) } | ||
} | ||
} catch (e: IOException) { | ||
Timber.e(e, "Get Request failed") | ||
uiThread { onGetComplete(false, null) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
val postRequestFactory = { http: OkHttpClient, gson: Gson -> | ||
{ url: String, body: Any, onPostComplete: (success: Boolean, response: String?) -> Unit -> | ||
doAsync { | ||
Timber.d("Post request to $url") | ||
try { | ||
val JSON = MediaType.get("application/json; charset=utf-8") | ||
val requestBody = RequestBody.create(JSON, gson.toJson(body)) | ||
val request = Request.Builder().url(url).post(requestBody).build() | ||
val response = http.newCall(request).execute() | ||
Timber.d("Response received.") | ||
if (!response.isSuccessful) { | ||
Timber.e("Post Request failed: (${response.code()}) ${response.body().toString()}") | ||
uiThread { onPostComplete(false, null) } | ||
} else { | ||
uiThread { onPostComplete(true, response.body()?.string()) } | ||
} | ||
} catch (e: IOException) { | ||
Timber.e(e, "Post Request failed") | ||
uiThread { onPostComplete(false, null) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
val getItemsFactory = { http: OkHttpClient, gson: Gson -> | ||
{ onGetItems: (success: Boolean, items: List<Item>?) -> Unit -> | ||
connect { server -> | ||
getRequestFactory(http)("$server/items") { success, str -> | ||
Timber.d("Result: $success, response: $str") | ||
if (!success) { | ||
onGetItems(success, null) | ||
} else { | ||
val response = gson.fromJson(str!!, GetItemsResponse::class.java) | ||
onGetItems(response.success, response.items) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
val getItems = getItemsFactory(httpClient, Gson()) | ||
|
||
val makeOrderFactory = { http: OkHttpClient, gson: Gson -> | ||
{ order: Order, onOrderComplete: ((success: Boolean) -> Unit)? -> | ||
connect { server -> | ||
postRequestFactory(http, gson)("$server/order", order) { success, str -> | ||
Timber.d("Result: $success, response: $str") | ||
if (!success) { | ||
onOrderComplete?.invoke(success) | ||
} else { | ||
val response = gson.fromJson(str!!, GetItemsResponse::class.java) | ||
onOrderComplete?.invoke(response.success) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
val makeOrder = makeOrderFactory(httpClient, Gson()) | ||
} |
3 changes: 3 additions & 0 deletions
3
android/app/src/main/java/io/github/assis10t/bobandroid/pojo/GetItemsResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package io.github.assis10t.bobandroid.pojo | ||
|
||
class GetItemsResponse(val success: Boolean = false, val items:List<Item> = listOf()) |
6 changes: 6 additions & 0 deletions
6
android/app/src/main/java/io/github/assis10t/bobandroid/pojo/Item.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.assis10t.bobandroid.pojo | ||
|
||
class Item ( | ||
val _id:String? = null, | ||
val name:String? = null | ||
) |
6 changes: 6 additions & 0 deletions
6
android/app/src/main/java/io/github/assis10t/bobandroid/pojo/Order.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.assis10t.bobandroid.pojo | ||
|
||
class Order( | ||
val _id: String? = null, | ||
val items: List<Item> = listOf() | ||
) |
7 changes: 7 additions & 0 deletions
7
android/app/src/main/java/io/github/assis10t/bobandroid/utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.github.assis10t.bobandroid | ||
|
||
import android.content.Context | ||
import android.util.TypedValue | ||
|
||
fun dp(context: Context, dp: Float) = | ||
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http:https://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z"/> | ||
</vector> |
Oops, something went wrong.