[swift][iot]mqttでpublishできたので、メモ
2024/05/06
awsiot
swift
あとでゆっくりまとめます
とりあえずできたので.. いろいろ注意する点はありますよ!
func importIdentity(fileName:String) {
guard let path = Bundle.main.path(forResource: fileName, ofType: "p12") else {
print("*** not found file")
return
}
let url = URL(fileURLWithPath: path)
guard let data = try? Data(contentsOf: url) else {
print("*** not get data")
return
}
DispatchQueue.global().async {
let b = AWSIoTManager.importIdentity(fromPKCS12Data: data, passPhrase: "", certificateId: "bbb")
if b {
print("*** ok import")
} else {
print("*** failed.")
}
let iotEndPoint = AWSEndpoint(
urlString: IOT_ENDPOINT
)
let iotDataConfiguration = AWSServiceConfiguration(
region: AWSRegionType.APNortheast1,
endpoint: iotEndPoint,
credentialsProvider: nil
)
let mqttConfig = AWSIoTMQTTConfiguration(keepAliveTimeInterval: 60.0,
baseReconnectTimeInterval: 1.0,
minimumConnectionTimeInterval: 20.0,
maximumReconnectTimeInterval: 128.0,
runLoop: RunLoop.current,
runLoopMode: RunLoop.Mode.default.rawValue,
autoResubscribe: true,
lastWillAndTestament: AWSIoTMQTTLastWillAndTestament())
AWSIoTDataManager.register(with:iotDataConfiguration!,
with:mqttConfig,
forKey: "aaaa")
}
}
func aaa() {
let iotDataManager = AWSIoTDataManager(forKey: "aaaa")
let b0 = iotDataManager.connect(withClientId: "xxx", cleanSession:false, certificateId:"bbb", statusCallback: {_ in } )
print("*** result \(b0)")
let topic = "things/Test-Device/shadow/update/accepted"
let b = iotDataManager.publishString("{\"message\": \"o_o! aaaaaaaaaaaaaaaa\"}", onTopic: topic, qoS: .messageDeliveryAttemptedAtLeastOnce)
print("*** result \(b)")
}
: