public void SensorData_Test_02_Subscribe_MaxInterval()
        {
            this.ConnectClients();
            try
            {
                ManualResetEvent    Done   = new ManualResetEvent(false);
                ManualResetEvent    Error  = new ManualResetEvent(false);
                IEnumerable <Field> Fields = null;

                SensorDataSubscriptionRequest Request = this.sensorClient.Subscribe(this.client2.FullJID, FieldType.All,
                                                                                    Duration.Parse("PT1S"), Duration.Parse("PT5S"), false);
                Request.OnStateChanged   += (sender, NewState) => Console.Out.WriteLine(NewState.ToString());
                Request.OnErrorsReceived += (sender, Errors) => Error.Set();
                Request.OnFieldsReceived += (sender, NewFields) =>
                {
                    Fields = NewFields;
                    Done.Set();
                };

                this.sensorServer.NewMomentaryValues(new QuantityField(ThingReference.Empty, DateTime.Now, "Temperature", this.temp, 1, "C",
                                                                       FieldType.Momentary, FieldQoS.AutomaticReadout));

                Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 10000), "Subscription not performed correctly");

                foreach (Field Field in Fields)
                {
                    Console.Out.WriteLine(Field.ToString());
                }
            }
            finally
            {
                this.DisposeClients();
            }
        }
        public void SensorData_Test_06_Subscribe_MinInterval()
        {
            this.ConnectClients();
            try
            {
                ManualResetEvent    Done   = new ManualResetEvent(false);
                ManualResetEvent    Error  = new ManualResetEvent(false);
                IEnumerable <Field> Fields = null;

                SensorDataSubscriptionRequest Request = this.sensorClient.Subscribe(this.client2.FullJID, FieldType.All,
                                                                                    new FieldSubscriptionRule[]
                {
                    new FieldSubscriptionRule("Temperature", this.temp, 1, null)
                },
                                                                                    Duration.Parse("PT1S"), Duration.Parse("PT5S"), false);
                Request.OnStateChanged   += (sender, NewState) => Console.Out.WriteLine(NewState.ToString());
                Request.OnErrorsReceived += (sender, Errors) => Error.Set();
                Request.OnFieldsReceived += (sender, NewFields) =>
                {
                    Fields = NewFields;
                    Done.Set();
                };

                int      Count = 6;
                DateTime Start = DateTime.Now;

                while (Count > 0)
                {
                    this.temp += 1;
                    this.sensorServer.NewMomentaryValues(new QuantityField(ThingReference.Empty, DateTime.Now, "Temperature", this.temp, 1, "C",
                                                                           FieldType.Momentary, FieldQoS.AutomaticReadout));

                    this.temp -= 1;
                    this.sensorServer.NewMomentaryValues(new QuantityField(ThingReference.Empty, DateTime.Now, "Temperature", this.temp, 1, "C",
                                                                           FieldType.Momentary, FieldQoS.AutomaticReadout));

                    switch (WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 100))
                    {
                    case 0:
                        Done.Reset();
                        Count--;
                        break;

                    case 1:
                        Assert.Fail("Subscription not performed correctly");
                        break;
                    }
                }

                TimeSpan Elapsed = DateTime.Now - Start;
                Assert.IsTrue(Elapsed > new TimeSpan(0, 0, 5));
            }
            finally
            {
                this.DisposeClients();
            }
        }
        private void SubscribeToSensorData()
        {
            RosterItem SensorItem;

            if (!string.IsNullOrEmpty(this.sensorJid) &&
                (SensorItem = this.xmppClient[this.sensorJid]) != null)
            {
                if (SensorItem.HasLastPresence && SensorItem.LastPresence.IsOnline)
                {
                    ThingReference[] Nodes;

                    if (this.sensor.IsEmpty)
                    {
                        Nodes = null;
                    }
                    else
                    {
                        Nodes = new ThingReference[] { this.sensor }
                    };

                    if (this.subscription != null)
                    {
                        this.subscription.Unsubscribe();
                        this.subscription = null;
                    }

                    Console.WriteLine("Subscribing to events. : " + SensorItem.LastPresenceFullJid);

                    this.subscription = this.sensorClient.Subscribe(SensorItem.LastPresenceFullJid,
                                                                    Nodes, FieldType.Momentary, new FieldSubscriptionRule[]
                    {
                        new FieldSubscriptionRule("Humidity", this.humidity, 1),
                        new FieldSubscriptionRule("Pressure", this.pressure, 1),
                    },
                                                                    new Waher.Content.Duration(false, 0, 0, 0, 0, 0, 1),
                                                                    new Waher.Content.Duration(false, 0, 0, 0, 0, 1, 0), true);

                    this.subscription.OnStateChanged   += Subscription_OnStateChanged;
                    this.subscription.OnFieldsReceived += Subscription_OnFieldsReceived;
                    this.subscription.OnErrorsReceived += Subscription_OnErrorsReceived;
                }
                else if (SensorItem.State == SubscriptionState.From || SensorItem.State == SubscriptionState.None)
                {
                    Console.Write("Requesting presence subscription. : " + this.sensorJid);
                    this.xmppClient.RequestPresenceSubscription(this.sensorJid);
                }
            }
        }
Example #4
0
		private void SubscribeToSensorData()
		{
			RosterItem SensorItem;

			if (!string.IsNullOrEmpty(this.sensorJid) &&
				(SensorItem = this.xmppClient[this.sensorJid]) != null)
			{
				if (SensorItem.HasLastPresence && SensorItem.LastPresence.IsOnline)
				{
					ThingReference[] Nodes;

					if (this.sensor.IsEmpty)
						Nodes = null;
					else
						Nodes = new ThingReference[] { this.sensor };

					if (this.subscription != null)
					{
						this.subscription.Unsubscribe();
						this.subscription = null;
					}

					Log.Informational("Subscribing to events.", SensorItem.LastPresenceFullJid);

					this.subscription = this.sensorClient.Subscribe(SensorItem.LastPresenceFullJid,
						Nodes, FieldType.Momentary, new FieldSubscriptionRule[]
						{
							new FieldSubscriptionRule("Light", this.light, 1),
							new FieldSubscriptionRule("Motion", this.motion.HasValue ?
								(double?)(this.motion.Value ? 1 : 0) : null, 1),
						},
						new Waher.Content.Duration(false, 0, 0, 0, 0, 0, 1),
						new Waher.Content.Duration(false, 0, 0, 0, 0, 1, 0), true);

					this.subscription.OnStateChanged += Subscription_OnStateChanged;
					this.subscription.OnFieldsReceived += Subscription_OnFieldsReceived;
					this.subscription.OnErrorsReceived += Subscription_OnErrorsReceived;
				}
				else if (SensorItem.State == SubscriptionState.From || SensorItem.State == SubscriptionState.None)
				{
					Log.Informational("Requesting presence subscription.", this.sensorJid);
					this.xmppClient.RequestPresenceSubscription(this.sensorJid);
				}
			}
		}
Example #5
0
		/// <summary>
		/// Invoked when application execution is being suspended.  Application state is saved
		/// without knowing whether the application will be terminated or resumed with the contents
		/// of memory still intact.
		/// </summary>
		/// <param name="sender">The source of the suspend request.</param>
		/// <param name="e">Details about the suspend request.</param>
		private void OnSuspending(object sender, SuspendingEventArgs e)
		{
			var deferral = e.SuspendingOperation.GetDeferral();

			this.subscription?.Unsubscribe();
			this.subscription = null;

			this.registryClient?.Dispose();
			this.registryClient = null;

			this.chatServer?.Dispose();
			this.chatServer = null;

			this.bobClient?.Dispose();
			this.bobClient = null;

			this.sensorServer?.Dispose();
			this.sensorServer = null;

			this.sensorClient?.Dispose();
			this.sensorClient = null;

			this.controlClient?.Dispose();
			this.controlClient = null;

			this.xmppClient?.Dispose();
			this.xmppClient = null;

			this.secondTimer?.Dispose();
			this.secondTimer = null;

			db?.Stop()?.Wait();
			db?.Flush()?.Wait();

			Log.Terminate();

			deferral.Complete();
		}