Ejemplo n.º 1
0
        public async override Task DoWork()
        {
            if (Controller.DidTimeoutOccur || Controller.DeviceEvent != DeviceEvent.None)
            {
                // recover device to idle
                IDeviceCancellationBroker cancellationBroker = Controller.GetDeviceCancellationBroker();

                LinkRequest          linkRequest      = StateObject as LinkRequest;
                LinkDeviceIdentifier deviceIdentifier = linkRequest.GetDeviceIdentifier();

                ICardDevice cardDevice = FindTargetDevice(deviceIdentifier);
                if (cardDevice != null)
                {
                    var timeoutPolicy = await cancellationBroker.ExecuteWithTimeoutAsync <bool>(
                        _ => cardDevice.DeviceRecovery(),
                        DeviceConstants.DeviceRecoveryTimeout,
                        this.CancellationToken);

                    if (timeoutPolicy.Outcome == Polly.OutcomeType.Failure)
                    {
                        //_ = Controller.LoggingClient.LogErrorAsync("Unable to recover device.");
                        Console.WriteLine("Unable to recover device.");
                    }
                }
            }

            if (StateObject != null)
            {
                Controller.SaveState(StateObject);
            }

            _ = Complete(this);
        }
        public override async Task DoWork()
        {
            if (StateObject is null)
            {
                //_ = Controller.LoggingClient.LogErrorAsync("Unable to find a state object while attempting to get device status.");
                Console.WriteLine("Unable to find a state object while attempting to get security configuration.");
                _ = Error(this);
            }
            else
            {
                LinkRequest linkRequest = StateObject as LinkRequest;
                IDeviceCancellationBroker cancellationBroker = Controller.GetDeviceCancellationBroker();

                List <LinkRequest> devicesRequest = new List <LinkRequest>();

                foreach (var device in Controller.TargetDevices)
                {
                    devicesRequest.Add(JsonConvert.DeserializeObject <LinkRequest>(JsonConvert.SerializeObject(linkRequest)));

                    var timeoutPolicy = await cancellationBroker.ExecuteWithTimeoutAsync <LinkRequest>(
                        _ => device.GetSecurityConfiguration(devicesRequest.Last()),
                        DeviceConstants.CardCaptureTimeout,
                        System.Threading.CancellationToken.None);

                    if (timeoutPolicy.Outcome == Polly.OutcomeType.Failure)
                    {
                        //_ = Controller.LoggingClient.LogErrorAsync($"Unable to obtain device status - '{Controller.DeviceEvent}'.");
                        Console.WriteLine($"Unable to obtain security configuration - '{Controller.DeviceEvent}'.");
                        BuildSubworkflowErrorResponse(linkRequest, device.DeviceInformation, Controller.DeviceEvent);
                    }
                }

                /*if (linkRequest.LinkObjects.LinkActionResponseList[0].DALResponse == null)
                 * {
                 *  linkRequest.LinkObjects.LinkActionResponseList[0].DALResponse = new LinkDeviceResponse();
                 * }
                 *
                 * if (linkRequest.LinkObjects.LinkActionResponseList[0].DALResponse.Devices == null)
                 * {
                 *  linkRequest.LinkObjects.LinkActionResponseList[0].DALResponse.Devices = new List<LinkDeviceResponse>();
                 * }
                 *
                 * foreach (var response in devicesRequest)
                 * {
                 *  if (response.LinkObjects.LinkActionResponseList[0].DALResponse?.Devices != null)
                 *  {
                 *      linkRequest.LinkObjects.LinkActionResponseList[0].DALResponse.Devices.Add(new LinkDeviceResponse
                 *      {
                 *          Manufacturer = response.LinkObjects.LinkActionResponseList[0].DALResponse?.Devices[0].Manufacturer,
                 *          Model = response.LinkObjects.LinkActionResponseList[0].DALResponse?.Devices[0].Model,
                 *          SerialNumber = response.LinkObjects.LinkActionResponseList[0].DALResponse?.Devices[0].SerialNumber
                 *      });
                 *  }
                 * }*/

                Controller.SaveState(linkRequest);

                _ = Complete(this);
            }
        }
        public override async Task DoWork()
        {
            if (StateObject is null)
            {
                //_ = Controller.LoggingClient.LogErrorAsync("Unable to find a state object while attempting to call abort command.");
                Console.WriteLine("Unable to find a state object while attempting to call abort command.");
                _ = Error(this);
            }
            else
            {
                LinkRequest               linkRequest        = StateObject as LinkRequest;
                LinkDeviceIdentifier      deviceIdentifier   = linkRequest.GetDeviceIdentifier();
                IDeviceCancellationBroker cancellationBroker = Controller.GetDeviceCancellationBroker();

                ICardDevice cardDevice = FindTargetDevice(deviceIdentifier);
                if (cardDevice != null)
                {
                    var timeoutPolicy = await cancellationBroker.ExecuteWithTimeoutAsync <LinkRequest>(
                        _ => cardDevice.AbortCommand(linkRequest),
                        DeviceConstants.CardCaptureTimeout,
                        this.CancellationToken);

                    if (timeoutPolicy.Outcome == Polly.OutcomeType.Failure)
                    {
                        //_ = Controller.LoggingClient.LogErrorAsync($"Unable to process Abort request from device - '{Controller.DeviceEvent}'.");
                        Console.WriteLine($"Unable to process Abort request from device - '{Controller.DeviceEvent}'.");
                        BuildSubworkflowErrorResponse(linkRequest, cardDevice.DeviceInformation, Controller.DeviceEvent);
                    }
                }
                else
                {
                    UpdateRequestDeviceNotFound(linkRequest, deviceIdentifier);
                }

                Controller.SaveState(linkRequest);

                _ = Complete(this);
            }
        }
Ejemplo n.º 4
0
        public override Task DoWork()
        {
            string             pluginPath            = Controller.PluginPath;
            List <ICardDevice> availableCardDevices  = null;
            List <ICardDevice> discoveredCardDevices = null;
            List <ICardDevice> validatedCardDevices  = null;

            try
            {
                availableCardDevices = Controller.DevicePluginLoader.FindAvailableDevices(pluginPath);

                // filter out devices that are disabled
                if (availableCardDevices.Count > 0)
                {
                    DeviceSection deviceSection = Controller.Configuration;
                    foreach (var device in availableCardDevices)
                    {
                        switch (device.ManufacturerConfigID)
                        {
                        case "IdTech":
                        {
                            device.SortOrder = deviceSection.IdTech.SortOrder;
                            break;
                        }

                        case "Verifone":
                        {
                            device.SortOrder = deviceSection.Verifone.SortOrder;
                            break;
                        }

                        case "Simulator":
                        {
                            device.SortOrder = deviceSection.Simulator.SortOrder;
                            break;
                        }
                        }
                    }
                    availableCardDevices.RemoveAll(x => x.SortOrder == -1);

                    if (availableCardDevices?.Count > 1)
                    {
                        availableCardDevices.Sort();
                    }
                }

                // Probe validated devices
                discoveredCardDevices = new List <ICardDevice>();
                validatedCardDevices  = new List <ICardDevice>();
                validatedCardDevices.AddRange(availableCardDevices);

                for (int i = validatedCardDevices.Count - 1; i >= 0; i--)
                {
                    if (string.Equals(availableCardDevices[i].ManufacturerConfigID, DeviceType.NoDevice.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    bool success = false;
                    try
                    {
                        List <DeviceInformation> deviceInformation = availableCardDevices[i].DiscoverDevices();

                        if (deviceInformation == null)
                        {
                            continue;
                        }

                        foreach (var deviceInfo in deviceInformation)
                        {
                            DeviceConfig deviceConfig = new DeviceConfig()
                            {
                                Valid = true
                            };
                            SerialDeviceConfig serialConfig = new SerialDeviceConfig
                            {
                                CommPortName = Controller.Configuration.DefaultDevicePort
                            };
                            deviceConfig.SetSerialDeviceConfig(serialConfig);

                            ICardDevice device = validatedCardDevices[i].Clone() as ICardDevice;

                            device.DeviceEventOccured += Controller.DeviceEventReceived;

                            // Device powered on status capturing: free up the com port and try again.
                            // This occurs when a USB device repowers the USB interface and the virtual port is open.
                            CancellationTokenSource   cancellationTokenSource = new CancellationTokenSource();
                            IDeviceCancellationBroker cancellationBroker      = Controller.GetCancellationBroker();
                            var timeoutPolicy = cancellationBroker.ExecuteWithTimeoutAsync <List <LinkErrorValue> >(
                                _ => device.Probe(deviceConfig, deviceInfo, out success),
                                Timeouts.DALGetStatusTimeout,
                                CancellationToken.None);

                            if (timeoutPolicy.Result.Outcome == Polly.OutcomeType.Failure)
                            {
                                Console.WriteLine($"Unable to obtain device status for - '{device.Name}'.");
                                device.DeviceEventOccured -= Controller.DeviceEventReceived;
                                device?.Dispose();
                                LastException = new StateException("Unable to find a valid device to connect to.");
                                _             = Error(this);
                                return(Task.CompletedTask);
                            }
                            else if (success)
                            {
                                discoveredCardDevices.Add(device);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"device: exception='{e.Message}'");

                        discoveredCardDevices[i].DeviceEventOccured -= Controller.DeviceEventReceived;

                        // Consume failures
                        if (success)
                        {
                            success = false;
                        }
                    }

                    if (success)
                    {
                        continue;
                    }

                    validatedCardDevices.RemoveAt(i);
                }
            }
            catch
            {
                availableCardDevices = new List <ICardDevice>();
            }

            if (discoveredCardDevices?.Count > 1)
            {
                discoveredCardDevices.Sort();
            }

            if (discoveredCardDevices?.Count > 0)
            {
                Controller.SetTargetDevices(discoveredCardDevices);
            }

            if (Controller.TargetDevices != null)
            {
                foreach (var device in Controller.TargetDevices)
                {
                    //Controller.LoggingClient.LogInfoAsync($"Device found: name='{device.Name}', model={device.DeviceInformation.Model}, " +
                    //    $"serial={device.DeviceInformation.SerialNumber}");
                    Console.WriteLine($"Device found: name='{device.Name}', model='{device?.DeviceInformation?.Model}', " +
                                      $"serial='{device?.DeviceInformation?.SerialNumber}'");
                    device.DeviceSetIdle();
                }
            }
            else
            {
                //Controller.LoggingClient.LogInfoAsync("Unable to find a valid device to connect to.");
                Console.WriteLine("Unable to find a valid device to connect to.");
                LastException = new StateException("Unable to find a valid device to connect to.");
                _             = Error(this);
                return(Task.CompletedTask);
            }

            _ = Complete(this);

            return(Task.CompletedTask);
        }