Esempio n. 1
0
        private static bool checkConfig()
        {
            var rv = true;


            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID")))
            {
                U.log("ERROR - No AWS_ACCESS_KEY_ID environment value set");
                rv = false;
            }

            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY")))
            {
                U.log("ERROR - No AWS_SECRET_ACCESS_KEY environment value set");
                rv = false;
            }

            if (string.IsNullOrEmpty(U.config["S3Region"]))
            {
                U.log("ERROR - No S3Region config value set");
                rv = false;
            }

            if (string.IsNullOrEmpty(U.config["S3Bucket"]))
            {
                U.log("ERROR - No S3Bucket config value set");
                return(false);
            }


            if (!rv)
            {
                return(false);
            }



            ///finally preflight
            var s3u = new S3Uploader(U.config["S3Region"], U.config["S3Bucket"]);

            return(s3u.preflight());
        }
Esempio n. 2
0
        public static void getEvents()
        {
            U.log("Starting main event loop");

            var upl = new S3Uploader(U.config["S3Region"], U.config["S3Bucket"]);

            var basePath = U.config["ImgBasePath"];

            if (basePath.Substring(basePath.Length - 1) != "/")
            {
                basePath += "/";
            }

            var sleepSecs = U.config["LoopPauseSeconds"] ?? "3";



            var sleepMs = Convert.ToInt32(sleepSecs) * 1000;

            var lastNotifyTime = DateTime.Now.AddDays(-1);

            using (MySqlConnection connReader = new MySqlConnection(U.config["MySqlConnection"]))
                using (MySqlConnection connWriter = new MySqlConnection(U.config["MySqlConnection"]))
                {
                    while (true)
                    {
                        try
                        {
                            if (connReader.State != System.Data.ConnectionState.Open)
                            {
                                connReader.Open();
                            }

                            if (connWriter.State != System.Data.ConnectionState.Open)
                            {
                                connWriter.Open();
                            }
                            MySqlCommand cmd = new MySqlCommand(U.config["ZMQuery"], connReader);
                            //occassionally write out that we're running..
                            if ((DateTime.Now - lastNotifyTime).TotalSeconds > 120)
                            {
                                U.log("Checking for new events");
                                lastNotifyTime = DateTime.Now;
                            }
                            using (var reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    var e = new ZoneMinderEvent(Convert.ToInt64(reader["frameid"]), reader["monitor_name"].ToString());
                                    e.starttime = Convert.ToDateTime(reader["starttime"]);
                                    var img = new zmImage(e, U.config["ImgBasePath"], Convert.ToInt64(reader["eventid"]));
                                    if (File.Exists(img.fpath))
                                    {
                                        try
                                        {
                                            var s3Key = $"{e.monitorName}/{e.starttime.Year}/{e.starttime.Month.ToString().PadLeft(2, '0')}/{e.starttime.Day.ToString().PadLeft(2, '0')}/{e.starttime.Hour.ToString().PadLeft(2, '0')}-{e.starttime.Minute.ToString().PadLeft(2, '0')}";



                                            //fire and forget here..
                                            upl.uploadFile(img.fpath, s3Key);

                                            // update to mark them as uploaded
                                            var          sql          = $"insert alarm_uploaded (frameid,upload_timestamp,eventid) values ( {e.frameID}, CURRENT_TIMESTAMP, {img.eventId})";
                                            MySqlCommand insertCmd    = new MySqlCommand(sql, connWriter);
                                            var          rowsAffected = insertCmd.ExecuteNonQuery();
                                            if (rowsAffected <= 0)
                                            {
                                                U.log("Did not insert a row into alarm_uploaded");
                                            }
                                        }
                                        catch (Exception exc)
                                        {
                                            U.log("Error uploading/updating db", exc);
                                        }
                                    }
                                    else
                                    {
                                        U.log("File does not exist - " + img.fpath);
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            U.log("Error in outer loop", e);
                        }
                        System.Threading.Thread.Sleep(sleepMs);
                    }
                }
            //return list;
        }