Exemple #1
0
        public async Task <bool> setOTAAJoinAsyncWithRetry(_otaa_join_cmd_t command, int timeoutPerTry, int retries, int delayBetweenRetries = 5000)
        {
            for (var attempt = 1; attempt <= retries; ++attempt)
            {
                TestLogger.Log($"Join attempt #{attempt}/{retries}");
                if (command == _otaa_join_cmd_t.JOIN)
                {
                    sendCommand("AT+JOIN\r\n");
                }
                else if (command == _otaa_join_cmd_t.FORCE)
                {
                    sendCommand("AT+JOIN=FORCE\r\n");
                }

                await Task.Delay(DEFAULT_TIMEWAIT);

                DateTime start = DateTime.Now;

                while (DateTime.Now.Subtract(start).TotalMilliseconds < timeoutPerTry)
                {
                    if (ReceivedSerial((s) => s.Contains("+JOIN: Network joined", StringComparison.Ordinal)))
                    {
                        return(true);
                    }
                    else if (ReceivedSerial(x => x.Contains("+JOIN: LoRaWAN modem is busy", StringComparison.Ordinal)))
                    {
                        break;
                    }
                    else if (ReceivedSerial(x => x.Contains("+JOIN: Join failed", StringComparison.Ordinal)))
                    {
                        break;
                    }

                    // wait a bit to not starve CPU, still waiting for a response from serial port
                    await Task.Delay(50);
                }

                await Task.Delay(delayBetweenRetries);

                // check serial log again before sending another request
                if (ReceivedSerial((s) => s.StartsWith("+JOIN: Network joined", StringComparison.Ordinal)))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #2
0
        public bool setOTAAJoin(_otaa_join_cmd_t command, int timeout)
        {
            if (command == _otaa_join_cmd_t.JOIN)
            {
                sendCommand("AT+JOIN\r\n");
            }
            else if (command == _otaa_join_cmd_t.FORCE)
            {
                sendCommand("AT+JOIN=FORCE\r\n");
            }

            Thread.Sleep(DEFAULT_TIMEWAIT);

            DateTime start = DateTime.Now;

            while (true)
            {
                if (ReceivedSerial(x => x.StartsWith("+JOIN: Done")))
                {
                    return(true);
                }
                else if (ReceivedSerial(x => x.StartsWith("+JOIN: LoRaWAN modem is busy")))
                {
                    return(false);
                }
                else if (ReceivedSerial(x => x.StartsWith("+JOIN: Join failed")))
                {
                    return(false);
                }
                else if (start.AddMilliseconds(timeout) < DateTime.Now)
                {
                    return(false);
                }
            }

            //    ptr = _buffer.Contains("+JOIN: Join failed");
            //if (ptr) return false;
            //ptr = _buffer.Contains("+JOIN: LoRaWAN modem is busy");
            //if (ptr) return false;
        }