Exemple #1
0
        public void CanDecode()
        {
            var pyint    = new PyInt(10).GetPythonType();
            var pyfloat  = new PyFloat(10).GetPythonType();
            var pystr    = new PyString("world").GetPythonType();
            var decoder1 = new DecoderReturningPredefinedValue <long>(pyint, decodeResult: 42);
            var decoder2 = new DecoderReturningPredefinedValue <string>(pyfloat, decodeResult: "atad:");
            var group    = new DecoderGroup {
                decoder1,
                decoder2,
            };

            Assert.IsTrue(group.CanDecode(pyint, typeof(long)));
            Assert.IsFalse(group.CanDecode(pyint, typeof(int)));
            Assert.IsTrue(group.CanDecode(pyfloat, typeof(string)));
            Assert.IsFalse(group.CanDecode(pystr, typeof(string)));
        }
Exemple #2
0
        public void Decodes()
        {
            var pyint    = new PyInt(10).GetPythonType();
            var pyfloat  = new PyFloat(10).GetPythonType();
            var decoder1 = new DecoderReturningPredefinedValue <long>(pyint, decodeResult: 42);
            var decoder2 = new DecoderReturningPredefinedValue <string>(pyfloat, decodeResult: "atad:");
            var group    = new DecoderGroup {
                decoder1,
                decoder2,
            };

            Assert.IsTrue(group.TryDecode(new PyInt(10), out long longResult));
            Assert.AreEqual(42, longResult);
            Assert.IsTrue(group.TryDecode(new PyFloat(10), out string strResult));
            Assert.AreSame("atad:", strResult);

            Assert.IsFalse(group.TryDecode(new PyInt(10), out int _));
        }
Exemple #3
0
        public void GetDecodersByTypes()
        {
            var pyint    = new PyInt(10).GetPythonType();
            var pyfloat  = new PyFloat(10).GetPythonType();
            var pystr    = new PyString("world").GetPythonType();
            var decoder1 = new DecoderReturningPredefinedValue <long>(pyint, decodeResult: 42);
            var decoder2 = new DecoderReturningPredefinedValue <string>(pyfloat, decodeResult: "atad:");
            var group    = new DecoderGroup {
                decoder1,
                decoder2,
            };

            var decoder = group.GetDecoder(pyfloat, typeof(string));

            Assert.AreSame(decoder2, decoder);
            decoder = group.GetDecoder(pystr, typeof(string));
            Assert.IsNull(decoder);
            decoder = group.GetDecoder(pyint, typeof(long));
            Assert.AreSame(decoder1, decoder);
        }
        public bool LoadSettings()
        {
            var ofd = new OpenFileDialog {
                Filter = @"*.csv|*.csv", Multiselect = false
            };

            DecoderGroups.Clear();

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return(DecoderGroups.Count > 0);
            }

            DecoderGroup dg = null;

            using (var sr = new StreamReader(ofd.FileName))
            {
                while (sr.EndOfStream == false)
                {
                    var line = sr.ReadLine();
                    if (line == null)
                    {
                        continue;
                    }

                    var items = line.Split(',');

                    if (items.Length == 6) //New Decode group
                    {
                        if (dg != null)
                        {
                            DecoderGroups.Add(dg);
                        }

                        //"Length,Sequence Number,Source Address,Source PAN ID,Destination PAN ID,Destination Address"
                        dg = new DecoderGroup
                        {
                            Header = new ZigBeeDecoder.ZigBeeHeader
                            {
                                Len         = byte.Parse(items[0]),
                                SequenceNum = byte.Parse(items[1]),
                                Src         = ushort.Parse(items[2]),
                                Pan         = ushort.Parse(items[3]),
                                DestPan     = ushort.Parse(items[4]),
                                DestAddr    = ushort.Parse(items[5])
                            }
                        };
                    }

                    if (items.Length == 9) //new Decode info
                    {
                        //"Name,Start Bit,Bit Length,Start Byte,Byte Length,Signed,Factor,Unit,Notes"
                        var di = new DecoderInfo
                        {
                            Name       = items[0],
                            StartBit   = int.Parse(items[1]),
                            BitLength  = int.Parse(items[2]),
                            StartByte  = int.Parse(items[3]),
                            ByteLength = float.Parse(items[4]),
                            Signed     = bool.Parse(items[5]),
                            Factor     = float.Parse(items[6]),
                            Unit       = items[7],
                            Notes      = items[8]
                        };

                        dg?.DecoderInfoList.Add(di);
                    }
                }

                DecoderGroups.Add(dg);
            }

            return(DecoderGroups.Count > 0);
        }