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 (); } }
public static void Main (string[] args) { Log.Register (new ConsoleOutEventLog (80)); Log.Information ("Initializing application..."); HttpSocketClient.RegisterHttpProxyUse (false, false); // Don't look for proxies. DB.BackupConnectionString = "Data Source=camera.db;Version=3;"; DB.BackupProviderName = "Clayster.Library.Data.Providers.SQLiteServer.SQLiteServerProvider"; db = DB.GetDatabaseProxy ("TheCamera"); defaultSettings = DefaultSettings.LoadSettings (); if (defaultSettings == null) { defaultSettings = new DefaultSettings (); defaultSettings.SaveNew (); } Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => { e.Cancel = true; executionLed.Low (); }; try { // HTTP Interface httpServer = new HttpServer (80, 10, true, true, 1); Log.Information ("HTTP Server receiving requests on port " + httpServer.Port.ToString ()); httpServer.RegisterAuthenticationMethod (new DigestAuthentication ("The Camera Realm", GetDigestUserPasswordHash)); httpServer.RegisterAuthenticationMethod (new SessionAuthentication ()); credentials = LoginCredentials.LoadCredentials (); if (credentials == null) { credentials = new LoginCredentials (); credentials.UserName = "******"; credentials.PasswordHash = CalcHash ("Admin", "Password"); credentials.SaveNew (); } httpServer.Register ("/", HttpGetRootProtected, HttpPostRoot, false); // Synchronous, no authentication httpServer.Register ("/html", HttpGetHtmlProtected, false); // Synchronous, no authentication httpServer.Register ("/camera", HttpGetImgProtected, true); // Synchronous, www-authentication httpServer.Register ("/credentials", HttpGetCredentials, HttpPostCredentials, false); // Synchronous, no authentication // UPnP Interface upnpServer = new HttpServer (8080, 10, true, true, 1); Log.Information ("UPnP Server receiving requests on port " + upnpServer.Port.ToString ()); upnpServer.Register ("/", HttpGetRootUnprotected, HttpPostRoot, false); // Synchronous, no authentication upnpServer.Register ("/html", HttpGetHtmlUnprotected, false); // Synchronous, no authentication upnpServer.Register ("/camera", HttpGetImgUnprotected, false); // Synchronous, no authentication upnpServer.Register ("/CameraDevice.xml", HttpGetCameraDevice, false); upnpServer.Register (new HttpServerEmbeddedResource ("/StillImageService.xml", "Camera.UPnP.StillImageService.xml")); upnpServer.Register (stillImageWS = new DigitalSecurityCameraStillImage ()); // Icons taken from: http://www.iconarchive.com/show/the-bourne-ultimatum-icons-by-leoyue/Camera-icon.html upnpServer.Register (new HttpServerEmbeddedResource ("/Icon/16x16.png", "Camera.UPnP.16x16.png")); upnpServer.Register (new HttpServerEmbeddedResource ("/Icon/24x24.png", "Camera.UPnP.24x24.png")); upnpServer.Register (new HttpServerEmbeddedResource ("/Icon/32x32.png", "Camera.UPnP.32x32.png")); upnpServer.Register (new HttpServerEmbeddedResource ("/Icon/48x48.png", "Camera.UPnP.48x48.png")); upnpServer.Register (new HttpServerEmbeddedResource ("/Icon/64x64.png", "Camera.UPnP.64x64.png")); upnpServer.Register (new HttpServerEmbeddedResource ("/Icon/128x128.png", "Camera.UPnP.128x128.png")); upnpServer.Register (new HttpServerEmbeddedResource ("/Icon/256x256.png", "Camera.UPnP.256x256.png")); ssdpClient = new SsdpClient (upnpServer, 10, true, true, false, false, false, 30); ssdpClient.OnNotify += OnSsdpNotify; ssdpClient.OnDiscovery += OnSsdpDiscovery; // Initializing camera Log.Information ("Initializing camera."); try { currentResolution = defaultSettings.Resolution; currentCompressionRatio = defaultSettings.CompressionLevel; try { camera.Reset (); // First try @ 38400 baud camera.SetImageSize (currentResolution); camera.Reset (); camera.SetBaudRate (LinkSpriteJpegColorCamera.BaudRate.Baud_115200); camera.Dispose (); camera = new LinkSpriteJpegColorCamera (LinkSpriteJpegColorCamera.BaudRate.Baud_115200); } catch (Exception) // If already at 115200 baud. { camera.Dispose (); camera = new LinkSpriteJpegColorCamera (LinkSpriteJpegColorCamera.BaudRate.Baud_115200); } finally { camera.SetCompressionRatio (currentCompressionRatio); } } catch (Exception ex) { Log.Exception (ex); errorLed.High (); camera = null; } // Main loop Log.Information ("Initialization complete. Application started."); while (executionLed.Value) { Thread.Sleep (1000); RemoveOldSessions (); } } catch (Exception ex) { Log.Exception (ex); executionLed.Low (); } finally { Log.Information ("Terminating application."); Log.Flush (); Log.Terminate (); ssdpClient.Dispose (); if (upnpServer != null) upnpServer.Dispose (); if (httpServer != null) httpServer.Dispose (); executionLed.Dispose (); cameraLed.Dispose (); networkLed.Dispose (); errorLed.Dispose (); camera.Dispose (); } }
private static void GetImageProperties(HttpServerRequest req, out string Encoding, out byte Compression, out LinkSpriteJpegColorCamera.ImageSize Resolution) { string s; if (req.Query.TryGetValue("Encoding", out Encoding)) { if (Encoding != "image/jpeg" && Encoding != "image/png" && Encoding != "image/bmp") { throw new HttpException(HttpStatusCode.ClientError_BadRequest); } } else { Encoding = defaultSettings.ImageEncoding; } if (req.Query.TryGetValue("Compression", out s)) { if (!byte.TryParse(s, out Compression)) { throw new HttpException(HttpStatusCode.ClientError_BadRequest); } } else { Compression = defaultSettings.CompressionLevel; } if (req.Query.TryGetValue("Resolution", out s)) { if (!Enum.TryParse <LinkSpriteJpegColorCamera.ImageSize> ("_" + s, out Resolution)) { throw new HttpException(HttpStatusCode.ClientError_BadRequest); } } else { Resolution = defaultSettings.Resolution; } }
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(); } }
public static void Main(string[] args) { Log.Register(new ConsoleOutEventLog(80)); Log.Information("Initializing application..."); HttpSocketClient.RegisterHttpProxyUse(false, false); // Don't look for proxies. DB.BackupConnectionString = "Data Source=camera.db;Version=3;"; DB.BackupProviderName = "Clayster.Library.Data.Providers.SQLiteServer.SQLiteServerProvider"; db = DB.GetDatabaseProxy("TheCamera"); defaultSettings = DefaultSettings.LoadSettings(); if (defaultSettings == null) { defaultSettings = new DefaultSettings(); defaultSettings.SaveNew(); } Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => { e.Cancel = true; executionLed.Low(); }; try { // HTTP Interface httpServer = new HttpServer(80, 10, true, true, 1); Log.Information("HTTP Server receiving requests on port " + httpServer.Port.ToString()); httpServer.RegisterAuthenticationMethod(new DigestAuthentication("The Camera Realm", GetDigestUserPasswordHash)); httpServer.RegisterAuthenticationMethod(new SessionAuthentication()); credentials = LoginCredentials.LoadCredentials(); if (credentials == null) { credentials = new LoginCredentials(); credentials.UserName = "******"; credentials.PasswordHash = CalcHash("Admin", "Password"); credentials.SaveNew(); } httpServer.Register("/", HttpGetRootProtected, HttpPostRoot, false); // Synchronous, no authentication httpServer.Register("/html", HttpGetHtmlProtected, false); // Synchronous, no authentication httpServer.Register("/camera", HttpGetImgProtected, true); // Synchronous, www-authentication httpServer.Register("/credentials", HttpGetCredentials, HttpPostCredentials, false); // Synchronous, no authentication // UPnP Interface upnpServer = new HttpServer(8080, 10, true, true, 1); Log.Information("UPnP Server receiving requests on port " + upnpServer.Port.ToString()); upnpServer.Register("/", HttpGetRootUnprotected, HttpPostRoot, false); // Synchronous, no authentication upnpServer.Register("/html", HttpGetHtmlUnprotected, false); // Synchronous, no authentication upnpServer.Register("/camera", HttpGetImgUnprotected, false); // Synchronous, no authentication upnpServer.Register("/CameraDevice.xml", HttpGetCameraDevice, false); upnpServer.Register(new HttpServerEmbeddedResource("/StillImageService.xml", "Camera.UPnP.StillImageService.xml")); upnpServer.Register(stillImageWS = new DigitalSecurityCameraStillImage()); // Icons taken from: http://www.iconarchive.com/show/the-bourne-ultimatum-icons-by-leoyue/Camera-icon.html upnpServer.Register(new HttpServerEmbeddedResource("/Icon/16x16.png", "Camera.UPnP.16x16.png")); upnpServer.Register(new HttpServerEmbeddedResource("/Icon/24x24.png", "Camera.UPnP.24x24.png")); upnpServer.Register(new HttpServerEmbeddedResource("/Icon/32x32.png", "Camera.UPnP.32x32.png")); upnpServer.Register(new HttpServerEmbeddedResource("/Icon/48x48.png", "Camera.UPnP.48x48.png")); upnpServer.Register(new HttpServerEmbeddedResource("/Icon/64x64.png", "Camera.UPnP.64x64.png")); upnpServer.Register(new HttpServerEmbeddedResource("/Icon/128x128.png", "Camera.UPnP.128x128.png")); upnpServer.Register(new HttpServerEmbeddedResource("/Icon/256x256.png", "Camera.UPnP.256x256.png")); ssdpClient = new SsdpClient(upnpServer, 10, true, true, false, false, false, 30); ssdpClient.OnNotify += OnSsdpNotify; ssdpClient.OnDiscovery += OnSsdpDiscovery; // Initializing camera Log.Information("Initializing camera."); try { currentResolution = defaultSettings.Resolution; currentCompressionRatio = defaultSettings.CompressionLevel; try { camera.Reset(); // First try @ 38400 baud camera.SetImageSize(currentResolution); camera.Reset(); camera.SetBaudRate(LinkSpriteJpegColorCamera.BaudRate.Baud_115200); camera.Dispose(); camera = new LinkSpriteJpegColorCamera(LinkSpriteJpegColorCamera.BaudRate.Baud_115200); } catch (Exception) // If already at 115200 baud. { camera.Dispose(); camera = new LinkSpriteJpegColorCamera(LinkSpriteJpegColorCamera.BaudRate.Baud_115200); } finally { camera.SetCompressionRatio(currentCompressionRatio); } } catch (Exception ex) { Log.Exception(ex); errorLed.High(); camera = null; } // Main loop Log.Information("Initialization complete. Application started."); while (executionLed.Value) { Thread.Sleep(1000); RemoveOldSessions(); } } catch (Exception ex) { Log.Exception(ex); executionLed.Low(); } finally { Log.Information("Terminating application."); Log.Flush(); Log.Terminate(); ssdpClient.Dispose(); if (upnpServer != null) { upnpServer.Dispose(); } if (httpServer != null) { httpServer.Dispose(); } executionLed.Dispose(); cameraLed.Dispose(); networkLed.Dispose(); errorLed.Dispose(); camera.Dispose(); } }