Ejemplo n.º 1
0
        /// <summary>
        /// Checks if the shutdwon instruction is sent to desktop client or not
        /// </summary>
        private static async Task CheckShutdownInstruction()
        {
            WaitListClass data = null;
            Dictionary <string, string> DatatoSend = new Dictionary <string, string>();

            try
            {
                lock (WaitList)
                {
                    var val = WaitList.Select(x => x.Value);

                    var machineMacList = WaitList.Where(x => DateTime.Now.Subtract(x.Value.TimeofExec).TotalSeconds >= 35).Select(x => x.Key).ToList();
                    foreach (var m in machineMacList)
                    {
                        data = WaitList.Where(x => x.Key == m).Select(x => x.Value).FirstOrDefault();
                        WaitList.Remove(m);
                        var deskmac = DesktopList.Where(x => x.Value == m).Select(x => x.Key).FirstOrDefault();
                        DatatoSend.Add("Type", "Command");
                        DatatoSend.Add("Code", "ExeShutdown");//
                        DatatoSend.Add("CCmac", m.ToUpper());
                        DatatoSend.Add("Deskmac", deskmac.ToUpper());
                        var    t    = System.Text.Json.JsonSerializer.Serialize(DatatoSend);
                        byte[] b    = Encoding.UTF8.GetBytes(t);
                        var    sock = Clients.Where(x => x.Value.MacAddress == deskmac).Select(x => x.Value.workSocket).FirstOrDefault();
                        Send(sock, b);
                    }
                }
                if (data != null)
                {
                    var stid     = data.Strategyid;
                    var stdescid = data.StrategyDescId;
                    var instr    = data.Inst;
                    var equipid  = data.EquipmentId;
                    await AsyncTcpListener.ReceiveMacFromDesktop(data.Ccmac, "CloseStrategy", stid, stdescid, instr, equipid);
                }
            }
            catch (Exception ex)
            {
                loggerFile.Debug(Environment.NewLine + DateTime.Now.ToLongDateString() + " "
                                 + DateTime.Now.ToLongTimeString() + " Error Message in CheckShutdownInstruction() : " +
                                 ex.Message + "  inner details : " + ex.InnerException + " stack trace: " + ex.StackTrace);
            }
        }
Ejemplo n.º 2
0
        private static async Task CheckShutdownInstruction()
        {
            //List<string> machineMacList = new List<string>();
            Dictionary <string, string> DatatoSend = new Dictionary <string, string>();

            try
            {
                lock (WaitList)
                {
                    var val = WaitList.Select(x => x.Value);

                    var machineMacList = WaitList.Where(x => DateTime.Now.Subtract(x.Value.TimeofExec).TotalSeconds >= 35).Select(x => x.Key).ToList();
                    foreach (var m in machineMacList)
                    {
                        var data = WaitList.Where(x => x.Key == m).Select(x => x.Value).FirstOrDefault();
                        if (data != null)
                        {
                            var stid     = data.Strategyid;
                            var stdescid = data.StrategyDescId;
                            AsyncTcpListener.ReceiveMacFromDesktop(m, "SystemOffStrategy", stid, stdescid);
                        }

                        WaitList.Remove(m);
                        var deskmac = DesktopList.Where(x => x.Value == m).Select(x => x.Key).FirstOrDefault();
                        DatatoSend.Add("Type", "Command");
                        DatatoSend.Add("Code", "ExeShutdown");
                        DatatoSend.Add("CCmac", m.ToUpper());
                        DatatoSend.Add("Deskmac", deskmac.ToUpper());
                        var    t    = JsonSerializer.Serialize(DatatoSend);
                        byte[] b    = Encoding.ASCII.GetBytes(t);
                        var    sock = Clients.Where(x => x.Value.MacAddress == deskmac).Select(x => x.Value.workSocket).FirstOrDefault();
                        Send(sock, b);
                    }
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "Error in CheckShutdownInstruction() : " + ex.StackTrace);
            }
        }
Ejemplo n.º 3
0
        private static int DecodeDataDesktop(Dictionary <string, string> data, Socket sock)
        {
            Dictionary <string, string>   DatatoSend = new Dictionary <string, string>();
            KeyValuePair <string, string> r          = new KeyValuePair <string, string>();

            try
            {
                if (data["Type"] == "MacAddress")
                {
                    //Log("mac received " + data["value"]);
                    var           mac = data["value"].Split(',');
                    GetMacAddress gt  = new GetMacAddress();
                    r = gt.GetMac(mac);
                    if (r.Key != null)
                    {
                        Console.WriteLine("mac from database: " + r.Key + "mac : " + r.Value);
                        if (!DesktopList.Contains(r))
                        {
                            DesktopList.Add(r.Key, r.Value);
                        }
                        var client = new List <KeyValuePair <Socket, StateObject> >();
                        if (Clients.Any(x => x.Value.MacAddress == r.Key.ToUpper()))
                        {
                            client = Clients.Where(x => x.Value.MacAddress == r.Key.ToUpper()).ToList();
                        }
                        // Console.WriteLine("total clients by same mac addresses: " + client.Count());
                        if (client.Count() > 0)
                        {
                            foreach (var c in client)
                            {
                                ClearSocketDesktop(c.Value.workSocket);
                            }
                        }
                        if (Clients.Any(x => x.Value.workSocket == sock))
                        {
                            var cc = Clients.Where(x => x.Value.workSocket == sock).FirstOrDefault();
                            cc.Value.MacAddress = r.Key.ToUpper();
                        }
                        Console.WriteLine("total clients Desktop CLients: " + Clients.Count());
                        File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "mac from desktop key: " + r.Key + " value: " + r.Value);
                    }
                    else
                    {
                        File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "mac from database key: " + r.Key + " value: " + r.Value);
                        DatatoSend.Add("Type", "Reply");
                        DatatoSend.Add("Status", "404");

                        var    s = JsonSerializer.Serialize(DatatoSend);
                        byte[] b = Encoding.ASCII.GetBytes(s);
                        Send(sock, b);
                        ClearSocketDesktop(sock);
                    }
                    //SendToDesktop(r.Key, r.Value, "Shutdown");
                }
                if (data["Type"] == "Command")
                {
                    var code    = data["Code"];
                    var ccmac   = data["CCmac"].ToUpper();
                    var deskmac = data["Deskmac"].ToUpper();
                    File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "received " + code + " for machine with mac " + ccmac);
                    if (code == "Shutdown")
                    {
                        //Instructions ins = new Instructions();
                        //d = "SystemOffStrategy";

                        try
                        {
                            if (WaitList.ContainsKey(ccmac))
                            {
                                var d = WaitList.Where(x => x.Key == ccmac).Select(x => x.Value).FirstOrDefault();
                                if (d != null)
                                {
                                    var stid     = d.Strategyid;
                                    var stdescid = d.StrategyDescId;
                                    AsyncTcpListener.ReceiveMacFromDesktop(ccmac, "SystemOffStrategy", stid, stdescid);
                                }
                            }
                            DatatoSend.Clear();
                            DatatoSend.Add("Type", "Command");
                            DatatoSend.Add("Code", "ExeShutdown");
                            DatatoSend.Add("CCmac", ccmac.ToUpper());
                            DatatoSend.Add("Deskmac", deskmac.ToUpper());
                            var t = JsonSerializer.Serialize(DatatoSend);
                            File.AppendAllText(docPath, Environment.NewLine + " " + DateTime.Now.ToShortTimeString() + " data sent to desktop client: " + t);
                            Console.WriteLine(DateTime.Now.ToShortTimeString() + " data sent to desktop client: " + t);
                            byte[] b = Encoding.ASCII.GetBytes(t);
                            Send(sock, b);
                        }
#pragma warning disable CS0168 // The variable 'ex' is declared but never used
                        catch (Exception ex)
#pragma warning restore CS0168 // The variable 'ex' is declared but never used
                        {
                            File.AppendAllText(docPath, Environment.NewLine + " " + DateTime.Now.ToShortTimeString() +
                                               " Decode Desktop Data exception under send data to machine " + " " + ex.Message + " " + ex.StackTrace);
                            // Console.WriteLine(ex.Message);
                        }
                    }
                }

                Console.WriteLine(JsonSerializer.Serialize(data.ToList()));
            }
            catch (Exception ex)
            {
                //Log("Error in Decode Desktop data: " + ex.StackTrace);
                File.AppendAllText(docPath, Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + " Decode Desktop Data exception 2 " + " " + ex.Message + " " + ex.StackTrace);
                //  ClearSocketDesktop(ip, ((IPEndPoint)sock.RemoteEndPoint).Port);
            }
            return(1);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Decodes the data desktop.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="sock">The sock.</param>
        /// please look into the method for data reference
        /// <returns>System.Int32.</returns>
        private static async Task <int> DecodeDataDesktop(Dictionary <string, string> data, Socket sock)
        {
            GetMacAddress gt = new GetMacAddress();
            Dictionary <string, string>   DatatoSend = new Dictionary <string, string>();
            KeyValuePair <string, string> r          = new KeyValuePair <string, string>();

            try
            {
                if (data["Type"] == "MacAddress")
                {
                    //Log("mac received " + data["value"]);
                    var mac = data["value"].Split(',');

                    r = gt.GetMac(mac);
                    if (r.Key != null)
                    {
                        loggerFile.Debug("mac from database: " + r.Key + " mac of machine : " + r.Value);
                        if (!DesktopList.Contains(r))
                        {
                            DesktopList.Add(r.Key, r.Value);
                        }
                        var client = new List <KeyValuePair <Socket, StateObject> >();
                        if (Clients.Any(x => x.Value.MacAddress == r.Key.ToUpper()))
                        {
                            client = Clients.Where(x => x.Value.MacAddress == r.Key.ToUpper()).ToList();
                        }
                        // loggerFile.Debug("total clients by same mac addresses: " + client.Count());
                        if (client.Count() > 0)
                        {
                            foreach (var c in client)
                            {
                                ClearSocketDesktop(c.Value.workSocket);
                            }
                        }
                        if (Clients.Any(x => x.Value.workSocket == sock))
                        {
                            var cc = Clients.Where(x => x.Value.workSocket == sock).FirstOrDefault();
                            cc.Value.MacAddress = r.Key.ToUpper();
                        }
                        DatatoSend.Add("Type", "MacAddress");
                        DatatoSend.Add("Deskmac", r.Key);
                        var    s = System.Text.Json.JsonSerializer.Serialize(DatatoSend);
                        byte[] b = Encoding.ASCII.GetBytes(s);
                        Send(sock, b);
                        loggerFile.Debug("total clients Desktop CLients: " + Clients.Count());

                        loggerFile.Debug("mac from desktop key: " + r.Key + " value: " + r.Value);
                    }
                    else
                    {
                        DatatoSend.Add("Type", "Reply");
                        DatatoSend.Add("Status", "404");
                        var    s = System.Text.Json.JsonSerializer.Serialize(DatatoSend);
                        byte[] b = Encoding.ASCII.GetBytes(s);
                        Send(sock, b);
                        ClearSocketDesktop(sock);
                        try
                        {
                            loggerFile.Debug(Environment.NewLine + DateTime.Now.ToLongDateString() + " " +
                                             DateTime.Now.ToLongTimeString() + " desktop mac from database key: " +
                                             r.Key + " value: " + r.Value);
                        }
                        catch (Exception) { }
                    }
                    //SendToDesktop(r.Key, r.Value, "Shutdown");
                }
                if (data["Type"] == "Command")
                {
                    var code    = data["Code"];
                    var ccmac   = data["CCmac"].ToUpper();
                    var deskmac = data["Deskmac"].ToUpper();

                    if (code == "Shutdown")
                    {
                        try
                        {
                            if (WaitList.ContainsKey(ccmac))
                            {
                                var d = WaitList.Where(x => x.Key == ccmac).Select(x => x.Value).FirstOrDefault();
                                if (d != null)
                                {
                                    var stid     = d.Strategyid;
                                    var stdescid = d.StrategyDescId;
                                    var instr    = d.Inst;
                                    var equipid  = d.EquipmentId;
                                    await AsyncTcpListener.ReceiveMacFromDesktop(ccmac, "CloseStrategy", stid, stdescid, instr, equipid);
                                }
                                WaitList.Remove(ccmac);
                            }

                            DatatoSend.Clear();
                            DatatoSend.Add("Type", "Command");
                            DatatoSend.Add("Code", "ExeShutdown");
                            DatatoSend.Add("CCmac", ccmac.ToUpper());
                            DatatoSend.Add("Deskmac", deskmac.ToUpper());
                            //DatatoSend.Add("isDev","on");
                            var t = System.Text.Json.JsonSerializer.Serialize(DatatoSend);

                            byte[] b = Encoding.ASCII.GetBytes(t);
                            Send(sock, b);
                            //loggerFile.Debug(DateTime.Now.ToLongTimeString() + " data sent to desktop client: " + t);
                            //loggerFile.Debug(Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + "received " + code + " for machine with mac " + ccmac);
                            //loggerFile.Debug(Environment.NewLine + " " + DateTime.Now.ToLongTimeString() + " data sent to desktop client: " + t);
                        }
#pragma warning disable CS0168 // The variable 'ex' is declared but never used
                        catch (Exception ex)
#pragma warning restore CS0168 // The variable 'ex' is declared but never used
                        {
                            loggerFile.Debug(Environment.NewLine + " " + DateTime.Now.ToLongTimeString() +
                                             " Decode Desktop Data exception under send data to machine " + " " + ex.Message + " " + ex.StackTrace);
                            // Console.WriteLine(ex.Message);
                        }
                    }
                }

                if (data["Type"] == "DesktopEvent")
                {
                    var deskmac      = data["Deskmac"].ToString();
                    var action       = data["Action"].ToString();
                    int affectedRows = await gt.SaveInactiveDesktopAsync(deskmac, action);

                    var mes = new Dictionary <string, string>
                    {
                        { "Action", action }, { "Type", "DesktopEvent" }
                    };
                    AsyncTcpListener.SendDesktopEventToWebsocket(deskmac, mes);
                }
                Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(data.ToList()));
            }
            catch (Exception ex)
            {
                //Log("Error in Decode Desktop data: " + ex.StackTrace);
                loggerFile.Debug(Environment.NewLine + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + " Decode Desktop Data exception 2 " + " " + ex.Message + " " + ex.StackTrace);
                //  ClearSocketDesktop(ip, ((IPEndPoint)sock.RemoteEndPoint).Port);
            }
            return(1);
        }