Пример #1
0
        public List <Snapshot> GetSnapshots(string key)
        {
            SnapmailRowData snapmail = SnapmailDao.Get(key);

            Evercam.SANDBOX = Settings.EvercamSandboxMode;
            Evercam evercam = new Evercam(snapmail.AccessToken);

            string[] cred = snapmail.AccessToken.Split(':');
            if (cred.Length >= 2)
            {
                evercam = new Evercam(cred[0], cred[1]);
            }
            List <Snapshot> snaps = new List <Snapshot>();

            string[]      camids = snapmail.Cameras.Split(',');
            List <Camera> cams   = evercam.GetCameras(null, snapmail.UserId, true);

            foreach (Camera c in cams)
            {
                var results = Array.FindAll(camids, s => s.Equals(c.ID));
                if (results.Length > 0 && c.Thumbnail != null)
                {
                    Snapshot snap = new Snapshot()
                    {
                        Data = c.Thumbnail
                    };
                    snaps.Add(snap);
                }
            }
            return(snaps);
        }
Пример #2
0
        private int StartSnapmailer(SnapmailRowData snapmail)
        {
            try
            {
                int pid = ProcessRunning(snapmail);

                if (snapmail.IsActive && !snapmail.IsScheduled)
                {
                    ProcessStartInfo process = new ProcessStartInfo(SnapmailExePath, snapmail.RowKey);
                    process.UseShellExecute = true;
                    process.WindowStyle     = ProcessWindowStyle.Normal;

                    Process currentProcess = Process.Start(process);

                    Utils.FileLog("Executor.StartSnapmailer(" + snapmail.RowKey + ")");

                    return(currentProcess.Id);
                }
                else if (snapmail.IsActive && snapmail.IsScheduled)
                {
                    if (pid == 0)
                    {
                        ProcessStartInfo process = new ProcessStartInfo(SnapmailExePath, snapmail.RowKey);
                        process.UseShellExecute = true;
                        process.WindowStyle     = ProcessWindowStyle.Normal;

                        Process currentProcess = Process.Start(process);

                        Utils.FileLog("Executor.StartSnapmailer(" + snapmail.RowKey + ") - re");

                        return(currentProcess.Id);
                    }
                }
                else if (!snapmail.IsActive && snapmail.IsScheduled)
                {
                    if (pid > 0)
                    {
                        KillProcess(pid, snapmail.RowKey);
                    }
                    //SnapmailDao.UpdateScheduled(snapmail.RowKey, false);
                }
                else if (!snapmail.IsActive && !snapmail.IsScheduled)
                {
                    if (pid > 0)
                    {
                        KillProcess(pid, snapmail.RowKey);
                    }
                }
            }
            catch (Exception x)
            {
                Utils.FileLog("Executor.StartSnapmailer(" + snapmail.RowKey + ") Error: " + x.Message);
            }
            return(0);
        }
Пример #3
0
 public static SnapmailRowData Insert(SnapmailRowData snapmail)
 {
     try
     {
         CloudTable     tblSnapmails = GetAzureTable(SNAPMAIL_TABLE_NAME);
         TableOperation insertEmail  = TableOperation.Insert(snapmail);
         tblSnapmails.Execute(insertEmail);
         return(Get(snapmail.RowKey));
     }
     catch (Exception x)
     {
         return(snapmail);
     }
 }
Пример #4
0
        public static SnapmailRowData UpdateSnapmailToken(string key, string access_token)
        {
            CloudTable      tblSnapmails = GetAzureTable(SNAPMAIL_TABLE_NAME);
            SnapmailRowData snapmail     = Get(key);

            try
            {
                snapmail.AccessToken = access_token;

                TableOperation updateToken = TableOperation.Replace(snapmail);
                var            result      = tblSnapmails.Execute(updateToken);
                return(Get(key));
            }
            catch (Exception x)
            {
                return(snapmail);
            }
        }
Пример #5
0
        public static SnapmailRowData UpdateScheduled(string key, bool scheduled)
        {
            CloudTable      tblSnapmails = GetAzureTable(SNAPMAIL_TABLE_NAME);
            SnapmailRowData snapmail     = Get(key);

            try
            {
                snapmail.IsScheduled = scheduled;

                TableOperation updateEmail = TableOperation.Replace(snapmail);
                var            result      = tblSnapmails.Execute(updateEmail);
                return(Get(key));
            }
            catch (Exception x)
            {
                return(snapmail);
            }
        }
Пример #6
0
        public static bool Delete(string key, bool active)
        {
            CloudTable      tblSnapmails = GetAzureTable(SNAPMAIL_TABLE_NAME);
            SnapmailRowData snapmail     = Get(key);

            try
            {
                snapmail.IsActive = active;

                TableOperation deleteEmail = TableOperation.Replace(snapmail);
                var            result      = tblSnapmails.Execute(deleteEmail);

                return(true);
            }
            catch (Exception x)
            {
                return(false);
            }
        }
Пример #7
0
        public static SnapmailRowData UpdateEmail(string key, string email, DateTime lastSent)
        {
            CloudTable      tblSnapmails = GetAzureTable(SNAPMAIL_TABLE_NAME);
            SnapmailRowData snapmail     = Get(key);

            try
            {
                snapmail.SentMail = email;
                snapmail.LastSent = lastSent.ToString();

                TableOperation updateEmail = TableOperation.Replace(snapmail);
                var            result      = tblSnapmails.Execute(updateEmail);
                return(Get(key));
            }
            catch (Exception x)
            {
                return(snapmail);
            }
        }
Пример #8
0
        public static SnapmailInfoModel ToInfoModel(SnapmailRowData data)
        {
            return(new SnapmailInfoModel()
            {
                key = data.RowKey,
                user_id = data.UserId,

                cameras = data.Cameras,
                camera_names = data.CameraNames,
                recipients = data.Recipients,
                notify_days = data.NotifyDays,
                notify_time = data.NotifyTime,
                timezone = data.TimeZone,
                last_sent = data.LastSent != null?data.LastSent.ToString() : DateTime.MinValue.ToString(),
                                sent_mail = data.SentMail,
                                is_active = data.IsActive,
                                is_scheduled = data.IsScheduled,
                                timestamp = data.Timestamp.ToString(),
                                access_token = data.AccessToken
            });
        }
Пример #9
0
        public static SnapmailRowData Unsubscribe(string key, string email)
        {
            CloudTable      tblSnapmails = GetAzureTable(SNAPMAIL_TABLE_NAME);
            SnapmailRowData snapmail     = Get(key);

            try
            {
                string emails = snapmail.Recipients;
                emails = emails.ToLower().Remove(emails.IndexOf(email.ToLower()), email.Length);

                snapmail.Recipients = emails;

                TableOperation updateEmail = TableOperation.Replace(snapmail);
                var            result      = tblSnapmails.Execute(updateEmail);
                return(Get(key));
            }
            catch (Exception x)
            {
                return(snapmail);
            }
        }
Пример #10
0
        public static SnapmailRowData Update(string key, SnapmailRowData snapmail)
        {
            try
            {
                CloudTable      tblSnapmails = GetAzureTable(SNAPMAIL_TABLE_NAME);
                SnapmailRowData old          = Get(key);
                SnapmailRowData mail         = new SnapmailRowData()
                {
                    RowKey       = key,
                    PartitionKey = snapmail.PartitionKey,
                    ETag         = snapmail.ETag,
                    Timestamp    = snapmail.Timestamp,
                    Cameras      = snapmail.Cameras,
                    CameraNames  = snapmail.CameraNames,
                    Message      = old.Message,
                    NotifyDays   = snapmail.NotifyDays,
                    NotifyTime   = snapmail.NotifyTime,
                    TimeZone     = snapmail.TimeZone,
                    Recipients   = snapmail.Recipients,
                    Subject      = old.Subject,
                    UserId       = snapmail.UserId,
                    UserName     = snapmail.UserName,
                    IsActive     = old.IsActive,
                    IsScheduled  = old.IsScheduled,
                    AccessToken  = snapmail.AccessToken,
                    RefreshToken = snapmail.RefreshToken,
                    TokenExpires = snapmail.TokenExpires,
                    TokenCreated = snapmail.TokenCreated
                };

                TableOperation updateEmail = TableOperation.Replace(mail);
                var            result      = tblSnapmails.Execute(updateEmail);
                return(Get(key));
            }
            catch (Exception x)
            {
                return(snapmail);
            }
        }
Пример #11
0
 private int ProcessRunning(SnapmailRowData snapmail)
 {
     try
     {
         int       id          = 0;
         Process[] processlist = Process.GetProcesses();
         foreach (Process process in processlist)
         {
             if (process.ProcessName.ToLower().StartsWith("snapmailer_"))
             {
                 string _id = process.ProcessName.Substring(
                     process.ProcessName.IndexOf("_") + 1,
                     process.ProcessName.Length - (process.ProcessName.IndexOf("_") + 1));
                 if (_id == Utils.CleanFileName(snapmail.RowKey))
                 {
                     if (process.Responding)
                     {
                         id = process.Id;
                         break;
                     }
                     else
                     {
                         id = 0;
                         KillProcess(process.Id, snapmail.RowKey);
                         break;
                     }
                 }
             }
         }
         return(id);
     }
     catch (Exception x)
     {
         Console.WriteLine("ProcessRunning: " + snapmail.RowKey);
         return(0);
     }
 }
Пример #12
0
        static void Main(string[] args)
        {
            Evercam.SANDBOX = Settings.EvercamSandboxMode;

            //// for testing
            //args = new string[1];
            //args[0] = "81f3e1dc-85b5-4ca3-bd4a-4636f8e66602";

            if (!string.IsNullOrEmpty(args[0]))
            {
                while ((data = SnapmailDao.Get(args[0])) != null && data.IsActive && !string.IsNullOrEmpty(data.Recipients))
                {
                    if (!string.IsNullOrEmpty(data.AccessToken))
                    {
                        evercam = new Evercam(data.AccessToken);
                        string[] cred = data.AccessToken.Split(':');
                        if (cred.Length >= 2)
                        {
                            evercam = new Evercam(cred[0], cred[1]);
                        }
                        int      hh        = int.Parse(data.NotifyTime.Substring(0, 2));
                        int      mm        = int.Parse(data.NotifyTime.Substring(3, 2));
                        DateTime scheduled = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, hh, mm, 0);
                        scheduled = Utils.ConvertToUtc(scheduled, data.TimeZone, true);

                        DayOfWeek[] days = Utils.GetDaysOfWeek(data.NotifyDays);
                        if (days.Contains <DayOfWeek>(DateTime.UtcNow.DayOfWeek))
                        {
                            if (DateTime.UtcNow >= scheduled && DateTime.UtcNow <= scheduled.AddMinutes(1))
                            {
                                Utils.FileLog("User: "******", Cameras: " + data.Cameras, data.RowKey);

                                List <string> attachments = new List <string>();
                                bool          anyImages   = false;
                                bool          anyErrors   = false;
                                bool          anyDebugs   = false;
                                string        images      = "";
                                string        errors      = "<ul>";
                                string        debugs      = "<ul>";
                                string[]      cc          = data.Cameras.Split(',', ' ');
                                foreach (string c in cc)
                                {
                                    bool   gotImage  = false;
                                    string timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmssfff");
                                    string temppath  = Path.Combine(Settings.TempImagePath, c + timestamp + ".jpg");

                                    Camera camera = new Camera();
                                    byte[] image  = null;
                                    for (int i = 1; i <= Settings.TryCount; i++)
                                    {
                                        try
                                        {
                                            if (camera == null || string.IsNullOrEmpty(camera.ID))
                                            {
                                                camera = evercam.GetCamera(c);
                                            }

                                            // store and returns live snapshot on evercam
                                            image = evercam.CreateSnapshot(c, Settings.EvercamClientName, true).ToBytes();
                                            Utils.FileLog("Image data retrieved (try#" + i + "): " + data.RowKey, data.RowKey);

                                            if (image != null && Storage.SaveFile(temppath, image))
                                            {
                                                attachments.Add(temppath);
                                                temppath = WebUtility.UrlDecode(Path.Combine(Settings.TempImagePath, c + timestamp + ".jpg")).Replace(@"/", @"\\");
                                                temppath = temppath.Replace(Settings.TempImagePath.Replace(@"/", @"\\"), Settings.ServerUrl + @"images/");
                                                images  += "<br /><img src='" + temppath + "' width='100%' /> <br /><br /><strong>" + camera.Name + "</strong> (" + camera.ID + ") - See the live view on Evercam by <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "/live'>clicking here</a><br />";

                                                anyImages = true;
                                                gotImage  = true;

                                                break;
                                            }
                                            else
                                            {
                                                debugs += "<li> <i>Image could not be saved from <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "'>" + c + "</a></i>";
                                                Utils.FileLog("Image could not be saved from Camera " + c, data.RowKey);
                                                anyDebugs = true;
                                            }
                                        }
                                        catch (Exception x)
                                        {
                                            anyDebugs = true;
                                            debugs   += "<li> <i>Image could not be saved from <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "'>" + c + "</a></i>. [Error: " + x.Message + "]";
                                            Utils.FileLog("Error (try#" + i + "): " + data.RowKey + ": " + x.ToString(), data.RowKey);

                                            if (x.Message.Contains("offline"))
                                            {
                                                break;
                                            }

                                            if (i < Settings.TryCount)
                                            {
                                                Thread.Sleep(Settings.RetryInterval);    // 15 seconds
                                            }
                                        }
                                    }

                                    if (!gotImage)
                                    {
                                        // download latest snapshot from evercam
                                        try
                                        {
                                            Snapshot snap = evercam.GetLatestSnapshot(c, true);
                                            image = snap.ToBytes();
                                            DateTime last = EvercamV2.Utility.ToWindowsDateTime(snap.CreatedAt);

                                            // assuming that snapshot timestamp is in camera timezone
                                            DateTime utc = Utils.ConvertToUtc(last, camera.Timezone, true);
                                            if (utc > DateTime.UtcNow.AddMinutes(-5))
                                            {
                                                if (Storage.SaveFile(temppath, image))
                                                {
                                                    attachments.Add(temppath);
                                                    temppath = WebUtility.UrlDecode(Path.Combine(Settings.TempImagePath, c + timestamp + ".jpg")).Replace(@"/", @"\\");
                                                    temppath = temppath.Replace(Settings.TempImagePath.Replace(@"/", @"\\"), Settings.ServerUrl + @"images/");
                                                    images  += "<br /><img src='" + temppath + "' width='100%' /> <br /><br /><strong>" + camera.Name + "</strong> (" + camera.ID + ") - See the live view on Evercam by <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "/live'>clicking here</a><br />";

                                                    anyImages = true;
                                                }
                                                else
                                                {
                                                    errors += "<li> <i>Could not retrieve an image from <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "'>" + c + "</a></i>";
                                                    debugs += "<li> <i>Latest image could not be retrieved from <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "'>" + c + "</a></i>";
                                                    Utils.FileLog("Latest image could not be saved from Camera " + c, data.RowKey);

                                                    anyErrors = anyDebugs = true;
                                                }
                                            }
                                            else
                                            {
                                                attachments.Add(temppath);
                                                temppath = WebUtility.UrlDecode(Path.Combine(Settings.TempImagePath, c + timestamp + ".jpg")).Replace(@"/", @"\\");
                                                temppath = temppath.Replace(Settings.TempImagePath.Replace(@"/", @"\\"), Settings.ServerUrl + @"images/");

                                                errors += "<li> <i>Could not retrieve an image from <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "'>" + c + "</a></i>";
                                                errors += "<br /><i>Here is the last image we received from this camera</i><br /><img src='" + temppath + "' width='50%' /></li>";

                                                debugs += "<li> <i>Latest image could not be retrieved from <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "'>" + c + "</a></i>";
                                                debugs += "<br /><i>Here is the last image we received from this camera @ " + last.ToString() + ":</i><br /><img src='" + temppath + "' width='50%' /></li>";
                                                Utils.FileLog("Latest image is too old from Camera " + c, data.RowKey);

                                                anyErrors = anyDebugs = true;
                                            }
                                        }
                                        catch (Exception x)
                                        {
                                            errors += "<li> <i>Could not retrieve an image from <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "'>" + c + "</a></i>";
                                            debugs += "<li> <i>Latest image could not be retrieved from <a target='_blank' href='https:////dash.evercam.io/v1/cameras/" + c + "'>" + c + "</a>. [Error: " + x.Message + "]</i>";
                                            Utils.FileLog("Latest image could not be retrieved from Camera " + c, data.RowKey);
                                            anyErrors = anyDebugs = true;
                                        }
                                    }
                                }
                                errors += "</ul>";
                                debugs += "</ul>";

                                string message = "";
                                string debug   = "";
                                if (anyImages)
                                {
                                    message = data.Message.Replace("{br}", "<br />").Replace("{snapshots}", images + (!anyErrors ? "" : "<br />But...<br />" + errors)).Replace("{unsubscribe}", "<center style='font-size:11px'>If you want to change your Snapmail settings, <a target='_blank' href='" + Settings.ServerUrl + "?user="******"'>click here</a>.<br />If you would prefer not to receive future emails for this Scheduled SnapMail @ " + data.NotifyTime + ", you may <a target='_blank' href='" + Settings.ServerUrl + "Unsubscribe.html?id=" + data.RowKey + "&email={email}'>unsubscribe here</a>.</center>");
                                    debug   = data.Message.Replace("{br}", "<br />").Replace("{snapshots}", images + (!anyDebugs ? "" : "<br />But...<br />" + debugs)).Replace("{unsubscribe}", "<center style='font-size:11px'>If you want to change your Snapmail settings, <a target='_blank' href='" + Settings.ServerUrl + "?user="******"'>click here</a>.<br />If you would prefer not to receive future emails for this Scheduled SnapMail @ " + data.NotifyTime + ", you may <a target='_blank' href='" + Settings.ServerUrl + "Unsubscribe.html?id=" + data.RowKey + "&email={email}'>unsubscribe here</a>.</center>");
                                }
                                else
                                {
                                    message = data.Message.Replace("{br}", "<br />").Replace("Here's the snapshot(s) from your cameras.", "").Replace("{snapshots}", (!anyErrors ? "" : errors)).Replace("{unsubscribe}", "<center style='font-size:11px'>If you want to change your Snapmail settings, <a target='_blank' href='" + Settings.ServerUrl + "?user="******"'>click here</a>.<br />If you would prefer not to receive future emails for this Scheduled SnapMail @ " + data.NotifyTime + ", you may <a target='_blank' href='" + Settings.ServerUrl + "Unsubscribe.html?id=" + data.RowKey + "&email={email}'>unsubscribe here</a>.</center>");
                                    debug   = data.Message.Replace("{br}", "<br />").Replace("Here's the snapshot(s) from your cameras.", "").Replace("{snapshots}", (!anyDebugs ? "" : debugs)).Replace("{unsubscribe}", "<center style='font-size:11px'>If you want to change your Snapmail settings, <a target='_blank' href='" + Settings.ServerUrl + "?user="******"'>click here</a>.<br />If you would prefer not to receive future emails for this Scheduled SnapMail @ " + data.NotifyTime + ", you may <a target='_blank' href='" + Settings.ServerUrl + "Unsubscribe.html?id=" + data.RowKey + "&email={email}'>unsubscribe here</a>.</center>");
                                }

                                // Finally send email
                                Utils.SendMail(data.Subject.Replace("{notify_time}", data.NotifyTime), message, data.Recipients, attachments);

                                if (!string.IsNullOrEmpty(Settings.DebugEmail) && anyDebugs)
                                {
                                    Utils.SendMail("[DEBUG] " + data.Subject.Replace("{notify_time}", data.NotifyTime), debug, Settings.DebugEmail, attachments);
                                }

                                SnapmailDao.UpdateEmail(data.RowKey, message.Replace("{email}", data.Recipients), DateTime.UtcNow);

                                Utils.FileLog("SendMail: " + message, data.RowKey);
                            }
                            else
                            {
                                Utils.FileLog("Schedule out of time @ UTC " + scheduled.ToString(), data.RowKey);
                            }
                        }
                        else
                        {
                            Utils.FileLog("Schedule out of days @ " + data.NotifyDays, data.RowKey);
                        }
                    }
                    else
                    {
                        Utils.FileLog("Evercam Access Token Not Found @ " + data.RowKey, data.RowKey);
                    }

                    if (!data.IsScheduled)
                    {
                        SnapmailDao.UpdateScheduled(data.RowKey, true);
                    }

                    Thread.Sleep(Settings.CheckInterval);
                }

                if (data != null && !string.IsNullOrEmpty(data.RowKey))
                {
                    Utils.FileLog("Exiting Snapmailer...", data.RowKey);
                }
                else
                {
                    Utils.FileLog("Exiting Snapmailer (no data)...", args[0]);
                }
            }
        }
Пример #13
0
        public SnapmailInfoModel Post(SnapmailModel data)
        {
            SnapmailRowData rowdata = SnapmailModel.ToRowData(data);

            return(SnapmailModel.ToInfoModel(SnapmailDao.Insert(rowdata)));
        }