Exemplo n.º 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            List <Beacons> requestBeaconList = new List <Beacons>();

            foreach (dynamic item in data)
            {
                Beacons requestBeaconItem = new Beacons
                {
                    uuid    = item?.uuid ?? null,
                    mac     = item?.mac ?? null,
                    major   = item?.major ?? null,
                    minor   = item?.minor ?? null,
                    message = item?.message ?? null
                };

                requestBeaconList.Add(requestBeaconItem);
            }

            cosmosClient = new CosmosClient(endpointUri, primaryKey, new CosmosClientOptions()
            {
                ApplicationName = "BeaconsACN"
            });

            database = cosmosClient.GetDatabase(databaseId);

            container = database.GetContainer(containerId);

            var sqlQueryText = "SELECT * FROM c";

            log.LogInformation(">>>");
            log.LogInformation("Running query: {0}\n", sqlQueryText);

            QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);

            FeedIterator <Beacons> queryResultSetIterator = container.GetItemQueryIterator <Beacons>(queryDefinition);

            List <Beacons> beaconQ = new List <Beacons>();

            log.LogInformation(Convert.ToString(queryResultSetIterator));
            log.LogInformation(Convert.ToString(queryResultSetIterator.HasMoreResults));
            while (queryResultSetIterator.HasMoreResults)
            {
                FeedResponse <Beacons> currentResultSet = await queryResultSetIterator.ReadNextAsync();

                log.LogInformation(Convert.ToString(currentResultSet));
                foreach (Beacons beacon_x in currentResultSet)
                {
                    foreach (Beacons beacon_item in requestBeaconList)
                    {
                        if (beacon_item.mac == beacon_x.mac)
                        {
                            beaconQ.Add(beacon_x);
                            log.LogInformation(">>>");
                            log.LogInformation("\tRead {0}\n", beacon_x);
                        }
                    }
                }
                log.LogInformation(Convert.ToString(queryResultSetIterator.HasMoreResults));
            }

            // if(beaconQ == null){
            //     Beacons voidBeacon = new Beacons
            //     {
            //         mensaje = "No se encontro registro"
            //     };
            //     beaconQ.Add(voidBeacon);
            // }

            return(new OkObjectResult(beaconQ));
        }
Exemplo n.º 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            cosmosClient = new CosmosClient(endpointUri, primaryKey, new CosmosClientOptions()
            {
                ApplicationName = "BeaconsACN"
            });

//-----------------------------CREANDO LA BASE DE DATOS---------------------------------//
            database = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId);

            log.LogInformation("Created Database: {0}\n", database.Id);
//-----------------------------CREANDO EL CONTENEDOR---------------------------//
            container = await database.CreateContainerIfNotExistsAsync(containerId, "/mac", 400);

            log.LogInformation("Created Container: {0}\n", container.Id);
//----------------------------INSTANCIANDO BEACONS Y CREANDO EL ITEM-----------//

            Beacons CreateItemBeacons = new Beacons
            {
                uuid    = data?.uuid ?? null,
                mac     = data?.mac ?? null,
                major   = data?.major ?? null,
                minor   = data?.minor ?? null,
                message = data?.message ?? null
            };

            try
            {
                //  Lea el item para ver si existe.
                ItemResponse <Beacons> CreateItemBeaconsResponse = await container.ReadItemAsync <Beacons>(CreateItemBeacons.mac, new PartitionKey(CreateItemBeacons.mac));

                log.LogInformation(">>>");
                log.LogInformation("Item in database with id: {0} already exists\n", CreateItemBeaconsResponse.Resource.mac);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                // Crea un item en el contenedor
                ItemResponse <Beacons> CreateItemBeaconsResponse = await container.UpsertItemAsync <Beacons>(CreateItemBeacons, new PartitionKey(CreateItemBeacons.mac));

                log.LogInformation(">>>");
                log.LogInformation("Created item in database with id: {0} Operation consumed {1} RUs.\n", CreateItemBeaconsResponse.Resource.mac, CreateItemBeaconsResponse.RequestCharge);
            }

            string response = "{\"UUID\": \"" + data?.uuid + "\", \"MAC\": \"" + data?.mac + "\", \"MAJOR\": \"" + data?.major + "\", \"MINOR\": \"" + data?.minor + "\", \"MESSAGE\": \"" + data?.message + "\",\"status\":200, \"message\":\"OK\"}";

            string responseMessage;

            if (data?.mac == null)
            {
                responseMessage = "{ \"status\":400, \"message\":\"Error HTTP 400 (Bad Request)\"}";
                log.LogError(responseMessage);
            }
            else
            {
                responseMessage = response;
                log.LogInformation(response);
            }

            return(new OkObjectResult(CreateItemBeacons));
        }