示例#1
0
        public async Task <IActionResult> FunctionBundler(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "FunctionBundler/{devEUI}")] HttpRequest req,
            ILogger logger,
            string devEUI)
        {
            try
            {
                VersionValidator.Validate(req);
            }
            catch (IncompatibleVersionException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            if (!DevEui.TryParse(devEUI, EuiParseOptions.ForbidInvalid, out var parsedDevEui))
            {
                return(new BadRequestObjectResult("Dev EUI is invalid."));
            }

            var requestBody = await req.ReadAsStringAsync();

            if (string.IsNullOrEmpty(requestBody))
            {
                return(new BadRequestObjectResult("missing body"));
            }

            var functionBundlerRequest = JsonConvert.DeserializeObject <FunctionBundlerRequest>(requestBody);
            var result = await HandleFunctionBundlerInvoke(parsedDevEui, functionBundlerRequest, logger);

            return(new OkObjectResult(result));
        }
        private async Task <IActionResult> RunGetDeviceByDevEUI(HttpRequest req, ILogger log)
        {
            string devEui = req.Query["DevEUI"];

            if (!DevEui.TryParse(devEui, out var parsedDevEui))
            {
                return(new BadRequestObjectResult("DevEUI missing or invalid."));
            }

            var device = await this.registryManager.GetDeviceAsync(parsedDevEui.ToString());

            if (device != null)
            {
                log.LogDebug($"Search for {devEui} found 1 device");
                return(new OkObjectResult(new
                {
                    DevEUI = devEui,
                    device.Authentication.SymmetricKey.PrimaryKey
                }));
            }
            else
            {
                log.LogInformation($"Search for {devEui} found 0 devices");
                return(new NotFoundResult());
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "cloudtodevicemessage/{devEUI}")] HttpRequest req,
            string devEUI)
        {
            DevEui parsedDevEui;

            try
            {
                VersionValidator.Validate(req);
                if (!DevEui.TryParse(devEUI, EuiParseOptions.ForbidInvalid, out parsedDevEui))
                {
                    return(new BadRequestObjectResult("Dev EUI is invalid."));
                }
            }
            catch (IncompatibleVersionException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }
            catch (ArgumentException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            var requestBody = await req.ReadAsStringAsync();

            if (string.IsNullOrEmpty(requestBody))
            {
                return(new BadRequestObjectResult("missing body"));
            }

            var c2dMessage = JsonConvert.DeserializeObject <LoRaCloudToDeviceMessage>(requestBody);

            c2dMessage.DevEUI = parsedDevEui;

            return(await SendCloudToDeviceMessageImplementationAsync(parsedDevEui, c2dMessage));
        }
        public async Task <IActionResult> GetDevice(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            if (req is null)
            {
                throw new ArgumentNullException(nameof(req));
            }

            try
            {
                VersionValidator.Validate(req);
            }
            catch (IncompatibleVersionException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            // ABP parameters
            string devAddrString = req.Query["DevAddr"];
            // OTAA parameters
            string rawDevEui   = req.Query["DevEUI"];
            string rawDevNonce = req.Query["DevNonce"];
            var    gatewayId   = req.Query["GatewayId"];

            DevEui?devEui = null;

            if (!string.IsNullOrEmpty(rawDevEui))
            {
                if (DevEui.TryParse(rawDevEui, EuiParseOptions.ForbidInvalid, out var parsedDevEui))
                {
                    devEui = parsedDevEui;
                }
                else
                {
                    return(new BadRequestObjectResult("Dev EUI is invalid."));
                }
            }

            try
            {
                DevNonce?devNonce = ushort.TryParse(rawDevNonce, NumberStyles.None, CultureInfo.InvariantCulture, out var d) ? new DevNonce(d) : null;
                DevAddr? devAddr  = DevAddr.TryParse(devAddrString, out var someDevAddr) ? someDevAddr : null;
                var      results  = await GetDeviceList(devEui, gatewayId, devNonce, devAddr, log);

                var json = JsonConvert.SerializeObject(results);
                return(new OkObjectResult(json));
            }
            catch (DeviceNonceUsedException)
            {
                return(new BadRequestObjectResult("UsedDevNonce"));
            }
            catch (JoinRefusedException ex)
            {
                log.LogDebug("Join refused: {msg}", ex.Message);
                return(new BadRequestObjectResult("JoinRefused: " + ex.Message));
            }
            catch (ArgumentException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }
        }
示例#5
0
        public async Task <IActionResult> NextFCntDownInvoke(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            if (req is null)
            {
                throw new ArgumentNullException(nameof(req));
            }

            try
            {
                VersionValidator.Validate(req);
            }
            catch (IncompatibleVersionException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            string rawDevEui         = req.Query["DevEUI"];
            var    fCntDown          = req.Query["FCntDown"];
            var    fCntUp            = req.Query["FCntUp"];
            var    gatewayId         = req.Query["GatewayId"];
            var    abpFcntCacheReset = req.Query["ABPFcntCacheReset"];

            if (!DevEui.TryParse(rawDevEui, EuiParseOptions.ForbidInvalid, out var devEui))
            {
                return(new BadRequestObjectResult("Dev EUI is invalid."));
            }

            if (!uint.TryParse(fCntUp, out var clientFCntUp))
            {
                throw new ArgumentException("Missing FCntUp");
            }

            if (abpFcntCacheReset != StringValues.Empty)
            {
                using (var deviceCache = new LoRaDeviceCache(this.deviceCache, devEui, gatewayId))
                {
                    if (await deviceCache.TryToLockAsync())
                    {
                        if (deviceCache.TryGetInfo(out var deviceInfo))
                        {
                            // only reset the cache if the current value is larger
                            // than 1 otherwise we likely reset it from another device
                            // and continued processing
                            if (deviceInfo.FCntUp > 1)
                            {
                                log.LogDebug("Resetting cache for device {devEUI}. FCntUp: {fcntup}", devEui, deviceInfo.FCntUp);
                                deviceCache.ClearCache();
                            }
                        }
                    }
                }

                return(new OkObjectResult(null));
            }

            // validate input parameters
            if (!uint.TryParse(fCntDown, out var clientFCntDown) ||
                StringValues.IsNullOrEmpty(gatewayId))
            {
                var errorMsg = "Missing FCntDown or GatewayId";
                throw new ArgumentException(errorMsg);
            }

            var newFCntDown = await GetNextFCntDownAsync(devEui, gatewayId, clientFCntUp, clientFCntDown);

            return(new OkObjectResult(newFCntDown));
        }