/// <summary> /// Initiates the asynchronous execution of the GetThingShadow operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the GetThingShadow operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public virtual Task <GetThingShadowResponse> GetThingShadowAsync(GetThingShadowRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken)) { var options = new InvokeOptions(); options.RequestMarshaller = GetThingShadowRequestMarshaller.Instance; options.ResponseUnmarshaller = GetThingShadowResponseUnmarshaller.Instance; return(InvokeAsync <GetThingShadowResponse>(request, options, cancellationToken)); }
internal virtual GetThingShadowResponse GetThingShadow(GetThingShadowRequest request) { var options = new InvokeOptions(); options.RequestMarshaller = GetThingShadowRequestMarshaller.Instance; options.ResponseUnmarshaller = GetThingShadowResponseUnmarshaller.Instance; return(Invoke <GetThingShadowResponse>(request, options)); }
/// <summary> /// Initiates the asynchronous execution of the GetThingShadow operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the GetThingShadow operation on AmazonIotDataClient.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndGetThingShadow /// operation.</returns> public virtual IAsyncResult BeginGetThingShadow(GetThingShadowRequest request, AsyncCallback callback, object state) { var options = new InvokeOptions(); options.RequestMarshaller = GetThingShadowRequestMarshaller.Instance; options.ResponseUnmarshaller = GetThingShadowResponseUnmarshaller.Instance; return(BeginInvoke(request, options, callback, state)); }
public ActionResult State(string devName) { GetThingShadowRequest req = new GetThingShadowRequest(); req.ThingName = devName; GetThingShadowResponse res = _client.GetThingShadow(req); string state = Encoding.UTF8.GetString(res.Payload.ToArray()); return(Content(Request["callback"] + "(" + state + ")")); }
public async Task <ThingState> GetThing() { var getThingShadowRequest = new GetThingShadowRequest { ThingName = _thingName }; var theThing = await _dataClient.GetThingShadowAsync(getThingShadowRequest, CancellationToken.None); var json = Encoding.UTF8.GetString(theThing.Payload.ToArray()); var shadowThing = JsonUtility.FromJson <ShadowThing>(json); return(shadowThing.state.reported); }
public async Task <ThingShadow <T> > GetThingShadow() { var getThingShadowRequest = new GetThingShadowRequest() { ThingName = this.thingName }; var getThingShadowResponse = await this.client.GetThingShadowAsync(getThingShadowRequest); return(await Task <ThingShadow <T> > .Factory.StartNew( () => { var thingShadow = Utils.Bind <ThingShadow <T> >(getThingShadowResponse.Payload); if (thingShadow.State.Desired == null) { thingShadow.State.Desired = new T(); } return thingShadow; })); }
private static void KeepGettingShadows() { while (true) { foreach (string device in _devices) { Console.WriteLine($"Getting shadow for {device}"); GetThingShadowRequest updateRequest = new GetThingShadowRequest(); updateRequest.ThingName = device; Task <GetThingShadowResponse> response = null; try { response = _iotClient.GetThingShadowAsync(updateRequest, new System.Threading.CancellationToken()); response.Wait(); } catch (Exception ex) { Console.WriteLine($"No shadow found for {device}. The following error occured: {ex.Message}"); continue; } string updatedResponse = null; if ((response.Result != null) && (response.Result.Payload != null)) { //Console.WriteLine($"Reading Payload from Response"); using (StreamReader reader = new StreamReader(response.Result.Payload)) updatedResponse = reader.ReadToEnd(); var json = JObject.Parse(updatedResponse); string eventType = json.SelectToken("state.reported.type").Value <string>().Trim(); string itemsStr = json.SelectToken("state.reported.items").Value <string>().Trim(); string aisleIdentifier = device.Substring(7).Trim(); //removing "store1_" int customerId = int.Parse(json.SelectToken("state.reported.customerId").Value <string>().Trim()); string newImage = $"{eventType}{itemsStr}{customerId}"; //Console.WriteLine($"Payload read: {updatedResponse}"); if (!string.IsNullOrWhiteSpace(updatedResponse)) { if (!_responses.ContainsKey(device)) { _responses.Add(device, newImage); UpdateDynamoDB(eventType, aisleIdentifier, itemsStr, customerId); Console.WriteLine($"The aisle {device} state got changed to {updatedResponse}"); } else if (string.Compare(newImage, _responses[device], StringComparison.OrdinalIgnoreCase) != 0) { Console.WriteLine($"New Time Stamp {newImage}"); Console.WriteLine($"Old Time Stamp {_responses[device]}"); _responses[device] = newImage; UpdateDynamoDB(eventType, aisleIdentifier, itemsStr, customerId); Console.WriteLine($"The aisle {device} state got changed to {updatedResponse}"); } else { Console.WriteLine($"No updates for {device}"); } } } } Thread.Sleep(30000); } }