Пример #1
0
        internal void ReceivedProperty(IntegraSystemExclusive systemExclusive)
        {
            //if (!IsCached)
            //    return;

            int offset = systemExclusive.Address - Address;

            // Offset 0x0100
            if (offset > 127)
            {
                offset -= 128;
            }

            var fields  = this.CachedFields();
            int indexer = 0;

            for (int dataOffset = 0; dataOffset < systemExclusive.Data.Length; dataOffset++)
            {
                int fieldOffset = offset + dataOffset;

                fields.TryGetValue(fieldOffset, out FieldInfo? field);

                // TODO: should never be null?
                // TODO: Create cached property entry for each indexed property
                if (field == null)
                {
                    for (int i = fieldOffset; i > 0; i--)
                    {
                        if (!fields.Keys.Contains(i))
                        {
                            indexer++;
                        }
                        else
                        {
                            field = fields[i];
                            break;
                        }
                    }
                }


                if (field == null)
                {
                    continue;
                }

                if (field.FieldType == typeof(byte))
                {
                    field.SetValue(this, systemExclusive.Data[dataOffset]);
                }
                else if (field.FieldType == typeof(bool))
                {
                    field.SetValue(this, Convert.ToBoolean(systemExclusive.Data[dataOffset]));
                }
                else if (field.FieldType == typeof(int))
                {
                    // TODO: Extension method
                    byte[] bytes = new byte[4];

                    for (int i = 0; i < bytes.Length; i++)
                    {
                        bytes[i] = systemExclusive.Data[dataOffset + i];
                    }

                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes);
                    }

                    field.SetValue(this, BitConverter.ToInt32(bytes, 0));
                    //pro += 4;
                    dataOffset += 3;
                }
                else if (field.FieldType.IsEnum)
                {
                    Type type = field.FieldType.GetEnumUnderlyingType();

                    if (type == typeof(byte))
                    {
                        field.SetValue(this, systemExclusive.Data[dataOffset]);
                    }
                    else if (type == typeof(int))
                    {
                        byte[] bytes = new byte[4];

                        for (int i = 0; i < bytes.Length; i++)
                        {
                            bytes[i] = systemExclusive.Data[dataOffset + i];
                        }

                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(bytes);
                        }

                        field.SetValue(this, BitConverter.ToInt32(bytes, 0));
                        //pro += 4;
                        dataOffset += 3;
                    }
                    else if (type == typeof(short))
                    {
                        byte[] bytes = new byte[2];

                        for (int i = 0; i < bytes.Length; i++)
                        {
                            bytes[i] = systemExclusive.Data[dataOffset + i];
                        }

                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(bytes);
                        }

                        field.SetValue(this, BitConverter.ToInt16(bytes, 0));

                        dataOffset += 1;
                    }
                    else
                    {
                        throw new NotImplementedException($"{GetType().Name}.{nameof(ReceivedProperty)}] {field.FieldType.Name} {field.Name}");
                    }
                }
                else if (field.FieldType.IsArray)
                {
                    Array?array = field.GetValue(this) as Array;

                    Debug.Assert(array != null);

                    Type?arrayType = array.GetType().GetElementType();

                    Debug.Assert(arrayType != null);

                    if (arrayType == typeof(byte))
                    {
                        for (int a = 0; a < array.Length; a++)
                        {
                            array.SetValue(systemExclusive.Data[dataOffset], a);
                            dataOffset++;

                            if (dataOffset + 1 > systemExclusive.Data.Length)
                            {
                                break;
                            }
                        }
                    }
                    else if (arrayType == typeof(int))
                    {
                        // TODO: Check if MFX parameter byte and if possible to do conversion here so the backing paramater array will contain actual integers without the parameter prefix
                        byte[] bytes = new byte[4];

                        // Divide the remaining system exclusive data length by four bytes to get the number of parameters
                        int parameterCount = (int)((systemExclusive.Data.Length - dataOffset) / 4);

                        int index = 0;

                        if (array.Length > parameterCount)
                        {
                            // The remaining system exclusive data length is smaller than the array, use the number of remaining parameters as indexer
                            index = parameterCount;
                        }
                        else
                        {
                            // The remaining system exclusive data length is greather than the array, use the length of the array as indexer
                            index = array.Length;
                        }

                        for (int a = 0; a < index; a++)
                        {
                            Array.Copy(systemExclusive.Data, dataOffset, bytes, 0, 4);

                            if (BitConverter.IsLittleEndian)
                            {
                                Array.Reverse(bytes);
                            }

                            array.SetValue(BitConverter.ToInt32(bytes, 0), a);

                            // Increment the data offset by four bytes
                            dataOffset += 4;
                        }
                    }
                    else
                    {
                        throw new NotImplementedException($"{GetType().Name}.{nameof(ReceivedProperty)}] {field.FieldType.Name} {field.Name}");
                    }
                }
                else
                {
                    throw new NotImplementedException($"{GetType().Name}.{nameof(ReceivedProperty)}] {field.FieldType.Name} {field.Name}");
                }

                string property = this.CachedProperties().Where(x => x.Value == fieldOffset).FirstOrDefault().Key;

                if (!string.IsNullOrEmpty(property))
                {
                    Debug.Print($"RX [{GetType().Name}] {property} = {field.GetValue(this)} {GetUID():X4}");

                    base.NotifyPropertyChanged(property);
                    //base.NotifyPropertyChanged(string.Empty);
                }
            }

            base.NotifyPropertyChanged(string.Empty);
        }
Пример #2
0
 public IntegraSystemExclusiveEventArgs(IntegraSystemExclusive systemExclusive) : base()
 {
     SystemExclusive = systemExclusive;
 }