public DeviceMapQueryResponse GetDevicesTwinMapAsync(DeviceMapQueryConfiguration queryMapConfiguration)
        {
            DevicesDBCache deviceDB = _Cache.DevicesDB;

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

            try
            {
                //Filtering search
                IEnumerable <DeviceTwinFlatModel> devices = deviceDB.Devices.Values;

                IEnumerable <DeviceTwinFlatModel> filteredQuery = InMemoryLinqDeviceQueryHelper.GetFilteredDevicesGroup(devices, new DeviceQueryRuleGroup(LogicalOperators.And)
                {
                    Rules = queryMapConfiguration.Filters
                });

                if (queryMapConfiguration.ViewId == "alerts")
                {
                    filteredQuery = filteredQuery.Where(p => p.StatusCode != 0);
                }

                List <DeviceMapEntity> mapDevicesAddresses = filteredQuery.Where(p => !string.IsNullOrEmpty(p.Location.CountryCode)).Select(p => new DeviceMapEntity()
                                                                                                                                            //{ Count = 1, Name = p.ProductName, GeoLatitude = p.GeoLatitude.ToString(), GeoLongitude = p.GeoLongitude.ToString() })
                {
                    GeoLatitude = p.Location.Latitude.ToString(), GeoLongitude = p.Location.Longitude.ToString()
                })
                                                             .ToList();
                return(new DeviceMapQueryResponse()
                {
                    Pushpins = mapDevicesAddresses,
                    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 DeviceMapQueryResponse()
                {
                    Pushpins = new List <DeviceMapEntity>(),
                    IsDatabaseLoaded = true,
                    IsDatabaseLoading = deviceDB.IsDevicesDBCacheLoading,
                    ErrorMessage = e.Message,
                    Success = true,
                    LastUpdate = deviceDB.LastUpdate
                });
            }
        }
Beispiel #2
0
 public IActionResult GetDevicesTwinMapAreaAsync([FromBody] DeviceMapQueryConfiguration queryMapConfiguration)
 {
     try
     {
         logger.LogInformation("Get DeviceTwin Map Area Properties called");
         DeviceMapAreaQueryResponse reponse = _deviceServiceDB.GetDevicesTwinMapAreaAsync(queryMapConfiguration);
         return(Ok(reponse));
     }
     catch (Exception e)
     {
         logger.LogError(e, "Get DeviceTwin Map Area Properties- Exception {message}", e.Message);
         throw;
     }
 }
        public DeviceMapAreaQueryResponse GetDevicesTwinMapAreaAsync(DeviceMapQueryConfiguration queryMapConfiguration)
        {
            DevicesDBCache deviceDB = _Cache.DevicesDB;

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

            try
            {
                //DeviceTwinSummaryAggregationsModel deviceSummaryAgg = new DeviceTwinSummaryAggregationsModel();
                Dictionary <string, int> devicesRetailer = new Dictionary <string, int>();

                IEnumerable <DeviceTwinFlatModel> devices = deviceDB.Devices.Values;

                IEnumerable <DeviceTwinFlatModel> filteredQuery = InMemoryLinqDeviceQueryHelper.GetFilteredDevicesGroup(devices, new DeviceQueryRuleGroup(LogicalOperators.And)
                {
                    Rules = queryMapConfiguration.Filters
                });

                IEnumerable <IGrouping <string, DeviceTwinFlatModel> > groups = null;

                if (queryMapConfiguration.ViewId == "count")
                {
                    groups = filteredQuery.Where(p => !string.IsNullOrEmpty(p.RetailerRegion)).GroupBy(p => p.RetailerRegion);
                    foreach (IGrouping <string, DeviceTwinFlatModel> group in groups)
                    {
                        devicesRetailer.Add(group.Key, group.Count());
                    }
                }
                else if (queryMapConfiguration.ViewId == "activated")
                {
                    groups = filteredQuery.Where(p => !string.IsNullOrEmpty(p.RetailerRegion)).GroupBy(p => p.RetailerRegion);
                    foreach (IGrouping <string, DeviceTwinFlatModel> group in groups)
                    {
                        devicesRetailer.Add(group.Key, 100 - (group.Where(x => x.ConnectionStatus == DeviceConnectionStatus.NotActivated).Count() * 100 / group.Count()));
                    }
                }
                else if (queryMapConfiguration.ViewId == "retailerName")
                {
                    string retailer = queryMapConfiguration.Filters.Find(p => p.Field == queryMapConfiguration.ViewId)?.Value;
                    if (!string.IsNullOrEmpty(retailer))
                    {
                        groups = devices.Where(p => !string.IsNullOrEmpty(p.RetailerRegion)).GroupBy(p => p.RetailerRegion);
                        foreach (IGrouping <string, DeviceTwinFlatModel> group in groups)
                        {
                            devicesRetailer.Add(group.Key, 100 - (group.Where(x => x.RetailerName != retailer).Count() * 100 / group.Count()));
                        }
                    }
                }
                else if (queryMapConfiguration.ViewId == "productFamily")
                {
                    string productFamily = queryMapConfiguration.Filters.Find(p => p.Field == queryMapConfiguration.ViewId)?.Value;
                    if (!string.IsNullOrEmpty(productFamily))
                    {
                        groups = devices.Where(p => !string.IsNullOrEmpty(p.RetailerRegion)).GroupBy(p => p.RetailerRegion);
                        foreach (IGrouping <string, DeviceTwinFlatModel> group in groups)
                        {
                            devicesRetailer.Add(group.Key, 100 - (group.Where(x => x.ProductFamily != productFamily).Count() * 100 / group.Count()));
                        }
                    }
                }

                return(new DeviceMapAreaQueryResponse()
                {
                    AreaItems = devicesRetailer,
                    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 DeviceMapAreaQueryResponse()
                {
                    AreaItems = new Dictionary <string, int>(),
                    IsDatabaseLoaded = true,
                    IsDatabaseLoading = deviceDB.IsDevicesDBCacheLoading,
                    ErrorMessage = e.Message,
                    Success = true,
                    LastUpdate = deviceDB.LastUpdate
                });
            }
        }