private async Task SetAgentButton_Clicked(Button setAgentButton) { List <Input> agentSelectionInputs = new List <Input>(); // show any existing agents List <SensingAgent> currentAgents = null; // android allows us to dynamically load code assemblies, but iOS does not. so, the current approach // is to only support dynamic loading on android and force compile-time assembly inclusion on ios. #if __ANDROID__ // try to extract agents from a previously loaded assembly try { currentAgents = _protocol.GetAgents(_protocol.AgentAssemblyBytes); } catch (Exception) { } #elif __IOS__ currentAgents = _protocol.GetAgents(); // display warning message, as there is no other option to load agents. if (currentAgents.Count == 0) { await SensusServiceHelper.Get().FlashNotificationAsync("No agents available."); return; } #endif // let the user pick from currently available agents ItemPickerPageInput currentAgentsPicker = null; if (currentAgents != null && currentAgents.Count > 0) { currentAgentsPicker = new ItemPickerPageInput("Available agent" + (currentAgents.Count > 1 ? "s" : "") + ":", currentAgents.Select(agent => agent.Id).Cast <object>().ToList()) { Required = false }; agentSelectionInputs.Add(currentAgentsPicker); } #if __ANDROID__ // add option to scan qr code to import a new assembly QrCodeInput agentAssemblyUrlQrCodeInput = new QrCodeInput(QrCodePrefix.SENSING_AGENT, "URL:", false, "Agent URL:") { Required = false }; agentSelectionInputs.Add(agentAssemblyUrlQrCodeInput); #endif List <Input> completedInputs = await SensusServiceHelper.Get().PromptForInputsAsync("Sensing Agent", agentSelectionInputs, null, true, "Set", null, null, null, false); if (completedInputs == null) { return; } // check for QR code on android. this doesn't exist on ios. string agentURL = null; #if __ANDROID__ agentURL = agentAssemblyUrlQrCodeInput.Value?.ToString(); #endif // if there is no URL, check if the user has selected an agent. if (string.IsNullOrWhiteSpace(agentURL)) { if (currentAgentsPicker != null) { string selectedAgentId = (currentAgentsPicker.Value as List <object>).FirstOrDefault() as string; SensingAgent selectedAgent = null; if (selectedAgentId != null) { selectedAgent = currentAgents.First(currentAgent => currentAgent.Id == selectedAgentId); } // set the selected agent, watching out for a null (clearing) selection that needs to be confirmed if (selectedAgentId != null || await DisplayAlert("Confirm", "Are you sure you wish to clear the sensing agent?", "Yes", "No")) { _protocol.Agent = selectedAgent; setAgentButton.Text = "Set Agent" + (_protocol.Agent == null ? "" : ": " + _protocol.Agent.Id); if (_protocol.Agent == null) { await SensusServiceHelper.Get().FlashNotificationAsync("Sensing agent cleared."); } } } } #if __ANDROID__ else { // download agent assembly from scanned QR code byte[] downloadedBytes = null; string downloadErrorMessage = null; try { // download the assembly and extract agents downloadedBytes = _protocol.AgentAssemblyBytes = await new WebClient().DownloadDataTaskAsync(new Uri(agentURL)); List <SensingAgent> qrCodeAgents = _protocol.GetAgents(downloadedBytes); if (qrCodeAgents.Count == 0) { throw new Exception("No agents were present in the specified file."); } } catch (Exception ex) { downloadErrorMessage = ex.Message; } // if error message is null, then we have 1 or more agents in the downloaded assembly. if (downloadErrorMessage == null) { // redisplay the current input prompt including the agents we just downloaded _protocol.AgentAssemblyBytes = downloadedBytes; await SetAgentButton_Clicked(setAgentButton); } else { SensusServiceHelper.Get().Logger.Log(downloadErrorMessage, LoggingLevel.Normal, GetType()); await SensusServiceHelper.Get().FlashNotificationAsync(downloadErrorMessage); } } #endif }
public SensingAgentPage(SensingAgent sensingAgent) { Label idleLabel = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, Text = SensingAgentState.Idle.ToString(), BindingContext = sensingAgent }; idleLabel.SetBinding(BackgroundColorProperty, new Binding(nameof(SensingAgent.State), converter: new SensingAgentStateColorConverter(), converterParameter: SensingAgentState.Idle)); Label opportunisticObservationLabel = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, Text = SensingAgentState.OpportunisticObservation.ToString(), BindingContext = sensingAgent }; opportunisticObservationLabel.SetBinding(BackgroundColorProperty, new Binding(nameof(SensingAgent.State), converter: new SensingAgentStateColorConverter(), converterParameter: SensingAgentState.OpportunisticObservation)); Label opportunisticControlLabel = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, Text = SensingAgentState.OpportunisticControl.ToString(), BindingContext = sensingAgent }; opportunisticControlLabel.SetBinding(BackgroundColorProperty, new Binding(nameof(SensingAgent.State), converter: new SensingAgentStateColorConverter(), converterParameter: SensingAgentState.OpportunisticControl)); Label activeObservationLabel = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, Text = SensingAgentState.ActiveObservation.ToString(), BindingContext = sensingAgent }; activeObservationLabel.SetBinding(BackgroundColorProperty, new Binding(nameof(SensingAgent.State), converter: new SensingAgentStateColorConverter(), converterParameter: SensingAgentState.ActiveObservation)); Label activeControlLabel = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, Text = SensingAgentState.ActiveControl.ToString(), BindingContext = sensingAgent }; activeControlLabel.SetBinding(BackgroundColorProperty, new Binding(nameof(SensingAgent.State), converter: new SensingAgentStateColorConverter(), converterParameter: SensingAgentState.ActiveControl)); Label endingControlLabel = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, Text = SensingAgentState.EndingControl.ToString(), BindingContext = sensingAgent }; endingControlLabel.SetBinding(BackgroundColorProperty, new Binding(nameof(SensingAgent.State), converter: new SensingAgentStateColorConverter(), converterParameter: SensingAgentState.EndingControl)); Content = new ScrollView { Content = new StackLayout { Orientation = StackOrientation.Vertical, HorizontalOptions = LayoutOptions.FillAndExpand, Children = { idleLabel, opportunisticObservationLabel, opportunisticControlLabel, activeObservationLabel, activeControlLabel, endingControlLabel } } }; }