Ejemplo n.º 1
0
        private async void InstanceOnItemClicked(object sender, HostSelectionDataSource.ServerDataItem e)
        {
            var endpointAddress = new HostEndpointAddress(e.EndPoint.Address.ToString(), e.HttpsPorts[0]);

            try
            {
                var portOpen = await SocketHelper.IsPortOpenAsync(endpointAddress.IpAddress, endpointAddress.Port, TimeSpan.FromSeconds(1));

                if (!portOpen)
                {
                    ToastHelper.Display($"Failed to connect to {endpointAddress.FullAddress}", ToastLength.Long);
                    return;
                }

                using (var agent = GrpcApplicationAgentFactory.Create(GrpcChannelHub.GetChannelFor(endpointAddress)))
                {
                    var authenticated = await CheckIsAuthenticatedAsync(agent);

                    if (!authenticated)
                    {
                        if (!await TryUpdateAuthenticationAsync(agent, endpointAddress))
                        {
                            return;
                        }
                    }

                    NavigateToHost(e);
                }
            }
            catch (Exception exception)
            {
                ToastHelper.Display($"Failed to connect to {endpointAddress.FullAddress}", ToastLength.Long);
                Log.Error(exception);
            }
        }
Ejemplo n.º 2
0
        protected override async Task <Data> DoWorkAsync(CancellationToken cancellationToken)
        {
            var notificationId = (GetStateKind() + "+" + _agentAddress).GetHashCode();

            Log.Debug("Spawned notification with Id {Id}", notificationId);
            try
            {
                var scheduled = await SystemStateManager.GetScheduledTimeAsync(_agentAddress, GetStateKind());

                if (scheduled == null || DateTime.Now > scheduled.Value)
                {
                    Log.Info("A system state event passed while the device was not working.");
                    return(Data.Empty);
                }

                var progressDataBuilder = new Data.Builder();

                UpdateProgress(progressDataBuilder, 0, false);
                if (!HostEndpointAddress.TryParse(_agentAddress, out var address))
                {
                    Log.Warn("Address {Address} could not be parsed - aborting process", _agentAddress);
                    return(Data.Empty);
                }

                using (var agent = GrpcApplicationAgentFactory.Create(GrpcChannelHub.GetChannelFor(address)))
                {
                    var hostName = await agent.DesktopClient.GetHostNameAsync(TimeSpan.FromSeconds(5));

                    var notification = NotificationHelper.DisplayNotification(notificationId, builder =>
                    {
                        builder.SetCategory(NotificationCompat.CategoryProgress);
                        builder.SetContentTitle(GetNotificationTitle(hostName));
                        builder.SetOnlyAlertOnce(true);
                        builder.SetSmallIcon(GetNotificationIcon());
                        var intent = new Intent();
                        intent.SetAction(GetAbortAction());
                        intent.PutExtra(AbortBroadcastReceiver.NotificationIdTag, notificationId);
                        intent.PutExtra(AbortBroadcastReceiver.WorkIdTag, Id.ToString());
                        intent.PutExtra(AbortBroadcastReceiver.HostAddressTag, _agentAddress);
                        var pendingIntent = PendingIntent.GetBroadcast(Application.Context, notificationId, intent, PendingIntentFlags.OneShot);
                        builder.AddAction(Android.Resource.Drawable.ButtonPlus, "Abort", pendingIntent);
                    }, GetNotificationChannel());

                    SetForegroundAsync(new ForegroundInfo(notificationId, notification.Build()));

                    var startDifference = scheduled.Value - DateTime.Now;
                    while (scheduled > DateTime.Now)
                    {
                        var currentDifference = scheduled.Value - DateTime.Now;
                        var progress          = (100f / startDifference.TotalSeconds) * currentDifference.TotalSeconds;
                        notification.SetProgress(100, (int)progress, false);
                        UpdateProgress(progressDataBuilder, progress, true);
                        notification.SetContentText(currentDifference.ToString("hh\\:mm\\:ss"));

                        NotificationHelper.UpdateNotification(ApplicationContext, notificationId, notification);

                        await Task.Delay(1000, cancellationToken);
                    }

                    UpdateProgress(progressDataBuilder, 0, false);
                    NotificationHelper.DestroyNotification(notificationId);
                    SystemStateManager.Clear(_agentAddress, GetStateKind());

                    var result = await ExecuteFinalizerAsync(agent);

                    Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
                    {
                        if (result)
                        {
                            ToastHelper.Display(GetSuccessToastMessage(hostName), ToastLength.Long);
                        }
                        else
                        {
                            ToastHelper.Display("Error", ToastLength.Short);
                        }
                    });
                }
            }
            catch (RpcException e)
            {
                Log.Debug(e, "Cancelling work because of an RPC exception");
                WorkManager.GetInstance(ApplicationContext).CancelWorkById(Id);
            }

            return(Data.Empty);
        }