private static void MonitorCoap () { CoapEndpoint Endpoint = new CoapEndpoint (); //Endpoint.RegisterLineListener (new ConsoleOutLineListenerSink (BinaryFormat.Hexadecimal)); CoapObserver LightObserver = new CoapObserver (Endpoint, true, "192.168.0.15", CoapEndpoint.DefaultCoapPort, "light/txt", string.Empty, 2 * 5); CoapObserver MotionObserver = new CoapObserver (Endpoint, true, "192.168.0.15", CoapEndpoint.DefaultCoapPort, "motion/txt", string.Empty, 10 * 5); bool HasLightValue = false; bool HasMotionValue = false; LightObserver.OnDataReceived += (o, e) => { string s = e.Response as string; double d; if (!string.IsNullOrEmpty (s) && s.EndsWith (" %") && XmlUtilities.TryParseDouble (s.Substring (0, s.Length - 2), out d)) { lightPercent = d; if (!HasLightValue) { HasLightValue = true; if (HasMotionValue) hasValues = true; } CheckControlRules (); } }; MotionObserver.OnDataReceived += (o, e) => { string s = e.Response as string; bool b; if (!string.IsNullOrEmpty (s) && XmlUtilities.TryParseBoolean (s, out b)) { motion = b; if (!HasMotionValue) { HasMotionValue = true; if (HasLightValue) hasValues = true; } CheckControlRules (); } }; try { WaitHandle[] Handles = new WaitHandle[]{ updateLeds, updateAlarm }; while (executing) { try { switch (WaitHandle.WaitAny (Handles, 1000)) { case 0: // Update LEDS int i; lock (synchObject) { i = lastLedMask; } Endpoint.StartPOST (true, "192.168.0.23", CoapEndpoint.DefaultCoapPort, "do/txt", string.Empty, i.ToString (), CoapBlockSize.BlockLimit_64Bytes, null, null); break; case 1: // Update Alarm bool b; lock (synchObject) { b = lastAlarm.Value; } Endpoint.StartPOST (true, "192.168.0.23", CoapEndpoint.DefaultCoapPort, "alarm/txt", string.Empty, b ? "1" : "0", CoapBlockSize.BlockLimit_64Bytes, null, null); if (b) { Thread T = new Thread (SendAlarmMail); T.Priority = ThreadPriority.BelowNormal; T.Name = "SendAlarmMail"; T.Start (); } break; default: // Timeout CheckSubscriptions (30); break; } } catch (ThreadAbortException) { // Don't log. Exception will be automatically re-raised. } catch (Exception ex) { Log.Exception (ex); } } } finally { LightObserver.Dispose (); MotionObserver.Dispose (); Endpoint.Dispose (); } }
private static void MonitorCoap() { CoapEndpoint Endpoint = new CoapEndpoint(); //Endpoint.RegisterLineListener (new ConsoleOutLineListenerSink (BinaryFormat.Hexadecimal)); CoapObserver LightObserver = new CoapObserver(Endpoint, true, "192.168.0.15", CoapEndpoint.DefaultCoapPort, "light/txt", string.Empty, 2 * 5); CoapObserver MotionObserver = new CoapObserver(Endpoint, true, "192.168.0.15", CoapEndpoint.DefaultCoapPort, "motion/txt", string.Empty, 10 * 5); bool HasLightValue = false; bool HasMotionValue = false; LightObserver.OnDataReceived += (o, e) => { string s = e.Response as string; double d; if (!string.IsNullOrEmpty(s) && s.EndsWith(" %") && XmlUtilities.TryParseDouble(s.Substring(0, s.Length - 2), out d)) { lightPercent = d; if (!HasLightValue) { HasLightValue = true; if (HasMotionValue) { hasValues = true; } } CheckControlRules(); } }; MotionObserver.OnDataReceived += (o, e) => { string s = e.Response as string; bool b; if (!string.IsNullOrEmpty(s) && XmlUtilities.TryParseBoolean(s, out b)) { motion = b; if (!HasMotionValue) { HasMotionValue = true; if (HasLightValue) { hasValues = true; } } CheckControlRules(); } }; try { WaitHandle[] Handles = new WaitHandle[] { updateLeds, updateAlarm }; while (executing) { try { switch (WaitHandle.WaitAny(Handles, 1000)) { case 0: // Update LEDS int i; lock (synchObject) { i = lastLedMask; } Endpoint.StartPOST(true, "192.168.0.23", CoapEndpoint.DefaultCoapPort, "do/txt", string.Empty, i.ToString(), CoapBlockSize.BlockLimit_64Bytes, null, null); break; case 1: // Update Alarm bool b; lock (synchObject) { b = lastAlarm.Value; } Endpoint.StartPOST(true, "192.168.0.23", CoapEndpoint.DefaultCoapPort, "alarm/txt", string.Empty, b ? "1" : "0", CoapBlockSize.BlockLimit_64Bytes, null, null); if (b) { Thread T = new Thread(SendAlarmMail); T.Priority = ThreadPriority.BelowNormal; T.Name = "SendAlarmMail"; T.Start(); } break; default: // Timeout CheckSubscriptions(30); break; } } catch (ThreadAbortException) { // Don't log. Exception will be automatically re-raised. } catch (Exception ex) { Log.Exception(ex); } } } finally { LightObserver.Dispose(); MotionObserver.Dispose(); Endpoint.Dispose(); } }