/// <summary>
        /// Runs the test timer.
        /// THis timer is for strategymode='test'
        /// </summary>
        /// <param name="state">The state.</param>
        private static void RunTestTimer(object state)
        {
            StrategyExec se   = new StrategyExec();
            var          data = se.GetTestTimeData();

            if (data.Count > 0)
            {
                loggerFile.Debug("total desktop client connected when test mode is running: "
                                 + Clients.Count);
                try
                {
                    foreach (var s in data)
                    {
                        Dictionary <string, object> DatatoSend = new Dictionary <string, object>();
                        DatatoSend.Add("Type", "Command");
                        DatatoSend.Add("CCmac", s.CCmac.ToUpper());
                        DatatoSend.Add("Deskmac", s.Deskmac.ToUpper());
                        DatatoSend.Add("publishTexts", s.PublishText);
                        DatatoSend.Add("publishTitle", s.PublishTitle);
                        DatatoSend.Add("startTime", s.StartTime);
                        DatatoSend.Add("endTime", s.EndTime);
                        DatatoSend.Add("Code", s.Code);
                        DatatoSend.Add("Subject", s.Subject);
                        var da   = JsonConvert.SerializeObject(DatatoSend);
                        var sock = Clients.Where(x => x.Value.MacAddress == s.Deskmac.ToUpper())
                                   .Select(x => x.Value.workSocket).FirstOrDefault();
                        if (sock != null)
                        {
                            byte[] bytes = Encoding.UTF8.GetBytes(da);
                            loggerFile.Debug("deskmac: " + s.Deskmac + " data: " +
                                             da);
                            Send(sock, bytes);
                        }
                        DatatoSend.Clear();
                    }
                }
                catch (Exception ex)
                {
                    loggerFile.Debug(Environment.NewLine + DateTime.Now.ToLongDateString() + " "
                                     + DateTime.Now.ToLongTimeString() + "Error in test mode sending data to desktop  "
                                     + ex.Message + " stack trace :" + ex.StackTrace + " Inner exception: " + ex.InnerException);
                }
            }
        }
Esempio n. 2
0
        private static void RunStrategyTimer(object state)
        {
            Machines.ForEach(x => Console.WriteLine("ip: " + ((IPEndPoint)x.workSocket.RemoteEndPoint).Address.ToString() + " Mac Address: " + x.MacAddress));
            StringBuilder record = new StringBuilder();

            foreach (var s in Machines)
            {
                record.AppendLine(JsonSerializer.Serialize("Mac: " + s.MacAddress + " ip: " + ((IPEndPoint)s.workSocket.RemoteEndPoint).Address.ToString()));
            }
            Console.WriteLine("To record in file {0}", record.ToString());
            var          tt           = DateTime.Now.ToString("HH:mm") + ":00";
            StrategyExec strategyExec = new StrategyExec();

            try
            {
                File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + "Connected machines details:  " + record.ToString());

                var          ff   = strategyExec.GetData(tt);
                Instructions inst = new Instructions();

                if (ff.Count > 0)
                {
                    //StrategyLogs stlogs = new StrategyLogs();
                    foreach (FinalResult f in ff)
                    {
                        try
                        {
                            File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() +
                                               " Strategy Command : " + DateTime.Now.ToShortTimeString() + JsonSerializer.Serialize(f));
                            if (f.Instruction == "SystemOffStrategy")
                            {
                                int r = AsyncDesktopServer.SendToDesktop(f.Ccmac, f.Deskmac, "Shutdown", f.StrategyDescId, f.StrategyId);
                                if (r == 0)
                                {
                                    lock (Machines)
                                    {
                                        if (Machines.Count > 0)
                                        {
                                            if (Machines.Any(x => x.MacAddress == f.Ccmac))
                                            {
                                                var t = Machines.Where(x => x.MacAddress == f.Ccmac).Select(x => x.workSocket).FirstOrDefault();

                                                if (isClientConnected(t))
                                                {
                                                    // stlogs.SaveStrategyLogInfo(f.Instruction, f.StrategyId, f.StrategyDescId, "1", f.Ccmac);
                                                    Send(t, inst.GetValues(f.Instruction));
                                                }
                                                else
                                                {
                                                    //stlogs.SaveStrategyLogInfo(f.Instruction, f.StrategyId, f.StrategyDescId, "0", f.Ccmac);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                lock (Machines)
                                {
                                    if (Machines.Count > 0)
                                    {
                                        if (Machines.Any(x => x.MacAddress == f.Ccmac))
                                        {
                                            var t = Machines.Where(x => x.MacAddress == f.Ccmac).Select(x => x.workSocket).FirstOrDefault();
                                            if (isClientConnected(t))
                                            {
                                                //stlogs.SaveStrategyLogInfo(f.Instruction, f.StrategyId, f.StrategyDescId, "1", f.Ccmac);
                                                Send(t, inst.GetValues(f.Instruction));
                                            }
                                            else
                                            {
                                                //stlogs.SaveStrategyLogInfo(f.Instruction, f.StrategyId, f.StrategyDescId, "0", f.Ccmac);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " +
                                               DateTime.Now.ToShortTimeString() + "Error in RunStrategyTimer sending instruction to machine : " + ex.StackTrace);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "Error in RunStrategyTimer: " + ex.StackTrace);
                Console.WriteLine(ex.Message);
            }
        }