public async Task <IObservable <MqttStringMessage> > CreateMessageStreamAsync(CancellationToken token) { var client = await CreateMqttClient(_deviceConfig.MqttBroker.Host); var messages = new Subject <MqttStringMessage>(); client.UseApplicationMessageReceivedHandler(eventArgs => { var msg = new MqttStringMessage { Topic = eventArgs.ApplicationMessage.Topic, Payload = Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload) }; _log.Debug("Mqtt received {@message}", msg); messages.OnNext(msg); }); token.Register(async() => { await client.StopAsync(); client.Dispose(); }); return(messages); }
public async Task Test_Combine() { var sensorState = new SensorState(); var mqttStringMessage = new MqttStringMessage { Payload = "1", Topic = "/mqtt/on" }; var inputMessages = new[] { mqttStringMessage }; await ExecuteTest(sensorState, inputMessages, list => { var action = list.Single(); Assert.IsInstanceOf <SwitchDevice>(action); var turnOnAction = (SwitchDevice)action; Assert.AreEqual(1, turnOnAction.HueDevice.Id); Assert.AreEqual("testHostName", turnOnAction.HueDevice.BridgeName); Assert.AreEqual(false, turnOnAction.HueDevice.IsGroup); }); }
public static bool MeetTopicAndPayloadFilter(this MqttSwitch sw, MqttStringMessage message) { bool res = sw.Topic == message.Topic; if (res && sw.PayloadFiltersCombinedByOr.Any()) { var payload = JsonConvert.DeserializeObject <Dictionary <string, object> >(message.Payload); foreach (var payloadFilter in sw.PayloadFiltersCombinedByOr) { if (payload.TryGetValue(payloadFilter.Key, out var value) && value.ToString() == payloadFilter.Value) { return(true); } } return(false); } return(res); }