public HttpResponse GetHwandazaAutomationCover()
        {
            var command = new HwandazaCommand()
            {
                Command = "cover",
            };

            byte[] responseData = Encoding.UTF8.GetBytes(GetResponseContentAsync(command).Result);
            return(new HttpResponse(Windows.Web.Http.HttpStatusCode.Ok, responseData));
        }
        private async Task <string> ResubmitRequestAppServiceAsync(HwandazaCommand command)
        {
            //re-establish appservice connection
            await AppServiceInstance.SetAppServiceConnection();

            _appServiceConnection = AppServiceInstance.Instance.GetAppServiceConnection();

            var responseContent = "{}";

            var appServiceResponse = RequestAppServiceAsync(command).Result;

            if (appServiceResponse != null)
            {
                if (appServiceResponse.Status == AppServiceResponseStatus.Success)
                {
                    responseContent = appServiceResponse.Message["Response"] as string;
                }
            }

            return(responseContent);
        }
        private async Task <string> GetResponseContentAsync(HwandazaCommand command)
        {
            var responseContent = "{}";

            var appServiceResponse = RequestAppServiceAsync(command).Result;

            if (appServiceResponse != null)
            {
                if (appServiceResponse.Status == AppServiceResponseStatus.Success)
                {
                    responseContent = appServiceResponse.Message["Response"] as string;
                }
                else
                {
                    //attempt a reconnections and resubmit the command the
                    responseContent = await ResubmitRequestAppServiceAsync(command);
                }
            }

            return(responseContent);
        }
        private async Task <AppServiceResponse> RequestAppServiceAsync(HwandazaCommand command)
        {
            AppServiceResponse response = null;

            try
            {
                var hwandazaMessage = new ValueSet {
                    { "HwandazaCommand", JsonConvert.SerializeObject(command) }
                };
#pragma warning disable CS4014
                response = await _appServiceConnection.SendMessageAsync(hwandazaMessage);

#pragma warning restore CS4014
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return(response);
        }