Ejemplo n.º 1
0
        private void GetResult(Guid verResultJobId)
        {
            bool gotIt    = false;
            int  waitTime = 1000;
            int  cnt      = 0;

            do
            {
                Thread.Sleep(waitTime);
                UpdateProgress(cnt++ % 2 == 0 ? $"Getting result..." : $"Please wait...");

                try
                {
                    using (var client = SslScsServiceClientBuilder.CreateSslClient <INetworkBVPSService>(
                               new ScsTcpEndPoint(Program.ServerIp, Program.ServerPort)
                               , Program.ServerPublicKey
                               , Program.ServerIp
                               , SslScsAuthMode.ServerAuth
                               , null
                               ))
                    {
                        client.Timeout = 10 * 60 * 1000;//timeout 10 minutes
                        var details = new RequiredCommunicationDetails
                        {
                            JobId           = verResultJobId,
                            DeviceID        = Program.DeviceId,
                            TimeStamp       = DateTime.Now,
                            ScanningDetails = RequiredDetailsHelper.GetRequiredScanningDetails()
                        };


                        var requiredDetails = new RequiredCommunicationDetailsPacket
                        {
                            DeviceID             = Program.DeviceId,
                            CommunicationDetails = details
                        };

                        requiredDetails.PrepareForSending(Program.AuthKey);

                        client.Connect();
                        var verResult = client.ServiceProxy.GetResult(requiredDetails);


                        switch (verResult.ScanningResult)
                        {
                        case ScanningReturnResult.InvalidAuthentication:
                            LogHelper.LogMessageToAll(Program.FileLogger, null, $"Scanning Refused by the server, due to ${ScanningReturnResult.InvalidAuthentication}, ID={Program.DeviceId}", LogMsgType.Warning);
                            break;

                        case ScanningReturnResult.Error:
                            MessageBox.Show(@"UNKNOWN ERROR, try again later!");
                            gotIt = true;
                            break;

                        case ScanningReturnResult.Success:
                            LogHelper.LogMessageToAll(Program.FileLogger, null, $"Scan result received succesfully for Device ID={Program.DeviceId} \n Received Response from Server {verResult.Message}", LogMsgType.Debug);
                            var report = ReportingHelper.GetCompleteReport(verResult.CompressedHTML);
                            var dial   = new SaveFileDialog();
                            dial.FileName = "Report.html";
                            dial.Filter   = "html files (*.html)|*.html";
                            BeginInvoke((Action)(() =>
                            {
                                if (dial.ShowDialog() == DialogResult.OK)
                                {
                                    File.WriteAllText(dial.FileName, report);
                                    LogHelper.LogMessageToAll(Program.FileLogger, null, $"User saved the report to {dial.FileName}", LogMsgType.Warning);
                                    Process.Start(dial.FileName);
                                }
                                else
                                {
                                    LogHelper.LogMessageToAll(Program.FileLogger, null, $"User selected not to save the report", LogMsgType.Warning);
                                }
                            }), null);


                            gotIt = true;
                            break;

                        case ScanningReturnResult.StillScanning:
                            waitTime *= 2;
                            break;

                        case ScanningReturnResult.ServerBusyJobRefused:
                            LogHelper.LogMessageToAll(Program.FileLogger, null, $"Scanning Refused by the server, due to ${ScanningReturnResult.ServerBusyJobRefused}, ID={Program.DeviceId}", LogMsgType.Warning);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(@"exception [most probably server is not running] " + ex);
                    Program.FileLogger.Log(@"exception [most probably server is not running] " + ex.Message,
                                           LogMsgType.Exception);
                }
            } while (!gotIt);
            EnableUI();
        }