-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Co-authored-by: e1turin <48864186+e1turin@users.noreply.github.com> Co-authored-by: Victor Smirnov <53015676+vityaman@users.noreply.github.com> Co-authored-by: FineGoose <90922868+YuikoSempai@users.noreply.github.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.idea | ||
|
||
# Compiled class file | ||
*.class | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
MD013: | ||
line_length: 80 | ||
line_length: 100 | ||
strict: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.vivlaniv.nexohub.util | ||
|
||
fun colorToRGB(color: Int): Triple<Int, Int, Int> = | ||
Triple((color shr 16) and 0xFF, (color shr 8) and 0xFF, color and 0xFF) | ||
|
||
const val colorMask = 0x00FFFFFF | ||
|
||
fun RGBToColor(red: Int, green: Int, blue: Int): Int = | ||
(((red shl 16) + (green shl 8) + blue) and colorMask) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.vivlaniv.nexohub.util.mqtt | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.asCoroutineDispatcher | ||
import kotlinx.coroutines.launch | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import org.eclipse.paho.client.mqttv3.MqttClient | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import java.util.concurrent.Executors | ||
|
||
val mqttLogger: Logger = LoggerFactory.getLogger("mqtt") | ||
|
||
val mqttScope = CoroutineScope(Executors.newFixedThreadPool(32).asCoroutineDispatcher()) | ||
|
||
inline fun <reified T> MqttClient.subscribe( | ||
topic: String, | ||
crossinline onReceived: suspend CoroutineScope.(String, T) -> Unit | ||
) { | ||
subscribe(topic, 2) { tpc, msg -> | ||
mqttScope.launch { | ||
mqttLogger.info("got message on topic $tpc: $msg") | ||
onReceived(tpc, Json.decodeFromString<T>(msg.payload.decodeToString())) | ||
} | ||
} | ||
} | ||
|
||
inline fun <reified T> MqttClient.publish(topic: String, message: T) { | ||
val msg = Json.encodeToString(message) | ||
mqttLogger.info("send message on topic $topic: $msg") | ||
publish(topic, msg.encodeToByteArray(), 2, false) | ||
} |