示例#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);
        }
示例#2
0
        public bool DiscoverDevices(string name)
        {
            ICardDevice device = DeviceProvider.GetDevice(name);

            if (device == null)
            {
                throw new Exception(StringValueAttribute.GetStringValue(DeviceDiscovery.NoDeviceAvailable));
            }

            return(device.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        }
        private protected ICardDevice FindMatchingDevice(LinkDeviceIdentifier deviceIdentifier)
        {
            ICardDevice cardDevice = null;

            foreach (var device in Controller.TargetDevices)
            {
                if (device.DeviceInformation != null)
                {
                    if (device.DeviceInformation.Manufacturer.Equals(deviceIdentifier.Manufacturer, StringComparison.CurrentCultureIgnoreCase) &&
                        device.DeviceInformation.Model.Equals(deviceIdentifier.Model, StringComparison.CurrentCultureIgnoreCase) &&
                        device.DeviceInformation.SerialNumber.Equals(deviceIdentifier.SerialNumber, StringComparison.CurrentCultureIgnoreCase))
                    {
                        cardDevice = device;
                        break;
                    }
                }
            }

            return(cardDevice);
        }
        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);
            }
        }
示例#5
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);
        }
        private async Task ProcessListenerRequest(LinkRequest linkRequest)
        {
            try
            {
                //_ = Controller.LoggingClient.LogInfoAsync($"Received from {linkRequest}");
                Console.WriteLine($"Received from {linkRequest}");
                LinkRequest linkRequestResponse = null;
                bool        deviceConnected     = false;

                // Setup workflow
                WorkflowOptions workflowOptions = new WorkflowOptions();
                workflowOptions.StateObject = linkRequest;

                Controller.SaveState(workflowOptions);

                // Payment: targeted device
                if (linkRequest.Actions?[0]?.Action == LinkAction.Payment ||
                    linkRequest.Actions?[0]?.Action == LinkAction.DALAction)
                {
                    LinkDeviceIdentifier deviceIdentifier = linkRequest.GetDeviceIdentifier();
                    ICardDevice          cardDevice       = FindTargetDevice(deviceIdentifier);
                    if (cardDevice != null)
                    {
                        deviceConnected = cardDevice.IsConnected(linkRequest);
                        if (deviceConnected)
                        {
                            workflowOptions.StateObject = linkRequest;
                        }
                    }
                }
                else
                {
                    // Session: device discovery
                    foreach (var device in Controller.TargetDevices)
                    {
                        deviceConnected = device.IsConnected(linkRequest);
                        if (deviceConnected)
                        {
                            workflowOptions.StateObject = linkRequest;
                        }
                    }
                }

                if (deviceConnected)
                {
                    Controller.SaveState(workflowOptions);
                }
                else
                {
                    //linkRequestResponse = RequestHelper.LinkRequestResponseError(linkRequest, ErrorCodes.NoDevice, ErrorDescriptions.DeviceNotFound);

                    object response = JsonConvert.SerializeObject(linkRequestResponse);
                    //_ = Controller.LoggingClient.LogInfoAsync($"Sending to Listener");
                    Console.WriteLine($"Sending to Listener");
                    //await Controller.Connector.Publish(response, new TopicOption[] { TopicOption.Servicer }).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                //_ = Controller.LoggingClient.LogErrorAsync($"{e.Message} {linkRequest}");
                Console.WriteLine($"{e.Message} {linkRequest}");
                //if (Controller.Connector != null)
                //{
                //    await Controller.Connector.Publish(RequestHelper.LinkRequestResponseError(null, ErrorCodes.DalError, e.Message), new TopicOption[] { TopicOption.Servicer }).ConfigureAwait(false);
                //}
            }
        }