阿里云alink协议实现模块。
- AliyunIotDemo - 基于JetBrains Compose for Desktop的阿里云Iot桌面应用示例
import com.gitee.xuankaicat.kmnkt.aliyuniot.AliyunMqtt
import com.gitee.xuankaicat.kmnkt.aliyuniot.mqtt
private val socket = mqtt(AliyunMqtt(
productKey = "",
deviceName = "",
deviceSecret = "",
regionId = "cn-shanghai"
))
socket.propertySet(
receiveOnce = false // 是否只接收一次,默认为false表示一直接收
) {
// 回调函数
}
val map = mutableMapOf<String, Int>()
if(temperature.toIntOrNull() != null) map["mtemp"] = temperature.toInt()
if(humidity.toIntOrNull() != null) map["mhumi"] = humidity.toInt()
if(brightness.toIntOrNull() != null) map["LightLuminance"] = brightness.toInt()
socket.propertyPost(
params = map, // 上报参数
receiveOnce = false // 期待服务端返回信息
) {
// 回调函数
}
val list = listOf("LightSwitch", "LightLuminance", "mtemp", "mhumi")
socket.desiredGet(list) {
// 回调函数
}
由于alink的实现还不完善,如果有需要可以自行实现所需请求的函数。 实现时可以参考event.kt。
fun IMqttSocket.函数名(
// ... 自定义参数
onReceive: OnReceiveAlinkResultFunc = {} // 回调函数
) {
this as AlinkMQTT
val id = nextId
// 构建AlinkBase对象,以下供参考
val msgObj = AlinkBase(id,
params = params.toJsonObject(),
sys = AlinkBase.Sys("0"),
method = "thing.event.property.post"
)
// 发送并接收消息,如果只想接收消息也可以用addInMessageTopic
sendAndReceiveAlink(id, "/sys/${productKey}/${deviceName}/topic", msgObj, false, onReceive)
}