public void Execute(IJobExecutionContext context)
 {
     Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"{nameof(HeartbeatFeeder)}: {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")} UTC");
     for (int i = 0; i < FeederHandler.Instance.ImeiList.Count; i++)
     {
         try
         {
             string   currentImei  = FeederHandler.Instance.ImeiList[i];
             string   LastHbUtcStr = FeederHandler.Instance.Connector.StringGet($"Product_{currentImei}_Heartbeat");
             DateTime LastHbUtc;
             if (DateTime.TryParse(LastHbUtcStr, out LastHbUtc))
             {
                 using (RedisStressContext ctx = new RedisStressContext())
                 {
                     var prod = ctx.Products.Where(p => p.Imei == currentImei).FirstOrDefault();
                     if (prod != null)
                     {
                         prod.LastHbUtc = LastHbUtc;
                         ctx.SaveChanges();
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             Log4netLogger.Error(MethodBase.GetCurrentMethod().DeclaringType, $"Exception in loop #{i}: {ex}");
         }
     }
 }
Esempio n. 2
0
        public void RunStartActions()
        {
            Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, "Windows service OnStart");
            try
            {
                FeederHandler.Instance.Connector = new RedisConnector(ConfigurationManager.AppSettings["redisserver"]);
                using (RedisStressContext ctx = new RedisStressContext())
                {
                    FeederHandler.Instance.ImeiList = ctx.Products.Select(p => p.Imei).ToList();
                }
                Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"Loaded {FeederHandler.Instance.ImeiList.Count} IMEIs from SQL Server database into memory.");

                #region Quartz

                _schedulerFactory = new StdSchedulerFactory();
                _scheduler        = _schedulerFactory.GetScheduler();
                IJobDetail job     = JobBuilder.Create <HeartbeatFeeder>().WithIdentity("StatusFeederJob", "StatusFeederGroup").Build();
                ITrigger   trigger = TriggerBuilder.Create().WithIdentity("StatusFeederTrigger", "StatusFeederGroup").WithCronSchedule(ConfigurationManager.AppSettings["StatusFeederCron"], x => x.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("UTC"))).Build();
                _scheduler.ScheduleJob(job, trigger);
                _scheduler.Start();

                #endregion
            }
            catch (Exception ex)
            {
                Log4netLogger.Error(MethodBase.GetCurrentMethod().DeclaringType, ex);
            }
        }
Esempio n. 3
0
        public void RunStartActions()
        {
            HbListener.Instance.Connector = new RedisConnector(ConfigurationManager.AppSettings["redisserver"]);
            Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, "Connected to Redis server.");
            List <Product> products = null;

            using (RedisStressContext ctx = new RedisStressContext())
            {
                products = ctx.Products.ToList();
                Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"Get {products.Count} products.");
            }
            var start = DateTime.UtcNow;
            int count = 0;
            int countNull = 0, countHasValue = 0;

            foreach (Product prod in products)
            {
                try
                {
                    if (prod.LastHbUtc != null)
                    {
                        countHasValue++;
                        HbListener.Instance.Connector.StringSet($"Product_{prod.Imei}_Heartbeat", prod.LastHbUtc.Value.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        count++;
                    }
                    else
                    {
                        countNull++;
                        HbListener.Instance.Connector.StringSet($"Product_{prod.Imei}_Heartbeat", "");
                        count++;
                    }
                }
                catch (Exception ex)
                {
                    Log4netLogger.Error(MethodBase.GetCurrentMethod().DeclaringType, ex);
                }
            }
            var end = DateTime.UtcNow;

            Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, $"{countHasValue} products (in database) do have {nameof(Product.LastHbUtc)} value and {countNull} products (in database) don't. Successfully set {count} Redis keys in {(end - start).TotalMilliseconds} millisecs.");
            HbListener.Instance.PacketConnection = new UdpConnection();
            HbListener.Instance.PacketConnection.DataReceived += evtHandlerReceived;
            HbListener.Instance.PacketConnection.DataSent     += evtHandlerSent;
            HbListener.Instance.PacketConnection.StartUdpListening(int.Parse(ConfigurationManager.AppSettings["listenport"]));
            Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, "Heartbeat listener is running...");

            InternalNetworkListener.Instance.TcpConnection = new TcpConnection();
            InternalNetworkListener.Instance.TcpConnection.StartListening(int.Parse(ConfigurationManager.AppSettings["tcpport"]));
            Log4netLogger.Info(MethodBase.GetCurrentMethod().DeclaringType, "Listen TCP...");
        }
Esempio n. 4
0
        public static ProductDto AddProductReturnDto(string Imei)
        {
            Product p = new Product()
            {
                Imei = Imei, State = 1
            };

            using (RedisStressContext ctx = new RedisStressContext())
            {
                ctx.Products.Add(p);
                ctx.SaveChanges();
            }
            return(AutoMapper.Mapper.Map <ProductDto>(p));
        }
Esempio n. 5
0
        public static long AddProductReturnId(string Imei)
        {
            Product p = new Product()
            {
                Imei = Imei, State = 1
            };

            using (RedisStressContext ctx = new RedisStressContext())
            {
                ctx.Products.Add(p);
                ctx.SaveChanges();
            }
            return(p.Id);
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            EventHandler <PacketDataReceivedEventArgs> evtHandlerReceived = new EventHandler <PacketDataReceivedEventArgs>(HbSpammer.Instance.DataReceived);
            EventHandler <PacketDataSentEventArgs>     evtHandlerSent     = new EventHandler <PacketDataSentEventArgs>(HbSpammer.Instance.DataSent);

            HbSpammer.Instance.PacketConnection = new UdpConnection();
            HbSpammer.Instance.PacketConnection.DataReceived += evtHandlerReceived;
            HbSpammer.Instance.PacketConnection.DataSent     += evtHandlerSent;
            HbSpammer.Instance.PacketConnection.StartUdpListening(int.Parse(ConfigurationManager.AppSettings["localPort"]));
            IPEndPoint localEndPoint, remoteEndPoint;

            localEndPoint  = HbSpammer.Instance.PacketConnection.ConnectionSocket.LocalEndPoint as IPEndPoint;
            remoteEndPoint = new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings["remoteIP"]), int.Parse(ConfigurationManager.AppSettings["remotePort"]));
            HbSpammer.Instance.PacketConnection.destinationTuple = new DestinationTuple(localEndPoint, remoteEndPoint);
            Console.Write("How many Heartbeats do you want to send very second? ");
            bool success;

            do
            {
                success = int.TryParse(Console.ReadLine(), out HbSpammer.Instance.frequency);
            }while (!success);
            try
            {
                List <string> ImeiList = null;
                using (RedisStressContext ctx = new RedisStressContext())
                {
                    ImeiList = ctx.Products.Select(pr => pr.Imei).ToList();
                }
                for (int i = 0; i < ImeiList.Count; i++)
                {
                    int    ImeiInt = int.Parse(ImeiList[i]);
                    byte[] ImeiByteArray = BitConverter.GetBytes(ImeiInt).Reverse().ToArray();
                    byte[] HbByteArray = new byte[] { 0xAB, 0x01, 0x06 }.Concat(ImeiByteArray).Concat(new byte[] { 0x00 }).ToArray();
                    HbSpammer.Instance.HbByteStreamList.Add(HbByteArray);
                }
                HbSpammer.Instance._scheduler1 = HbSpammer.Instance._schedulerFactory1.GetScheduler();
                IJobDetail job        = JobBuilder.Create <HbBatchSend>().WithIdentity("HbBatchSend", "HbBatchSendGroup").Build();
                string     cronString = "* * * * * ? *";
                ITrigger   trigger    = TriggerBuilder.Create().WithIdentity("EverySecond", "HbBatchSendGroup").WithCronSchedule(cronString, x => x.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("UTC"))).Build();
                HbSpammer.Instance._scheduler1.ScheduleJob(job, trigger);
                HbSpammer.Instance._scheduler1.Start();
            }
            catch (Exception ex)
            {
                Log4netLogger.Error(MethodBase.GetCurrentMethod().DeclaringType, ex);
            }
        }