Beispiel #1
0
        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);
        }
 private static bool HasCloudEventsContentType(Message message, out string contentType)
 {
     contentType = message.Properties.ContentType?.ToString();
     return(MimeUtilities.IsCloudEventsContentType(contentType));
 }
Beispiel #3
0
 private static bool HasCloudEventsBatchContentType(HttpRequest request) =>
 MimeUtilities.IsCloudEventsBatchContentType(request?.ContentType);
        private static void HttpGetImg(HttpServerResponse resp, HttpServerRequest req, bool Protected)
        {
            networkLed.High();
            try
            {
                if (Protected)
                {
                    string SessionId = req.Header.GetCookie("SessionId");
                    if (!CheckSession(SessionId))
                    {
                        throw new HttpException(HttpStatusCode.ClientError_Forbidden);
                    }
                }

                LinkSpriteJpegColorCamera.ImageSize Resolution;
                string Encoding;
                byte   Compression;
                ushort Size;
                byte[] Data;

                GetImageProperties(req, out Encoding, out Compression, out Resolution);

                lock (cameraLed)
                {
                    try
                    {
                        cameraLed.High();

                        if (Resolution != currentResolution)
                        {
                            try
                            {
                                camera.SetImageSize(Resolution);
                                currentResolution = Resolution;
                                camera.Reset();
                            } catch (Exception)
                            {
                                camera.Dispose();
                                camera = new LinkSpriteJpegColorCamera(LinkSpriteJpegColorCamera.BaudRate.Baud__38400);
                                camera.SetBaudRate(LinkSpriteJpegColorCamera.BaudRate.Baud_115200);
                                camera.Dispose();
                                camera = new LinkSpriteJpegColorCamera(LinkSpriteJpegColorCamera.BaudRate.Baud_115200);
                            }
                        }

                        if (Compression != currentCompressionRatio)
                        {
                            camera.SetCompressionRatio(Compression);
                            currentCompressionRatio = Compression;
                        }

                        camera.TakePicture();
                        Size = camera.GetJpegFileSize();
                        Data = camera.ReadJpegData(Size);

                        errorLed.Low();
                    } catch (Exception ex)
                    {
                        errorLed.High();
                        Log.Exception(ex);
                        throw new HttpException(HttpStatusCode.ServerError_ServiceUnavailable);
                    } finally
                    {
                        cameraLed.Low();
                        camera.StopTakingPictures();
                    }
                }

                resp.ContentType = Encoding;
                resp.Expires     = DateTime.Now;
                resp.ReturnCode  = HttpStatusCode.Successful_OK;

                if (Encoding != "imgage/jpeg")
                {
                    MemoryStream ms  = new MemoryStream(Data);
                    Bitmap       Bmp = new Bitmap(ms);
                    Data = MimeUtilities.EncodeSpecificType(Bmp, Encoding);
                }

                resp.WriteBinary(Data);
            } finally
            {
                networkLed.Low();
            }
        }
 private static bool HasCloudEventsContentType(HttpListenerRequest request) =>
 MimeUtilities.IsCloudEventsContentType(request.ContentType);
 public void IsCloudEventsContentType(string contentType, bool expectedResult) =>
 Assert.Equal(expectedResult, MimeUtilities.IsCloudEventsContentType(contentType));
        public void CreateContentTypeOrNull_WithContentType(string text)
        {
            ContentType ct = MimeUtilities.CreateContentTypeOrNull(text);

            Assert.Equal(text, ct?.ToString());
        }
 public void ContentTypeConversions_Null()
 {
     Assert.Null(MimeUtilities.ToMediaTypeHeaderValue(default(ContentType)));
     Assert.Null(MimeUtilities.ToContentType(default(MediaTypeHeaderValue)));
 }
Beispiel #9
0
 private static bool HasCloudEventsBatchContentType(HttpContent content) =>
 MimeUtilities.IsCloudEventsBatchContentType(content?.Headers?.ContentType?.MediaType);
        private static void HttpGetHistoryGraph(HttpServerResponse resp, HttpServerRequest req)
        {
            networkLed.High();
            try
            {
                string SessionId = req.Header.GetCookie("SessionId");
                if (!CheckSession(SessionId))
                {
                    throw new HttpException(HttpStatusCode.ClientError_Forbidden);
                }

                string ValueAxis;
                string ParameterName;
                string s;
                int    Width, Height;

                if (!req.Query.TryGetValue("w", out s) || !int.TryParse(s, out Width) || Width <= 0 || Width > 2048)
                {
                    throw new HttpException(HttpStatusCode.ClientError_BadRequest);
                }

                if (!req.Query.TryGetValue("h", out s) || !int.TryParse(s, out Height) || Height <= 0 || Height > 2048)
                {
                    throw new HttpException(HttpStatusCode.ClientError_BadRequest);
                }

                if (!req.Query.TryGetValue("p", out s))
                {
                    throw new HttpException(HttpStatusCode.ClientError_BadRequest);
                }

                switch (s)
                {
                case "temp":
                    ParameterName = "TemperatureC";
                    ValueAxis     = "Temperature (C)";
                    break;

                case "light":
                    ParameterName = "LightPercent";
                    ValueAxis     = "Light (%)";
                    break;

                case "motion":
                    ParameterName = "Motion";
                    ValueAxis     = "Motion";
                    break;

                default:
                    throw new HttpException(HttpStatusCode.ClientError_BadRequest);
                }

                if (!req.Query.TryGetValue("base", out s))
                {
                    throw new HttpException(HttpStatusCode.ClientError_BadRequest);
                }

                Variables v   = new Variables();
                DateTime  Now = DateTime.Now;

                lock (synchObject)
                {
                    switch (s)
                    {
                    case "sec":
                        v ["Records"] = perSecond.ToArray();
                        resp.Expires  = Now;
                        break;

                    case "min":
                        v ["Records"] = perMinute.ToArray();
                        resp.Expires  = new DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, 0).AddMinutes(1);
                        break;

                    case "h":
                        v ["Records"] = perHour.ToArray();
                        resp.Expires  = new DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, 0, 0).AddHours(1);
                        break;

                    case "day":
                        v ["Records"] = perDay.ToArray();
                        resp.Expires  = new DateTime(Now.Year, Now.Month, Now.Day, 0, 0, 0).AddDays(1);
                        break;

                    case "month":
                        v ["Records"] = perMonth.ToArray();
                        resp.Expires  = new DateTime(Now.Year, Now.Month, 1, 0, 0, 0).AddMonths(1);
                        break;

                    default:
                        throw new HttpException(HttpStatusCode.ClientError_BadRequest);
                    }
                }
                Graph Result;

                if (ParameterName == "Motion")
                {
                    Result = Expression.ParseCached("scatter2d(Records.Timestamp,if (Values:=Records.Motion) then 1 else 0,5,if Values then 'Red' else 'Blue','','Motion')").Evaluate(v) as Graph;
                }
                else
                {
                    Result = Expression.ParseCached("line2d(Records.Timestamp,Records." + ParameterName + ",'','" + ValueAxis + "')").Evaluate(v) as Graph;
                }

                Image  Img  = Result.GetImage(Width, Height);
                byte[] Data = MimeUtilities.Encode(Img, out s);

                resp.ContentType = s;
                resp.ReturnCode  = HttpStatusCode.Successful_OK;

                resp.WriteBinary(Data);
            } finally
            {
                networkLed.Low();
            }
        }