public async Task<ActionResult> Index()
        {
            var model = new DashboardModel();
            var filters = new List<Infrastructure.Models.FilterInfo>
            {
                new Infrastructure.Models.FilterInfo()
                {
                    ColumnName = "status",
                    FilterType = FilterType.Status,
                    FilterValue = "Running"
                }
            };


            var query = new DeviceListQuery()
            {
                Skip = 0,
                Take = MaxDevicesToDisplayOnDashboard,
                SortColumn = "DeviceID",
                Filters = filters
            };

            DeviceListQueryResult queryResult = await _deviceLogic.GetDevices(query);

            if ((queryResult != null) && (queryResult.Results != null))
            {
                foreach (DeviceModel devInfo in queryResult.Results)
                {
                    string deviceId;
                    try
                    {
                        deviceId = devInfo.DeviceProperties.DeviceID;
                    }
                    catch (DeviceRequiredPropertyNotFoundException)
                    {
                        continue;
                    }

                    model.DeviceIdsForDropdown.Add(new StringPair(deviceId, deviceId));
                }
            }

            // Set key to empty if passed value 0 from arm template
            string key = _configProvider.GetConfigurationSettingValue("MapApiQueryKey");
            model.MapApiQueryKey = key.Equals("0") ? string.Empty : key;

            return View(model);
        }
        public async Task<ActionResult> Index()
        {
            var model = new DashboardModel();

            List<Infrastructure.Models.FilterInfo> filters = new List<Infrastructure.Models.FilterInfo>();
            filters.Add(new Infrastructure.Models.FilterInfo()
                {
                    ColumnName = "status", 
                    FilterType = FilterType.Status, 
                    FilterValue = "Running"
                });
            var query = new DeviceListQuery()
            {
                Skip = 0,
                Take = MaxDevicesToDisplayOnDashboard,
                SortColumn = "DeviceID",
                Filters = filters
            };

            DeviceListQueryResult queryResult = await _deviceLogic.GetDevices(query);
            if ((queryResult != null) && (queryResult.Results != null))
            {
                foreach (dynamic devInfo in queryResult.Results)
                {

                    string deviceId;
                    try
                    {
                        deviceId = DeviceSchemaHelper.GetDeviceID(devInfo);
                    }
                    catch (DeviceRequiredPropertyNotFoundException)
                    {
                        continue;
                    }

                    model.DeviceIdsForDropdown.Add(new StringPair(deviceId, deviceId));
                }
            }

            model.MapApiQueryKey = _configProvider.GetConfigurationSettingValue("MapApiQueryKey");

            return View(model);
        }
        private async Task<List<DeviceModel>> GetDevices()
        {
            var query = new DeviceListQuery
            {
                Take = 1000
            };

            var devices = await _deviceLogic.GetDevices(query);
            return devices.Results;
        }
        public async Task<ActionResult> Index()
        {
            string deviceId;
            DashboardModel model;
            DeviceListQuery query;
            DeviceListQueryResult queryResult;

            model = new DashboardModel();

            query = new DeviceListQuery()
            {
                Skip = 0,
                Take = MaxDevicesToDisplayOnDashboard,
                SortColumn = "DeviceID"
            };

            //The results of this query are used for populating the dropdown
            //As well as extracting location data. We want to include disabled
            //devices on the map, but not in the dropdown. The filters used 
            //IN the query are apply additively to a "column". As a result, we
            //cannot filter on enabled AND disabled because the filters are
            //mutually exclusive. Also we cannot filter on !Pending. So to get
            //all that we need for both uses we need to just get all devices up
            //to the Take value and filter manually in the loop. The map will
            //filter out unregistered devices by virtue of their not having 
            //location data.
            queryResult = await _deviceLogic.GetDevices(query);
            if ((queryResult != null) &&
                (queryResult.Results != null))
            {
                string enabledState = "";
                dynamic props = null;
                foreach (dynamic devInfo in queryResult.Results)
                {
                    try
                    {
                        deviceId = DeviceSchemaHelper.GetDeviceID(devInfo);
                        props = DeviceSchemaHelper.GetDeviceProperties(devInfo);
                        enabledState = props.HubEnabledState;
                    }
                    catch (DeviceRequiredPropertyNotFoundException)
                    {
                        continue;
                    }

                    if (!string.IsNullOrEmpty(deviceId) && !string.IsNullOrWhiteSpace(enabledState) && enabledState.ToLower() == "true")
                    {
                        model.DeviceIdsForDropdown.Add(new StringPair(deviceId, deviceId));
                    }
                }
            }

            model.DeviceLocations = _deviceLogic.ExtractLocationsData(queryResult.Results);
            model.MapApiQueryKey = _configProvider.GetConfigurationSettingValue("MapApiQueryKey");

            return View(model);
        }
        public async Task<DeviceListQueryResult> GetDeviceList(DeviceListQuery query)
        {
            List<DeviceModel> deviceList = await this.GetAllDevicesAsync();

            IQueryable<DeviceModel> filteredDevices = FilterHelper.FilterDeviceList(deviceList.AsQueryable<DeviceModel>(), query.Filters);

            IQueryable<DeviceModel> filteredAndSearchedDevices = this.SearchDeviceList(filteredDevices, query.SearchQuery);

            IQueryable<DeviceModel> sortedDevices = this.SortDeviceList(filteredAndSearchedDevices, query.SortColumn, query.SortOrder);

            List<DeviceModel> pagedDeviceList = sortedDevices.Skip(query.Skip).Take(query.Take).ToList();

            int filteredCount = filteredAndSearchedDevices.Count();

            return new DeviceListQueryResult()
            {
                Results = pagedDeviceList,
                TotalDeviceCount = deviceList.Count,
                TotalFilteredCount = filteredCount
            };
        }
        public async Task<HttpResponseMessage> GetDevicesAsync(
            [FromUri]string search = null,
            [FromUri]string sortColumn = null,
            [FromUri]QuerySortOrder sortOrder = QuerySortOrder.Ascending,
            [FromUri]int skip = 0,
            [FromUri]int take = 50,
            [FromUri]string[] filterColumn = null,
            [FromUri]FilterType[] filterType = null,
            [FromUri]string[] filterValue = null)
        {
            var filters = new List<FilterInfo>();
            if (filterColumn != null && filterType != null && filterValue != null)
            {
                // valid filters must send ALL three values--ignore unmatched extras
                int validFiltersCount = Math.Min(filterColumn.Length, Math.Min(filterType.Length, filterValue.Length));
                for (int i = 0; i < validFiltersCount; ++i)
                {
                    var f = new FilterInfo()
                    {
                        ColumnName = filterColumn[i],
                        FilterType = filterType[i],
                        FilterValue = filterValue[i]
                    };

                    filters.Add(f);
                }
            }

            var q = new DeviceListQuery()
            {
                SearchQuery = search,
                SortColumn = sortColumn,
                SortOrder = sortOrder,
                Skip = skip,
                Take = take,
                Filters = filters
            };

            return await GetServiceResponseAsync(async () => (await _deviceLogic.GetDevices(q)).Results);
        }
        public async Task<HttpResponseMessage> DeleteAllDevices()
        {
            return await GetServiceResponseAsync(async () =>
            {
                // note that you could also hardcode a query to delete a subset of devices
                var query = new DeviceListQuery()
                {
                    Skip = 0,
                    Take = 1000,
                    SortColumn = "DeviceID",
                };

                var devices = await _deviceLogic.GetDevices(query);

                foreach(var d in devices.Results)
                {
                    if (d.DeviceProperties != null && d.DeviceProperties.DeviceID != null)
                    {
                        string deviceId = DeviceSchemaHelper.GetDeviceID(d);

                        // do this in serial so as not to overload anything
                        Debug.Write("DELETING DEVICE: " + deviceId + "...");
                        await _deviceLogic.RemoveDeviceAsync(deviceId);
                        Debug.WriteLine("  (Deleted)");
                    }
                }
                return true;
            });
        }
        public async Task<HttpResponseMessage> GetDevices([FromBody]JObject requestData)
        {
            var dataTableRequest = requestData.ToObject<DataTablesRequest>();
            var sortColumnIndex = dataTableRequest.SortColumns[0].ColumnIndex;

            var listQuery = new DeviceListQuery()
            {
                SortOrder = dataTableRequest.SortColumns[0].SortOrder,
                SortColumn = dataTableRequest.Columns[sortColumnIndex].Name,
                
                SearchQuery = dataTableRequest.Search.Value,

                Filters = dataTableRequest.Filters,

                Skip = dataTableRequest.Start,
                Take = dataTableRequest.Length
            };

            return await GetServiceResponseAsync<DataTablesResponse>(async () =>
            {
                var queryResult = await _deviceLogic.GetDevices(listQuery);

                var dataTablesResponse = new DataTablesResponse()
                {
                    Draw = dataTableRequest.Draw,
                    RecordsTotal = queryResult.TotalDeviceCount,
                    RecordsFiltered = queryResult.TotalFilteredCount,
                    Data = queryResult.Results.ToArray()
                };

                return dataTablesResponse;

            }, false);
        }
 public async Task<DeviceListQueryResult> GetDevices(DeviceListQuery q)
 {
     return await _deviceRegistryListRepository.GetDeviceList(q);
 }
        public async Task<HttpResponseMessage> GetDeviceLocationData()
        {
            return await GetServiceResponseAsync<DeviceListLocationsModel>(async () =>
            {
                var query = new DeviceListQuery()
                {
                    Skip = 0,
                    Take = MAX_DEVICES_TO_DISPLAY_ON_DASHBOARD,
                    SortColumn = "DeviceID"
                };

                DeviceListQueryResult queryResult = await _deviceLogic.GetDevices(query);
                DeviceListLocationsModel dataModel = _deviceLogic.ExtractLocationsData(queryResult.Results);
 
                return dataModel;
            }, false);
        }
        private async Task<List<dynamic>> LoadAllDevicesAsync()
        {
            var query = new DeviceListQuery()
                    {
                Skip = 0,
                Take = MAX_DEVICES_TO_DISPLAY_ON_DASHBOARD,
                SortColumn = "DeviceID"
                    };

            string deviceId;
            var devices = new List<dynamic>();
            DeviceListQueryResult queryResult = await _deviceLogic.GetDevices(query);
            if ((queryResult != null) &&  (queryResult.Results != null))
            {
                string enabledState = "";
                dynamic props = null;
                foreach (dynamic devInfo in queryResult.Results)
                {
                    try
                    {
                        deviceId = DeviceSchemaHelper.GetDeviceID(devInfo);
                        props = DeviceSchemaHelper.GetDeviceProperties(devInfo);
                        enabledState = props.HubEnabledState;
                    }
                    catch (DeviceRequiredPropertyNotFoundException)
                    {
                        continue;
                    }

                    if (!string.IsNullOrWhiteSpace(deviceId))
                    {
                        devices.Add(devInfo);
                    }
                }
            }

            return devices;
        }
        private async Task<List<DeviceModel>> LoadAllDevicesAsync()
        {
            var query = new DeviceListQuery()
            {
                Skip = 0,
                Take = MAX_DEVICES_TO_DISPLAY_ON_DASHBOARD,
                SortColumn = "DeviceID"
            };

            string deviceId;
            var devices = new List<DeviceModel>();
            DeviceListQueryResult queryResult = await  _deviceLogic.GetDevices(query);


            if ((queryResult != null) && (queryResult.Results != null))
            {
                bool? enabledState;
                DeviceProperties props;
                foreach (var devInfo in queryResult.Results)
                {
                    try
                    {
                        deviceId = devInfo.DeviceProperties.DeviceID;
                        props = devInfo.DeviceProperties;
                        enabledState = props.HubEnabledState;
                    }
                    catch (NullReferenceException)
                    {
                        continue;
                    }

                    if (!string.IsNullOrWhiteSpace(deviceId))
                    {
                        devices.Add(devInfo);
                    }
                }
            }

            return devices;
        }