Sending actor data to MQTT
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package mqttClient
|
||||
|
||||
import com.hivemq.client.mqtt.mqtt5.{Mqtt5AsyncClient, Mqtt5Client}
|
||||
import play.api.Configuration
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
object MqttClientFactory {
|
||||
def createClient(configuration: Configuration, suffix: String = ""): Mqtt5AsyncClient = {
|
||||
val mqttConfig = configuration.get[Configuration]("mqtt")
|
||||
|
||||
var builder = Mqtt5Client.builder()
|
||||
.identifier(mqttConfig.get[String]("clientId") + suffix)
|
||||
.serverHost(mqttConfig.get[String]("host"))
|
||||
.serverPort(mqttConfig.get("port"))
|
||||
|
||||
mqttConfig.getOptional[String]("username").foreach { username =>
|
||||
val password = mqttConfig.get[String]("password").getBytes(StandardCharsets.US_ASCII)
|
||||
builder = builder.simpleAuth()
|
||||
.username(username)
|
||||
.password(password)
|
||||
.applySimpleAuth()
|
||||
}
|
||||
|
||||
builder.buildAsync()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user