Skip to content

Commit

Permalink
Added server
Browse files Browse the repository at this point in the history
  • Loading branch information
Deliner committed Sep 6, 2020
1 parent d65018c commit abaa782
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ android {
versionName "1.0"
}

packagingOptions {
exclude 'META-INF/kotlinx-coroutines-core.kotlin_module'
}

buildTypes {
release {
minifyEnabled false
Expand All @@ -29,6 +33,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'

implementation 'io.ktor:ktor-client-android:1.3.1'

implementation 'com.google.maps.android:android-maps-utils:2.0.3'
implementation 'com.google.android.gms:play-services-maps:17.0.0'

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".system.MosFaunaApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/FaunaTheme">
Expand Down
81 changes: 76 additions & 5 deletions app/src/main/java/com/deliner/mosfauna/fragment/GuideFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ import com.google.maps.android.clustering.Cluster
import com.google.maps.android.clustering.ClusterManager
import com.google.maps.android.clustering.view.DefaultClusterRenderer
import com.google.maps.android.ui.IconGenerator
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.runBlocking
import java.util.*
import kotlin.collections.HashSet


class GuideFragment : CommonFragment(), OnMapReadyCallback,
Expand All @@ -44,6 +50,10 @@ class GuideFragment : CommonFragment(), OnMapReadyCallback,
private var currentBirb: Bird? = null
private var clickPos: LatLng? = null

private val markerSet = HashSet<Marker>()

private val timer = Timer()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -104,7 +114,6 @@ class GuideFragment : CommonFragment(), OnMapReadyCallback,
if (currentBirb != null) {
clickPos = it
showDialogEx(DialogTags.PLACE_MARKER, bundleOf("KEY_NAME" to currentBirb!!.name))
// Toast.makeText(context, "ground", Toast.LENGTH_SHORT).show()
}
}
mClusterManager!!.setOnClusterClickListener(this)
Expand All @@ -113,9 +122,38 @@ class GuideFragment : CommonFragment(), OnMapReadyCallback,
mClusterManager!!.setOnClusterItemInfoWindowClickListener(this)
addItems()
mClusterManager!!.cluster()
timer.schedule(object : TimerTask() {
override fun run() {
activity!!.runOnUiThread {
markerSet.forEach { it.remove() }
markerSet.clear()
try {
runBlocking {
val result = HttpClient().get<String>(
host = "95.165.151.46",
port = 2378,
path = "/get_markers"
)
if (result.isNotEmpty()) {
markerSet.addAll(result.split("\n").map {
val name = decodeBirdName(it.split("_")[0])
val pos = it.split("_")[1]
val lan = pos.split("x")[0].toDouble()
val lgt = pos.split("x")[1].toDouble()
googleMap!!.addMarker(
MarkerOptions().position(LatLng(lan, lgt)).title(name)
)
})
}
}
} catch (e: Exception) {
Toast.makeText(context, "Что-то пошло не так", Toast.LENGTH_SHORT).show()
}
}
}
}, 0, 5000)
}


override fun handleServiceMessage(msg: Message) {
when (msg.what) {
CoreConst.ON_SEND_MARKER -> sendMarker(currentBirb!!, clickPos!!)
Expand All @@ -128,9 +166,44 @@ class GuideFragment : CommonFragment(), OnMapReadyCallback,
}

private fun sendMarker(currentBird: Bird, clickPos: LatLng) {
val marker = MarkerOptions().position(clickPos).title(currentBird.name)
markerSet.add(googleMap!!.addMarker(marker))

runBlocking {
val response = HttpClient().get<HttpResponse>(
host = "95.165.151.46",
port = 2378,
path = "/add_marker_${encodeBirdName(currentBird.name)}_${clickPos.latitude}x${clickPos.longitude}"
)
if (response.status.isSuccess()) {
Toast.makeText(context, "Маркер отправлен", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Ошибка отправки маркера", Toast.LENGTH_SHORT).show()
}
}

}

private fun encodeBirdName(name: String): String {
return when (name) {
"Обыкновенный гоголь" -> "1"
"Ястреб-перепелятник" -> "2"
"Ястреб-тетеревятник" -> "3"
"Серая неясыть" -> "4"
else -> "5"
}
}

private fun decodeBirdName(name: String): String {
return when (name) {
"1" -> "Обыкновенный гоголь"
"2" -> "Ястреб-перепелятник"
"3" -> "Ястреб-тетеревятник"
"4" -> "Серая неясыть"
else -> "Ошибка"
}
}

override fun onCreateDialogEx(tag: String, args: Bundle?): CommonDialogFragment {
return when (tag) {
DialogTags.PLACE_MARKER -> PlaceMarkerDialog.getInstance(args!!.getString("KEY_NAME")!!)
Expand Down Expand Up @@ -167,7 +240,6 @@ class GuideFragment : CommonFragment(), OnMapReadyCallback,
intent.putExtra("KEY_BIRD", item.name)
intent.putExtra("KEY_PHOTO", item.photo)
startActivity(intent)
// Toast.makeText(context, "info", Toast.LENGTH_SHORT).show()
}

private fun addItems() {
Expand Down Expand Up @@ -269,8 +341,7 @@ class GuideFragment : CommonFragment(), OnMapReadyCallback,
mIconGenerator.setContentView(mImageView)
}
}



companion object {
private val handler = StaticHandler()
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">95.165.151.46</domain>
</domain-config>
</network-security-config>

0 comments on commit abaa782

Please sign in to comment.