public ArtDmxPackage Get(short universe)
        {
            var entity = context.ArtDmxEntities.FirstOrDefault(e => e.Universe == universe);
            var data = DataConverter.GetBytesFromEntity(entity);
            var package = new ArtDmxPackage(data);
            package.Universe = universe;
            if (entity != null)
                package.Sequence = entity.Sequence;

            return package;
        }
        //private IDictionary<short, Fade> fades;

        /// <summary>
        /// Initializes a new instance of the <see cref="ArtNetFascade"/> class.
        /// </summary>
        /// <param name="universe">The universe.</param>
        /// <param name="hosts">The hosts.</param>
        public ArtNetFascade(short universe, ISet<string> hosts)
        {
            persistence = new Persistence();
            package = persistence.GetAsync(universe).Result;
            sender = new Sender(hosts);
            sendTimer = new Timer(10);
            sendTimer.Elapsed += sendTimer_Elapsed;
            sendTimer.Start();
            saveTimer = new Timer(2000);
            saveTimer.Elapsed += saveTimer_Elapsed;
            saveTimer.Start();
            //fades = new Dictionary<short, Fade>();
        }
        public void ArtDmxPackageCtorByteArray2Elements()
        {
            // Arrange
            var data = new byte[] { 1, 2 };

            // Act
            var actual = new ArtDmxPackage(data);
            var actualBytes = actual.GetBytes();

            // Assert
            Assert.AreEqual(0, actual.Universe);
            Assert.AreEqual(1, actualBytes[18]);
            Assert.AreEqual(2, actualBytes[19]);
        }
        public async Task UpdateAsync(ArtDmxPackage package)
        {
            if (package == null)
                throw new ArgumentNullException(nameof(package));

            if (!Directory.Exists(DataFolder))
                Directory.CreateDirectory(DataFolder);

            var fileContent = JsonConvert.SerializeObject(package, Formatting.Indented);
            var fullPath = GeneratFullPath(package.Universe);

            using (var writer = File.CreateText(fullPath))
            {
                await writer.WriteAsync(fileContent);
            }
        }
        public void ArtDmxPackageCtorByteArray512Elements()
        {
            // Arrange
            var data = Enumerable.Range(0, 512).Select(i => (byte)i).ToArray();

            // Act
            var actual = new ArtDmxPackage(data);
            var actualBytes = actual.GetBytes();

            // Assert
            Assert.AreEqual(512, actual.Length);
            Assert.AreEqual(0, actual.Universe);
            Assert.AreEqual(530, actualBytes.Length);
            for (int i = 18; i < 530; i++)
            {
                Assert.AreEqual(data[i - 18], actualBytes[i], "i = " + i);
            }
        }
        public async Task UpdateAsync(ArtDmxPackage package)
        {
            if (package == null)
                throw new ArgumentNullException("package");

            var entity = context.ArtDmxEntities.FirstOrDefault(r => r.Universe == package.Universe);

            if (entity == null)
            {
                entity = context.ArtDmxEntities.Add(context.ArtDmxEntities.Create());
                entity.Universe = package.Universe;
            }

            DataConverter.SetBytesToEntity(entity, package.data);
            entity.Sequence = package.Sequence;

            await context.SaveChangesAsync();
        }
        public async Task SendTest()
        {
            // Arrange
            var package = new ArtDmxPackage(new byte[] { 1, 2 });
            var expected = package.GetBytes();
            expected[12] = 2;
            var receiver = new UdpClient(Sender.ArtNetDefaultPort);
            var sender = new Sender("127.0.0.1");

            // Act
            var t = receiver.ReceiveAsync();
            await sender.SendAsync(package);
            var actual = (await t).Buffer;

            // Assert
            Assert.IsNotNull(actual);
            CollectionAssert.AreEqual(expected, actual);
        }
        public void ArtDmxPackageTestForCorrectHeader()
        {
            // Arrange
            var data = new byte[0];

            // Act
            var actual = new ArtDmxPackage(data);
            var actualBytes = actual.GetBytes();

            // Assert
            Assert.AreEqual(65, actualBytes[0], "A");
            Assert.AreEqual(114, actualBytes[1], "r");
            Assert.AreEqual(116, actualBytes[2], "t");
            Assert.AreEqual(45, actualBytes[3], "-");
            Assert.AreEqual(78, actualBytes[4], "N");
            Assert.AreEqual(101, actualBytes[5], "e");
            Assert.AreEqual(116, actualBytes[6], "t");
            Assert.AreEqual(0, actualBytes[7], "\0");
            Assert.AreEqual(0, actualBytes[8], "OpCodeLo");
            Assert.AreEqual(80, actualBytes[9], "OpCodeHi");
            Assert.AreEqual(0, actualBytes[10], "ProtVerHi");
            Assert.AreEqual(14, actualBytes[11], "ProtVerLo");
            Assert.AreEqual(1, actualBytes[12], "Sequence");
            Assert.AreEqual(0, actualBytes[13], "Physical");
            Assert.AreEqual(0, actualBytes[14], "SubUni");
            Assert.AreEqual(0, actualBytes[15], "Net");
            Assert.AreEqual(2, actualBytes[16], "LengthHi");
            Assert.AreEqual(0, actualBytes[17], "Length");
        }
        public void ArtDmxPackageCtorPairs513Elements()
        {
            // Arrange
            var data = Enumerable.Range(1, 513).Select(i => (byte)i).ToArray();
            var pairs = Enumerable.Range(1, 514).Select(i => new AddressValuePair((short)i, (byte)i)).ToArray();

            // Act
            var actual = new ArtDmxPackage(pairs);
            var actualBytes = actual.GetBytes();

            // Assert
            Assert.AreEqual(512, actual.Length);
            Assert.AreEqual(0, actual.Universe);
            Assert.AreEqual(530, actualBytes.Length);
            for (int i = 18; i < 530; i++)
            {
                Assert.AreEqual(data[i - 18], actualBytes[i], "i = " + i);
            }
        }
        public void ArtDmxPackageCtorPairs4Explicit()
        {
            // Arrange

            // Act
            var actual = new ArtDmxPackage(1, 1, 2, 2, 3, 3, 4, 4);
            var actualBytes = actual.GetBytes();

            // Assert
            Assert.AreEqual(0, actual.Universe);
            Assert.AreEqual(1, actualBytes[18]);
            Assert.AreEqual(2, actualBytes[19]);
            Assert.AreEqual(3, actualBytes[20]);
            Assert.AreEqual(4, actualBytes[21]);
        }
        public void ArtDmxPackageCtorPairs1ExplicitMaxValue()
        {
            // Arrange
            var data = new byte[0];

            // Act
            var actual = new ArtDmxPackage(512, 1);
            var actualBytes = actual.GetBytes();

            // Assert
            Assert.AreEqual(512, actual.Length);
            Assert.AreEqual(0, actual.Universe);
            Assert.AreEqual(530, actualBytes.Length);
            for (int i = 18; i < 529; i++)
            {
                Assert.AreEqual(0, actualBytes[i], "i = " + i);
            }

            Assert.AreEqual(1, actualBytes[529]);
        }
        public void ArtDmxPackageCtorPairs1ExplicitEmpty()
        {
            // Arrange
            var data = new byte[0];

            // Act
            var actual = new ArtDmxPackage(0, 0);
            var actualBytes = actual.GetBytes();

            // Assert
            Assert.AreEqual(0, actual.Universe);
            Assert.AreEqual(0, actualBytes[18]);
            Assert.AreEqual(0, actualBytes[19]);
        }
        public void ArtDmxPackageCtorPairs0Elements()
        {
            // Arrange
            var pairs = new AddressValuePair[0];

            // Act
            var actual = new ArtDmxPackage(pairs);
            var actualBytes = actual.GetBytes();

            // Assert
            Assert.AreEqual(0, actual.Universe);
            Assert.AreEqual(0, actualBytes[18]);
            Assert.AreEqual(0, actualBytes[19]);
        }