@wooj2 Thanks for your precious help on Connecting to AWS & Updating thing it saved huge time for me. With reference to that as per your suggestion I used iotDataManager to connect to AWS and update thing state.
But now I need list of things in group or list of things from AWS with that I tried to find solution from AWSIoT Reference So i have used below code to get it. Previously i used to get it using normal API call from our backend service but i need fully use with AWS.
```
func initializeAWS() {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:AWS_REGION,
identityPoolId:IDENTITY_POOL_ID)
initializeControlPlane(credentialsProvider: credentialsProvider)
}
func initializeControlPlane(credentialsProvider: AWSCredentialsProvider) {
let controlPlaneServiceConfiguration = AWSServiceConfiguration(region:AWS_REGION, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = controlPlaneServiceConfiguration
iot = AWSIoT.default()
let request = AWSIoTListThingsInThingGroupRequest()
request?.thingGroupName = "XXXGroupName"
let output = iot.listThings(inThingGroup: request!)
print("output is \(output.result)")
print("error is \(output.error)")
}
```
I have used here AWSIoT & AWSIoTListThingsInThingGroupRequest object to get list of things may i know is this right way to fetch ? if it is I'm output and error both objects getting nil.
I even tried to find solution for the AWS IOT example from Github and stackoverflow I didnt get anything relevant answer to this. Or is there anything in iotDataManager that will give list of things ? Please can you help me on this ? @wooj2 @rohandubal @royjit @awsmobilesdk @palpatim @minbi @lawmicha
By mistake i closed, Please let me know. Thanks.
Hey @kshrikant
Thanks for reaching out again, and glad that we were able to save you some time :).
To be honest, I've never run that request before, but I looked in both in the AWSIotDataManager and the AWSIotManager and neither seem to have a high level interface you can use to make a AWSIoTListThingsInThingGroupRequest. That being said, I was able to:
As a result of making the request, my console shows:
2020-04-21 10:52:35:095 IoTSampleSwift[40493:22726792] Request headers:
{
Authorization = "AWS4-HMAC-SHA256 Credential=--redacted--/20200421/us-west-2/execute-api/aws4_request, SignedHeaders=content-type;host;user-agent;x-amz-date;x-amz-security-token, Signature=--redacted--";
"Content-Type" = "application/x-amz-json-1.0";
Host = "iot.us-west-2.amazonaws.com";
"User-Agent" = "aws-sdk-iOS/2.13.2 iOS/13.3 en_US";
"X-Amz-Date" = 20200421T175235Z;
"X-Amz-Security-Token" = "--redacted--";
}
And for a response, I see:
2020-04-21 10:52:35:095 IoTSampleSwift[40493:22726792] Request headers:
{
error is nil
2020-04-21 10:52:35:097 IoTSampleSwift[40493:22726792] Initializing AWSIoTMqttConfiguration with KeepAlive:300.000000, baseReconnectTime:1.000000,minimumConnectionTime:20.000000, maximumReconnectTime:128.000000, autoResubscribe:Enabled, lwt topic: message:
2020-04-21 10:52:35:443 IoTSampleSwift[40493:22726911] Response headers:
{
"Access-Control-Allow-Origin" = "*";
"Content-Length" = 42;
"Content-Type" = "application/json";
Date = "Tue, 21 Apr 2020 17:52:35 GMT";
"x-amz-apigw-id" = "--redacted--";
"x-amzn-requestid" = "--redacted--";
"x-amzn-trace-id" = "Root=--redacted--";
}
2020-04-21 10:52:35:443 IoTSampleSwift[40493:22726911] Response body:
{"nextToken":null,"things":["XXXThing"]}
At this point, I suspect it might be configuration related: either the way we are configuring the client, or maybe some configuration in the backend (permission, IoT group, etc).
Is it possible to add the following to your app delegate to see some additional information about the request being sent to the service:
AWSDDLog.sharedInstance.logLevel = .debug
AWSDDLog.add(AWSDDTTYLogger.sharedInstance)
(please avoid posting any personally identifiable information)
Thanks @wooj2 ! For quick response. I've enabled log level and tested. All configuration was right only thing i wasn't aware about is how to get response of things, the way to get things is as below.
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:AWS_REGION,
identityPoolId:IDENTITY_POOL_ID)
let controlPlaneServiceConfiguration = AWSServiceConfiguration(region:AWS_REGION, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = controlPlaneServiceConfiguration
iot = AWSIoT.default()
let request = AWSIoTListThingsInThingGroupRequest()
request?.thingGroupName = "XXXGroupName"
let output = iot.listThings(inThingGroup: request!)
output.continueOnSuccessWith { (response) -> Any? in
if let result = response.result, let things = result.things {
self.awsDevices = things
completionHandler(true)
}
return self.awsDevices
}
Hey @kshrikant !
Great to hear you're unblocked. In reviewing your code, it looks like your code has continueOnSuccess on a separate line, which seems like there is a potential for a race condition with the returning network call. You might want to consider using something like this, which allows you to get the response & error when making that call:
iot.listThings(inThingGroup: groupRequest) { (response, error) in
if let error = error {
print("got error: \(error)")
return
}
guard let things = response?.things else {
return
}
for thing in things {
print("In group \(thingGroupName), found thing:\(thing)")
}
}
Best of luck!
Thank you @wooj2 I will update code as per your suggestion to avoid race condition. Thanks for response! 馃憤
Most helpful comment
Hey @kshrikant !
Great to hear you're unblocked. In reviewing your code, it looks like your code has
continueOnSuccesson a separate line, which seems like there is a potential for a race condition with the returning network call. You might want to consider using something like this, which allows you to get the response & error when making that call:Best of luck!