Ejemplo n.º 1
0
        // 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);

            var joinRequestUplinkMessage = joinRequest.SerializeUplink(this.AppKey);

            packetForwarder.SubscribeOnce((response) =>
            {
                // handle join
                var txpk = Txpk.CreateTxpk(response, this.LoRaDevice.AppKey);
                byte[] convertedInputMessage = Convert.FromBase64String(txpk.Data);

                var joinAccept = new LoRaPayloadJoinAccept(convertedInputMessage, this.AppKey);

                var result = this.HandleJoinAccept(joinAccept); // may need to return bool and only release if true.
                joinCompleted.Release();

                return(result);
            });

            await packetForwarder.SendAsync(header, joinRequest.GetByteMessage());

            TestLogger.Log($"[{this.LoRaDevice.DeviceID}] Join request: {BitConverter.ToString(header).Replace("-", string.Empty)}");

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                timeoutInMs = 60 * 1000;
            }
#endif

            return(await joinCompleted.WaitAsync(timeoutInMs));
        }
Ejemplo n.º 2
0
        // Sends confirmed message
        public async Task SendConfirmedMessageAsync(SimulatedPacketForwarder simulatedPacketForwarder, string payload)
        {
            var token = await RandomTokenGenerator.GetTokenAsync();

            if (this.LastPayload == null)
            {
                this.LastPayload = new PhysicalPayload(token, PhysicalIdentifier.PUSH_DATA, null);
            }
            var header = this.LastPayload.GetSyncHeader(simulatedPacketForwarder.MacAddress);

            var confirmedMessage = this.CreateConfirmedDataUpMessage(payload);

            confirmedMessage.SerializeUplink(this.AppSKey, this.NwkSKey);
            this.LastPayload = await simulatedPacketForwarder.SendAsync(header, confirmedMessage.GetByteMessage());

            TestLogger.Log($"[{this.LoRaDevice.DeviceID}] Confirmed data: {BitConverter.ToString(header).Replace("-", string.Empty)} {payload}");

            // TestLogger.Log($"[{this.LoRaDevice.DevAddr}] Sending data: {BitConverter.ToString(header).Replace("-", "")}{Encoding.UTF8.GetString(gatewayInfo)}");
        }