@@ -12,13 +12,14 @@ import android.os.Bundle
1212import android.view.View
1313import android.widget.EditText
1414import 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
1816import org.java_websocket.client.WebSocketClient
1917import org.java_websocket.handshake.ServerHandshake
2018import java.lang.Exception
2119import java.net.URI
20+ import com.beust.klaxon.*
21+
22+ private val klaxon = Klaxon ()
2223
2324class 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+ }
0 commit comments