private async Task SendWorker(CancellationToken cancellationToken) { INetworkPacket networkPacket = null; while (!cancellationToken.IsCancellationRequested) { try { if (!_queue.TryDequeue(out networkPacket)) { //await Task.Delay(100); continue; } var correlationId = Guid.NewGuid(); switch (networkPacket) { case PushDataPacket pushDataPacket: { var data = _payloadSerializer.Serialize(pushDataPacket.Payload); var message = new Microsoft.Azure.Devices.Client.Message(data); message.CorrelationId = correlationId.ToString(); message.Properties["EventId"] = correlationId.ToString(); message.Properties["CorrelationId"] = correlationId.ToString(); message.Properties["DeviceId"] = pushDataPacket.Eui; message.Properties["ProtocolName"] = "Lora Packet Forwarder"; message.Properties["ProtocolVersion"] = pushDataPacket.ProtocolVersion.ToString(); message.Properties["MessageType"] = pushDataPacket.MessageType.ToString(); message.CreationTimeUtc = DateTime.UtcNow; _logger.Info($"Sending PushDataPacket to IoT Hub by deviceid '{_deviceId}'", GetType().Name); await _device.Send(message); } break; } } catch (BrokenCircuitException ex) { _logger.Error($"Circuit breaker open -> An error occured while sending a message to IoT Hub by deviceid '{_deviceId}'. Discarding message", GetType().Name, ex); } catch (Exception ex) { _logger.Error($"An error occured while sending a message to IoT Hub by deviceid '{_deviceId}'", GetType().Name, ex); } } }
public int Serialize(INetworkPacket packet, BinaryWriter writer) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } var length = 0; try { switch (packet) { case PushAckPacket pushAckPacket: { writer.Write((byte)pushAckPacket.ProtocolVersion); var token = BitConverter.GetBytes(pushAckPacket.Token); writer.Write(token, 0, 2); var messageType = BitConverter.GetBytes((ushort)pushAckPacket.MessageType); writer.Write(messageType, 0, 1); length = (int)writer.BaseStream.Length; } break; case PullAckPacket pullAckPacket: { writer.Write((byte)pullAckPacket.ProtocolVersion); var token = BitConverter.GetBytes(pullAckPacket.Token); writer.Write(token, 0, 2); var messageType = BitConverter.GetBytes((ushort)pullAckPacket.MessageType); writer.Write(messageType, 0, 1); length = (int)writer.BaseStream.Length; } break; case PullRespPacket pullRespPacket: { writer.Write((byte)pullRespPacket.ProtocolVersion); writer.Write(new byte[2], 0, 2); var messageType = BitConverter.GetBytes((ushort)pullRespPacket.MessageType); writer.Write(messageType, 0, 1); if (pullRespPacket.Payload == null) { throw new NullReferenceException(nameof(pullRespPacket.Payload)); } var data = _payloadSerialiser.Serialize(pullRespPacket.Payload); writer.Write(data, 0, data.Length); length = (int)writer.BaseStream.Length; } break; case PushDataPacket pushDataPacket: { writer.Write((byte)pushDataPacket.ProtocolVersion); var token = BitConverter.GetBytes(pushDataPacket.Token); writer.Write(token, 0, 2); var messageType = BitConverter.GetBytes((ushort)pushDataPacket.MessageType); writer.Write(messageType, 0, 1); var eui = pushDataPacket.Eui.HexToBytes(); writer.Write(eui, 0, 8); if (pushDataPacket.Payload == null) { throw new NullReferenceException(nameof(pushDataPacket.Payload)); } var data = _payloadSerialiser.Serialize(pushDataPacket.Payload); writer.Write(data, 0, data.Length); length = (int)writer.BaseStream.Length; } break; case PullDataPacket pullDataPacket: { writer.Write((byte)pullDataPacket.ProtocolVersion); var token = BitConverter.GetBytes(pullDataPacket.Token); writer.Write(token, 0, 2); var messageType = BitConverter.GetBytes((ushort)pullDataPacket.MessageType); writer.Write(messageType, 0, 1); var eui = pullDataPacket.Eui.HexToBytes(); writer.Write(eui, 0, 8); length = (int)writer.BaseStream.Length; } break; case TxAckPacket txAckPacket: { writer.Write((byte)txAckPacket.ProtocolVersion); var token = BitConverter.GetBytes(txAckPacket.Token); writer.Write(token, 0, 2); var messageType = BitConverter.GetBytes((ushort)txAckPacket.MessageType); writer.Write(messageType, 0, 1); var eui = txAckPacket.Eui.HexToBytes(); writer.Write(eui, 0, 8); length = (int)writer.BaseStream.Length; } break; default: throw new NotImplementedException($"Packet type '{packet.GetType().Name}' is not yet implemented."); } return(length); } catch (Exception ex) { throw new PicocellSerializeException($"Could not serialize message.", ex); } }