Ejemplo n.º 1
0
        public async Task <IActionResult> GetDevice(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                VersionValidator.Validate(req);
            }
            catch (IncompatibleVersionException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            // ABP parameters
            string devAddr = req.Query["DevAddr"];
            // OTAA parameters
            string devEUI    = req.Query["DevEUI"];
            string devNonce  = req.Query["DevNonce"];
            string gatewayId = req.Query["GatewayId"];

            if (devEUI != null)
            {
                EUIValidator.ValidateDevEUI(devEUI);
            }

            try
            {
                var results = await this.GetDeviceList(devEUI, gatewayId, devNonce, devAddr, log);

                string 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));
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "cloudtodevicemessage/{devEUI}")] HttpRequest req,
            string devEUI)
        {
            EUIValidator.ValidateDevEUI(devEUI);

            var requestBody = await req.ReadAsStringAsync();

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

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

            c2dMessage.DevEUI = devEUI;

            return(await this.SendCloudToDeviceMessageImplementationAsync(devEUI, c2dMessage));
        }
Ejemplo n.º 3
0
        public IActionResult NextFCntDownInvoke(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                VersionValidator.Validate(req);
            }
            catch (IncompatibleVersionException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            string devEUI            = req.Query["DevEUI"];
            string fCntDown          = req.Query["FCntDown"];
            string fCntUp            = req.Query["FCntUp"];
            string gatewayId         = req.Query["GatewayId"];
            string abpFcntCacheReset = req.Query["ABPFcntCacheReset"];
            uint   newFCntDown       = 0;

            EUIValidator.ValidateDevEUI(devEUI);

            if (!string.IsNullOrEmpty(abpFcntCacheReset))
            {
                this.deviceCache.KeyDelete(devEUI);
                return((ActionResult) new OkObjectResult(null));
            }

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

            newFCntDown = this.GetNextFCntDown(devEUI, gatewayId, clientFCntUp, clientFCntDown);

            return((ActionResult) new OkObjectResult(newFCntDown));
        }
        public async Task <IActionResult> NextFCntDownInvoke(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                VersionValidator.Validate(req);
            }
            catch (IncompatibleVersionException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            string devEUI            = req.Query["DevEUI"];
            string fCntDown          = req.Query["FCntDown"];
            string fCntUp            = req.Query["FCntUp"];
            string gatewayId         = req.Query["GatewayId"];
            string abpFcntCacheReset = req.Query["ABPFcntCacheReset"];
            uint   newFCntDown       = 0;

            EUIValidator.ValidateDevEUI(devEUI);

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

            if (!string.IsNullOrEmpty(abpFcntCacheReset))
            {
                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. FCntUp: {fcntup}", deviceInfo.FCntUp);
                                deviceCache.ClearCache();
                            }
                        }
                    }
                }

                return((ActionResult) new OkObjectResult(null));
            }

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

            newFCntDown = await this.GetNextFCntDownAsync(devEUI, gatewayId, clientFCntUp, clientFCntDown);

            return((ActionResult) new OkObjectResult(newFCntDown));
        }