Exemplo n.º 1
0
        /// <summary>
        /// Submits a usage report synchronously.  Should be called from a background thread.
        /// </summary>
        private static void PerfDataReportBackground()
        {
            try
            {
                // Generate record.
                Upload_Record record = GetPerfDataRecord();
                if (record == null)
                {
                    return;
                }

                // Submit record.
                if (saveLocalNextReport)
                {
                    File.WriteAllText("perfdata.json", JsonConvert.SerializeObject(record, Formatting.Indented));
                }

                if (uploadNextReport)
                {
                    string jsonPayload = JsonConvert.SerializeObject(record);
                    string md5         = Hash.GetMD5Hex(jsonPayload);
                    using (WebClient wc = new WebClient())
                    {
                        wc.UploadString("https://biupdatehelper.hopto.org/api/uploadUsageRecord2", jsonPayload + md5);
                    }
                }

                // Save the time so this doesn't happen again for a while.
                onReportUploaded();
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                Logger.Debug(ex, "Unable to generate anonymous performance data record.");
            }
            finally
            {
                sem.Release();
            }
        }
Exemplo n.º 2
0
        /// <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>
        private static Upload_Record GetPerfDataRecord()
        {
            Upload_Record record = new Upload_Record();

            BlueIrisConfiguration c = new BlueIrisConfiguration();

            c.Load();

            record.CpuUsage                = c.activeStats.CpuUsage;
            record.BiCpuUsage              = c.activeStats.BiCpuUsage;
            record.CpuThreads              = (short)Environment.ProcessorCount;
            record.BiVersion               = c.activeStats.BiVersion;
            record.BiMemUsageMB            = c.activeStats.BiMemUsageMB;
            record.BiPeakVirtualMemUsageMB = c.activeStats.BiPeakVirtualMemUsageMB;
            record.ConsoleOpen             = c.activeStats.ConsoleOpen;
            record.ConsoleWidth            = c.activeStats.ConsoleWidth;
            record.ConsoleHeight           = c.activeStats.ConsoleHeight;
            record.Secret = getSecretString();
            record.OS     = c.OS;

            if (c.cpu == null)
            {
                record.CpuModel = "Unknown";
                record.CpuMHz   = 0;
            }
            else
            {
                record.CpuModel = c.cpu.GetModel();
                record.CpuMHz   = NumberUtil.ParseInt(c.cpu.maxClockSpeed);
            }

            record.HelperVersion  = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            record.HwAccel        = (byte)c.global.HardwareAcceleration;
            record.ServiceMode    = c.global.ServiceMode;
            record.LivePreviewFPS = (short)c.global.LivePreviewFPS;

            record.MemMB     = c.activeStats.MemMB;
            record.MemFreeMB = c.activeStats.MemFreeMB;

            record.RamGiB        = c.mem.GiB;
            record.RamChannels   = c.mem.Channels;
            record.DimmLocations = c.mem.DimmLocations;
            record.RamMHz        = c.mem.MHz;

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

            int  currentProfile = 1;
            bool isAdmin        = false;
            bool gotCamlist     = false;
            bool gotStatus      = false;

            BiServerInfo.Reload();
            //BiUserInfo.Reload();
            if (BiServerInfo.enabled)
            {
                try
                {
                    using (WebClient wc = new WebClient())
                    {
                        UserInfo user    = BiUserInfo.CreateTemporaryUser();
                        string   session = CameraWebInterfaceLinker.GetSecureAuthenticatedSession(wc, out isAdmin, user.name, user.GetDecodedPassword());
                        try
                        {
                            try
                            {
                                string          response        = wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"camlist\",\"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;
                                        }
                                    }
                                    gotCamlist = true;
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Debug(ex, "Error reading camera list from web server.");
                            }

                            try
                            {
                                string         response       = wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"status\",\"session\":\"" + session + "\"}");
                                StatusResponse statusResponse = JsonConvert.DeserializeObject <StatusResponse>(response);
                                if (statusResponse != null && statusResponse.result == "success")
                                {
                                    currentProfile = statusResponse.data.profile;
                                    gotStatus      = true;
                                    if (currentProfile < 1 || currentProfile > 7)
                                    {
                                        currentProfile = 1;
                                        gotStatus      = false;
                                    }
                                    else
                                    {
                                        record.ProfileConfirmed = true;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Debug(ex, "Error reading camera list from web server.");
                            }
                        }
                        finally
                        {
                            wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"logout\",\"session\":\"" + session + "\"}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Debug(ex, "Error dealing with web server.");
                }
            }
            record.webserverState = 0;
            if (gotCamlist && gotStatus)
            {
                record.webserverState = 1;
                if (isAdmin)                 // and any future admin-requiring stuff all worked
                {
                    record.webserverState = 2;
                }
            }


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

            foreach (Camera camSrc in c.cameras.Values)
            {
                if (!camSrc.enabled)
                {
                    continue;
                }

                Upload_Camera cam = new Upload_Camera();

                if (fpsMap.TryGetValue(camSrc.shortname, out double fps))
                {
                    cam.FPS          = (byte)Math.Round(fps).Clamp(0, 255);
                    cam.FPSConfirmed = true;
                }
                else
                {
                    cam.FPS = (byte)(Math.Round(camSrc.MaxRate).Clamp(0, 255));
                }
                cam.CapType     = (byte)camSrc.CapType;
                cam.Hwaccel     = (byte)camSrc.Hwva;
                cam.LimitDecode = camSrc.LimitDecode;
                cam.Pixels      = camSrc.Pixels;
                if (camSrc.hasSubStream)
                {
                    cam.MainPixels = camSrc.MainPixels;
                }
                cam.Type              = (byte)camSrc.Type;
                cam.MotionDetector    = camSrc.triggerSettings[currentProfile].motionDetectionEnabled;
                cam.RecordTriggerType = (byte)camSrc.recordSettings[currentProfile].triggerType;
                cam.RecordFormat      = (byte)camSrc.recordSettings[currentProfile].recordingFormat;
                cam.DirectToDisk      = camSrc.recordSettings[currentProfile].DirectToDisc;
                cam.VCodec            = camSrc.recordSettings[currentProfile].VCodec;
                cameras.Add(cam);
            }

            record.AllFPSConfirmed = cameras.All(cam => cam.FPSConfirmed);             // Ignored and recalculated by server. This exists here for the sake of local json output.
            record.cameras         = cameras.ToArray();
            record.gpus            = c.gpus.Select(g => new Upload_Gpu()
            {
                Name = g.Name, Version = g.DriverVersion
            }).ToArray();

            record.Total_FPS = record.Total_Megapixels = record.Total_MPPS = 0;
            foreach (Upload_Camera cam in cameras)
            {
                float MP = cam.Pixels / 1000000f;
                record.Total_Megapixels += MP;
                record.Total_FPS        += cam.FPS;
                record.Total_MPPS       += MP * cam.FPS;
            }

            return(record);
        }