python - AWS IoT 设备未收到 iOS 应用程序发布的消息

标签 python ios swift amazon-web-services aws-iot

所以我正在构建一个可以通过 AWS IoT 与设备通信的 iOS 应用程序。设备和应用程序都订阅了更新/接受、获取/接受和更新/增量。我可以从任何一方发布更新,并且更新反射(reflect)在 AWS IoT 事物影子上,但不知何故,更新从未从一个设备中继到另一个设备。因此,如果我在应用程序上按下发送,更新将显示在 AWS IoT 上,但应该订阅主题的设备似乎永远不会收到此类更新,反之亦然。 下面是设备端的代码片段。

# For certificate based connection
myShadowClient = AWSIoTMQTTShadowClient("myDevice")
myShadowClient.configureEndpoint("xxxxxxxxx.iot.us-east-1.amazonaws.com", 8883)
myShadowClient.configureCredentials("certs/root-CA.crt", "certs/private.pem.key", "certs/certificate.pem.crt")
myShadowClient.configureConnectDisconnectTimeout(10)  # 10 sec
myShadowClient.configureMQTTOperationTimeout(5)  # 5 sec


# Custom MQTT message callback
def customCallback(client, userdata, message):
    print("Received a new message: ")
    print(message.payload)
    print("from topic: ")
    print(message.topic)
    print("--------------\n\n")

# Custom Shadow callback
def customShadowCallback_Update(payload, responseStatus, token):
    # payload is a JSON string ready to be parsed using json.loads(...)
    # in both Py2.x and Py3.x
    if responseStatus == "timeout":
        print("Update request " + token + " time out!")
    if responseStatus == "accepted":
        payloadDict = json.loads(payload)
        print("~~~~~~~~~~~~~~~~~~~~~~~")
        print("Update request with token: " + token + " accepted!")
        print("property: " + str(payloadDict["state"]["desired"]["property"]))
        print("~~~~~~~~~~~~~~~~~~~~~~~\n\n")
    if responseStatus == "rejected":
        print("Update request " + token + " rejected!")

def customShadowCallback_Delete(payload, responseStatus, token):
    if responseStatus == "timeout":
        print("Delete request " + token + " time out!")
    if responseStatus == "accepted":
        print("~~~~~~~~~~~~~~~~~~~~~~~")
        print("Delete request with token: " + token + " accepted!")
        print("~~~~~~~~~~~~~~~~~~~~~~~\n\n")
    if responseStatus == "rejected":
        print("Delete request " + token + " rejected!")

#MQTT Operations
JSONPayload = '{"state":{"reported":{"property": "published from Device"}}}'


myShadowClient.connect()
myMQTTClient = myShadowClient.getMQTTConnection()

# AWSIoTMQTTClient connection configuration
myMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
myMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
myMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
myMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec
myMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec

myMQTTClient.publish("$aws/things/myDevice/shadow/update", JSONPayload, 1)
myMQTTClient.publish("$aws/things/myDevice/shadow/get", "", 1)
myMQTTClient.subscribe("$aws/things/myDevice/shadow/update/accepted", 1, customCallback)
myMQTTClient.subscribe("$aws/things/myDevice/shadow/update/rejected", 1, customCallback)

myMQTTClient.subscribe("$aws/things/myDevice/shadow/get/accepted", 1, customCallback)
myMQTTClient.subscribe("$aws/things/myDevice/shadow/get/rejected", 1, customCallback)
myMQTTClient.subscribe("$aws/things/myDevice/shadow/update/delta", 1, customCallback)


# Create a device shadow instance using persistent subscription

myDeviceShadow = myShadowClient.createShadowHandlerWithName("Bot", True)
while True:
    time.sleep(10)

最佳答案

这是预期的行为。解释可见here

Delta state is a virtual type of state that contains the difference between the desired and reported states. Fields in the desired section that are not in the reported section are included in the delta. Fields that are in the reported section and not in the desired section are not included in the delta.

关于python - AWS IoT 设备未收到 iOS 应用程序发布的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39004902/

相关文章:

python - 权限错误 : Permission denied to reading CSV File in Python

python - 如何禁用输入输入直到双击?

objective-c - 如何检查 iOS 设备自启动后是否已重启

ios - 在 UITableView 处于编辑模式时选择行

ios - 从堆栈为 ViewController 中的 NavigationController 准备 ForSegue

python - 导入tensorflow使python 3.6.5报错

python - pandas:切片 Multiindex 有很多索引

ios - XCode iOS 改变现有项目的界面

swift - 一直从 Core Data 中获取数据还是将所有对象都保存在内存中?

swift - 在 Xcode 中,如何向我的 Mac 应用程序添加教程窗口,该窗口仅在首次启动时打开