Esempio n. 1
0
		private static void MonitorHttp ()
		{
			HttpSocketClient HttpClient = null;
			HttpResponse Response;
			XmlDocument Xml;
			Thread ControlThread;
			string Resource;

			ControlThread = new Thread (ControlHttp);
			ControlThread.Name = "Control HTTP";
			ControlThread.Priority = ThreadPriority.Normal;
			ControlThread.Start ();

			try
			{
				while (executing)
				{
					try
					{
						if (HttpClient == null)
						{
							HttpClient = new HttpSocketClient ("192.168.0.15", 80, new DigestAuthentication ("Peter", "Waher"));
							HttpClient.ReceiveTimeout = 30000;
							HttpClient.Open ();
						}

						if (hasValues)
						{
							int NrLeds = (int)System.Math.Round ((8 * lightPercent) / 100);
							double LightNextStepDown = 100 * (NrLeds - 0.1) / 8;
							double LightNextStepUp = 100 * (NrLeds + 1) / 8;
							double DistDown = System.Math.Abs (lightPercent - LightNextStepDown);
							double DistUp = System.Math.Abs (LightNextStepUp - lightPercent);
							double Dist20 = System.Math.Abs (20 - lightPercent);
							double MinDist = System.Math.Min (System.Math.Min (DistDown, DistUp), Dist20);

							if (MinDist < 1)
								MinDist = 1;

							StringBuilder sb = new StringBuilder ();

							sb.Append ("/event/xml?Light=");
							sb.Append (XmlUtilities.DoubleToString (lightPercent, 1));
							sb.Append ("&LightDiff=");
							sb.Append (XmlUtilities.DoubleToString (MinDist, 1));
							sb.Append ("&Motion=");
							sb.Append (motion ? "1" : "0");
							sb.Append ("&Timeout=25");

							Resource = sb.ToString ();
						} else
							Resource = "/xml?Momentary=1&Light=1&Motion=1";
					
						Response = HttpClient.GET (Resource);

						Xml = Response.Xml;
						if (UpdateFields (Xml))
						{
							hasValues = true;
							CheckControlRules ();
						}

					} catch (Exception ex)
					{
						Log.Exception (ex.Message);

						HttpClient.Dispose ();
						HttpClient = null;
					}
				}
			} finally
			{
				ControlThread.Abort ();
				ControlThread = null;
			}

			if (HttpClient != null)
				HttpClient.Dispose ();
		}
Esempio n. 2
0
        private static void MonitorHttp()
        {
            HttpSocketClient HttpClient = null;
            HttpResponse     Response;
            XmlDocument      Xml;
            Thread           ControlThread;
            string           Resource;

            ControlThread          = new Thread(ControlHttp);
            ControlThread.Name     = "Control HTTP";
            ControlThread.Priority = ThreadPriority.Normal;
            ControlThread.Start();

            try
            {
                while (executing)
                {
                    try
                    {
                        if (HttpClient == null)
                        {
                            HttpClient = new HttpSocketClient("192.168.0.29", 80, new DigestAuthentication("Peter", "Waher"));
                            HttpClient.ReceiveTimeout = 30000;
                            HttpClient.Open();
                        }

                        if (hasValues)
                        {
                            int    NrLeds            = (int)System.Math.Round((8 * lightPercent) / 100);
                            double LightNextStepDown = 100 * (NrLeds - 0.1) / 8;
                            double LightNextStepUp   = 100 * (NrLeds + 1) / 8;
                            double DistDown          = System.Math.Abs(lightPercent - LightNextStepDown);
                            double DistUp            = System.Math.Abs(LightNextStepUp - lightPercent);
                            double Dist20            = System.Math.Abs(20 - lightPercent);
                            double MinDist           = System.Math.Min(System.Math.Min(DistDown, DistUp), Dist20);

                            if (MinDist < 1)
                            {
                                MinDist = 1;
                            }

                            StringBuilder sb = new StringBuilder();

                            sb.Append("/event/xml?Light=");
                            sb.Append(XmlUtilities.DoubleToString(lightPercent, 1));
                            sb.Append("&LightDiff=");
                            sb.Append(XmlUtilities.DoubleToString(MinDist, 1));
                            sb.Append("&Motion=");
                            sb.Append(motion ? "1" : "0");
                            sb.Append("&Timeout=25");

                            Resource = sb.ToString();
                        }
                        else
                        {
                            Resource = "/xml?Momentary=1&Light=1&Motion=1";
                        }

                        Response = HttpClient.GET(Resource);

                        Xml = Response.Xml;
                        if (UpdateFields(Xml))
                        {
                            hasValues = true;
                            CheckControlRules();
                        }
                    } catch (Exception ex)
                    {
                        Log.Exception(ex.Message);

                        HttpClient.Dispose();
                        HttpClient = null;
                    }
                }
            } finally
            {
                ControlThread.Abort();
                ControlThread = null;
            }

            if (HttpClient != null)
            {
                HttpClient.Dispose();
            }
        }