Beispiel #1
0
        static void Main(string[] args)
        {
            //Utils.UpdateTimelapsesOnAzure();
            //Utils.CopyTimelapsesToAzure();

            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
            int tId = Convert.ToInt32(args[0]);

            // testing any timelapse
            //int tId = 398;

            Evercam.SANDBOX = Settings.EvercamSandboxMode;
            Evercam = new Evercam(Settings.EvercamClientID, Settings.EvercamClientSecret, Settings.EvercamClientUri);
            Timelapse timelapse = new Timelapse();
            try
            {
                tl = timelapse = TimelapseDao.Get(tId);
                string cleanCameraId = BLL.Common.Utils.RemoveSymbols(timelapse.CameraId);

                if (timelapse.ID == 0)
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Timelapse details not found", timelapse.TimeZone);
                    ExitProcess("Timelapse not found. ID = " + tId);
                }
                if (!timelapse.IsRecording)
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.Stopped, "Recording stopped", timelapse.TimeZone);
                    ExitProcess("Timelapse stopped. ID = " + tId);
                }
                if (string.IsNullOrEmpty(cleanCameraId))
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Camera details could not be retreived from Evercam", timelapse.TimeZone);
                    ExitProcess("Invalid Camera ID. Timelapse ID = " + tId + ", Camera ID = " + timelapse.CameraId);
                }

                //// User AuthToken is Unauthorized to access certain cameras, e.g. wayra_office
                //// may be shared cameras ?
                if (!string.IsNullOrEmpty(timelapse.OauthToken))
                    Evercam = new Evercam(timelapse.OauthToken);

                for (int i = 1; i <= TRY_COUNT; i++)
                {
                    //// tests x times if camera is available instantly otherwise exits
                    try
                    {
                        var data = Evercam.GetLiveImage(timelapse.CameraId);
                        break;
                    }
                    catch(Exception x) {
                        Utils.TimelapseLog(timelapse, "Main Error (try#" + i + "): " + x.ToString());
                        if (i < TRY_COUNT)
                            Thread.Sleep(RETRY_INTERVAL * 1000);    // 7 seconds
                        else
                        {
                            Snapshot snap = Evercam.GetLatestSnapshot(timelapse.CameraId, true);
                            byte[] data = snap.ToBytes();
                            if (data != null && data.Length > 0)
                                break;
                            else
                            {
                                BLL.Common.Utils.AppLog("Main Error in Timelapse#" + tId + ". Camera recording not found.");
                                TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.Failed, "Camera not accessible", timelapse.TimeZone);
                                ExitProcess("Camera not accessible");
                            }
                        }
                    }
                }

                Camera = Evercam.GetCamera(timelapse.CameraId);

                if (Camera == null || string.IsNullOrEmpty(Camera.ID))
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Camera details could not be retreived from Evercam", timelapse.TimeZone);
                    ExitProcess("Camera not found. ID = " + timelapse.CameraId);
                }
                if (!Camera.IsOnline)
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.Failed, "Camera went offline", timelapse.TimeZone);
                    ExitProcess("Camera is offline. ID = " + timelapse.CameraId);
                }

                Console.Title = "Timelapse (#" + tId + ") - Camera (#" + cleanCameraId + ")";
                Console.WriteLine("Running Timelapse (#" + tId + ") - Camera (#" + cleanCameraId + ")");

                UpPath = Path.Combine(FilePath, cleanCameraId, timelapse.ID.ToString());
                DownPath = Path.Combine(FilePath, cleanCameraId, timelapse.ID.ToString(), "images");
                TempPath = Path.Combine(FilePath, cleanCameraId, timelapse.ID.ToString(), "temp");

                if (!Directory.Exists(FfmpegCopyPath))
                    Directory.CreateDirectory(FfmpegCopyPath);
                if (!Directory.Exists(FilePath))
                    Directory.CreateDirectory(FilePath);
                if (!Directory.Exists(UpPath))
                    Directory.CreateDirectory(UpPath);
                if (!Directory.Exists(DownPath))
                    Directory.CreateDirectory(DownPath);
                if (!Directory.Exists(TempPath))
                    Directory.CreateDirectory(TempPath);

                Recorder recorder = new Recorder(timelapse);
                recorder.Start();
            }
            catch (Exception x)
            {
                if (x.Message.ToLower().Contains("not found"))
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Camera details could not be retreived from Evercam", timelapse.TimeZone);
                else if (x.Message.ToLower().Contains("not exist"))
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Camera details could not be retreived from Evercam", timelapse.TimeZone);
                else
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.Failed, "Camera not accessible", timelapse.TimeZone);

                BLL.Common.Utils.AppLog("Main Error in Timelapse#" + tId + ". ERR: " + x.Message);
                ExitProcess(x.Message);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //Utils.UpdateTimelapsesOnAzure();
            //Utils.CopyTimelapsesToAzure();

            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
            int tId = Convert.ToInt32(args[0]);

            Evercam.SANDBOX = Settings.EvercamSandboxMode;
            Evercam = new Evercam(Settings.EvercamClientID, Settings.EvercamClientSecret, Settings.EvercamClientUri);
            Timelapse timelapse = new Timelapse();
            try
            {
                tl = timelapse = TimelapseDao.Get(tId);
                string cleanCameraId = BLL.Common.Utils.RemoveSymbols(timelapse.CameraId);
                if (timelapse.ID == 0)
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Timelapse details not found", timelapse.TimeZone);
                    ExitProcess("Timelapse not found. ID = " + tId);
                }
                if (!timelapse.IsRecording)
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.Stopped, "Recording stopped", timelapse.TimeZone);
                    ExitProcess("Timelapse stopped. ID = " + tId);
                }
                if (string.IsNullOrEmpty(cleanCameraId))
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Camera details could not be retreived from Evercam", timelapse.TimeZone);
                    ExitProcess("Invalid Camera ID. Timelapse ID = " + tId + ", Camera ID = " + timelapse.CameraId);
                }

                //// User AuthToken is Unauthorized to access certain cameras, e.g. wayra_office
                //// may be shared cameras ?
                if (!string.IsNullOrEmpty(timelapse.OauthToken))
                {
                    string[] cred = timelapse.OauthToken.Split(':');
                    if (cred.Length >= 2)
                        Evercam = new Evercam(cred[0], cred[1]);
                    else
                        Evercam = new Evercam(timelapse.OauthToken);
                }

                for (int i = 1; i <= TRY_COUNT; i++)
                {
                    //// tests x times if camera is available instantly otherwise exits
                    try
                    {
                        var data = Evercam.GetLiveImage(timelapse.CameraId);
                        break;
                    }
                    catch(Exception x) {
                        Utils.TimelapseLog(timelapse, "Main Error (try#" + i + "): " + x.ToString());
                        if (i < TRY_COUNT)
                            Thread.Sleep(RETRY_INTERVAL * 1000);    // 7 seconds
                        else
                        {
                            byte[] data = Evercam.GetThumbnail(timelapse.CameraId, true);

                            if (data != null && data.Length > 0)
                                break;
                            else
                            {
                                BLL.Common.Utils.AppLog("Main Error in Timelapse#" + tId + ". Camera recording not found.");
                                TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.Failed, "Camera not accessible", timelapse.TimeZone);
                                ExitProcess("Camera not accessible");
                            }
                        }
                    }
                }
                FilePath = timelapse.TimelapsePath + Settings.BucketName;
                UpPath = Path.Combine(FilePath, cleanCameraId, timelapse.ID.ToString());
                DownPath = Path.Combine(FilePath, cleanCameraId, timelapse.ID.ToString(), "images");
                TsPath = Path.Combine(FilePath, cleanCameraId, timelapse.ID.ToString(), "ts");
                TempPath = Path.Combine(FilePath, cleanCameraId, timelapse.ID.ToString(), "temp");
                string hlsFileUrl = "http://timelapse.evercam.io/timelapses/" + cleanCameraId + "/" + timelapse.ID.ToString();

                if (!Directory.Exists(FilePath))
                    Directory.CreateDirectory(FilePath);
                if (!Directory.Exists(UpPath))
                    Directory.CreateDirectory(UpPath);
                if (!Directory.Exists(DownPath))
                    Directory.CreateDirectory(DownPath);
                if (!Directory.Exists(TsPath))
                    Directory.CreateDirectory(TsPath);
                if (!Directory.Exists(TempPath))
                    Directory.CreateDirectory(TempPath);
                //if (!File.Exists(Path.Combine(UpPath, "timelapse.m3u8")))
                CreateManifestFile(UpPath);
                CreateManifestFileForDownload(UpPath, hlsFileUrl);
                CreateBashFile(timelapse.FPS, DownPath, TsPath, chunkSize, timelapse.SnapsInterval);

                Recorder recorder = new Recorder(timelapse);
                string bashFile = Path.Combine(Program.UpPath, "build.sh");

                if (timelapse.RecreateHls)
                {
                    string old_ts_path = Path.Combine(FilePath, cleanCameraId, timelapse.ID.ToString(), "ts_old");
                    Directory.Move(TsPath, old_ts_path);
                    if (!Directory.Exists(TsPath))
                        Directory.CreateDirectory(TsPath);
                    recorder.CreateVideoChunks(bashFile);
                    TimelapseDao.UpdateReCreateHlsParams(timelapse.Code, false, false);
                    Directory.Delete(old_ts_path, true);
                    Utils.TimelapseLog(timelapse, "Program <<< Recreate HLS stream");
                }

                DirectoryInfo imagesDirectory = new DirectoryInfo(Program.DownPath);
                int imagesCount = imagesDirectory.GetFiles("*.jpg").Length;
                DirectoryInfo ts = new DirectoryInfo(TsPath);
                int hasTsFiles = ts.GetFiles("*.*").Length;
                if (imagesCount > timelapse.SnapsCount && (imagesCount - timelapse.SnapsCount) > 1000)
                {
                    ExitProcess("Something went wrong with timelapse: " + timelapse.ID + ", imagesCount: " + imagesCount + ", SnapsCount: " + timelapse.SnapsCount);
                }
                if (hasTsFiles == 0 && imagesCount > 24)
                {
                    recorder.CreateVideoChunks(bashFile);
                    Utils.TimelapseLog(timelapse, "Program <<< CreateVideoChunks");
                }

                Camera = Evercam.GetCamera(timelapse.CameraId);

                if (Camera == null || string.IsNullOrEmpty(Camera.ID))
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Camera details could not be retreived from Evercam", timelapse.TimeZone);
                    ExitProcess("Camera not found. ID = " + timelapse.CameraId);
                }
                if (!Camera.IsOnline)
                {
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.Failed, "Camera went offline", timelapse.TimeZone);
                    ExitProcess("Camera is offline. ID = " + timelapse.CameraId);
                }

                Console.Title = "Timelapse (#" + tId + ") - Camera (#" + cleanCameraId + ")";
                Console.WriteLine("Running Timelapse (#" + tId + ") - Camera (#" + cleanCameraId + ")");

                recorder.Start();
            }
            catch (Exception x)
            {
                if (x.Message.ToLower().Contains("not found"))
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Camera details could not be retreived from Evercam", timelapse.TimeZone);
                else if (x.Message.ToLower().Contains("not exist"))
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.NotFound, "Camera details could not be retreived from Evercam", timelapse.TimeZone);
                else
                    TimelapseDao.UpdateStatus(timelapse.Code, TimelapseStatus.Failed, "Camera not accessible", timelapse.TimeZone);

                BLL.Common.Utils.AppLog("Main Error in Timelapse#" + tId + ". ERR: " + x.Message);
                ExitProcess(x.Message);
            }
        }