@@ -4,6 +4,7 @@ package com.thanksmister.iot.mqtt.alarmpanel.network
4
4
import android.R.id.message
5
5
import android.content.Context
6
6
import android.text.TextUtils
7
+ import com.thanksmister.iot.mqtt.alarmpanel.R
7
8
import com.thanksmister.iot.mqtt.alarmpanel.utils.MqttUtils
8
9
import com.thanksmister.iot.mqtt.alarmpanel.utils.StringUtils
9
10
import org.eclipse.paho.android.service.MqttAndroidClient
@@ -55,6 +56,7 @@ class MQTTService(private var context: Context, options: MQTTOptions,
55
56
56
57
@Throws(MqttException ::class )
57
58
override fun close () {
59
+ Timber .d(" close" )
58
60
if (mqttClient != null ) {
59
61
// TODO IllegalArgumentException: Invalid ClientHandle and no dialog showing sound stuck
60
62
mqttClient?.setCallback(null )
@@ -111,8 +113,6 @@ class MQTTService(private var context: Context, options: MQTTOptions,
111
113
*/
112
114
private fun initialize (options : MQTTOptions ) {
113
115
Timber .d(" initialize" )
114
- if (options == null ) return ;
115
-
116
116
try {
117
117
mqttOptions = options
118
118
Timber .i(" Service Configuration:" )
@@ -133,15 +133,18 @@ class MQTTService(private var context: Context, options: MQTTOptions,
133
133
}
134
134
} catch (e: MqttException ) {
135
135
if (listener != null ) {
136
- listener!! .handleMqttException(" Could not initialize MQTT: " + e.message)
136
+ // listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
137
+ listener!! .handleMqttException(context.getString(R .string.error_mqtt_connection))
137
138
}
138
139
} catch (e: IOException ) {
139
140
if (listener != null ) {
140
- listener!! .handleMqttException(" Could not initialize MQTT: " + e.message)
141
+ // listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
142
+ listener!! .handleMqttException(context.getString(R .string.error_mqtt_connection))
141
143
}
142
144
} catch (e: GeneralSecurityException ) {
143
145
if (listener != null ) {
144
- listener!! .handleMqttException(" Could not initialize MQTT: " + e.message)
146
+ // listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
147
+ listener!! .handleMqttException(context.getString(R .string.error_mqtt_connection))
145
148
}
146
149
}
147
150
@@ -158,13 +161,17 @@ class MQTTService(private var context: Context, options: MQTTOptions,
158
161
options.password = mqttOptions!! .getPassword()!! .toCharArray()
159
162
}
160
163
161
- mqttClient = MqttUtils .getMqttAndroidClient(context, mqttOptions?.brokerUrl!! , mqttOptions?.getClientId()!! , object : MqttCallbackExtended {
164
+ /* mqttClient = MqttUtils.getMqttAndroidClient(context, mqttOptions?.brokerUrl!!, mqttOptions?.getClientId()!!)
165
+ subscribeToTopics(mqttOptions!!.stateTopics)*/
166
+
167
+ /* mqttClient = MqttUtils.getMqttAndroidClient(context, mqttOptions?.brokerUrl!!, mqttOptions?.getClientId()!!, object : MqttCallbackExtended {
162
168
@Throws(NullPointerException::class)
163
169
override fun connectComplete(reconnect: Boolean, serverURI: String) {
164
170
if (reconnect) {
165
171
Timber.d("Reconnected to : " + serverURI)
166
172
// Because Clean Session is true, we need to re-subscribe
167
173
subscribeToTopics(mqttOptions!!.stateTopics)
174
+ mqttClient?.setCallback(null)
168
175
} else {
169
176
Timber.d("Connected to: " + serverURI)
170
177
}
@@ -182,7 +189,7 @@ class MQTTService(private var context: Context, options: MQTTOptions,
182
189
183
190
override fun deliveryComplete(token: IMqttDeliveryToken) {}
184
191
})
185
-
192
+ */
186
193
options.isAutomaticReconnect = true
187
194
options.isCleanSession = false
188
195
@@ -205,14 +212,15 @@ class MQTTService(private var context: Context, options: MQTTOptions,
205
212
override fun onFailure (asyncActionToken : IMqttToken , exception : Throwable ) {
206
213
if (listener != null && mqttOptions != null ) {
207
214
Timber .e(" Failed to connect to: " + mqttOptions!! .brokerUrl + " exception: " + exception)
208
- listener!! .handleMqttException(" Error connecting to the broker and port: " + mqttOptions!! .brokerUrl)
215
+ // listener!!.handleMqttException("Error connecting to the broker and port: " + mqttOptions!!.brokerUrl)
216
+ listener!! .handleMqttException(context.getString(R .string.error_mqtt_subscription))
209
217
}
210
218
}
211
219
})
212
220
} catch (e: MqttException ) {
213
221
Timber .e(e, " MqttException" )
214
222
if (listener != null ) {
215
- listener?.handleMqttException(" Error while connecting: " + e.message)
223
+ listener?.handleMqttException(" " + e.message)
216
224
}
217
225
}
218
226
@@ -224,7 +232,7 @@ class MQTTService(private var context: Context, options: MQTTOptions,
224
232
} catch (e: Exception ) {
225
233
e.printStackTrace()
226
234
if (listener != null ) {
227
- listener!! .handleMqttException(" Error while connecting: " + e.message)
235
+ listener!! .handleMqttException(" " + e.message)
228
236
}
229
237
}
230
238
}
@@ -240,7 +248,8 @@ class MQTTService(private var context: Context, options: MQTTOptions,
240
248
Timber .e(" Error Sending Command: " + e.message)
241
249
e.printStackTrace()
242
250
if (listener != null ) {
243
- listener!! .handleMqttException(" Error Sending Command: " + e.message)
251
+ // listener!!.handleMqttException("Error Sending Command: " + e.message)
252
+ listener!! .handleMqttException(context.getString(R .string.error_mqtt_subscription))
244
253
}
245
254
}
246
255
}
0 commit comments