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);
        }
예제 #2
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);
        }
        public static void Main(string[] args)
        {
            Log.Register(new ConsoleOutEventLog(80));
            Log.Information("Initializing application...");

            HttpSocketClient.RegisterHttpProxyUse(false, false);                // Don't look for proxies.

            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) =>
            {
                e.Cancel  = true;
                executing = false;
            };

            // Object database setup

            DB.BackupConnectionString = "Data Source=controller.db;Version=3;";
            DB.BackupProviderName     = "Clayster.Library.Data.Providers.SQLiteServer.SQLiteServerProvider";
            db = DB.GetDatabaseProxy("TheController");

            // Mail setup

            mailSettings = MailSettings.LoadSettings();
            if (mailSettings == null)
            {
                mailSettings           = new MailSettings();
                mailSettings.Host      = "Enter mailserver SMTP host name here.";
                mailSettings.Port      = 25;
                mailSettings.Ssl       = false;
                mailSettings.From      = "Enter address of sender here.";
                mailSettings.User      = "******";
                mailSettings.Password  = "******";
                mailSettings.Recipient = "Enter recipient of alarm mails here.";
                mailSettings.SaveNew();
            }

            SmtpOutbox.Host       = mailSettings.Host;
            SmtpOutbox.Port       = mailSettings.Port;
            SmtpOutbox.Ssl        = mailSettings.Ssl;
            SmtpOutbox.From       = mailSettings.From;
            SmtpOutbox.User       = mailSettings.User;
            SmtpOutbox.Password   = mailSettings.Password;
            SmtpOutbox.OutboxPath = "MailOutbox";
            SmtpOutbox.Start(Directory.GetCurrentDirectory());

            // UPnP Interface

            upnpServer = new HttpServer(8080, 10, true, true, 1);
            Log.Information("UPnP Server receiving requests on port " + upnpServer.Port.ToString());

            ssdpClient        = new SsdpClient(upnpServer, 10, true, true, false, false, false, 30);
            stillImageCameras = new Dictionary <string, IUPnPService> ();
            subscriptions     = new SortedDictionary <DateTime, Subscription> ();
            stateVariables    = new Dictionary <string, Dictionary <string, string> > ();
            events            = new UPnPEvents("/events");
            upnpServer.Register(events);

            ssdpClient.OnUpdated    += NetworkUpdated;
            events.OnEventsReceived += EventsReceived;

            // Main loop

            Log.Information("Initialization complete. Application started...");

            try
            {
                                #if USE_HTTP
                MonitorHttp();
                                #elif USE_COAP
                MonitorCoap();
                                #endif
            } catch (Exception ex)
            {
                Log.Exception(ex);
            } finally
            {
                Log.Information("Terminating application.");
                Log.Flush();
                Log.Terminate();
                SmtpOutbox.Terminate();

                ssdpClient.Dispose();
                upnpServer.Dispose();
            }
        }