// Performs join public async Task <bool> JoinAsync(SimulatedPacketForwarder packetForwarder, int timeoutInMs = 30 * 1000) { if (this.IsJoined) { return(true); } var token = await RandomTokenGenerator.GetTokenAsync(); this.LastPayload = new PhysicalPayload(token, PhysicalIdentifier.PUSH_DATA, null); var header = this.LastPayload.GetSyncHeader(packetForwarder.MacAddress); var joinRequest = this.CreateJoinRequest(); var joinCompleted = new SemaphoreSlim(0); packetForwarder.SubscribeOnce(token, PhysicalIdentifier.PULL_RESP, (response) => { // handle join var loraMessage = new LoRaMessageWrapper(response, true, this.LoRaDevice.AppKey); if (loraMessage.LoRaMessageType == LoRaMessageType.JoinAccept) { this.HandleJoinAccept(loraMessage); joinCompleted.Release(); } }); await packetForwarder.SendAsync(header, joinRequest); TestLogger.Log($"[{this.LoRaDevice.DeviceID}] Join request: {BitConverter.ToString(header).Replace("-", "")}"); return(await joinCompleted.WaitAsync(timeoutInMs)); }
// Sends unconfirmed message public async Task SendUnconfirmedMessageAsync(SimulatedPacketForwarder simulatedPacketForwarder, string payload) { var token = await RandomTokenGenerator.GetTokenAsync(); this.LastPayload = new PhysicalPayload(token, PhysicalIdentifier.PUSH_DATA, null); var header = this.LastPayload.GetSyncHeader(simulatedPacketForwarder.MacAddress); var unconfirmedMessage = this.CreateUnconfirmedDataUpMessage(payload); await simulatedPacketForwarder.SendAsync(header, unconfirmedMessage); TestLogger.Log($"[{this.LoRaDevice.DeviceID}] Unconfirmed data: {BitConverter.ToString(header).Replace("-", "")} {payload}"); //TestLogger.Log($"[{this.LoRaDevice.DevAddr}] Sending data: {BitConverter.ToString(header).Replace("-", "")}{Encoding.UTF8.GetString(gatewayInfo)}"); }