public void Test222()
        {
            try
            {
                try
                {
                    File.Delete(PacketBufferHelper.GetBufferDbPath());
                }
                catch { }


                var buffer = new RealmPacketBuffer();


                var list = new List <byte[]>()
                {
                    "120808020B002F".ToByteArray(),
                };

                object message = null;

                foreach (var bytes in list)
                {
                    message = Message.Unpack(bytes, buffer);
                }
            }
            catch (Exception e)
            {
                Debugger.Break();
            }
        }
        Message __Unpack(List <Packet> packets)
        {
            var     buffer  = new InMemoryBuffer();
            Message message = null;

            foreach (var p in packets)
            {
                message = Message.Unpack(p.Payload, buffer);
            }

            return(message);
        }
        public void FileTest()
        {
            try
            {
                File.Delete(PacketBufferHelper.GetBufferDbPath());
            }
            catch { }

            try
            {
                var file = File.OpenRead(@"C:\Users\Banana\Desktop\file.txt");
                var m    = ChatMessageMT.Create(
                    ProtocolVersion.v3__WeatherExtension,
                    new Subscriber("79999740562", SubscriberNetwork.Mobile), null, null,
                    "hello image",
                    lat: 12,
                    lon: -56,
                    alt: 561,
                    file: file,
                    fileExtension: FileExtension.Jpg,
                    imageQuality: ImageQuality.Low);

                var packets = m.Pack();

                string s = packets[0].Payload.ToHexString();

                var           buffer  = new RealmPacketBuffer();
                ChatMessageMT message = null;

                foreach (var p in packets)
                {
                    message = Message.Unpack(p.Payload, buffer) as ChatMessageMT;
                }

                if (m.File.Length != message.File.Length)
                {
                    Assert.Fail();
                }

                var bytes = (message.File as MemoryStream).GetBuffer();
                File.WriteAllBytes(@"/Users/banana/Downloads/rockstar-media-test[unpacked].jpg", bytes);
            }
            catch (Exception e)
            {
            }
        }
        public void MessageSentTest()
        {
            try
            {
                byte group = 35;

                var bytes   = MessageSentMO.Create(ProtocolVersion.v3__WeatherExtension, group).Pack()[0].Payload;
                var message = Message.Unpack(bytes) as MessageSentMO;

                if (message.SentGroup != group)
                {
                    Assert.Fail();
                }
            }
            catch (Exception e)
            {
            }
        }
        public async Task Pack__WeatherMTTest()
        {
            //var r = await new HttpClient().GetAsync("http://demo.iridium360.ru/connect/weather?auth=d9fc554e3ad74919bf274e11bdfe07c3&lat=55.67578125&lon=37.255859375&interval=6");
            var r = await new HttpClient().GetAsync("http://demo.iridium360.ru/connect/weather?auth=d9fc554e3ad74919bf274e11bdfe07c3&lat=25.761&lon=-80.208&interval=6");
            var s = await r.Content.ReadAsStringAsync();


            try
            {
                var ffff = JsonConvert.DeserializeObject <i360WeatherForecast>(s);

                var fs = ffff.Forecasts.Select(x => new i360PointForecast()
                {
                    Lat        = x.Lat,
                    Lon        = x.Lon,
                    DayInfos   = x.DayInfos,
                    TimeOffset = x.TimeOffset,
                    Forecasts  = x.Forecasts.Select(z => new i360Forecast()
                    {
                        WindGust      = z.WindGust,
                        CloudHeight   = z.CloudHeight,
                        Visibility    = z.Visibility,
                        Cloud         = z.Cloud,
                        Date          = z.Date,
                        Precipitation = z.Precipitation,
                        WindSpeed     = z.WindSpeed,
                        WindDirection = z.WindDirection,
                        Temperature   = z.Temperature,
                        SnowRisk      = z.SnowRisk,
                        Pressure      = z.Pressure
                    }).Take(16).ToList()
                }).FirstOrDefault();

                var message = WeatherMT.Create(ProtocolVersion.v2__LocationFix, fs);
                var packets = message.Pack(33);

                var hex = packets[0].Payload.ToHexString();
                var m   = Message.Unpack(packets[0].Payload) as WeatherMT;
            }
            catch (Exception e)
            {
            }
        }
        public void ResendMessagePartsTest()
        {
            try
            {
                byte[] indexes = new byte[] { 8, 9, 19, 20, 21, 78 };
                byte   group   = 159;

                var bytes   = MessageAckMT.Create(ProtocolVersion.v3__WeatherExtension, group, indexes).Pack()[0].Payload;
                var message = Message.Unpack(bytes) as MessageAckMT;

                if (message.TargetGroup != group || !message.ResendIndexes.SequenceEqual(indexes))
                {
                    Assert.Fail();
                }
            }
            catch (Exception e)
            {
            }
        }
        public async Task <ushort> SendData(byte[] data)
        {
            try
            {
                await locker.WaitAsync();

                await Task.Delay(1000);


                ushort _messageId   = (ushort)storage.GetShort(device.Id, "message-id", 0);
                var    ___messageId = _messageId + 1;
                storage.PutShort(device.Id, "message-id", (short)___messageId);

                i++;

                if (i % 8 == 0)
                {
                    throw new Exception("Dummy send error");
                }


                var m = Message.Unpack(data, new InMemoryBuffer());

                if (m is BalanceMO balance)
                {
                    thread.PostDelayed(() =>
                    {
                        var balanceMT = BalanceMT.Create(ProtocolVersion.v3__WeatherExtension, DateTime.UtcNow, DateTime.UtcNow.AddDays(-12), DateTime.UtcNow.AddDays(30), 672, 1000, 328).Pack();

                        PacketReceived(this, new PacketReceivedEventArgs()
                        {
                            Payload   = balanceMT[0].Payload,
                            MessageId = (short)(10006 + ___messageId),
                        });
                    }, TimeSpan.FromSeconds(15));
                }
                if (m is MessageSentMO sent)
                {
                    thread.PostDelayed(() =>
                    {
                        var resendIndexes = new byte[resendParts];
                        for (int k = 0; k < resendParts; k++)
                        {
                            resendIndexes[k] = (byte)k;
                        }

                        resendParts -= 2;

                        if (resendParts < 0)
                        {
                            resendParts = 2;
                        }

                        var ack = MessageAckMT.Create(ProtocolVersion.v3__WeatherExtension, (byte)sent.SentGroup, resendIndexes).Pack();

                        PacketReceived(this, new PacketReceivedEventArgs()
                        {
                            Payload   = ack[0].Payload,
                            MessageId = (short)(10005 + ___messageId),
                        });
                    }, TimeSpan.FromSeconds(10));
                }


                thread.Post(async() =>
                {
                    await Task.Delay(TimeSpan.FromSeconds(4));


                    PacketStatusUpdated(this, new PacketStatusUpdatedEventArgs()
                    {
                        MessageId = (short)_messageId,
                        Status    = MessageStatus.Transmitted,
                    });


                    //await Task.Delay(TimeSpan.FromSeconds(6));


                    //var m = Message.Unpack(data) as ChatMessageMO;

                    //if (m != null && m.TotalParts == 1)
                    //{
                    //var p = ChatMessageMT.Create(m.Version, m.Subscriber, m.Id, m.Conversation, m.Text, m.Lat, m.Lon, m.Alt, m.ByskyToken, m.File, m.FileExtension, m.ImageQuality, m.Subject).Pack();

                    //PacketReceived(this, new PacketReceivedEventArgs()
                    //{
                    //    Payload = p[0].Payload,
                    //    MessageId = (short)(10000 + ___messageId),
                    //});
                    //}
                });

                return(_messageId);
            }
            finally
            {
                locker.Release();
            }
        }