예제 #1
0
        public static void Reload()
        {
            RegistryKey server = RegistryUtil.HKLM.OpenSubKey("SOFTWARE\\Perspective Software\\Blue Iris\\server");

            if (server == null)
            {
                return;
            }
            enabled = RegistryUtil.GetStringValue(server, "enable") == "1";
            if (enabled)
            {
                try
                {
                    lanIp         = RegistryUtil.GetStringValue(server, "lanip");
                    bindLanIpOnly = RegistryUtil.GetStringValue(server, "bind") == "1";
                    port          = RegistryUtil.GetIntValue(server, "port", 80);
                    authenticate  = (AuthenticationMode)RegistryUtil.GetIntValue(server, "authenticate", 0);
                    secureonly    = RegistryUtil.GetStringValue(server, "secureonly") == "1";
                }
                catch
                {
                    enabled = false;
                }
            }
        }
        /// <summary>
        /// Creates an anonymous usage record.  This method spends 10 seconds measuring CPU usage.  Returns null if any BlueIris.exe processes close while CPU usage is being measured, or if no BlueIris.exe processes were open.
        /// </summary>
        /// <returns></returns>
        public static Upload_Record GetPerfDataRecord()
        {
            Upload_Record record = new Upload_Record();

            // Begin measuring CPU usage.
            using (PerformanceCounter totalCpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"))
            {
                totalCpuCounter.NextValue();
                Stopwatch sw = new Stopwatch();
                sw.Start();
                List <Process> biProcs = Process.GetProcesses().Where(p => p.ProcessName.ToLower() == "blueiris").ToList();
                if (biProcs.Count < 1)
                {
                    Logger.Info("Unable to generate anonymous performance data record because Blue Iris is not running.");
                    return(null);
                }
                TimeSpan[] startTimes = new TimeSpan[biProcs.Count];
                for (int i = 0; i < biProcs.Count; i++)
                {
                    startTimes[i] = biProcs[i].TotalProcessorTime;
                }

                // Wait for CPU usage to happen.
                Thread.Sleep(10000);

                // Take CPU usage measurements.
                record.CpuUsage = (byte)Math.Round(totalCpuCounter.NextValue());
                sw.Stop();
                TimeSpan totalTime = TimeSpan.Zero;
                for (int i = 0; i < biProcs.Count; i++)
                {
                    biProcs[i].Refresh();
                    if (biProcs[i].HasExited)
                    {
                        Logger.Info("Unable to generate anonymous performance data record because Blue Iris exited while CPU usage was being measured.");
                        return(null);
                    }
                    totalTime += biProcs[i].TotalProcessorTime - startTimes[i];
                }
                double fraction = totalTime.TotalMilliseconds / sw.Elapsed.TotalMilliseconds;
                record.BiCpuUsage = (byte)Math.Round((fraction / Environment.ProcessorCount) * 100);
                record.CpuThreads = (short)Environment.ProcessorCount;

                long physicalMemUsage = 0;
                long virtualMemUsage  = 0;
                foreach (Process p in biProcs)
                {
                    if (record.BiVersion == null)
                    {
                        record.BiVersion = p.MainModule.FileVersionInfo.FileVersion + " " + (MainSvc.Is64Bit(p) ? "x64" : "x86");
                    }
                    physicalMemUsage += p.WorkingSet64;
                    virtualMemUsage  += p.VirtualMemorySize64;
                }
                record.BiMemUsageMB            = (int)(physicalMemUsage / 1000000);
                record.BiPeakVirtualMemUsageMB = (int)(virtualMemUsage / 1000000);

                foreach (Process p in biProcs)
                {
                    IntPtr handle = p.MainWindowHandle;
                    if (handle == IntPtr.Zero)
                    {
                        // This is the service.
                    }
                    else
                    {
                        // This is the console.
                        record.ConsoleOpen = true;
                        try
                        {
                            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
                            if (GetWindowPlacement(handle, ref placement))
                            {
                                if (placement.showCmd == 2)
                                {
                                    // Minimized
                                    record.ConsoleWidth  = -2;
                                    record.ConsoleHeight = -2;
                                }
                                else
                                {
                                    // Not Minimized
                                    RECT Rect = new RECT();
                                    if (GetWindowRect(handle, ref Rect))
                                    {
                                        record.ConsoleWidth  = (short)NumberUtil.Clamp(Rect.right - Rect.left, 0, short.MaxValue);
                                        record.ConsoleHeight = (short)NumberUtil.Clamp(Rect.bottom - Rect.top, 0, short.MaxValue);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Debug(ex);
                        }
                    }
                }
                if (biProcs.Count > 1 && !record.ConsoleOpen)
                {
                    record.ConsoleOpen = true;
                }
            }

            record.Secret = Program.settings.secret;
            record.OS     = GetOsVersion();
            CpuInfo cpuInfo = GetCpuInfo();

            if (cpuInfo == null)
            {
                record.CpuModel = "Unknown";
                record.CpuMHz   = NumberUtil.ParseInt(cpuInfo.maxClockSpeed);
            }
            else
            {
                record.CpuModel = cpuInfo.GetModel();
                record.CpuMHz   = NumberUtil.ParseInt(cpuInfo.maxClockSpeed);
            }
            record.HelperVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            record.HwAccel       = (byte)RegistryUtil.GetHKLMValue <int>(@"SOFTWARE\Perspective Software\Blue Iris\Options", "hwaccel", 0);
            record.ServiceMode   = RegistryUtil.GetHKLMValue <int>(@"SOFTWARE\Perspective Software\Blue Iris\Options", "Service", 0) == 1;
            if (RegistryUtil.GetHKLMValue <int>(@"SOFTWARE\Perspective Software\Blue Iris\Options", "limitlive", 0) == 0)
            {
                record.LivePreviewFPS = -2;
            }
            else
            {
                record.LivePreviewFPS = (short)RegistryUtil.GetHKLMValue <int>(@"SOFTWARE\Perspective Software\Blue Iris\Options", "livefps", -1);
            }

            ComputerInfo computerInfo = new ComputerInfo();

            record.MemMB     = (int)(computerInfo.TotalPhysicalMemory / 1000000);
            record.MemFreeMB = (int)(computerInfo.AvailablePhysicalMemory / 1000000);

            RamInfo ramInfo = GetRamInfo();

            record.RamGiB        = ramInfo.GiB;
            record.RamChannels   = ramInfo.Channels;
            record.DimmLocations = ramInfo.DimmLocations;
            record.RamMHz        = ramInfo.MHz;

            // Get camera info.
            // Get frame rates (only accessible via BI's web server).
            Dictionary <string, double> fpsMap = new Dictionary <string, double>();

            BiServerInfo.Reload();
            if (BiServerInfo.enabled)
            {
                try
                {
                    using (WebClient wc = new WebClient())
                    {
                        string session  = CameraWebInterfaceLinker.GetSecureAuthenticatedSession(wc);
                        string response = wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"camlist\",\"session\":\"" + session + "\"}");
                        wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"logout\",\"session\":\"" + session + "\"}");
                        CamListResponse camListResponse = JsonConvert.DeserializeObject <CamListResponse>(response);
                        if (camListResponse != null && camListResponse.result == "success")
                        {
                            foreach (CameraListCamera camera in camListResponse.data)
                            {
                                if (camera.group == null)
                                {
                                    fpsMap[camera.optionValue] = camera.FPS;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Debug(ex, "Error reading camera list from web server.");
                }
            }

            // Get camera info from registry
            List <Upload_Camera> cameras = new List <Upload_Camera>();

            RegistryKey camerasKey = RegistryUtil.GetHKLMKey(@"SOFTWARE\Perspective Software\Blue Iris\Cameras");

            foreach (string camName in camerasKey.GetSubKeyNames())
            {
                RegistryKey camKey = camerasKey.OpenSubKey(camName);
                if (RegistryUtil.GetIntValue(camKey, "enabled", 0) != 1)
                {
                    continue;
                }
                string        shortName = RegistryUtil.GetStringValue(camKey, "shortname");
                Upload_Camera cam       = new Upload_Camera();

                if (fpsMap.TryGetValue(shortName, out double fps))
                {
                    cam.FPS = (byte)NumberUtil.Clamp(Math.Round(fps), 0, 255);
                }
                else
                {
                    int interval = RegistryUtil.GetIntValue(camKey, "interval", 1000000);
                    if (interval <= 0)
                    {
                        cam.FPS = 0;
                    }
                    else
                    {
                        cam.FPS = (byte)NumberUtil.Clamp(Math.Round(10000000.0 / interval), 0, 255);
                    }
                }
                cam.CapType     = (byte)RegistryUtil.GetIntValue(camKey, "screencap", 0);
                cam.Hwaccel     = (byte)RegistryUtil.GetIntValue(camKey, "ip_hwaccel", 0);
                cam.LimitDecode = RegistryUtil.GetIntValue(camKey, "smartdecode", 0) == 1;
                cam.Pixels      = RegistryUtil.GetIntValue(camKey, "fullxres", 0) * RegistryUtil.GetIntValue(camKey, "fullyres", 0);
                cam.Type        = (byte)RegistryUtil.GetIntValue(camKey, "type", 0);
                RegistryKey motionKey = camKey.OpenSubKey("Motion");
                cam.MotionDetector    = RegistryUtil.GetIntValue(motionKey, "enabled", 0) == 1;
                cam.RecordTriggerType = (byte)RegistryUtil.GetIntValue(motionKey, "continuous", 0);
                RegistryKey clipsKey = camKey.OpenSubKey("Clips");
                cam.RecordFormat = (byte)RegistryUtil.GetIntValue(clipsKey, "movieformat", 0);
                cam.DirectToDisk = RegistryUtil.GetIntValue(clipsKey, "transcode", 0) == 0;
                cam.VCodec       = RegistryUtil.GetStringValue(clipsKey, "vcodec");
                cameras.Add(cam);
            }

            record.cameras = cameras.ToArray();
            record.gpus    = GetGpuInfo().Select(g => new Upload_Gpu()
            {
                Name = g.Name, Version = g.DriverVersion
            }).ToArray();

            return(record);
        }