Exemplo n.º 1
0
        private static void HttpGetHistoryGraph(HttpServerResponse resp, HttpServerRequest req)
        {
            networkLed.High();
            try
            {
                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();
            }
        }
Exemplo n.º 2
0
		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 ();
			}
		}
Exemplo n.º 3
0
		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 ();
			}
		}
        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();
            }
        }