Exemple #1
0
 public async Task FanOn()
 {
     await Task.Run(() =>
     {
         RelayBoard.Set(RelayNumbers.K05, Relaystate.ON);
     });
 }
Exemple #2
0
 private void ForwardButton_Click(object sender, EventArgs e)
 {
     RelayBoard.Set(RelayNumbers.All, Relaystate.OFF);
     RelayBoard.Set(RelayNumbers.K03, Relaystate.ON);
     Thread.Sleep(1000);
     RelayBoard.Set(RelayNumbers.K03, Relaystate.OFF);
 }
Exemple #3
0
        public void SingleInputSingleOutput()
        {
            var board = new RelayBoard();

            var input  = new RelayInputMock("I");
            var output = new RelayOutputMock("O");

            board.Connect(input, output);
            board.Initialize();

            output.Check(false);

            input.Notify();
            output.Check(true);

            output.Reset();
            output.Check(false);

            input.Notify();
            output.Check(true);

            input.Notify();
            output.Check(true);

            output.Reset();
            output.Check(false);

            output.Reset();
            output.Check(false);

            board.Dispose();
        }
Exemple #4
0
 /// <summary>
 /// Initializes this instance.
 /// </summary>
 /// <returns>Task.</returns>
 public async Task Initialize()
 {
     _timer          = new DispatcherTimer();
     _timer.Tick    += Timer_Elapsed;
     _timer.Interval = TimeSpan.FromMilliseconds(90000);
     await RelayBoard.InitializeAsync();
 }
Exemple #5
0
        public PinInitializerTests()
        {
            _config = new IrrigationConfig
            {
                MasterControlValveGpio = 1,
                PressureBleedTime      = TimeSpan.FromSeconds(0),
                Valves = new List <PinMapping>
                {
                    new PinMapping {
                        GpioPin = 2, ValveNumber = 1
                    },
                    new PinMapping {
                        GpioPin = 3, ValveNumber = 2
                    },
                    new PinMapping {
                        GpioPin = 4, ValveNumber = 3
                    }
                }
            };

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

            irrigationOptionsMock.SetupGet(x => x.Value).Returns(_config);
            _gpioDriver = new MemoryGpioDriver(pinCount: 50);


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

            _pinInitializer = new PinInitializer(relayBoard, irrigationOptionsMock.Object);
        }
Exemple #6
0
 /// <summary>
 /// Initializes this instance.
 /// </summary>
 /// <returns>Task.</returns>
 public async Task Initialize()
 {
     _timer          = new System.Windows.Forms.Timer();
     _timer.Tick    += Timer_Elapsed;
     _timer.Interval = 90000;
     await RelayBoard.InitializeAsync();
 }
Exemple #7
0
 private void ReverseButton_Click(object sender, EventArgs e)
 {
     RelayBoard.Set(RelayNumbers.All, Relaystate.OFF);
     RelayBoard.Set(RelayNumbers.K01 | RelayNumbers.K02, Relaystate.ON);
     RelayBoard.Set(RelayNumbers.K04, Relaystate.ON);
     Thread.Sleep(1000);
     RelayBoard.Set(RelayNumbers.K04, Relaystate.OFF);
 }
Exemple #8
0
 public async Task LampOff()
 {
     await Task.Run(() =>
     {
         RelayBoard.Set(RelayNumbers.K06, Relaystate.OFF);
         _timer.Start();
     });
 }
Exemple #9
0
        public void SingleInputManyOutputs()
        {
            const int nbSusbscriber = 10;

            var board = new RelayBoard();

            var input   = new RelayInputMock("I");
            var outputs = new RelayOutputMock[nbSusbscriber];

            for (var i = 0; i < nbSusbscriber; i++)
            {
                outputs[i] = new RelayOutputMock("O" + (i + 1));
            }

            for (var i = 0; i < nbSusbscriber; i++)
            {
                board.Connect(input, outputs[i]);
            }
            board.Initialize();

            for (var i = 0; i < nbSusbscriber; i++)
            {
                outputs[i].Check(false);
            }

            input.Notify();
            for (var i = 0; i < nbSusbscriber; i++)
            {
                outputs[i].Check(true);
            }

            for (var i = 0; i < nbSusbscriber; i++)
            {
                outputs[i].Reset();
                outputs[i].Check(false);
            }

            input.Notify();
            for (var i = 0; i < nbSusbscriber; i++)
            {
                outputs[i].Check(true);
            }

            input.Notify();
            for (var i = 0; i < nbSusbscriber; i++)
            {
                outputs[i].Check(true);
            }

            for (var i = 0; i < nbSusbscriber; i++)
            {
                outputs[i].Reset();
                outputs[i].Check(false);
            }

            board.Dispose();
        }
Exemple #10
0
        public async Task LampOn()
        {
            await FanOn();

            await Task.Run(() =>
            {
                RelayBoard.Set(RelayNumbers.K06, Relaystate.ON);
                _lampWasOn = true;
            });
        }
Exemple #11
0
 public async Task Forward()
 {
     await Task.Run(() =>
     {
         RelayBoard.Set(RelayNumbers.K01 | RelayNumbers.K02 | RelayNumbers.K04, Relaystate.OFF); // clear all
         Thread.Sleep(50);
         RelayBoard.Set(RelayNumbers.K03, Relaystate.ON);                                        // Set direction
         Thread.Sleep(500);
         RelayBoard.Set(RelayNumbers.K03, Relaystate.OFF);
     });
 }
Exemple #12
0
 public async void Initialize()
 {
     try
     {
         await RelayBoard.InitializeAsync();
     }
     catch (RelayBoardException ex)
     {
         MessageBox.Show(ex.Message, "RelayBoardException", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemple #13
0
 /// <summary>
 /// Handles the Click event of the ResetRelaysMeu control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
 private async void ResetRelaysMeu_Click(object sender, EventArgs e)
 {
     try
     {
         await Task.Run(() => RelayBoard.Initialize(true));
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Relay Board Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemple #14
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();
        }
Exemple #15
0
        public void UnsubcribeUseCaseFailed()
        {
            var board = new RelayBoard();

            var i1 = new RelayInputMock("I1");
            var i2 = new RelayInputMock("I2");
            var i3 = new RelayInputMock("I3");
            var i4 = new RelayInputMock("I4");

            var o1 = new RelayOutputMock("O1");

            var c1 = board.Connect(i1, o1);
            var s1 = c1.Subscribe(p => { });
            var c2 = board.Connect(i2, o1);
            var s2 = c2.Subscribe(p => { });
            var c3 = board.Connect(i3, o1);
            var s3 = c3.Subscribe(p => { });
            var c4 = board.Connect(i4, o1);
            var s4 = c4.Subscribe(p => { });

            board.Initialize();

            o1.Check(false);

            i1.Notify();
            board.Poll(DateTime.Now);
            o1.CheckAndReset(true);

            i1.Notify();
            board.Poll(DateTime.Now);
            o1.CheckAndReset(true);

            i1.Notify();
            i2.Notify();
            i4.Notify();
            board.Poll(DateTime.Now);
            o1.CheckAndReset(true);

            i4.Notify();
            board.Poll(DateTime.Now);
            o1.CheckAndReset(true);

            c2.Dispose();
            s2.Dispose();

            i4.Notify();
            i1.Notify();
            board.Poll(DateTime.Now);
            o1.CheckAndReset(true);

            board.Dispose();
        }
Exemple #16
0
 /// <summary>
 /// Handles the Click event of the ResetRelaysButton control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
 private async void ResetRelaysButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         await RelayBoard.InitializeAsync(true);
     }
     catch (RelayBoardException)
     {
         LampToggleButton.IsEnabled = false;
         FanToggleButton.IsEnabled  = false;
         LoadButton.IsEnabled       = false;
     }
 }
Exemple #17
0
 public async Task FanOff(bool force = false)
 {
     await Task.Run(() =>
     {
         if (_lampWasOn && !force)
         {
             _timer.Start();
         }
         else
         {
             RelayBoard.Set(RelayNumbers.K05, Relaystate.OFF);
         }
     });
 }
Exemple #18
0
        public void ManyInputsManyOutputs()
        {
            const int nbInputs  = 10;
            const int nbOutputs = 10;

            var board = new RelayBoard();

            var inputs  = new RelayInputMock[nbInputs];
            var outputs = new RelayOutputMock[nbOutputs];

            for (var i = 0; i < outputs.Length; i++)
            {
                outputs[i] = new RelayOutputMock("S" + (i + 1));
            }

            for (var i = 0; i < inputs.Length; i++)
            {
                inputs[i] = new RelayInputMock("F" + (i + 1));
            }

            for (var i = 0; i < outputs.Length; i++)
            {
                for (var j = 0; j <= i && j < inputs.Length; j++)
                {
                    board.Connect(inputs[j], outputs[i]);
                }
            }

            board.Initialize();

            // Check if all subscribers are invalid first
            for (var i = 0; i < outputs.Length; i++)
            {
                outputs[i].Check(false);
            }

            for (var j = 0; j < inputs.Length; j++)
            {
                inputs[j].Notify();
                for (var i = 0; i < outputs.Length; i++)
                {
                    outputs[i].Check(i >= j);
                    outputs[i].Reset();
                }
            }
        }
Exemple #19
0
        public void ManyInputsSingleOutput()
        {
            const int nbInputs = 10;

            var board  = new RelayBoard();
            var output = new RelayOutputMock("O");

            var inputs = new RelayInputMock[nbInputs];

            for (var i = 0; i < nbInputs; i++)
            {
                inputs[i] = new RelayInputMock("I" + (i + 1));
                board.Connect(inputs[i], output);
            }

            board.Initialize();

            output.Check(false);

            for (var i = 0; i < inputs.Length; i++)
            {
                inputs[i].Notify();
                output.Check(true);

                output.Reset();
                output.Check(false);
            }

            for (var i = 0; i < inputs.Length; i++)
            {
                inputs[i].Notify();
            }
            output.Check(true);

            output.Reset();
            output.Check(false);

            for (var i = 0; i < inputs.Length; i++)
            {
                inputs[i].Notify();
                output.Check(true);

                output.Reset();
                output.Check(false);
            }
        }
Exemple #20
0
        public IrrigationProcessor(ChannelReader <IrrigationJob> jobReader,
                                   RelayBoard relayBoard,
                                   IIrrigationStopper irrigationStopper,
                                   IrrigationProcessorStatus status,
                                   IOptions <IrrigationConfig> config,
                                   ILogger <IrrigationProcessor> logger)
        {
            _jobReader         = jobReader;
            _relayBoard        = relayBoard;
            _irrigationStopper = irrigationStopper;
            _config            = config.Value;
            _logger            = logger;
            _status            = status;

            _irrigationCts = new CancellationTokenSource();
            _irrigationStopper.StopRequested += StopIrrigation;
        }
Exemple #21
0
        public void UnsubcribeCallbackManyInputSingleOutput()
        {
            var board = new RelayBoard();

            var i1 = new RelayInputMock("I1");
            var i2 = new RelayInputMock("I2");
            var o1 = new RelayOutputMock("O1");

            var c1 = board.Connect(i1, o1)
                     .Subscribe(p => { });
            var c2 = board.Connect(i2, o1)
                     .Subscribe(p => { });

            board.Initialize();

            o1.Check(false);

            i1.Notify();
            o1.Check(true);

            o1.Reset();
            o1.Check(false);

            i2.Notify();
            o1.Check(true);

            o1.Reset();
            o1.Check(false);

            c2.Dispose();

            i1.Notify();
            o1.Check(true);

            o1.Reset();
            o1.Check(false);

            i2.Notify();
            o1.Check(true);

            board.Dispose();
        }
Exemple #22
0
        public void UnsubcribeConnectorSingleInputManyOutput()
        {
            var board = new RelayBoard();

            var i1 = new RelayInputMock("I1");
            var o1 = new RelayOutputMock("O1");
            var o2 = new RelayOutputMock("O2");

            var c1 = board.Connect(i1, o1);

            c1.Subscribe(p => { });
            var c2 = board.Connect(i1, o2);

            c2.Subscribe(p => { });

            board.Initialize();
            Console.WriteLine(board.Report());

            o1.Check(false);
            o2.Check(false);

            i1.Notify();
            o1.Check(true);
            o2.Check(true);

            o1.Reset();
            o2.Reset();
            o1.Check(false);
            o2.Check(false);

            c2.Dispose();
            Console.WriteLine(board.Report());

            i1.Notify();
            o1.Check(true);
            o2.Check(false);

            board.Dispose();
        }
Exemple #23
0
 public PinInitializer(RelayBoard relayBoard, IOptions <IrrigationConfig> config)
 {
     _relayBoard = relayBoard;
     _config     = config.Value;
 }
Exemple #24
0
        void Relays_CheckedChanged(object sender, EventArgs e)
        {
            var box = sender as CheckBox;

            RelayBoard.Set((RelayNumbers)box.Tag, box.Checked ? Relaystate.ON : Relaystate.OFF);
        }
Exemple #25
0
        public void RandomSetup(int nbInputs, int nbOutputs, double percentOfObyI)
        {
            var random = new Random(42);

            var inputs = new RelayInputMock[nbInputs];

            for (var i = 0; i < inputs.Length; i++)
            {
                inputs[i] = new RelayInputMock("Feed" + (i + 1));
            }

            var outputs = new RelayOutputMock[nbOutputs];

            for (var i = 0; i < outputs.Length; i++)
            {
                outputs[i] = new RelayOutputMock("S" + (i + 1));
            }

            var dependencies = new Dictionary <string, List <string> >();
            var manager      = new RelayBoard();

            for (var i = 0; i < inputs.Length; i++)
            {
                var list = new List <string>();
                dependencies[inputs[i].Name] = list;
                for (var j = 0; j < outputs.Length; j++)
                {
                    if (random.NextDouble() <= percentOfObyI)
                    {
                        manager.Connect(inputs[i], outputs[j]);
                        list.Add(outputs[j].Name);
                    }
                }
            }

            manager.Initialize();
            Console.WriteLine(manager.Report());

            var now            = DateTime.Now;
            var iterations     = 150;
            var percentOfNotif = 0.77;
            var outByName      = outputs.ToDictionary(p => p.Name, p => p);

            var hash       = new HashSet <string>();
            var stack      = new Stack <string>();
            var timestamps = outputs.ToDictionary(p => p.Name, p => DateTime.MinValue);

            for (var i = 0; i < iterations; i++)
            {
                now = now.AddSeconds(i);
                for (var j = 0; j < inputs.Length; j++)
                {
                    if (random.NextDouble() <= percentOfNotif)
                    {
                        inputs[j].Notify();
                        stack.Push(inputs[j].Name);
                    }
                }

                while (stack.Count > 0)
                {
                    var deps = dependencies[stack.Pop()];
                    foreach (var dep in deps)
                    {
                        if (hash.Contains(dep))
                        {
                            continue;
                        }
                        outByName[dep].Check(true);
                        timestamps[dep] = now;
                        hash.Add(dep);
                    }
                }

                for (var j = 0; j < outputs.Length; j++)
                {
                    if (!hash.Contains(outputs[i].Name))
                    {
                        outputs[i].Check(false);
                    }
                    outputs[j].Reset();
                }

                hash.Clear();
            }
        }
Exemple #26
0
        public void Benchmark(int nbInputs, int nbOutputs, double percentOfObyI)
        {
            var random = new Random(42);

            var inputs = new RelayInputMock[nbInputs];

            for (var i = 0; i < inputs.Length; i++)
            {
                inputs[i] = new RelayInputMock("Feed" + (i + 1));
            }

            var outputs = new RelayOutputMock[nbOutputs];

            for (var i = 0; i < outputs.Length; i++)
            {
                outputs[i] = new RelayOutputMock("S" + (i + 1));
            }

            var manager = new RelayBoard();

            for (var i = 0; i < inputs.Length; i++)
            {
                var list = new List <string>();
                for (var j = 0; j < outputs.Length; j++)
                {
                    if (random.NextDouble() <= percentOfObyI)
                    {
                        manager.Connect(inputs[i], outputs[j]);
                        list.Add(outputs[j].Name);
                    }
                }
            }

            manager.Initialize();
            Console.WriteLine(manager.Report());

            var iterations     = 1500;
            var percentOfNotif = 0.77;

            var stack = new Stack <string>();

            var randoms = new double[iterations * inputs.Length];

            for (var i = 0; i < iterations; i++)
            {
                randoms[i] = random.NextDouble();
            }

            var jitterIterations = (int)(iterations * 0.1);

            for (int i = 0, k = 0; i < jitterIterations; i++)
            {
                for (var j = 0; j < inputs.Length; j++)
                {
                    if (randoms[k++] <= percentOfNotif)
                    {
                        inputs[j].Notify();
                        stack.Push(inputs[j].Name);
                    }
                }

                for (var j = 0; j < outputs.Length; j++)
                {
                    if (outputs[j].IsInvalidated)
                    {
                        outputs[j].Reset();
                    }
                }
            }

            var sw = Stopwatch.StartNew();

            for (int i = 0, k = 0; i < iterations; i++)
            {
                for (var j = 0; j < inputs.Length; j++)
                {
                    if (randoms[k++] <= percentOfNotif)
                    {
                        inputs[j].Notify();
                        stack.Push(inputs[j].Name);
                    }
                }

                for (var j = 0; j < outputs.Length; j++)
                {
                    if (outputs[j].IsInvalidated)
                    {
                        outputs[j].Reset();
                    }
                }
            }
            sw.Stop();
            Console.WriteLine("Elapsed: {0} ms", sw.Elapsed.TotalMilliseconds);
        }
Exemple #27
0
        public Test()
        {
            InitializeComponent();
            try { RelayBoard.Initialize(); }
            catch (Exception ex)
            {
                SlideProjectorGroupBox.Enabled = false;
            }
            CameraGroupBox.Enabled = true;
            try
            {
                Camera.Initialize();
                LiveView.Initialize(Camera.GetDevice());

                //  Configure ISO slider
                _isos = Camera.GetIsos();
                IsoBar.SetRange(0, _isos.Length - 1);
                IsoBar.Scroll += (sender, e) => IsoLabel.Text = _isos[IsoBar.Value];
                string iso = Camera.GetIso();
                if (iso != null)
                {
                    IsoBar.Value  = _isos.ToList().IndexOf(iso);
                    IsoLabel.Text = iso;
                }

                // Configure Aperture slider
                _apertures = Camera.GetApertures();
                ApertureBar.SetRange(0, _apertures.Length - 1);
                ApertureBar.Scroll += (sender, e) => ApertureLabel.Text = _apertures[ApertureBar.Value];
                string aperture = Camera.GetAperture();
                if (aperture != null)
                {
                    ApertureBar.Value  = _apertures.ToList().IndexOf(aperture);
                    ApertureLabel.Text = aperture;
                }

                // Configure shutter speed
                _shutters = Camera.GetShutterSpeeds();
                ShutterSpeedBar.SetRange(0, _shutters.Length - 1);
                ShutterSpeedBar.Scroll += (sender, e) => ShutterSpeedLabel.Text = _shutters[ShutterSpeedBar.Value];
                string shutter = Camera.GetShutterSpeed();
                if (shutter != null)
                {
                    ShutterSpeedBar.Value  = _shutters.ToList().IndexOf(shutter);
                    ShutterSpeedLabel.Text = shutter;
                }

                // Configure exposure
                _exposures = Camera.GetExposureCompensations();
                ExposureCompensationBar.SetRange(0, _exposures.Length - 1);
                ExposureCompensationBar.Scroll += (sender, e) => ExposureCompensationLabel.Text = _exposures[ExposureCompensationBar.Value];
                string exposure = Camera.GetExposureCompensation();
                if (exposure != null)
                {
                    ExposureCompensationBar.Value = _exposures.ToList().IndexOf(exposure);
                    ShutterSpeedLabel.Text        = exposure;
                }
            }
            catch (Exception ex)
            {
                CameraGroupBox.Enabled = false;
            }
        }
Exemple #28
0
 public async Task Unload()
 {
     await Task.Run(() => RelayBoard.Set(RelayNumbers.K07 | RelayNumbers.K08, Relaystate.ON));
 }