-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Showing
9 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
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
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
51 changes: 51 additions & 0 deletions
51
...in/kotlin/ch/rmy/android/http_shortcuts/scripting/actions/types/SendMQTTMessagesAction.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,51 @@ | ||
package ch.rmy.android.http_shortcuts.scripting.actions.types | ||
|
||
import ch.rmy.android.framework.extensions.applyIfNotNull | ||
import ch.rmy.android.framework.extensions.logException | ||
import ch.rmy.android.http_shortcuts.exceptions.ActionException | ||
import ch.rmy.android.http_shortcuts.scripting.ExecutionContext | ||
import io.reactivex.Completable | ||
import io.reactivex.schedulers.Schedulers | ||
import org.eclipse.paho.client.mqttv3.MqttClient | ||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions | ||
import org.eclipse.paho.client.mqttv3.MqttException | ||
import org.eclipse.paho.client.mqttv3.MqttMessage | ||
|
||
class SendMQTTMessagesAction( | ||
private val serverUri: String, | ||
private val username: String?, | ||
private val password: String?, | ||
private val messages: List<Message>, | ||
) : BaseAction() { | ||
|
||
override fun execute(executionContext: ExecutionContext): Completable = | ||
Completable.fromAction { | ||
try { | ||
val client = MqttClient(serverUri, MqttClient.generateClientId(), null) | ||
val options = MqttConnectOptions() | ||
.apply { | ||
isCleanSession = true | ||
} | ||
.applyIfNotNull(username) { | ||
userName = it | ||
} | ||
.applyIfNotNull(password) { | ||
password = it.toCharArray() | ||
} | ||
client.connect(options) | ||
messages.forEach { message -> | ||
client.publish(message.topic, MqttMessage(message.payload)) | ||
} | ||
client.disconnect() | ||
client.close() | ||
} catch (e: MqttException) { | ||
logException(e) | ||
throw ActionException { | ||
"Failed to send MQTT message: $e" | ||
} | ||
} | ||
} | ||
.subscribeOn(Schedulers.io()) | ||
|
||
data class Message(val topic: String, val payload: ByteArray) | ||
} |
55 changes: 55 additions & 0 deletions
55
...otlin/ch/rmy/android/http_shortcuts/scripting/actions/types/SendMQTTMessagesActionType.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,55 @@ | ||
package ch.rmy.android.http_shortcuts.scripting.actions.types | ||
|
||
import ch.rmy.android.http_shortcuts.scripting.ActionAlias | ||
import ch.rmy.android.http_shortcuts.scripting.actions.ActionDTO | ||
import org.json.JSONObject | ||
import org.liquidplayer.javascript.JSValue | ||
|
||
class SendMQTTMessagesActionType : BaseActionType() { | ||
|
||
override val type = TYPE | ||
|
||
override fun fromDTO(actionDTO: ActionDTO): SendMQTTMessagesAction { | ||
val options = if (actionDTO.argCount >= 3) { | ||
actionDTO.getObject(1) | ||
} else { | ||
null | ||
} | ||
val messages = if (actionDTO.argCount >= 3) { | ||
actionDTO.getList(2) | ||
} else { | ||
actionDTO.getList(1) | ||
} | ||
.orEmpty() | ||
.mapNotNull { | ||
when (it) { | ||
is JSValue -> { | ||
val obj = JSONObject(it.toJSON()) | ||
SendMQTTMessagesAction.Message( | ||
topic = obj.getString("topic"), | ||
payload = obj.getString("payload").toByteArray(), | ||
) | ||
} | ||
else -> null | ||
} | ||
} | ||
|
||
return SendMQTTMessagesAction( | ||
serverUri = actionDTO.getString(0) ?: "", | ||
username = options?.get("username") as? String, | ||
password = options?.get("password") as? String, | ||
messages = messages, | ||
) | ||
} | ||
|
||
override fun getAlias() = ActionAlias( | ||
functionName = FUNCTION_NAME, | ||
functionNameAliases = setOf("sendMQTTMessage", "sendMqttMessages", "sendMqttMessage"), | ||
parameters = 3, | ||
) | ||
|
||
companion object { | ||
private const val TYPE = "send_mqtt_messages" | ||
private const val FUNCTION_NAME = "sendMQTTMessages" | ||
} | ||
} |
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