-
Notifications
You must be signed in to change notification settings - Fork 10
/
MainActivity.kt
131 lines (113 loc) · 5.02 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.example.myapplication
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.mqtt.MqttClientHelper
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended
import org.eclipse.paho.client.mqttv3.MqttException
import org.eclipse.paho.client.mqttv3.MqttMessage
import java.util.*
import kotlin.concurrent.schedule
class MainActivity : AppCompatActivity() {
private val mqttClient by lazy {
MqttClientHelper(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
textViewMsgPayload.movementMethod = ScrollingMovementMethod()
setMqttCallBack()
// initialize 'num msgs received' field in the view
textViewNumMsgs.text = "0"
// pub button
btnPub.setOnClickListener { view ->
var snackbarMsg : String
val topic = editTextPubTopic.text.toString().trim()
snackbarMsg = "Cannot publish to empty topic!"
if (topic.isNotEmpty()) {
snackbarMsg = try {
mqttClient.publish(topic, editTextMsgPayload.text.toString())
"Published to topic '$topic'"
} catch (ex: MqttException) {
"Error publishing to topic: $topic"
}
}
Snackbar.make(view, snackbarMsg, 300)
.setAction("Action", null).show()
}
// sub button
btnSub.setOnClickListener { view ->
var snackbarMsg : String
val topic = editTextSubTopic.text.toString().trim()
snackbarMsg = "Cannot subscribe to empty topic!"
if (topic.isNotEmpty()) {
snackbarMsg = try {
mqttClient.subscribe(topic)
"Subscribed to topic '$topic'"
} catch (ex: MqttException) {
"Error subscribing to topic: $topic"
}
}
Snackbar.make(view, snackbarMsg, Snackbar.LENGTH_SHORT)
.setAction("Action", null).show()
}
Timer("CheckMqttConnection", false).schedule(3000) {
if (!mqttClient.isConnected()) {
Snackbar.make(textViewNumMsgs, "Failed to connect to: '$SOLACE_MQTT_HOST' within 3 seconds", Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show()
}
}
}
private fun setMqttCallBack() {
mqttClient.setCallback(object : MqttCallbackExtended {
override fun connectComplete(b: Boolean, s: String) {
val snackbarMsg = "Connected to host:\n'$SOLACE_MQTT_HOST'."
Log.w("Debug", snackbarMsg)
Snackbar.make(findViewById(android.R.id.content), snackbarMsg, Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
override fun connectionLost(throwable: Throwable) {
val snackbarMsg = "Connection to host lost:\n'$SOLACE_MQTT_HOST'"
Log.w("Debug", snackbarMsg)
Snackbar.make(findViewById(android.R.id.content), snackbarMsg, Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
@Throws(Exception::class)
override fun messageArrived(topic: String, mqttMessage: MqttMessage) {
Log.w("Debug", "Message received from host '$SOLACE_MQTT_HOST': $mqttMessage")
textViewNumMsgs.text = ("${textViewNumMsgs.text.toString().toInt() + 1}")
val str: String = "------------"+ Calendar.getInstance().time +"-------------\n$mqttMessage\n${textViewMsgPayload.text}"
textViewMsgPayload.text = str
}
override fun deliveryComplete(iMqttDeliveryToken: IMqttDeliveryToken) {
Log.w("Debug", "Message published to host '$SOLACE_MQTT_HOST'")
}
})
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
override fun onDestroy() {
mqttClient.destroy()
super.onDestroy()
}
}