public async Task EmitThePowerReadingWhenAMessageIsReceivedOnThe( string powerReadingPayloadRegex, IEnumerable <Recorded <Notification <string> > > source, IEnumerable <Recorded <Notification <double> > > expected) { const string topic = "powerReadings"; var scheduler = new TestScheduler(); var config = new PowerFull.Messaging.Config { PowerReadingTopic = topic, PowerReadingPayloadValueRegex = powerReadingPayloadRegex }; (var messages, var mqttClient, var subject) = await CreateSubject(config : config, devices : new[] { DeviceA, DeviceB }); scheduler.CreateColdObservable(source.ToArray()) .Select(payload => new MqttApplicationMessage(topic, payload == null ? null : Encoding.UTF8.GetBytes(payload))) .Subscribe(messages); var lastMessage = source.Select(notification => TimeSpan.FromTicks(notification.Time)).Max(); var actual = scheduler.Start(() => subject.RealPower, lastMessage); actual.Messages.AssertEqual(expected); }
public async Task SubscribeToExpectedTopics() { var config = new PowerFull.Messaging.Config { PowerReadingTopic = "TestPowerReadingTopic" }; (var messages, var mqttClient, var subject) = await CreateSubject(config : config, devices : new[] { DeviceA, DeviceB }); A.CallTo(() => mqttClient.SubscribeAsync("PowerStateRequestTopicA", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); A.CallTo(() => mqttClient.SubscribeAsync("PowerStateResponseTopicA", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); A.CallTo(() => mqttClient.SubscribeAsync("PowerOnRequestTopicA", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); A.CallTo(() => mqttClient.SubscribeAsync("PowerOffRequestTopicA", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); A.CallTo(() => mqttClient.SubscribeAsync("PowerStateRequestTopicB", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); A.CallTo(() => mqttClient.SubscribeAsync("PowerStateResponseTopicB", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); A.CallTo(() => mqttClient.SubscribeAsync("PowerOnRequestTopicB", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); A.CallTo(() => mqttClient.SubscribeAsync("PowerOffRequestTopicB", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); A.CallTo(() => mqttClient.SubscribeAsync("TestPowerReadingTopic", MqttQualityOfService.AtLeastOnce)).MustHaveHappenedOnceExactly(); }
private static async Task <(Subject <MqttApplicationMessage>, IMqttClient, PowerFull.Messaging.Facade.Implementation)> CreateSubject(PowerFull.Messaging.Config config = null, IEnumerable <IDevice> devices = null) { config = config ?? new PowerFull.Messaging.Config(); var messages = new Subject <MqttApplicationMessage>(); var mqttClient = A.Fake <IMqttClient>(); A.CallTo(() => mqttClient.MessageStream).Returns(messages); var mqttFactory = A.Fake <PowerFull.Messaging.Mqtt.IFactory>(); A.CallTo(() => mqttFactory.Create()).Returns(new ValueTask <IMqttClient>(mqttClient)); var logger = A.Fake <ILogger <PowerFull.Messaging.IFacade> >(); var subject = new PowerFull.Messaging.Facade.Implementation(config, mqttFactory, devices, logger); await subject.InitializeAsync(); return(messages, mqttClient, subject); }