public static byte[] ConvertToMqtt(MqttSession session, EventMessage message) { if (message.Protocol == ProtocolType.MQTT) { return(MqttConversion(session, message.Message)); } if (message.Protocol == ProtocolType.COAP) { CoapMessage msg = CoapMessage.DecodeMessage(message.Message); CoapUri curi = new CoapUri(msg.ResourceUri.ToString()); QualityOfServiceLevelType qos = QualityOfServiceLevelType.AtLeastOnce; QualityOfServiceLevelType?qosType = session.GetQoS(curi.Resource); qos = qosType ?? QualityOfServiceLevelType.AtLeastOnce; PublishMessage pub = new PublishMessage(false, qos, false, session.NewId(), curi.Resource, msg.Payload); return(pub.Encode()); } if (message.Protocol == ProtocolType.REST) { PublishMessage pubm = new PublishMessage(false, session.GetQoS(message.ResourceUri).Value, false, session.NewId(), message.ResourceUri, message.Message); return(pubm.Encode()); } return(MqttConversion(session, message.Message)); }
public static byte[] ConvertToMqtt(MqttSession session, EventMessage message) { if (message.Protocol == ProtocolType.MQTT) { return(MqttConversion(session, message.Message)); } else if (message.Protocol == ProtocolType.COAP) { CoapMessage msg = CoapMessage.DecodeMessage(message.Message); CoapUri curi = new CoapUri(msg.ResourceUri.ToString()); QualityOfServiceLevelType qos = QualityOfServiceLevelType.AtLeastOnce; try { QualityOfServiceLevelType?qosType = session.GetQoS(curi.Resource); qos = qosType.HasValue ? qosType.Value : QualityOfServiceLevelType.AtLeastOnce; } catch (Exception ex) { Trace.TraceWarning("{0} - Fault in ProtocolTransition.ConvertToMqtt", DateTime.UtcNow.ToString()); Trace.TraceError("{0} - {1} - {2}", DateTime.UtcNow.ToString(""), "ProtocolTransition", ex.Message); } PublishMessage pub = new PublishMessage(false, qos, false, session.NewId(), curi.Resource, msg.Payload); return(pub.Encode()); } else if (message.Protocol == ProtocolType.REST) { PublishMessage pubm = new PublishMessage(false, session.GetQoS(message.ResourceUri).Value, false, session.NewId(), message.ResourceUri, message.Message); return(pubm.Encode()); } else { return(MqttConversion(session, message.Message, message.ContentType)); } }
private static byte[] MqttConversion(MqttSession session, byte[] message, string contentType = null) { PublishMessage msg = MqttMessage.DecodeMessage(message) as PublishMessage; MqttUri uri = new MqttUri(msg.Topic); QualityOfServiceLevelType?qos = session.GetQoS(uri.Resource); PublishMessage pm = new PublishMessage(false, qos.HasValue ? qos.Value : QualityOfServiceLevelType.AtMostOnce, false, session.NewId(), uri.Resource, msg.Payload); if (pm.QualityOfService != QualityOfServiceLevelType.AtMostOnce) { session.Quarantine(pm, SkunkLab.Protocols.Mqtt.Handlers.DirectionType.Out); } return(pm.Encode()); }
private static byte[] MqttConversion(MqttSession session, byte[] message) { PublishMessage msg = MqttMessage.DecodeMessage(message) as PublishMessage; MqttUri uri = new MqttUri(msg.Topic); QualityOfServiceLevelType?qos = session.GetQoS(uri.Resource); PublishMessage pm = new PublishMessage(false, qos ?? QualityOfServiceLevelType.AtMostOnce, false, session.NewId(), uri.Resource, msg.Payload); if (pm.QualityOfService != QualityOfServiceLevelType.AtMostOnce) { session.Quarantine(pm, DirectionType.Out); } return(pm.Encode()); }