예제 #1
0
        public FrameController(IHubContext <Default> hubContext, IHostingEnvironment env, IMemoryCache memoryCache)
        {
            _hubContext = hubContext;
            _env        = env;
            _cache      = memoryCache;

            try
            {
                Type xType = Type.GetType("DevCDRServer.Default");

                MemberInfo[] memberInfos = xType.GetMember("wwwrootPath", BindingFlags.Public | BindingFlags.Static);
                ((FieldInfo)memberInfos[0]).SetValue(new string(""), _env.WebRootPath);
            }
            catch { }


            if (string.IsNullOrEmpty(AzureLog.WorkspaceId))
            {
                if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Log-WorkspaceID")) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Log-SharedKey")))
                {
                    AzureLog = new DevCDR.Extensions.AzureLogAnalytics(Environment.GetEnvironmentVariable("Log-WorkspaceID"), Environment.GetEnvironmentVariable("Log-SharedKey"), "DevCDR_" + (Environment.GetEnvironmentVariable("INSTANCENAME") ?? "Default"));
                    //AzureLog.PostAsync(new { Computer = Environment.MachineName, EventID = 0001, Description = "DevCDRController started" });
                }
            }
        }
예제 #2
0
        public async void Status2(string name, string Status, string signature)
        {
            name = name.ToLower();
            X509AgentCert oSig = new X509AgentCert(signature, true); //do not validate signature for performance

            if (string.IsNullOrEmpty(AzureLog.WorkspaceId))
            {
                if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Log-WorkspaceID")) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Log-SharedKey")))
                {
                    AzureLog = new DevCDR.Extensions.AzureLogAnalytics(Environment.GetEnvironmentVariable("Log-WorkspaceID"), Environment.GetEnvironmentVariable("Log-SharedKey"), "DevCDR_" + (Environment.GetEnvironmentVariable("INSTANCENAME") ?? "Default"));
                }
            }

            var J1 = JObject.Parse(Status);

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("IP2LocationURL")))
            {
                string ClientIP = Context.GetHttpContext().Connection.RemoteIpAddress.ToString();
                J1["Internal IP"] = ClientIP;
                try
                {
                    J1["Internal IP"] = GetLocAsync(ClientIP).Result;
                    if (J1["IP"] == null)
                    {
                        J1.Add(new JProperty("IP", ClientIP));
                    }
                    else
                    {
                        J1["IP"] = ClientIP;
                    }
                }
                catch { }
            }

            if (J1["Customer"] == null)
            {
                J1.Add("Customer", "");
            }

            bool bChange = false;

            try
            {
                if (string.IsNullOrEmpty(J1.GetValue("Hostname").Value <string>()))
                {
                    return;
                }

                if (string.IsNullOrEmpty(J1.GetValue("id").Value <string>()))
                {
                    return;
                }

                if (jData.SelectTokens("[?(@.id == '" + J1.GetValue("id") + "')]").Count() == 0) //Prevent Duplicates
                {
                    J1.Add("ConnectionId", Context.ConnectionId);
                    lock (jData)
                    {
                        jData.Add(J1);
                    }
                    bChange = true;
                    _connections.Add(name, Context.ConnectionId);               //Add Device
                    await Clients.Group("web").SendAsync("newData", "add", ""); //Enforce PageUpdate

                    AzureLog.PostAsync(new { Computer = J1.GetValue("Hostname"), EventID = 3000, Description = J1.GetValue("ScriptResult").ToString() });
                    if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("fnDevCDR")))
                    {
                        if (!string.IsNullOrEmpty(oSig.CustomerID))
                        {
                            await setStatusAsync(oSig.CustomerID, oSig.DeviceID, J1.ToString());
                        }
                        else
                        {
                            await setStatusAsync(Environment.GetEnvironmentVariable("INSTANCENAME") ?? "Default", J1.GetValue("id").ToString(), J1.ToString());
                        }
                    }
                }
                else
                {
                    var jTemp = JObject.Parse(jData.SelectTokens("[?(@.id == '" + J1.GetValue("id") + "')]", false).First().ToString());

                    await Clients.Group("web").SendAsync("newData", jTemp.ToString(), ""); //Enforce PageUpdate

                    //Changes ?
                    if ((jTemp["ScriptResult"].Value <string>().ToLower() != J1["ScriptResult"].Value <string>().ToLower()) || (jTemp["Version"].Value <string>().ToLower() != J1["Version"].Value <string>().ToLower()))
                    {
                        lock (jData)
                        {
                            jData.SelectTokens("[?(@.id == '" + J1.GetValue("id") + "')]", false).First().Replace(J1);
                            bChange = true;
                        }
                    }

                    //if (jData.SelectTokens("[?(@.Hostname == '" + J1.GetValue("Hostname") + "')]").First().ToString(Formatting.None).ToLower() != J1.ToString(Formatting.None).ToLower())
                    //{
                    //    await Clients.Group("web").SendAsync("newData", "change", jData.ToString()); //Enforce PageUpdate
                    //    lock (jData)
                    //    {
                    //        jData.SelectTokens("[?(@.Hostname == '" + J1.GetValue("Hostname") + "')]", false).First().Replace(J1);
                    //        bChange = true;
                    //    }
                    //    await Clients.Group("web").SendAsync("newData", "done", jData.ToString()); //Enforce PageUpdate
                    //}

                    if (bChange)
                    {
                        _ = Task.Run(() =>
                        {
                            AzureLog.PostAsync(new { Computer = J1.GetValue("Hostname"), EventID = 3000, Description = J1.GetValue("ScriptResult").ToString() });

                            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("fnDevCDR")))
                            {
                                if (!string.IsNullOrEmpty(oSig.CustomerID))
                                {
                                    setStatusAsync(oSig.CustomerID, J1.GetValue("id").ToString(), J1.ToString()).Wait(2000);
                                }
                                else
                                {
                                    setStatusAsync(Environment.GetEnvironmentVariable("INSTANCENAME") ?? "Default", J1.GetValue("id").ToString(), J1.ToString()).Wait(2000);
                                }
                            }
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                bChange = true; //update on error...
            }

            if (bChange)
            {
                try
                {
                    await Clients.Group("web").SendAsync("newData", name, ""); //Enforce PageUpdate
                }
                catch (Exception ex)
                {
                    ex.Message.ToString();
                }
            }
        }
예제 #3
0
        public async void Status(string name, string Status)
        {
            name = name.ToLower();

            if (string.IsNullOrEmpty(AzureLog.WorkspaceId))
            {
                if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Log-WorkspaceID")) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Log-SharedKey")))
                {
                    AzureLog = new DevCDR.Extensions.AzureLogAnalytics(Environment.GetEnvironmentVariable("Log-WorkspaceID"), Environment.GetEnvironmentVariable("Log-SharedKey"), "DevCDR_" + (Environment.GetEnvironmentVariable("INSTANCENAME") ?? "Default"));
                }
            }

            var J1 = JObject.Parse(Status);

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("fnDevCDR")))
            {
                J1["Groups"] = "unknown";
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("IP2LocationURL")))
            {
                string ClientIP = Context.GetHttpContext().Connection.RemoteIpAddress.ToString();
                J1["Internal IP"] = ClientIP;
                try
                {
                    J1["Internal IP"] = GetLocAsync(ClientIP).Result;
                }
                catch { }
            }

            if (J1["Customer"] == null)
            {
                J1.Add("Customer", "");
            }

            bool bChange = false;

            try
            {
                if (string.IsNullOrEmpty(J1.GetValue("Hostname").Value <string>()))
                {
                    return;
                }

                if (jData.SelectTokens("[?(@.Hostname == '" + J1.GetValue("Hostname") + "')]").Count() == 0) //Prevent Duplicates
                {
                    J1.Add("ConnectionId", Context.ConnectionId);

                    lock (jData)
                    {
                        jData.Add(J1);
                    }
                    bChange = true;
                    _connections.Add(name, Context.ConnectionId);

                    await Clients.Group("web").SendAsync("newData", "add", ""); //Enforce PageUpdate

                    if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("fnDevCDR")))
                    {
                        try
                        {
                            await setStatusAsync(Environment.GetEnvironmentVariable("INSTANCENAME") ?? "Default", J1.GetValue("id").ToString(), J1.ToString());
                        }
                        catch { }
                    }

                    AzureLog.PostAsync(new { Computer = J1.GetValue("Hostname"), EventID = 3000, Description = J1.GetValue("ScriptResult").ToString() });
                }
                else
                {
                    lock (jData)
                    {
                        //Changes ?
                        if (jData.SelectTokens("[?(@.Hostname == '" + J1.GetValue("Hostname") + "')]").First().ToString(Formatting.None) != J1.ToString(Formatting.None))
                        {
                            jData.SelectTokens("[?(@.Hostname == '" + J1.GetValue("Hostname") + "')]").First().Replace(J1);
                            bChange = true;
                            AzureLog.PostAsync(new { Computer = J1.GetValue("Hostname"), EventID = 3000, Description = J1.GetValue("ScriptResult").ToString() });

                            //if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("fnDevCDR")))
                            //{
                            //    J1.Add("ConnectionId", Context.ConnectionId);
                            //    setStatusAsync(Environment.GetEnvironmentVariable("INSTANCENAME") ?? "Default", J1.GetValue("id").ToString(), J1.ToString());
                            //}
                        }
                        else
                        {
                            //if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("fnDevCDR")))
                            //{
                            //    string sJSON = "{\"ConnectionID\":\"" + Context.ConnectionId + "\"}";
                            //    setStatusAsync(Environment.GetEnvironmentVariable("INSTANCENAME") ?? "Default", J1.GetValue("Hostname").ToString(), sJSON);
                            //}
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                bChange = true;
                ex.Message.ToString();
            }

            if (bChange)
            {
                try
                {
                    await Clients.Group("web").SendAsync("newData", name, ""); //Enforce PageUpdate
                }
                catch (Exception ex)
                {
                    ex.Message.ToString();
                }
            }
        }