예제 #1
0
        public async Task BootstrapHass()
        {
            Windows.Storage.ApplicationDataContainer localSettings;

            try
            {
                StatusMessage = "Bootstrap started...";
                VoiceCmdList  = null;
                localSettings = ApplicationData.Current.LocalSettings;
                if (localSettings.Values.ContainsKey("WebAddress"))
                {
                    string uri = (string)localSettings.Values["WebAddress"];

                    Hass hass = new Hass(new Uri(uri));
                    await hass.Bootstrap();

                    string filter;
                    if (localSettings.Values.ContainsKey("EntityFilter"))
                    {
                        filter = (string)localSettings.Values["EntityFilter"];
                    }
                    else
                    {
                        filter = String.Empty;
                    }

                    VoiceCmdList = hass.GetCommandList(uri, filter);
                    if (VoiceCmdList.Count < 100)
                    {
                        InstallVoiceCommands(VoiceCmdList);
                        StatusMessage = String.Format("Bootstrap succeeded");
                        Bootstrapped  = true;
                    }
                    else
                    {
                        StatusMessage = String.Format("Too many commands ({0}), use entity filter to reduce amount", VoiceCmdList.Count.ToString());
                        Bootstrapped  = true;
                    }
                }
            }
            catch (Exception ex)
            {
                StatusMessage = "Bootstrap failed because: " + ex.Message;
            }
        }
예제 #2
0
        public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary <string, object> suspensionState)
        {
            StatusMessage = "Ready";
            if (!Bootstrapped)
            {
                await BootstrapHass();
            }
            else if (parameter != null)
            {
                VoiceCommand vc = VoiceCmdList.Find(x => x.Name == parameter.ToString());
                if (vc != null)
                {
                    try
                    {
                        Hass.CallApiServiceAsync(vc.WebAddress, vc.Domain, vc.Service, vc.Entity);

                        /*
                         * string state = async Hass.CallApiServiceAsync(vc.WebAddress, vc.Domain, vc.Service, vc.Entity);
                         * if (state != null)
                         * {
                         *  var userMessage = new VoiceCommandUserMessage();
                         *  userMessage.SpokenMessage = state;
                         *  VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage);
                         *
                         *  //AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
                         *  //VoiceCommandServiceConnection voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails()
                         *  //await voiceServiceConnection.ReportSuccessAsync(response);
                         * }
                         */
                        StatusMessage = "Service call succeeded";
                    }
                    catch (Exception ex)
                    {
                        StatusMessage = "Service call failed because: " + ex.Message;
                    }
                }
            }
            await Task.CompletedTask;
        }