Beispiel #1
0
        public void TC006_TestWrite()
        {
            //Arrange
            string         newFile      = "state_test.yml";
            State          testState    = new State("state.yml");
            State          newTestState = new State(newFile);
            StateException exception    = null;

            //Act
            try
            {
                newTestState.Write(testState.ToHash());
            }
            catch (StateException stateException)
            {
                exception = stateException;
            }

            //Assert
            Assert.IsNull(exception);
            Assert.AreEqual(newTestState.Job.Name, testState.Job.Name);
        }
Beispiel #2
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);
        }
        //static private LinkDeviceActionType lastDeviceAction = LinkDeviceActionType.GetStatus;

        //static private bool hasStatus;

        public override bool DoDeviceDiscovery()
        {
            LastException = new StateException("device recovery is needed");
            _             = Error(this);
            return(true);
        }