Exemplo n.º 1
0
 public IActionResult GetDevicesTwinList([FromBody] DeviceQueryConfiguration config)
 {
     try
     {
         logger.LogInformation("Get DeviceTwin Properties called");
         DeviceQueryResponse response = _deviceServiceDB.GetDevicesTwinInfoAsync(config);
         return(Ok(response));
     }
     catch (Exception e)
     {
         logger.LogError(e, "Get DeviceTwin Properties- Exception {message}", e.Message);
         throw;
     }
 }
        private DeviceQueryResponse GetGroupedDevicesResponse(DevicesDBCache deviceDB, IEnumerable <DeviceTwinFlatModel> filteredQuery, DeviceQueryConfiguration queryConfiguration)
        {
            try
            {
                IEnumerable <DeviceGroup> groups = InMemoryLinqDeviceQueryHelper.GetDevicesGroups(filteredQuery, queryConfiguration.GroupBy, queryConfiguration.OrderBySorting);

                int nbrGroups = groups.Count();
                groups = groups
                         .Skip(queryConfiguration.PageIndex * queryConfiguration.ItemsPerPage)
                         .Take(queryConfiguration.ItemsPerPage);

                return(new DeviceQueryResponse()
                {
                    Groups = groups,
                    GroupsCount = nbrGroups,
                    ItemsCount = filteredQuery.Count(),
                    IsDatabaseLoaded = deviceDB.IsDevicesDBCacheInitialized,
                    IsDatabaseLoading = deviceDB.IsDevicesDBCacheLoading,
                    ErrorMessage = string.Empty,
                    Success = true,
                    LastUpdate = deviceDB.LastUpdate
                });
            }
            catch (Exception e)
            {
                Log.Error("Get IoTHub Devices Twin error {@error}", e.Message);
                return(new DeviceQueryResponse()
                {
                    Groups = new List <DeviceGroup>(),
                    GroupsCount = 0,
                    ItemsCount = 0,
                    IsDatabaseLoaded = deviceDB.IsDevicesDBCacheInitialized,
                    IsDatabaseLoading = deviceDB.IsDevicesDBCacheLoading,
                    ErrorMessage = string.Empty,
                    Success = true,
                    LastUpdate = deviceDB.LastUpdate
                });
            }
        }
        public DeviceQueryResponse GetDevicesTwinInfoAsync(DeviceQueryConfiguration queryConfiguration)
        {
            DevicesDBCache deviceDB = _Cache.DevicesDB;

            if (!deviceDB.IsDevicesDBCacheInitialized)
            {
                return new DeviceQueryResponse()
                       {
                           Success = true
                       }
            }
            ;

            try
            {
                //Get cached devices
                IEnumerable <DeviceTwinFlatModel> devices = deviceDB.Devices.Values;

                //GroupBy Filter
                IEnumerable <DeviceTwinFlatModel> filteredQuery = InMemoryLinqDeviceQueryHelper.GetFilteredDevicesGroup(devices, queryConfiguration.Where);

                int nbrPages = filteredQuery.Count();
                if (string.IsNullOrEmpty(queryConfiguration.GroupBy))
                {
                    if (filteredQuery.Count() > 0)
                    {
                        filteredQuery = InMemoryLinqDeviceQueryHelper.GetOrderedDevices(filteredQuery, queryConfiguration.OrderBy, queryConfiguration.OrderBySorting)
                                        .Skip(queryConfiguration.PageIndex * queryConfiguration.ItemsPerPage)
                                        .Take(queryConfiguration.ItemsPerPage);
                    }
                    return(new DeviceQueryResponse()
                    {
                        Items = filteredQuery.Count() > 0 ? filteredQuery.Select(p => new DeviceInfoEntity(p)) : new List <DeviceInfoEntity>(),
                        ItemsCount = nbrPages,
                        IsDatabaseLoading = deviceDB.IsDevicesDBCacheLoading,
                        IsDatabaseLoaded = deviceDB.IsDevicesDBCacheInitialized,
                        ErrorMessage = string.Empty,
                        Success = true,
                        LastUpdate = deviceDB.LastUpdate
                    });
                }
                else
                {
                    return(GetGroupedDevicesResponse(deviceDB, filteredQuery, queryConfiguration));
                }
            }
            catch (Exception e)
            {
                Log.Error("Get IoTHub Devices Twin error {@error}", e.Message);
                return(new DeviceQueryResponse()
                {
                    Items = new List <DeviceInfoEntity>(),
                    ItemsCount = 0,
                    IsDatabaseLoading = deviceDB.IsDevicesDBCacheLoading,
                    IsDatabaseLoaded = true,
                    ErrorMessage = e.Message,
                    Success = true,
                    LastUpdate = deviceDB.LastUpdate
                });
            }
        }