コード例 #1
0
ファイル: Program.cs プロジェクト: wrtcoder/Learning-IoT-MQTT
		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 ();
			}
		}
        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();
            }
        }