private async Task ReceiveWorker(CancellationToken cancellationToken) { Microsoft.Azure.Devices.Client.Message message = null; INetworkPacket packet = null; while (!cancellationToken.IsCancellationRequested) { try { message = await _device.Receive(); if (message == null) { continue; } var messageType = MessageType.PULL_RESP;// Enum.Parse(typeof(MessageType), message.Properties["MessageType"]); var data = message.GetBytes(); _logger.Info($"Receiving '{messageType}' packet from IoT Hub by deviceid '{_deviceId}'", GetType().Name); switch (messageType) { case MessageType.PULL_RESP: { packet = new PullRespPacket { ProtocolVersion = int.Parse(message.Properties["ProtocolVersion"]), //Token = message.Properties["Token"], Payload = _payloadSerializer.Deserialize <PullRespPacketPayload>(data), }; } break; default: { continue; } } PacketReceived(this, new PacketEventArgs(packet, this)); } catch (BrokenCircuitException ex) { _logger.Error($"Circuit breaker open -> An error occured while receiving a message from IoT Hub by deviceid '{_deviceId}'", GetType().Name, ex); await Task.Delay(TimeSpan.FromSeconds(30)); } catch (Exception ex) { _logger.Error($"An error occured while receiving a message from IoT Hub by deviceid '{_deviceId}'", GetType().Name, ex); } } }
public INetworkPacket Deserialize(BinaryReader reader) { var buffer = reader.ReadBytes((int)reader.BaseStream.Length); var messageType = (MessageType)buffer[3]; try { switch (messageType) { case MessageType.PUSH_ACK: { var protocolVersion = (int)buffer[0]; var packet = new PushAckPacket { ProtocolVersion = protocolVersion, Token = BitConverter.ToUInt16(buffer, 1), }; return(packet); } case MessageType.PULL_ACK: { var protocolVersion = (int)buffer[0]; var packet = new PullAckPacket { ProtocolVersion = protocolVersion, Token = BitConverter.ToUInt16(buffer, 1), }; return(packet); } case MessageType.PULL_RESP: { var json = Encoding.ASCII.GetString(buffer, 4, buffer.Length - 4); var protocolVersion = (int)buffer[0]; var packet = new PullRespPacket { ProtocolVersion = protocolVersion, Token = BitConverter.ToUInt16(buffer, 1), Payload = _payloadSerialiser.Deserialize <PullRespPacketPayload>(json, protocolVersion) }; return(packet); } case MessageType.PUSH_DATA: { var json = Encoding.ASCII.GetString(buffer, 12, buffer.Length - 12); var eui = buffer.Skip(4).Take(8).ToArray().ToHex(string.Empty); var protocolVersion = (int)buffer[0]; var packet = new PushDataPacket { ProtocolVersion = protocolVersion, Token = BitConverter.ToUInt16(buffer, 1), Eui = eui, Payload = _payloadSerialiser.Deserialize <PushDataPacketPayload>(json, protocolVersion) }; return(packet); } case MessageType.PULL_DATA: { var protocolVersion = (int)buffer[0]; var json = Encoding.ASCII.GetString(buffer, 12, buffer.Length - 12); var eui = buffer.Skip(4).Take(8).ToArray().ToHex(string.Empty); var packet = new PullDataPacket { ProtocolVersion = protocolVersion, Token = BitConverter.ToUInt16(buffer, 1), Eui = eui, }; return(packet); } case MessageType.TX_ACK: { var json = Encoding.ASCII.GetString(buffer, 12, buffer.Length - 12); var eui = buffer.Skip(4).Take(8).ToArray().ToHex(string.Empty); var protocolVersion = (int)buffer[0]; var packet = new TxAckPacket { ProtocolVersion = protocolVersion, Token = BitConverter.ToUInt16(buffer, 1), Eui = eui, Payload = _payloadSerialiser.Deserialize <TxAckPacketPayload>(json, protocolVersion) }; return(packet); } default: throw new NotImplementedException($"Message type '{messageType.GetType().Name}' is not yet implemented."); } } catch (Exception ex) { throw new PicocellDeserializeException($"Could not deserialize message with message type {messageType.ToString()}.", buffer, ex); } }