Skip to content

Commit 47bdae9

Browse files
committed
Added ack, countdown and location support
1 parent 60669fd commit 47bdae9

File tree

3 files changed

+116
-73
lines changed

3 files changed

+116
-73
lines changed

app/build.gradle

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,12 @@ dependencies {
3737
implementation 'androidx.appcompat:appcompat:1.4.1'
3838
implementation 'com.google.android.material:material:1.5.0'
3939
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
40-
implementation 'com.google.code.gson:gson:2.10.1'
40+
implementation 'com.beust:klaxon:5.5'
4141
testImplementation 'junit:junit:4.13.2'
4242
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
4343
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
4444

4545
// WebSocket
4646
implementation "org.java-websocket:Java-WebSocket:1.5.3"
4747

48-
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
49-
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
50-
51-
// Moshi
52-
implementation 'com.squareup.moshi:moshi:1.9.2'
53-
5448
}

app/src/main/java/com/atom/letmein/MainActivity.kt

Lines changed: 110 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import android.os.Bundle
1212
import android.view.View
1313
import android.widget.EditText
1414
import androidx.appcompat.app.AlertDialog
15-
import com.squareup.moshi.JsonAdapter
16-
import com.squareup.moshi.JsonClass
17-
import com.squareup.moshi.Moshi
15+
import com.beust.klaxon.Json
1816
import org.java_websocket.client.WebSocketClient
1917
import org.java_websocket.handshake.ServerHandshake
2018
import java.lang.Exception
2119
import java.net.URI
20+
import com.beust.klaxon.*
21+
22+
private val klaxon = Klaxon()
2223

2324
class MainActivity : AppCompatActivity() {
2425
lateinit var notificationManager: NotificationManager
@@ -38,15 +39,28 @@ class MainActivity : AppCompatActivity() {
3839
}
3940

4041
private fun initWebSocket(location: String) {
41-
//val hostString = R.string.wsURI
42-
val wsURL = "wss://letmein.csh.rit.edu/knock/socket/$location"
42+
val hostString = getString(R.string.wsURI)
43+
val wsURL = "wss://$hostString/knock/socket/$location"
4344
val wsURI: URI? = URI(wsURL)
4445

46+
if (findViewById<EditText>(R.id.userName).text.isEmpty()) {
47+
showAlert("Error", "Missing name!")
48+
return
49+
}
50+
if (this::webSocketClient.isInitialized) {
51+
if (webSocketClient.isOpen) {
52+
showAlert("Error", "There is already a request running!")
53+
return
54+
}
55+
}
56+
4557
createWebSocketClient(wsURI, location)
58+
webSocketClient.connect()
4659
}
4760

4861
private fun createWebSocketClient(wsURI: URI?, location: String) {
4962
webSocketClient = object : WebSocketClient(wsURI) {
63+
var idWS: Int = 0
5064
override fun onOpen(handshakedata: ServerHandshake?) {
5165
println("Connection opened!")
5266
val name = findViewById<EditText>(R.id.userName).text
@@ -57,36 +71,50 @@ class MainActivity : AppCompatActivity() {
5771

5872
override fun onMessage(message: String?) {
5973
message?.let {
60-
val moshi = Moshi.Builder().build()
61-
val adapter: JsonAdapter<ServerResponse> = moshi.adapter(ServerResponse::class.java)
62-
val response = adapter.fromJson(message)
63-
when (response?.event) {
64-
"LOCATION" -> println("LOCATION")
65-
"COUNTDOWN" -> println("COUNTDOWN")
66-
"ACKNOWLEDGE" -> println("ACKNOWLEDGE")
67-
"TIMEOUT" -> println("TIMEOUT")
68-
else -> println("Unknown message received...")
74+
val response = ServerResponse.fromJson(message)
75+
println(response?.toJson())
76+
val id = response?.id?.split("_")?.last()?.toInt()
77+
id?.let {
78+
when (response?.event) {
79+
"LOCATION" -> {
80+
if (idWS == 0) {
81+
idWS = it
82+
sendPermanentPush("LetMeIn", "${getString(R.string.req_sent)} (45s)", it)
83+
}
84+
}
85+
"COUNTDOWN" -> updatePush("${getString(R.string.req_sent)} (${response?.currentTime}s)", it)
86+
"TIMEOUT" -> {
87+
sendTemporaryPush("LetMeIn", getString(R.string.req_timeout))
88+
this.close()
89+
}
90+
"ACKNOWLEDGE" -> {
91+
sendTemporaryPush("LetMeIn", getString(R.string.req_ack))
92+
this.close()
93+
}
94+
}
6995
}
7096
}
7197
}
7298

7399
override fun onClose(code: Int, reason: String?, remote: Boolean) {
74100
println("Connection was closed! Reason: $reason")
101+
notificationManager.cancel(idWS)
102+
sendTemporaryPush("Error", getString(R.string.req_error))
75103
}
76104

77105
override fun onError(ex: Exception?) {
78106
println("Connection ran into an error: ${ex.toString()}")
107+
ex?.printStackTrace()
108+
notificationManager.cancel(idWS)
79109
}
80110
}
81111
}
82112

83-
fun sendPush(title: String, description: String) {
113+
fun sendPermanentPush(title: String, description: String, code: Int = 2023) {
84114
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
85115
notificationChannel =
86116
NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
87-
notificationChannel.enableLights(true)
88117
notificationChannel.lightColor = Color.MAGENTA
89-
notificationChannel.enableVibration(true)
90118
notificationManager.createNotificationChannel(notificationChannel)
91119

92120
builder = Notification.Builder(this, channelId)
@@ -96,9 +124,37 @@ class MainActivity : AppCompatActivity() {
96124
builder.setSmallIcon(R.drawable.csh_logo)
97125
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.csh_logo))
98126
.setContentText(description)
99-
notificationManager.notify(1234, builder.build())
127+
.setContentTitle(title)
128+
.setAutoCancel(false)
129+
.setOngoing(true)
130+
.setOnlyAlertOnce(true)
131+
notificationManager.notify(code, builder.build())
100132
}
101133

134+
fun sendTemporaryPush(title: String, description: String, code: Int = 2023) {
135+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
136+
notificationChannel =
137+
NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
138+
notificationChannel.lightColor = Color.MAGENTA
139+
notificationManager.createNotificationChannel(notificationChannel)
140+
141+
builder = Notification.Builder(this, channelId)
142+
} else {
143+
builder = Notification.Builder(this)
144+
}
145+
builder.setSmallIcon(R.drawable.csh_logo)
146+
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.csh_logo))
147+
.setContentText(description)
148+
.setContentTitle(title)
149+
notificationManager.notify(code, builder.build())
150+
}
151+
152+
fun updatePush(description: String, code: Int = 2023) {
153+
builder.setContentText(description)
154+
notificationManager.notify(code, builder.build())
155+
}
156+
157+
102158
fun showAlert(title: String, description: String) {
103159
val builder = AlertDialog.Builder(this)
104160
builder.setTitle(title)
@@ -112,70 +168,59 @@ class MainActivity : AppCompatActivity() {
112168

113169
/** button events **/
114170
fun reqLWell(view: View) {
115-
if (findViewById<EditText>(R.id.userName).text.isEmpty()) {
116-
showAlert("Error", "Missing name!")
117-
return
118-
} else if (webSocketClient.isOpen) {
119-
showAlert("Error", "Please wait before putting in another request!")
120-
return
121-
}
122-
sendPush("LetMeIn", "Your request was sent!")
123171
initWebSocket("l_well")
124-
webSocketClient.connect()
125172
}
126173

127174
fun reqFirstElevator(view: View) {
128-
if (findViewById<EditText>(R.id.userName).text.isEmpty()) {
129-
showAlert("Error", "Missing name!")
130-
return
131-
} else if (webSocketClient.isOpen) {
132-
showAlert("Error", "Please wait before putting in another request!")
133-
return
134-
}
135-
sendPush("LetMeIn", "Your request was sent!")
136175
initWebSocket("level_1")
137-
webSocketClient.connect()
138176
}
139177

140178
fun reqAElevator(view: View) {
141-
if (findViewById<EditText>(R.id.userName).text.isEmpty()) {
142-
showAlert("Error", "Missing name!")
143-
return
144-
} else if (webSocketClient.isOpen) {
145-
showAlert("Error", "Please wait before putting in another request!")
146-
return
147-
}
148-
sendPush("LetMeIn", "Your request was sent!")
149179
initWebSocket("level_a")
150-
webSocketClient.connect()
151180
}
152181

153182
fun reqNorthStairwell(view: View) {
154-
if (findViewById<EditText>(R.id.userName).text.isEmpty()) {
155-
showAlert("Error", "Missing name!")
156-
return
157-
} else if (webSocketClient.isOpen) {
158-
showAlert("Error", "Please wait before putting in another request!")
159-
return
160-
}
161-
sendPush("LetMeIn", "Your request was sent!")
162183
initWebSocket("n_stairs")
163-
webSocketClient.connect()
164184
}
165185

166186
fun reqSouthStairwell(view: View) {
167-
if (findViewById<EditText>(R.id.userName).text.isEmpty()) {
168-
showAlert("Error", "Missing name!")
169-
return
170-
} else if (webSocketClient.isOpen) {
171-
showAlert("Error", "Please wait before putting in another request!")
172-
return
173-
}
174-
sendPush("LetMeIn", "Your request was sent!")
175187
initWebSocket("s_stairs")
176-
webSocketClient.connect()
177188
}
178189
}
179190

180-
@JsonClass(generateAdapter = true)
181-
data class ServerResponse(val event: String?, val name: String?, val id: String?)
191+
192+
data class ServerResponse (
193+
@Json(name = "ID")
194+
val id: String,
195+
196+
@Json(name = "Event")
197+
val event: String,
198+
199+
@Json(name = "CurrentTime")
200+
val currentTime: Long,
201+
202+
@Json(name = "MaxTime")
203+
val maxTime: Long,
204+
205+
@Json(name = "Name")
206+
val name: String,
207+
208+
@Json(name = "Location")
209+
val location: String,
210+
211+
@Json(name = "ShortLocation")
212+
val shortLocation: String,
213+
214+
@Json(name = "ClientLocation")
215+
val clientLocation: String,
216+
217+
@Json(name = "SlackMessageTS")
218+
val slackMessageTS: String
219+
)
220+
{
221+
public fun toJson() = klaxon.toJsonString(this)
222+
223+
companion object {
224+
public fun fromJson(json: String) = klaxon.parse<ServerResponse>(json)
225+
}
226+
}

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@
1010
<string name="first_elevator">Level 1 Elevator Lobby</string>
1111
<string name="l_well">L Well</string>
1212

13-
<string name="wsURI">letmein.csh.rit.edu</string>
13+
<string name="wsURI">letmein2-dev.cs.house</string>
14+
<string name="req_sent">Your request has been sent!</string>
15+
<string name="req_timeout">Your request timed out unfortunately. Please try again later.</string>
16+
<string name="req_ack">Someone answered your request! They will be arriving shortly.</string>
17+
<string name="req_error">Unfortunately, there was an error in your request. Please try again later.</string>
1418
</resources>

0 commit comments

Comments
 (0)