internal override string RegisterInHostThing() { string error = null; string propertyName = DisplayName; TheThing targetThing = null; if (this.IsSubscribedAsThing) { // Dedicated event thing targetThing = MyBaseThing; } if (this.IsSubscribedAsProperty) { // This is an external, multi-event thing: use the browsepath to avoid collisions with properties from multiple events targetThing = this.GetHostThing().GetBaseThing(); } if (targetThing != null) { var prop = targetThing.GetProperty(propertyName, true); var sensorMeta = prop.GetSensorMeta(); prop = targetThing.DeclareSensorProperty(propertyName, ePropertyTypes.NOCHANGE, sensorMeta); if (prop != null) { var opcSubscriptionInfo = new TheOPCSensorSubscription { SampleRate = this.SampleRate, SensorId = this.NodeIdName, ChangeTrigger = this.ChangeTrigger, DeadbandValue = this.DeadbandFilterValue, EventInfo = this.EventInfo, }; var providerInfo = new cdeP.TheProviderInfo(this.MyOPCServer.GetBaseThing(), opcSubscriptionInfo); prop.SetSensorProviderInfo(providerInfo); } else { error = "ERROR: Property not found"; } } else { error = "ERROR: Thing not found"; } return(error); }
public void TestSensorConsumerModel() { var deviceTypes = TheThingRegistry.GetAllDeviceTypesByCap(nsCDEngine.ViewModels.eThingCaps.SensorConsumer, true); Assert.Greater(deviceTypes.Count, 0, "No sensor consumer device types found"); var sensorThing = new TheThing { EngineName = "CDMyVThings.TheVThings", DeviceType = "Memory Tag", ID = "TestSensorThing01" }; TheThingRegistry.RegisterThing(sensorThing); sensorThing.DeclareSensorProperty("Sensor01_1", ePropertyTypes.TNumber, new cdeP.TheSensorMeta { }).Value = 12345.67; sensorThing.DeclareSensorProperty("Sensor01_2", ePropertyTypes.TString, new cdeP.TheSensorMeta { }).Value = "Hello World!"; sensorThing.DeclareSensorProperty("Sensor01_3", ePropertyTypes.TDate, new cdeP.TheSensorMeta { }).Value = DateTimeOffset.Now; var sensorThing2 = new TheThing { EngineName = "CDMyVThings.TheVThings", DeviceType = "Memory Tag", ID = "TestSensorThing02" }; TheThingRegistry.RegisterThing(sensorThing); sensorThing2.DeclareSensorProperty("Sensor02_1", ePropertyTypes.TNumber, new cdeP.TheSensorMeta { }).Value = 12345.67; sensorThing2.DeclareSensorProperty("Sensor02_2", ePropertyTypes.TString, new cdeP.TheSensorMeta { }).Value = "Hello World!"; sensorThing2.DeclareSensorProperty("Sensor02_3", ePropertyTypes.TDate, new cdeP.TheSensorMeta { }).Value = DateTimeOffset.Now; foreach (var deviceType in deviceTypes) { TestInfo testInfo; var consumer = CreateThingForTest(deviceType, out testInfo); WriteLine($"{deviceType}: Consumer created"); var thingSubscriptions = new List <TheThing.TheThingSubscription> { new TheThing.TheThingSubscription { ThingReference = sensorThing, }, new TheThing.TheThingSubscription { ThingReference = sensorThing2, }, }; { var subscribeToThingsResponse = consumer.SubscribeToThingsAsync(thingSubscriptions).Result; Assert.NotNull(subscribeToThingsResponse, $"Timeout or subscribe to things message not implemented by consumer {deviceType}"); Assert.IsTrue(string.IsNullOrEmpty(subscribeToThingsResponse.Error), $"Error from consumer {deviceType}: {subscribeToThingsResponse.Error}"); Assert.GreaterOrEqual(thingSubscriptions.Count, subscribeToThingsResponse.SubscriptionStatus.Count, $"Not enough status reports for {deviceType}"); foreach (var subStatus in subscribeToThingsResponse.SubscriptionStatus) { Assert.IsTrue(string.IsNullOrEmpty(subStatus.Error), $"Error from consumer {deviceType}: {subStatus.Error}"); Assert.IsTrue(subStatus.Subscription.SubscriptionId.HasValue && subStatus.Subscription.SubscriptionId != Guid.Empty, $"No subscriptionid from consumer {deviceType}: {subStatus.Subscription.SubscriptionId}"); } } // TODO verify that subscriptions are actually getting consumed (at least for some consumer plug-ins) { var getSubscriptionsResponse = consumer.GetThingSubscriptionsAsync().Result; Assert.NotNull(getSubscriptionsResponse, $"Timeout or get things message not implemented by consumer {deviceType}"); Assert.IsTrue(string.IsNullOrEmpty(getSubscriptionsResponse.Error), $"Error from consumer {deviceType}: {getSubscriptionsResponse.Error}"); Assert.AreEqual(thingSubscriptions.Count, getSubscriptionsResponse.ThingSubscriptions.Count, $"Missing subscriptions for {deviceType}"); foreach (var sub in getSubscriptionsResponse.ThingSubscriptions) { Assert.IsTrue(sub.SubscriptionId.HasValue && sub.SubscriptionId != Guid.Empty, $"No subscriptionid from consumer {deviceType}: {sub.SubscriptionId}"); } var unsubscribeResponse = consumer.UnsubscribeFromThingsAsync(getSubscriptionsResponse.ThingSubscriptions.Select(sub => sub.SubscriptionId ?? Guid.Empty).Take(1)).Result; Assert.NotNull(unsubscribeResponse, $"Timeout or unsubscribe things message not implemented by consumer {deviceType}"); Assert.IsTrue(string.IsNullOrEmpty(unsubscribeResponse.Error), $"Error from consumer {deviceType}: {unsubscribeResponse.Error}"); Assert.IsTrue(unsubscribeResponse.Failed != null && unsubscribeResponse.Failed.Count == 0, $"Errors during unsubscribe from consumer {deviceType}: {unsubscribeResponse.Failed.Aggregate("", (s, us) => $"{s} {us.Subscription.SubscriptionId}:{us.Error}")}"); } { var getSubscriptionsResponse = consumer.GetThingSubscriptionsAsync().Result; Assert.NotNull(getSubscriptionsResponse, $"Timeout or get things message not implemented by consumer {deviceType}"); Assert.IsTrue(string.IsNullOrEmpty(getSubscriptionsResponse.Error), $"Error from consumer {deviceType}: {getSubscriptionsResponse.Error}"); Assert.AreEqual(thingSubscriptions.Count - 1, getSubscriptionsResponse.ThingSubscriptions.Count, $"Missing subscriptions for {deviceType}"); foreach (var sub in getSubscriptionsResponse.ThingSubscriptions) { Assert.IsTrue(sub.SubscriptionId.HasValue && sub.SubscriptionId != Guid.Empty, $"No subscriptionid from consumer {deviceType}: {sub.SubscriptionId}"); } var unsubscribeResponse = consumer.UnsubscribeFromThingsAsync(getSubscriptionsResponse.ThingSubscriptions.Select(sub => sub.SubscriptionId ?? Guid.Empty)).Result; Assert.NotNull(unsubscribeResponse, $"Timeout or unsubscribe things message not implemented by consumer {deviceType}"); Assert.IsTrue(string.IsNullOrEmpty(unsubscribeResponse.Error), $"Error from consumer {deviceType}: {unsubscribeResponse.Error}"); Assert.IsTrue(unsubscribeResponse.Failed != null && unsubscribeResponse.Failed.Count == 0, $"Errors during unsubscribe from consumer {deviceType}: {unsubscribeResponse.Failed.Aggregate("", (s, us) => $"{s} {us.Subscription.SubscriptionId}:{us.Error}")}"); } { var getSubscriptionsResponse = consumer.GetThingSubscriptionsAsync().Result; Assert.NotNull(getSubscriptionsResponse, $"Timeout or get things message not implemented by consumer {deviceType}"); Assert.IsTrue(string.IsNullOrEmpty(getSubscriptionsResponse.Error), $"Error from consumer {deviceType}: {getSubscriptionsResponse.Error}"); Assert.AreEqual(0, getSubscriptionsResponse.ThingSubscriptions.Count, $"Leaked subscriptions for {deviceType}"); } if (consumer.DeviceType != eKnownDeviceTypes.IBaseEngine) { TheThingRegistry.DeleteThing(consumer); } } TheThingRegistry.DeleteThing(sensorThing); TheThingRegistry.DeleteThing(sensorThing2); }