async Task ProcessLocalBuildingCommandAsync( VoiceCommandServiceConnection voiceConnection, BuildingCommandDetails commandDetails) { if (string.IsNullOrEmpty(commandDetails.Room)) { commandDetails.Room = await this.DisambiguateRoomAsync( voiceConnection, BuildingPersistence.Instance.Rooms); } // Local building, relatively easy. if (string.IsNullOrEmpty(commandDetails.Room)) { BuildingPersistence.Instance.SwitchAllLights(commandDetails.OnOff.Value); } else { var room = BuildingPersistence.Instance.GetRoomByName(commandDetails.Room); if (room != null) { room.SwitchLights(commandDetails.OnOff.Value); } } await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); }
async void OnDone(object sender, RoutedEventArgs e) { BuildingPersistence.Instance.Rooms = new List <Room>(); int gpioPin = 0; foreach (var room in this.Rooms) { BuildingPersistence.Instance.Rooms.Add( new Room() { RoomType = room.RoomType, Lights = Enumerable .Range(1, room.Count) .Select( (c, i) => new Light() { Id = i, GpioPinNumber = GpioPinLookup.GetGpioPinIndexForOrdinal(gpioPin++), Room = room.RoomType } ).ToList() } ); } await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); Navigator.Navigate(typeof(MonitorPage), null); }
static async Task CreateUIAndLoadBuildingDataAsync( bool prelaunchActivated) { Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); Navigator.Frame = rootFrame; // Place the frame in the current Window Window.Current.Content = rootFrame; } if (prelaunchActivated == false) { if (rootFrame.Content == null) { BuildingPersistence.Instance = await BuildingPersistence.LoadAsync(); Type pageType = typeof(MonitorPage); if (BuildingPersistence.Instance == null) { pageType = typeof(SetupBuildingPage); } rootFrame.Navigate(pageType, null); } Window.Current.Activate(); } }
async Task <LightControlSwitchBuildingResult> SwitchBuildingAsync(bool interfaceMemberOnOff) { BuildingPersistence.Instance?.SwitchAllLights(interfaceMemberOnOff); await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); return(LightControlSwitchBuildingResult.CreateSuccessResult()); }
async void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); deferral.Complete(); }
async void OnNameBuildingAndContinue(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(this.txtBuildingName.Text)) { BuildingPersistence.Instance = new Model.Building(); BuildingPersistence.Instance.Name = this.txtBuildingName.Text; await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); Navigator.Navigate(typeof(SetupRoomsPage), null); } }
async Task <LightControlToggleRoomLightResult> InternalToggleRoomLightAsync( string interfaceMemberRoomName, int interfaceMemberLightIndex) { var room = BuildingPersistence.Instance?.GetRoomByName(interfaceMemberRoomName); room?.ToggleLight(interfaceMemberLightIndex); await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); return(LightControlToggleRoomLightResult.CreateSuccessResult()); }
async Task <LightControlSwitchRoomResult> InternalSwitchRoomAsync( string interfaceMemberRoomName, bool interfaceMemberOnOff) { var room = BuildingPersistence.Instance?.GetRoomByName(interfaceMemberRoomName); room?.SwitchLights(interfaceMemberOnOff); await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); return(LightControlSwitchRoomResult.CreateSuccessResult()); }
async Task SwitchLightsInRoomAsync(Room room, bool onOff) { room.SwitchLights(onOff); if (this.isLocalBuilding) { await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); } else { await this.currentConsumer?.SwitchRoomAsync( room.RoomType.ToString(), onOff); } }
async void OnDataChanged(ApplicationData sender, object args) { await this.Dispatch( async() => { // Something has written our data file so we should reread it. BuildingPersistence.Instance = await BuildingPersistence.LoadAsync(); // If we are showing that building on the screen then we need to refresh. if (this.isLocalBuilding) { // to ensure change notification fires. this.Building = null; this.Building = BuildingPersistence.Instance; } } ); }
/// <summary> /// Big note - we do not deal with cancellation in this background task /// and we should. We should handle the situation both the background /// task being cancelled and also the voice command firing its /// Completed event to do things properly /// </summary> /// <param name="taskInstance"></param> public async void Run(IBackgroundTaskInstance taskInstance) { var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails?.Name == "buildingCommandService") { var deferral = taskInstance.GetDeferral(); // Load up our stored local building configuration if not already loaded. if (BuildingPersistence.Instance == null) { BuildingPersistence.Instance = await BuildingPersistence.LoadAsync(); } // Start building a list of remote building configurations if not already // started.. RemoteLightControlManager.Initialise(BuildingPersistence.Instance.Name); // Get the connection to the 'Cortana' end of this conversation. var voiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); // We should handle voiceConnection.VoiceCommandCompleted here, we // don't yet. // What command has been issued? var voiceCommand = await voiceConnection.GetVoiceCommandAsync(); // Is this the command that we understand? if (voiceCommand.CommandName == "backgroundSwitchLights") { await this.ProcessCommandAsync(voiceConnection, voiceCommand.SpeechRecognitionResult); await voiceConnection.ReportSuccessAsync(MakeResponse("all done")); } else { // Need to report an error await ReportErrorAsync(voiceConnection, "the command name passed does not match across XML/code"); } deferral.Complete(); } }
async void OnUILightBulbPressed(object sender, PointerRoutedEventArgs e) { // Sorry about this, really should have bitten the bullet and written // viewmodels. var bulb = (LightBulb)sender; var light = (Light)bulb.Tag; light.IsOn = !light.IsOn; if (this.isLocalBuilding) { await BuildingPersistence.SaveAsync(BuildingPersistence.Instance); } else { await this.currentConsumer?.ToggleRoomLightAsync( light.Room.ToString(), light.Id); } e.Handled = true; }