Exemple #1
0
        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));
        }
Exemple #2
0
        public async Task <HttpResponseMessage> GetDeviceLocationData()
        {
            return(await GetServiceResponseAsync <DeviceListLocationsModel>(async() =>
            {
                var filter = new DeviceListFilter()
                {
                    Skip = 0,
                    Take = MAX_DEVICES_TO_DISPLAY_ON_DASHBOARD,
                    SortColumn = "twin.deviceId"
                };

                DeviceListFilterResult filterResult = await _deviceLogic.GetDevices(filter);
                DeviceListLocationsModel dataModel = _deviceLogic.ExtractLocationsData(filterResult.Results);

                return dataModel;
            }, false));
        }
Exemple #3
0
        public async Task <HttpResponseMessage> GetLatestAlertHistoryAsync()
        {
            Func <Task <AlertHistoryResultsModel> > loadHistoryItems =
                async() =>
            {
                // Dates are stored internally as UTC and marked as such.
                // When parsed, they'll be made relative to the server's
                // time zone.  This is only in an issue on servers machines,
                // not set to GMT.
                DateTime currentTime = DateTime.Now;

                var historyItems = new List <AlertHistoryItemModel>();
                var deviceModels = new List <AlertHistoryDeviceModel>();
                var resultsModel = new AlertHistoryResultsModel();

                IEnumerable <AlertHistoryItemModel> data =
                    await _alertsLogic.LoadLatestAlertHistoryAsync(
                        currentTime.Subtract(CautionAlertMaxDelta),
                        DISPLAYED_HISTORY_ITEMS);

                if (data != null)
                {
                    historyItems.AddRange(data);

                    List <dynamic> devices = await LoadAllDevicesAsync();

                    if (devices != null)
                    {
                        //Michael
                        var deviceStatusBYD = await _bydErrorLogic.GetLastErrorsAsync();

                        //End
                        DeviceListLocationsModel locationsModel = _deviceLogic.ExtractLocationsData(devices);
                        if (locationsModel != null)
                        {
                            resultsModel.MaxLatitude  = locationsModel.MaximumLatitude;
                            resultsModel.MaxLongitude = locationsModel.MaximumLongitude;
                            resultsModel.MinLatitude  = locationsModel.MinimumLatitude;
                            resultsModel.MinLongitude = locationsModel.MinimumLongitude;

                            if (locationsModel.DeviceLocationList != null)
                            {
                                Func <string, DateTime?> getStatusTime =
                                    _deviceTelemetryLogic.ProduceGetLatestDeviceAlertTime(historyItems);

                                foreach (DeviceLocationModel locationModel in locationsModel.DeviceLocationList)
                                {
                                    if ((locationModel == null) || string.IsNullOrWhiteSpace(locationModel.DeviceId))
                                    {
                                        continue;
                                    }

                                    var deviceModel = new AlertHistoryDeviceModel()
                                    {
                                        DeviceId  = locationModel.DeviceId,
                                        Latitude  = locationModel.Latitude,
                                        Longitude = locationModel.Longitude
                                    };

                                    DateTime?lastStatusTime = getStatusTime(locationModel.DeviceId);
                                    if (lastStatusTime.HasValue)
                                    {
                                        TimeSpan deltaTime = currentTime - lastStatusTime.Value;

                                        if (deltaTime < CriticalAlertMaxDelta)
                                        {
                                            deviceModel.Status = AlertHistoryDeviceStatus.Critical;
                                        }
                                        else if (deltaTime < CautionAlertMaxDelta)
                                        {
                                            deviceModel.Status = AlertHistoryDeviceStatus.Caution;
                                        }
                                    }
                                    //Michael
                                    var statusDevice = deviceStatusBYD.Where(b => string.Compare(deviceModel.DeviceId, b.DeviceId, true) == 0).FirstOrDefault();
                                    deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_Unknown;
                                    if (statusDevice != null)
                                    {
                                        switch (statusDevice.StatusCode)
                                        {
                                        case 0:
                                            deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_Error;
                                            break;

                                        case 1:
                                            deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_Shutdown;
                                            break;

                                        case 2:
                                            deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_Disconnected;
                                            break;

                                        case 3:
                                            deviceModel.BYDDeviceStatus = AlertHistoryDeviceStatus.BYD_StandingBy;
                                            break;
                                        }
                                    }
                                    //End
                                    deviceModels.Add(deviceModel);
                                }
                            }
                        }
                    }
                }

                resultsModel.Data               = historyItems.Take(DISPLAYED_HISTORY_ITEMS).ToList();
                resultsModel.Devices            = deviceModels;
                resultsModel.TotalAlertCount    = historyItems.Count;
                resultsModel.TotalFilteredCount = historyItems.Count;

                return(resultsModel);
            };

            return(await GetServiceResponseAsync <AlertHistoryResultsModel>(loadHistoryItems, false));
        }