public void GetPumpsState_PumpList_ReturnsPumpStateForEachPump()
        {
            var forecourt = new EZTech(new EZTechMockIO());
            var pump1 = new Pump(1);
            var pump2 = new Pump(2);
            var pumpList = new List<Pump>() { pump1, pump2 };

            var dic = forecourt.GetPumpsState(pumpList);
            forecourt.Dispose();

            dic[pump1].Should().Be(PumpState.Delivering);
            dic[pump2].Should().Be(PumpState.Free);
        }
        private void InitializeHosesAndPumps() 
        { 
            _pumpList = new List<Pump>(); 
            _hoseList = new List<Hose>();   

            for (int i = 1; i <= 32; i++) 
            {
                var status = string.Empty;
                var result = _wayneIO.SendAndReceive(WayneCommands.GetPumpConfiguration, i.ToString().PadLeft(2, '0'), ref status);

                if (status == "00")
                {
                    var hoseCount = int.Parse(result.Substring(5, 1));
                    var pump = new Pump(i);

                    for (int j = 1; j <= hoseCount; j++)
                    {
                        var hose = new Hose(GetHoseID(i, j));
                        _hoseList.Add(hose);
                        pump.Hoses.Add(hose);
                    }

                    if (hoseCount > 0)
                        _pumpList.Add(pump);
                }
            }
        }
        public void GetPumpsState_PumpList_ReturnsPumpStateForEachPump()
        {
            var status = string.Empty;
            _mockIO.Setup(c => c.SendAndReceive(WayneCommands.GetPumpsState, string.Empty, ref status))
                .Callback(() => status = "00")
                .Returns("0000070001");
            var forecourt = new Wayne(_mockIO.Object);
            var pump1 = new Pump(1);
            var pump2 = new Pump(2);
            var pumpList = new List<Pump>() { pump1, pump2 };

            var dic = forecourt.GetPumpsState(pumpList);
            forecourt.Dispose();

            status.Should().Be("00");
            dic[pump1].Should().Be(PumpState.Delivering);
            dic[pump2].Should().Be(PumpState.Free);
        }
 public PumpEventArgs(Pump pump)
 {
     this.Pump = pump;
 }
        public void GetPumpsState_PumpList_ReturnsPumpStateForEachPump()
        {
            _mockIO.Setup(c => c.SendAndReceive(CompanyCommands.GetPumpsState)).Returns("(SAL)");
            var forecourt = new Company(_mockIO.Object);
            var pump1 = new Pump(1);
            var pump2 = new Pump(2);
            var pumpList = new List<Pump>() { pump1, pump2 };

            var dic = forecourt.GetPumpsState(pumpList);
            forecourt.Dispose();
            
            dic[pump1].Should().Be(PumpState.Delivering);
            dic[pump2].Should().Be(PumpState.Free);
        }
        private void InitializeHosesAndPumps()
        {
            _pumpList = new List<Pump>();
            _hoseList = new List<Hose>();

            var report = _companyIO.SendAndReceive(CompanyCommands.GetPumpsAndHosesConfiguration);
            var pattern = "C[0-9]{2}[^ \t\r\n\v\f]{3}[0-9]{6}";
            var nextID = 1;
            var hoseCount = 0;
            var channel = 0;
            var pump = 0;

            foreach (Match match in Regex.Matches(report, pattern))
            {
                channel = Convert.ToInt32(match.Value.Substring(1, 2));
                pump = Convert.ToInt32(match.Value.Substring(6, 2)) - 1;
                hoseCount = Convert.ToInt32(match.Value.Substring(8, 2));
                if (hoseCount > 0)
                {
                    var newPump = new Pump(nextID, PumpState.Free);

                    for (int i = 0; i < hoseCount; i++)
                    {
                        var hose = new Hose(GetHoseID(channel, pump, i));
                        newPump.Hoses.Add(hose);
                        _hoseList.Add(hose);
                    }

                    _pumpList.Add(newPump);
                }

                nextID++;
            }
        }
        private void dataGridViewPumps_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewPumps.SelectedRows.Count > 0)
            {
                var pump = (Pump)dataGridViewPumps.SelectedRows[0].DataBoundItem;
                dataGridViewHoses.DataSource = pump.Hoses;
                _selectedPump = pump;

                labelHoses.Text = "Bicos da bomba " + pump.ID;
            }
        }
 public PumpProgressInfo(Pump pump)
 {
     Pump = pump;
 }
        private void ReadEvents()
        {
            Int16 eventType = 0;

            while (!_tasksCancellation.IsCancellationRequested)
            {
                Thread.Sleep(300); // TODO: verificar tempo correto

                _eztechIO.ProcessEvents();

                var result = _eztechIO.GetNextEventType(ref eventType); // TODO: adicionar validação do retorno do comando

                switch (eventType)
                {
                    case 2: // 2 == Delivery Event
                        {
                        Int32 DeliveryID = -1;
                        Int32 PumpID = -1;
                        Int16 PumpNumber = 0;
                        Int32 HoseID = -1;
                        Int16 HoseNumber = 0;
                        Int32 GradeID = -1;
                        string GradeName = string.Empty;
                        string ShortGradeName = string.Empty;
                        Int16 PriceLevel = 1;
                        Double Price = 0;
                        Double Volume = 0;
                        Double Value = 0;
                        Int16 DeliveryState = 0;
                        Int16 DeliveryType = 0;
                        Int32 LockedBy = -1;
                        Int32 ReservedBy = -1;
                        Int32 Age = 0;
                        DateTime CompletedDT = System.DateTime.Now;
                        Int32 AttendantID = -1;
                        Double OldVolumeETot = 0;
                        Double OldVolume2ETot = 0;
                        Double OldValueETot = 0;
                        Double NewVolumeETot = 0;
                        Double NewVolume2ETot = 0;
                        Double NewValueETot = 0;
                        Int64 Tag = 0;
                        Int32 Duration = 0;

                        _eztechIO.GetNextDeliveryEventEx2(ref DeliveryID, ref PumpID, ref PumpNumber, ref HoseID, ref HoseNumber,
                                    ref GradeID, ref GradeName, ref ShortGradeName, ref PriceLevel, ref Price, ref Volume, ref Value, ref DeliveryState,
                                    ref DeliveryType, ref LockedBy, ref ReservedBy, ref Age, ref CompletedDT, ref AttendantID,
                                    ref OldVolumeETot, ref OldVolume2ETot, ref OldValueETot,
                                    ref NewVolumeETot, ref NewVolume2ETot, ref NewValueETot,
                                    ref Tag, ref Duration); // TODO: adicionar validação do retorno do comando

                        if (DeliveryID != -1 && LockedBy == -1 && OnDeliveryFinalized != null)
                        {
                            _eztechIO.LockAndClearDelivery(DeliveryID, DeliveryType); // Removes the delivery from the forecourt

                            var delivery = new Delivery();
                            delivery.Date = CompletedDT;
                            delivery.Hose = new Hose(HoseID);
                            delivery.TotalValue = Value;
                            delivery.UnitPrice = Price;
                            delivery.Volume = Volume;

                            _progressDeliveryFinalized.Report(new DeliveryProgressInfo(delivery));
                        }
                        break;
                    }
                    case 1: // 1 == Pump Event
                        {
                            Int32 PumpID = -1;
                            Int16 PumpNumber = 0;
                            Int16 State = 0;
                            Int16 ReservedFor = 0;
                            Int32 ReservedBy = -1;
                            Int32 HoseID = -1;
                            Int16 HoseNumber = 0;
                            Int32 GradeID = -1;
                            string GradeName = string.Empty;
                            string ShortGradeName = string.Empty;
                            Int16 PriceLevel = 1;
                            Double Price = 0;
                            Double Volume = 0;
                            Double Value = 0;
                            Int16 StackSize = 0;
                            string PumpName = string.Empty;
                            Int16 PhysicalNumber = 0;
                            Int16 Side = 0;
                            Int16 Address = 0;
                            Int16 PriceLevel1 = 0;
                            Int16 PriceLevel2 = 0;
                            Int16 Type = 0;
                            Int32 PortID = -1;
                            Int16 AuthMode = 0;
                            Int16 StackMode = 0;
                            Int16 PrepayAllowed = 0;
                            Int16 PreauthAllowed = 0;
                            Int16 PriceFormat = 0;
                            Int16 ValueFormat = 0;
                            Int16 VolumeFormat = 0;

                            _eztechIO.GetNextPumpEventEx(ref PumpID, ref PumpNumber, ref State, ref ReservedFor, ref ReservedBy,
                                ref HoseID, ref HoseNumber, ref GradeID, ref GradeName, ref ShortGradeName, ref PriceLevel,
                                ref Price, ref Volume, ref Value, ref StackSize, ref PumpName, ref PhysicalNumber, ref Side, ref Address, ref PriceLevel1,
                                ref PriceLevel2, ref Type, ref PortID, ref AuthMode, ref StackMode, ref PrepayAllowed, ref PreauthAllowed, ref PriceFormat,
                                ref ValueFormat, ref VolumeFormat);

                            if (OnPumpStateChanged != null && HoseID == -1)
                            {
                                var pump = new Pump(PumpID, ToPumpState(State));
                                _progressPumpStateChanged.Report(new PumpProgressInfo(pump));
                            }

                            if (OnHoseCurrentVolumeChanged != null && HoseID != -1)
                            {
                                var hose = new Hose(HoseID);
                                hose.CurrentVolume = Volume;
                                _progressHoseCurrentVolumeChanged.Report(new HoseProgressInfo(hose));
                            }

                            break;
                        }
                    default:
                        _eztechIO.DiscardNextEvent();
                        break;
                }
            }
        }