-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
deen13
committed
Oct 29, 2020
1 parent
dbc7b85
commit 5719d65
Showing
9 changed files
with
132 additions
and
44 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/de/smartsquare/kortance/mqtt/ClientFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package de.smartsquare.kortance.mqtt | ||
|
||
import com.hivemq.client.mqtt.mqtt3.Mqtt3Client | ||
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client | ||
import de.smartsquare.kortance.CredentialOptions | ||
|
||
object ClientFactory { | ||
|
||
fun create(host: String, port: Int, cred: CredentialOptions?, ssl: Boolean, ver: SupportedMqttVersion): MqttClient { | ||
return if (ver == SupportedMqttVersion.V5) { | ||
buildFive(host, port, cred, ssl) | ||
} else { | ||
buildThree(host, port, cred, ssl) | ||
} | ||
} | ||
|
||
private fun buildThree(host: String, port: Int, credentials: CredentialOptions?, ssl: Boolean): Mqtt3ClientWrapper { | ||
val baseClient = Mqtt3Client.builder() | ||
.serverHost(host) | ||
.serverPort(port) | ||
|
||
val authClient = if (credentials != null) { | ||
baseClient | ||
.simpleAuth() | ||
.username(credentials.username) | ||
.password(credentials.password.toByteArray()) | ||
.applySimpleAuth() | ||
} else { | ||
baseClient | ||
} | ||
|
||
return if (ssl) { | ||
Mqtt3ClientWrapper(authClient.sslWithDefaultConfig().build().toBlocking()) | ||
} else { | ||
Mqtt3ClientWrapper(authClient.build().toBlocking()) | ||
} | ||
} | ||
|
||
private fun buildFive(host: String, port: Int, credentials: CredentialOptions?, ssl: Boolean): Mqtt5ClientWrapper { | ||
val baseClient = Mqtt5Client.builder() | ||
.serverHost(host) | ||
.serverPort(port) | ||
|
||
val authClient = if (credentials != null) { | ||
baseClient | ||
.simpleAuth() | ||
.username(credentials.username) | ||
.password(credentials.password.toByteArray()) | ||
.applySimpleAuth() | ||
} else { | ||
baseClient | ||
} | ||
|
||
return if (ssl) { | ||
Mqtt5ClientWrapper(authClient.sslWithDefaultConfig().build().toBlocking()) | ||
} else { | ||
Mqtt5ClientWrapper(authClient.build().toBlocking()) | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/de/smartsquare/kortance/mqtt/Mqtt3ClientWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.smartsquare.kortance.mqtt | ||
|
||
import com.hivemq.client.mqtt.mqtt3.Mqtt3BlockingClient | ||
|
||
class Mqtt3ClientWrapper(private val hivemqClient: Mqtt3BlockingClient) : MqttClient { | ||
|
||
override fun connect() { | ||
hivemqClient.connect() | ||
} | ||
|
||
override fun publish(topic: String, payload: ByteArray) { | ||
hivemqClient.publishWith().topic(topic).payload(payload).send() | ||
} | ||
|
||
override fun disconnect() { | ||
hivemqClient.disconnect() | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/de/smartsquare/kortance/mqtt/Mqtt5ClientWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.smartsquare.kortance.mqtt | ||
|
||
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient | ||
|
||
class Mqtt5ClientWrapper(private val hivemqClient: Mqtt5BlockingClient) : MqttClient { | ||
|
||
override fun connect() { | ||
hivemqClient.connect() | ||
} | ||
|
||
override fun publish(topic: String, payload: ByteArray) { | ||
hivemqClient.publishWith().topic(topic).payload(payload).send() | ||
} | ||
|
||
override fun disconnect() { | ||
hivemqClient.disconnect() | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/de/smartsquare/kortance/mqtt/MqttClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.smartsquare.kortance.mqtt | ||
|
||
interface MqttClient { | ||
|
||
fun publish(topic: String, payload: ByteArray) | ||
|
||
fun connect() | ||
|
||
fun disconnect() | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/de/smartsquare/kortance/mqtt/SupportedMqttVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package de.smartsquare.kortance.mqtt | ||
|
||
enum class SupportedMqttVersion { | ||
V3, V5; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters