예제 #1
0
        private static void SendAlarmMail()
        {
            Log.Information("Preparing alarm mail.");

            MailMessage       Msg = new MailMessage(mailSettings.Recipient, "Motion Detected.", string.Empty, Clayster.Library.Internet.SMTP.MessageType.Html);
            List <WaitHandle> ThreadTerminationEvents = new List <WaitHandle> ();
            StringBuilder     Html = new StringBuilder();
            ManualResetEvent  Done;

            EditableObject[] Cameras = Topology.Source.GetObjects(typeof(Camera), User.AllPrivileges);
            Camera           Camera;
            int i, j, c = Cameras.Length;

            Html.Append("<html><head/><body><h1>Motion detected</h1>");
            Html.Append("<p>Motion has been detected while the light is turned off.</p>");

            if (c > 0)
            {
                Html.Append("<h2>Camera Photos</h2>");
                Html.Append("<table cellspacing='0' cellpadding='10' border='0'>");

                for (i = 0; i < c; i++)
                {
                    Camera = (Camera)Cameras [i];
                    Html.Append("<tr>");

                    if (!string.IsNullOrEmpty(Camera.CameraUrl) && Camera.CameraWidth > 0 && Camera.CameraHeight > 0)
                    {
                        for (j = 1; j <= 3; j++)
                        {
                            Html.Append("<td align='center'><img src='cid:cam");
                            Html.Append((i + 1).ToString());
                            Html.Append("img");
                            Html.Append(j.ToString());
                            Html.Append("' width='");
                            Html.Append(Camera.CameraWidth.ToString());
                            Html.Append("' height='");
                            Html.Append(Camera.CameraHeight.ToString());
                            Html.Append("'/></td>");
                        }

                        Done = new ManualResetEvent(false);
                        ThreadTerminationEvents.Add(Done);

                        Thread T = new Thread(GetPhotos);
                        T.Priority = ThreadPriority.BelowNormal;
                        T.Name     = "GetPhotos#" + (i + 1).ToString();
                        T.Start(new object[] { i, Camera, Msg, Done });
                    }
                    else
                    {
                        Html.Append("<td colspan='3'>Camera not accessible at this time.</td>");
                    }

                    Html.Append("</tr>");
                }
            }

            Html.Append("</table></body></html>");

            if (ThreadTerminationEvents.Count > 0)
            {
                WaitHandle.WaitAll(ThreadTerminationEvents.ToArray(), 30000);
            }

            Msg.Body = Html.ToString();
            SmtpOutbox.SendMail(Msg, mailSettings.From);
        }
        private static void SendAlarmMail()
        {
            MailMessage                 Msg = new MailMessage(mailSettings.Recipient, "Motion Detected.", string.Empty, MessageType.Html);
            List <WaitHandle>           ThreadTerminationEvents = new List <WaitHandle> ();
            Dictionary <string, string> VariableValues;
            StringBuilder               Html = new StringBuilder();
            string           Resolution;
            string           ContentType;
            string           Extension;
            ManualResetEvent Done;

            IUPnPService[] Cameras;
            int            i, j, c;

            lock (stillImageCameras)
            {
                c       = stillImageCameras.Count;
                Cameras = new IUPnPService[c];
                stillImageCameras.Values.CopyTo(Cameras, 0);
            }

            Html.Append("<html><head/><body><h1>Motion detected</h1>");
            Html.Append("<p>Motion has been detected while the light is turned off.</p>");

            if (c > 0)
            {
                Html.Append("<h2>Camera Photos</h2>");
                Html.Append("<table cellspacing='0' cellpadding='10' border='0'>");

                for (i = 0; i < c; i++)
                {
                    lock (stateVariables)
                    {
                        if (!stateVariables.TryGetValue(Cameras [i].Device.UDN, out VariableValues))
                        {
                            VariableValues = null;
                        }
                    }

                    Html.Append("<tr>");

                    if (VariableValues != null &&
                        VariableValues.TryGetValue("DefaultResolution", out Resolution) &&
                        VariableValues.TryGetValue("DefaultEncoding", out ContentType))
                    {
                        Extension = MimeUtilities.GetDefaultFileExtension(ContentType);

                        for (j = 1; j <= 3; j++)
                        {
                            Html.Append("<td align='center'><img src='cid:cam");
                            Html.Append((i + 1).ToString());
                            Html.Append("img");
                            Html.Append(j.ToString());
                            Html.Append(".");
                            Html.Append(Extension);
                            Html.Append("' width='");
                            Html.Append(Resolution.Replace("x", "' height='"));
                            Html.Append("'/></td>");
                        }

                        Done = new ManualResetEvent(false);
                        ThreadTerminationEvents.Add(Done);

                        Thread T = new Thread(GetPhotos);
                        T.Priority = ThreadPriority.BelowNormal;
                        T.Name     = "GetPhotos#" + (i + 1).ToString();
                        T.Start(new object[] { i, Cameras [i], ContentType, Extension, Msg, Done });
                    }
                    else
                    {
                        Html.Append("<td colspan='3'>Camera not accessible at this time.</td>");
                    }

                    Html.Append("</tr>");
                }
            }

            Html.Append("</table></body></html>");

            if (ThreadTerminationEvents.Count > 0)
            {
                WaitHandle.WaitAll(ThreadTerminationEvents.ToArray(), 30000);
            }

            Msg.Body = Html.ToString();
            SmtpOutbox.SendMail(Msg, mailSettings.From);
        }