Ejemplo n.º 1
0
        public IrrigationProcessorTests()
        {
            _channel = Channel.CreateUnbounded <IrrigationJob>(
                new UnboundedChannelOptions()
            {
                AllowSynchronousContinuations = false,
                SingleWriter = true,
                SingleReader = true
            });

            _gpioDriver = new MemoryGpioDriver(pinCount: 50);

            var gpioController = new GpioController(PinNumberingScheme.Logical, _gpioDriver);

            _stopper = new IrrigationStopper();
            _config  = new IrrigationConfig
            {
                MasterControlValveGpio = 1,
                PressureBleedTime      = TimeSpan.Zero,
                ZoneSwitchDelay        = TimeSpan.Zero,
                Valves = new List <PinMapping>
                {
                    new PinMapping {
                        GpioPin = 2, ValveNumber = 1
                    },
                    new PinMapping {
                        GpioPin = 3, ValveNumber = 2
                    },
                    new PinMapping {
                        GpioPin = 4, ValveNumber = 3
                    }
                }
            };

            var pins       = (_config.Valves.Select(v => v.GpioPin).Union(new[] { _config.MasterControlValveGpio })).ToArray();
            var relayBoard = new RelayBoard(RelayType.NormallyOpen, gpioController, pins);

            var irrigationOptionsMock = new Mock <IOptions <IrrigationConfig> >();

            irrigationOptionsMock.SetupGet(x => x.Value).Returns(_config);

            _loggerMock = new Mock <ILogger <IrrigationProcessor> >();
            _status     = new IrrigationProcessorStatus();

            _processor = new IrrigationProcessor(_channel.Reader, relayBoard, _stopper, _status, irrigationOptionsMock.Object, _loggerMock.Object);

            //before the Microsoft GPIO controller can be used, the pins must be opened and set to the desired input/output state
            //run our initializer code to make that happen
            var pinInitializer = new PinInitializer(relayBoard, irrigationOptionsMock.Object);

            pinInitializer.StartAsync(CancellationToken.None).Wait();
        }
        public void RequestStop_RaisesStopEvent(bool wireEvent, bool expectedRaised)
        {
            var stopRaised = false;

            void OnStop(object sender, EventArgs e)
            {
                stopRaised = true;
            }

            var stopper = new IrrigationStopper();

            if (wireEvent)
            {
                stopper.StopRequested += OnStop;
            }

            stopper.RequestStop();

            Assert.Equal(expectedRaised, stopRaised);
        }