private static PumpCard PopulatePumpCard(List <ModbusRegisterValue> registerValues)
        {
            if (registerValues == null || registerValues.Count == 0)
            {
                System.Console.WriteLine("Empty register values passed.");
                return(null);
            }

            Console.WriteLine("Parsing pump card.");
            PumpCard pumpCard = new PumpCard()
            {
                CardCoordinates = new List <CardCoordinate>()
            };

            var timestampProp = ModbusMessage.PumpLayoutConfig.Timestamp;

            if (timestampProp != null)
            {
                int timestamp       = 0;
                var timeStampValues = GetValueArray(timestampProp.RegisterNumber, timestampProp.NumberOfRegisters, registerValues);

                if (timeStampValues != null && timeStampValues.Count > 1)
                {
                    short left  = (short)Int32.Parse(timeStampValues[0]);
                    short right = (short)Int32.Parse(timeStampValues[1]);

                    timestamp = left;
                    timestamp = (timestamp << 16);
                    timestamp = timestamp | (int)(ushort)right;
                }

                DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                dateTime           = dateTime.AddSeconds(timestamp).ToLocalTime();
                pumpCard.Timestamp = dateTime;
            }

            var maxLoadProp = ModbusMessage.PumpLayoutConfig.MaxLoad;

            if (maxLoadProp != null)
            {
                var maxLoadpValues = GetValueArray(maxLoadProp.RegisterNumber, maxLoadProp.NumberOfRegisters, registerValues);
                if (maxLoadpValues != null && maxLoadpValues.Count > 0)
                {
                    pumpCard.ScaledMaxLoad = Int32.Parse(maxLoadpValues.First());
                }
            }

            var minLoadProp = ModbusMessage.PumpLayoutConfig.MinLoad;

            if (minLoadProp != null)
            {
                var minLoadValues = GetValueArray(minLoadProp.RegisterNumber, minLoadProp.NumberOfRegisters, registerValues);
                if (minLoadValues != null && minLoadValues.Count > 0)
                {
                    pumpCard.ScaledMinLoad = Int32.Parse(minLoadValues.First());
                }
            }

            var grossStroke = ModbusMessage.PumpLayoutConfig.GrossStroke;

            if (grossStroke != null)
            {
                var grossStrokeValues = GetValueArray(grossStroke.RegisterNumber, grossStroke.NumberOfRegisters, registerValues);
                if (grossStrokeValues != null && grossStrokeValues.Count > 0)
                {
                    pumpCard.GrossStroke = Int32.Parse(grossStrokeValues.First());
                }
            }

            var netStroke = ModbusMessage.PumpLayoutConfig.NetStroke;

            if (netStroke != null)
            {
                var netStrokeValues = GetValueArray(netStroke.RegisterNumber, netStroke.NumberOfRegisters, registerValues);
                if (netStrokeValues != null && netStrokeValues.Count > 0)
                {
                    pumpCard.NetStroke = Int32.Parse(netStrokeValues.First());
                }
            }

            var fluidLoad = ModbusMessage.PumpLayoutConfig.NetStroke;

            if (fluidLoad != null)
            {
                var fluidLoadValues = GetValueArray(fluidLoad.RegisterNumber, fluidLoad.NumberOfRegisters, registerValues);
                if (fluidLoadValues != null && fluidLoadValues.Count > 0)
                {
                    pumpCard.FluidLoad = Int32.Parse(fluidLoadValues.First());
                }
            }

            var pumpFillage = ModbusMessage.PumpLayoutConfig.NetStroke;

            if (pumpFillage != null)
            {
                var pumpFillageValues = GetValueArray(pumpFillage.RegisterNumber, pumpFillage.NumberOfRegisters, registerValues);
                if (pumpFillageValues != null && pumpFillageValues.Count > 0)
                {
                    pumpCard.PumpFillage = Int32.Parse(pumpFillageValues.First());
                }
            }

            var numberOfPointsProp = ModbusMessage.PumpLayoutConfig.NumberOfPoints;
            int numberOfDataPoints = 0;

            if (numberOfPointsProp != null)
            {
                var numberOfPointsValues = GetValueArray(numberOfPointsProp.RegisterNumber, numberOfPointsProp.NumberOfRegisters, registerValues);
                if (numberOfPointsValues != null && numberOfPointsValues.Count > 0)
                {
                    numberOfDataPoints = Int32.Parse(numberOfPointsValues.First());

                    //Get just the low byte of the value
                    numberOfDataPoints = Int32.Parse(numberOfPointsValues.First());
                    Console.WriteLine($"Number of pump card points 16-bit value: {numberOfDataPoints}");

                    numberOfDataPoints = (int)(ushort)numberOfDataPoints;
                    Console.WriteLine($"Number of pump card points 8-bit value: {numberOfDataPoints}");
                    pumpCard.NumberOfPoints = numberOfDataPoints;

                    if (numberOfDataPoints == 0)
                    {
                        //If the number of detail points is 0, that's an indication we have bad data.
                        Console.WriteLine("Zero coordinates found. Returning null card.");
                        return(null);
                    }

                    List <CardCoordinate> cardCoordinates = new List <CardCoordinate>();
                    var pointArrayProperty = ModbusMessage.PumpLayoutConfig.Point;

                    for (int i = 0; i < pumpCard.NumberOfPoints; i += pointArrayProperty.NumberOfRegisters)
                    {
                        var pointsArray = GetValueArray(pointArrayProperty.RegisterNumber + i, pointArrayProperty.NumberOfRegisters, registerValues);
                        if (pointsArray != null && pointsArray.Count > 1)
                        {
                            cardCoordinates.Add(new CardCoordinate()
                            {
                                Load     = Int32.Parse(pointsArray[0]),
                                Position = Int32.Parse(pointsArray[1])
                            });
                        }
                    }

                    pumpCard.CardCoordinates = cardCoordinates;
                }
            }

            return(pumpCard);
        }
示例#2
0
 public DynoCard()
 {
     SurfaceCard = new SurfaceCard();
     PumpCard    = new PumpCard();
 }