Пример #1
0
 private void Ping(PingModel pingModel)
 {
     while (isPinging == true)
     {
         Thread.Sleep(Convert.ToInt32(pingModel.PingInterval));
         if (socket.IsAlive)
         {
             socket.Send("0[\"ping\"]");
         }
         else
         {
             isPinging = false;
         }
     }
 }
Пример #2
0
        private void HandleData(string queuedMessage)
        {
            string id    = Regex.Match(queuedMessage, @"^([0-9]{1,2})").Value;
            string value = queuedMessage.Substring(id.Length);

            if (!string.IsNullOrEmpty(value))
            {
                dynamic thing = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(value);
                if (id == "0" && isPinging == false)
                {
                    PingModel pingModel = JsonConvert.DeserializeObject <PingModel>(value);
                    ThreadPool.QueueUserWorkItem(_ => Ping(pingModel));
                    isPinging = true;
                }
                if (id == "42")
                {
                    CryptoCompareSet set = new CryptoCompareSet(thing[1].ToString());
                    if (set.Length < 6)
                    {
                        return;
                    }
                    if (set.Type == "5")
                    {
                        if (set.Ignore == true)
                        {
                            return;
                        }

                        int exchangeID   = 0;
                        int instrumentID = 0;
                        int interval     = 0;
                        // = Manager.FirstOrDefault(x => x.instrument.InstrumentID == instrument.InstrumentID);
                        using (MarketsEntities context = new MarketsEntities())
                        {
                            Instruments instrument = context.Instruments.FirstOrDefault(x => x.Name == set.LASTMARKET && x.BaseCurrency == set.FromCurrency && x.QuoteCurrency == set.ToCurrency && x.Active == true);
                            if (instrument == null)
                            {
                                instrument = DataCalls.AddInstrument(new Instruments()
                                {
                                    ExchangeID = 2, MarketID = 1, Name = set.LASTMARKET, BaseCurrency = set.FromCurrency, QuoteCurrency = set.ToCurrency, Active = true, Interval = 60
                                });
                            }
                            exchangeID   = instrument.ExchangeID;
                            instrumentID = instrument.InstrumentID;
                            interval     = instrument.Interval;
                        };
                        InstrumentManager current = Manager.FirstOrDefault(x => x.InstrumentID == instrumentID && x.Type == 5);

                        if (current != null)
                        {
                            if (DateTime.UtcNow <= current.Last.AddSeconds(interval))
                            {
                                return;
                            }
                            else
                            {
                                current.Last = DateTime.UtcNow;
                            }
                        }
                        else if (current == null)
                        {
                            Manager.Add(new InstrumentManager()
                            {
                                InstrumentID = instrumentID, Last = DateTime.UtcNow, Type = 5
                            });
                        }

                        CurrentAggData currentAggData = new CurrentAggData()
                        {
                            ExchangeID     = exchangeID,
                            InstrumentID   = instrumentID,
                            MarketID       = 1,
                            Flag           = set.Flag,
                            Price          = set.Price,
                            LastUpdate     = FromUnixTime(set.LastUpdate),
                            LastVolume     = set.LastVolume,
                            LastVolumeTo   = set.LastVolumeTo,
                            LastTradeId    = set.LastTradeId,
                            Volume24Hour   = set.VOLUME24HOUR,
                            Volume24HourTo = set.VOLUME24HOURTO,
                            Low24Hour      = set.LOW24HOUR,
                            High24Hour     = set.HIGH24HOUR,
                            Open24Hour     = set.OPEN24HOUR,
                            Volume24h      = set.Volume24h,
                            Volume24hTo    = set.Volume24hTo,
                            MaskInt        = set.MaskInt
                        };
                        DataCalls.AddCurrentAggData(currentAggData);
                    }
                    else
                    {
                        if ((set.Flag == 1 || set.Flag == 2 || set.Flag == 4)) //|| set.Flag == 5))
                        {
                            int exchangeID   = 0;
                            int instrumentID = 0;
                            int interval     = 0;
                            // = Manager.FirstOrDefault(x => x.instrument.InstrumentID == instrument.InstrumentID);
                            using (MarketsEntities context = new MarketsEntities())
                            {
                                Instruments instrument = context.Instruments.FirstOrDefault(x => x.Name == set.ExchangeName && x.BaseCurrency == set.FromCurrency && x.QuoteCurrency == set.ToCurrency && x.Active == true);
                                if (instrument == null)
                                {
                                    instrument = DataCalls.AddInstrument(new Instruments()
                                    {
                                        ExchangeID = 2, MarketID = 1, Name = set.ExchangeName, BaseCurrency = set.FromCurrency, QuoteCurrency = set.ToCurrency, Active = true, Interval = 60
                                    });
                                }
                                exchangeID   = instrument.ExchangeID;
                                instrumentID = instrument.InstrumentID;
                                interval     = instrument.Interval;
                            };
                            InstrumentManager current = Manager.FirstOrDefault(x => x.InstrumentID == instrumentID && x.Type == 4);

                            if (current != null)
                            {
                                if (DateTime.UtcNow <= current.Last.AddSeconds(interval))
                                {
                                    return;
                                }
                                else
                                {
                                    current.Last = DateTime.UtcNow;
                                }
                            }
                            else if (current == null)
                            {
                                Manager.Add(new InstrumentManager()
                                {
                                    InstrumentID = instrumentID, Last = DateTime.UtcNow, Type = 4
                                });
                            }

                            CurrentData currentData = new CurrentData()
                            {
                                //Instruments = instrument,
                                ExchangeID   = exchangeID,
                                InstrumentID = instrumentID,
                                MarketID     = 1,
                                Flag         = set.Flag,
                                Price        = set.Price,
                                LastUpdate   = FromUnixTime(set.LastUpdate),
                                LastVolume   = set.LastVolume,
                                LastVolumeTo = set.LastVolumeTo,
                                LastTradeId  = set.LastTradeId,
                                Volume24h    = set.Volume24h,
                                Volume24hTo  = set.Volume24hTo,
                                MaskInt      = set.MaskInt
                            };
                            DataCalls.AddCurrentData(currentData);
                        }
                    }
                }

                return;
            }
            else
            {
                return;
            }
        }