Skip to content

Latest commit

 

History

History
60 lines (49 loc) · 2.09 KB

9_IoT_Explorer_DirectMethod_Example.md

File metadata and controls

60 lines (49 loc) · 2.09 KB

Example 9 - Device twin Direct Method Command from IoT Explorer

Code:

namespace XServerIoTOnboardTaskProject
{
        ....

        private async void HttpRestServer_ClientRequestEvent(object sender, HttpRestServerService.ClientRequestEventArgs e)
        {
            IO.SimpleHttpServer.Result res = new IO.SimpleHttpServer.Result();

            DirectMethodResponse response = new DirectMethodResponse();  //My command response object

            try
            {
                if (e.RequestMethod == RequestMethodType.GET)
                {
                    //...
                }
                else if (e.RequestMethod == RequestMethodType.POST)
                {
                    if (e.uriString.ToLower() == "/onboardtask/command")
                    {
                        var newcommand = JsonConvert.DeserializeObject<Command>(e.HttpContent);

                        response.Success = true;
                        response.Message = "OK";

                        res = await RestServer.ServerResponse(HTTPStatusCodes.OK, e.OStream, JsonConvert.SerializeObject(response));

                        //Command management code ....
                    }
                }
                else
                {
                    res = await RestServer.ServerResponse(HTTPStatusCodes.Not_Found, e.OStream, null);
                }
            }
            catch (Exception ex)
            {
                EventLogging.AddLogMessage(MessageType.ExceptionError, this.GetType().Name + " - " + ServiceDisplayName + " - " + "Http REST server exception error! Error: " + ex.Message);
            }
        }

        //My classes
        private class DirectMethodResponse
        {
            public bool Success { get; set; }
            public string Message { get; set; }
        }

        private class Command
        {
            public string SetValue { get; set; }
        }
    }
}