public async Task <ActionResult> EditDeviceProperties(string deviceId)
        {
            EditDevicePropertiesModel model;
            IEnumerable <DevicePropertyValueModel> propValModels;

            model = new EditDevicePropertiesModel
            {
                DevicePropertyValueModels = new List <DevicePropertyValueModel>()
            };

            var device = await _deviceLogic.GetDeviceAsync(deviceId);

            if (device != null)
            {
                if (device.DeviceProperties == null)
                {
                    throw new DeviceRequiredPropertyNotFoundException("Required DeviceProperties not found");
                }

                model.DeviceId = device.DeviceProperties.DeviceID;
                propValModels  = _deviceLogic.ExtractDevicePropertyValuesModels(device);
                propValModels  = ApplyDevicePropertyOrdering(propValModels);

                model.DevicePropertyValueModels.AddRange(propValModels);
            }

            return(View("EditDeviceProperties", model));
        }
Beispiel #2
0
        public async Task <HttpResponseMessage> GetDeviceAsync(string id)
        {
            ValidateArgumentNotNullOrWhitespace("id", id);

            return(await GetServiceResponseAsync <dynamic>(async() =>
            {
                return await _deviceLogic.GetDeviceAsync(id);
            }));
        }
        public async Task <ActionResult> Index(string deviceId)
        {
            DeviceModel device = await _deviceLogic.GetDeviceAsync(deviceId);

            if (device.DeviceProperties == null)
            {
                throw new DeviceRequiredPropertyNotFoundException("'DeviceProperties' property is missing");
            }

            IList <SelectListItem> commandListItems = MethodListItems(device);

            bool deviceIsEnabled = device.DeviceProperties.GetHubEnabledState();

            DeviceCommandModel deviceCommandsModel = new DeviceCommandModel
            {
                CommandHistory   = device.CommandHistory.Where(c => c.DeliveryType == DeliveryType.Method).ToList(),
                CommandsJson     = JsonConvert.SerializeObject(device.Commands.Where(c => c.DeliveryType == DeliveryType.Method)),
                SendCommandModel = new SendCommandModel
                {
                    DeviceId              = device.DeviceProperties.DeviceID,
                    CommandSelectList     = commandListItems,
                    CanSendDeviceCommands = deviceIsEnabled && PermsChecker.HasPermission(Permission.SendCommandToDevices)
                },
                DeviceId = device.DeviceProperties.DeviceID
            };

            return(View(deviceCommandsModel));
        }
 public async Task <HttpResponseMessage> GetDeviceAsync(string id)
 {
     return(await GetServiceResponseAsync <dynamic>(async() =>
     {
         return await _deviceLogic.GetDeviceAsync(id);
     }));
 }
Beispiel #5
0
        private async Task UpdateDeviceAssociation(string deviceId, string iccid)
        {
            if (string.IsNullOrEmpty(deviceId))
            {
                throw new ArgumentNullException();
            }

            DeviceModel device = await _deviceLogic.GetDeviceAsync(deviceId);

            device.SystemProperties.ICCID = iccid;
            await _deviceLogic.UpdateDeviceAsync(device);
        }
Beispiel #6
0
        public async Task <ActionResult> Index(string deviceId)
        {
            DeviceModel device = await _deviceLogic.GetDeviceAsync(deviceId);

            if (device.DeviceProperties == null)
            {
                throw new DeviceRequiredPropertyNotFoundException("'DeviceProperties' property is missing");
            }

            IList <SelectListItem> commandListItems = CommandListItems(device);

            bool deviceIsEnabled = device.DeviceProperties.GetHubEnabledState();

            //MDS bae 2017.0615
            var appSettingsReader = new AppSettingsReader();
            var connectionString  = "----";
            CloudStorageAccount storageaccount = CloudStorageAccount.Parse(connectionString);

            blobClient    = storageaccount.CreateCloudBlobClient();
            blobContainer = blobClient.GetContainerReference(blobContainerName);
            await blobContainer.CreateIfNotExistsAsync();

            await blobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

            CloudBlobDirectory blobDirectory = blobContainer.GetDirectoryReference(deviceId);

            foreach (IListBlobItem blob in blobDirectory.ListBlobs())
            {
                if (blob.GetType() == typeof(CloudBlockBlob))
                {
                    //string blobfilename = blob.Uri.Segments.Last();
                    //blobfilename = blobfilename.Remove(blobfilename.Length - 4);
                    //if (blobfilename == device.CommandHistory[device.CommandHistory.Count - 1].MessageId)
                    flirimageurl = blob.Uri.ToString();
                }
            }

            DeviceCommandModel deviceCommandsModel = new DeviceCommandModel
            {
                CommandHistory   = device.CommandHistory.Where(c => c.DeliveryType == DeliveryType.Message).ToList(),
                CommandsJson     = JsonConvert.SerializeObject(device.Commands.Where(c => c.DeliveryType == DeliveryType.Message)),
                SendCommandModel = new SendCommandModel
                {
                    DeviceId              = device.DeviceProperties.DeviceID,
                    CommandSelectList     = commandListItems,
                    CanSendDeviceCommands = deviceIsEnabled && PermsChecker.HasPermission(Permission.SendCommandToDevices)
                },
                ImageUrl = flirimageurl,    //MDS bae 2017.0615
                DeviceId = device.DeviceProperties.DeviceID,
            };

            return(View(deviceCommandsModel));
        }
Beispiel #7
0
        public async Task <ActionResult> EditDeviceProperties(string deviceId)
        {
            EditDevicePropertiesModel model;
            IEnumerable <DevicePropertyValueModel> propValModels;

            model = new EditDevicePropertiesModel()
            {
                DevicePropertyValueModels = new List <DevicePropertyValueModel>()
            };

            var device = await _deviceLogic.GetDeviceAsync(deviceId);

            if (!object.ReferenceEquals(device, null))
            {
                model.DeviceId = DeviceSchemaHelper.GetDeviceID(device);

                propValModels = _deviceLogic.ExtractDevicePropertyValuesModels(device);
                propValModels = ApplyDevicePropertyOrdering(propValModels);

                model.DevicePropertyValueModels.AddRange(propValModels);
            }

            return(View("EditDeviceProperties", model));
        }
Beispiel #8
0
        public async Task <ActionResult> Index(string deviceId)
        {
            dynamic device = await _deviceLogic.GetDeviceAsync(deviceId);

            List <SelectListItem> commandListItems = CommandListItems(device);

            var deviceCommandsModel = new DeviceCommandModel
            {
                CommandHistory   = new List <dynamic>(CommandHistorySchemaHelper.GetCommandHistory(device)),
                CommandsJson     = JsonConvert.SerializeObject(device.Commands),
                SendCommandModel = new SendCommandModel
                {
                    DeviceId              = DeviceSchemaHelper.GetDeviceID(device),
                    CommandSelectList     = commandListItems,
                    CanSendDeviceCommands = DeviceSchemaHelper.GetHubEnabledState(device) == true &&
                                            PermsChecker.HasPermission(Permission.SendCommandToDevices)
                },
                DeviceId = DeviceSchemaHelper.GetDeviceID(device)
            };

            return(View(deviceCommandsModel));
        }
        private async void UpdateDeviceRecord(FeedbackRecord record, DateTime enqueuDateTime)
        {
            Trace.TraceInformation(
                "{0}{0}*** Processing Feedback Record ***{0}{0}DeviceId: {1}{0}OriginalMessageId: {2}{0}Result: {3}{0}{0}",
                Console.Out.NewLine,
                record.DeviceId,
                record.OriginalMessageId,
                record.StatusCode);

            var device = await _deviceLogic.GetDeviceAsync(record.DeviceId);

            var existingCommand = device?.CommandHistory.FirstOrDefault(x => x.MessageId == record.OriginalMessageId);

            if (existingCommand == null)
            {
                return;
            }

            var updatedTime = record.EnqueuedTimeUtc;

            if (updatedTime == default(DateTime))
            {
                updatedTime = enqueuDateTime == default(DateTime) ? DateTime.UtcNow : enqueuDateTime;
            }

            existingCommand.UpdatedTime = updatedTime;
            existingCommand.Result      = record.StatusCode.ToString();

            if (record.StatusCode == FeedbackStatusCode.Success)
            {
                existingCommand.ErrorMessage = string.Empty;
            }


            await _deviceLogic.UpdateDeviceAsync(device);
        }
Beispiel #10
0
        public async Task <HttpResponseMessage> GetDashboardDevicePaneDataAsync(string deviceId)
        {
            ValidateArgumentNotNullOrWhitespace("deviceId", deviceId);

            DashboardDevicePaneDataModel result = new DashboardDevicePaneDataModel()
            {
                DeviceId = deviceId
            };

            Func <Task <DashboardDevicePaneDataModel> > getTelemetry =
                async() =>
            {
                DeviceModel device = await _deviceLogic.GetDeviceAsync(deviceId);

                IEnumerable <LocationJerkModel> locationJerks = await _locationJerkLogic.LoadLatestLocationJerkInfoAsync();


                IList <DeviceTelemetryFieldModel> telemetryFields = null;

                try
                {
                    telemetryFields = _deviceLogic.ExtractTelemetry(device);
                    result.DeviceTelemetryFields = telemetryFields != null?
                                                   telemetryFields.ToArray() : null;
                }
                catch
                {
                    HttpResponseMessage message = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError);
                    message.Content = new StringContent(
                        string.Format(Strings.InvalidDeviceTelemetryFormat, deviceId));
                    throw new HttpResponseException(message);
                }

                // Get Telemetry Summary
                DeviceTelemetrySummaryModel summaryModel;

                result.DeviceTelemetrySummaryModel = summaryModel =
                    await _deviceTelemetryLogic.LoadLatestDeviceTelemetrySummaryAsync(
                        deviceId, DateTime.Now.AddMinutes(-MAX_DEVICE_SUMMARY_AGE_MINUTES));

                if (summaryModel == null)
                {
                    result.DeviceTelemetrySummaryModel =
                        new DeviceTelemetrySummaryModel();
                }

                // Get Telemetry History
                IEnumerable <DeviceTelemetryModel> telemetryModels;
                DateTime minTime = DateTime.Now.AddMinutes(-MAX_DEVICE_SUMMARY_AGE_MINUTES);
                telemetryModels = await _deviceTelemetryLogic.LoadLatestDeviceTelemetryAsync(deviceId, telemetryFields, minTime);

                if (telemetryModels == null)
                {
                    result.DeviceTelemetryModels = new DeviceTelemetryModel[0];
                }
                else
                {
                    result.DeviceTelemetryModels =
                        telemetryModels.OrderBy(t => t.Timestamp).ToArray();
                }

                return(result);
            };

            return(await GetServiceResponseAsync <DashboardDevicePaneDataModel>(
                       getTelemetry,
                       false));
        }
Beispiel #11
0
        //[WebApiRequirePermission(Permission.ViewTelemetry)]
        //public async Task<HttpResponseMessage> GetDashboardDevicePaneDataAsync(string deviceId)
        public async Task <HttpResponseMessage> GetDashboardDevicePaneDataAsync()
        {
            // TESTING
            var deviceId = "SampleDevice001_375";

            ValidateArgumentNotNullOrWhitespace("deviceId", deviceId);

            DashboardDevicePaneDataModel result = new DashboardDevicePaneDataModel()
            {
                DeviceId = deviceId
            };

            Func <Task <DashboardDevicePaneDataModel> > getTelemetry =
                async() =>
            {
                var device = await _deviceLogic.GetDeviceAsync(deviceId);

                IList <DeviceTelemetryFieldModel> telemetryFields;

                try
                {
                    telemetryFields = _deviceLogic.ExtractTelemetry(device);
                    result.DeviceTelemetryFields = telemetryFields?.ToArray();
                }
                catch
                {
                    HttpResponseMessage message = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError);
                    message.Content = new StringContent($"Device {deviceId} has an invalid Telemetry specification on its DeviceInfo");
                    throw new HttpResponseException(message);
                }

                // Get Telemetry Summary
                DeviceTelemetrySummaryModel summaryModel;

                result.DeviceTelemetrySummaryModel = summaryModel =
                    await _deviceTelemetryLogic.LoadLatestDeviceTelemetrySummaryAsync(
                        deviceId, DateTime.Now.AddMinutes(-MaxDeviceSummaryAgeMinutes));

                if (summaryModel == null)
                {
                    result.DeviceTelemetrySummaryModel =
                        new DeviceTelemetrySummaryModel();
                }

                // Get Telemetry History
                DateTime minTime         = DateTime.Now.AddMinutes(-MaxDeviceSummaryAgeMinutes);
                var      telemetryModels = await _deviceTelemetryLogic.LoadLatestDeviceTelemetryAsync(deviceId, telemetryFields, minTime);

                if (telemetryModels == null)
                {
                    result.DeviceTelemetryModels = new DeviceTelemetryModel[0];
                }
                else
                {
                    result.DeviceTelemetryModels =
                        telemetryModels.OrderBy(t => t.Timestamp).ToArray();
                }

                return(result);
            };

            return(await GetServiceResponseAsync(getTelemetry, false));
        }
        public async Task <HttpResponseMessage> GetDashboardDevicePaneDataAsync([FromBody] string[] deviceIds)
        {
            HttpResponseMessage r = new HttpResponseMessage();

            Func <Task <List <DashboardDevicePaneDataModel> > > getTelemetry =
                async() =>
            {
                List <DashboardDevicePaneDataModel> results = new List <DashboardDevicePaneDataModel>();

                foreach (var deviceId in deviceIds)
                {
                    DashboardDevicePaneDataModel result = new DashboardDevicePaneDataModel()
                    {
                        DeviceId = deviceId
                    };

                    dynamic device = await _deviceLogic.GetDeviceAsync(deviceId);

                    result.MessengerUser = Convert.ToString(device.DeviceProperties.MessengerUser);

                    IList <DeviceTelemetryFieldModel> telemetryFields = null;

                    try
                    {
                        telemetryFields = _deviceLogic.ExtractTelemetry(device);
                        result.DeviceTelemetryFields = telemetryFields != null?
                                                       telemetryFields.ToArray() :
                                                           null;
                    }
                    catch
                    {
                        HttpResponseMessage message = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError);
                        message.Content = new StringContent(
                            string.Format(Strings.InvalidDeviceTelemetryFormat, deviceId));
                        throw new HttpResponseException(message);
                    }

                    // Get Telemetry History
                    IEnumerable <DeviceTelemetryModel> telemetryModels;
                    DateTime minTime = DateTime.Now.AddMinutes(-MAX_DEVICE_SUMMARY_AGE_MINUTES);
                    telemetryModels = await _deviceTelemetryLogic.LoadLatestDeviceTelemetryAsync(deviceId, telemetryFields, minTime);

                    if (telemetryModels == null)
                    {
                        result.DeviceTelemetryModels = new DeviceTelemetryModel[0];
                    }
                    else
                    {
                        result.DeviceTelemetryModels =
                            telemetryModels.OrderBy(t => t.Timestamp).ToArray();
                    }

                    results.Add(result);
                }

                return(results);
            };

            return(await GetServiceResponseAsync <List <DashboardDevicePaneDataModel> >(
                       getTelemetry,
                       false));
        }
Beispiel #13
0
        private async Task RunProcess(CancellationToken token)
        {
            FeedbackBatch batch;
            FeedbackReceiver <FeedbackBatch> batchReceiver;
            dynamic device;
            dynamic existingCommand;
            IEnumerable <FeedbackRecord> records;
            ServiceClient serviceClient;
            DateTime      updatedTime;

            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            serviceClient = null;
            try
            {
                serviceClient =
                    ServiceClient.CreateFromConnectionString(
                        _iotHubConnectionString);

                await serviceClient.OpenAsync();

                while (!token.IsCancellationRequested)
                {
                    batchReceiver = serviceClient.GetFeedbackReceiver();
                    batch         =
                        await batchReceiver.ReceiveAsync(
                            TimeSpan.FromSeconds(10.0));

                    if ((batch == null) ||
                        ((records = batch.Records) == null))
                    {
                        continue;
                    }

                    records =
                        records.Where(
                            t =>
                            (t != null) &&
                            !string.IsNullOrEmpty(t.DeviceId) &&
                            !string.IsNullOrEmpty(t.OriginalMessageId));
                    foreach (FeedbackRecord record in records)
                    {
                        device =
                            await _deviceLogic.GetDeviceAsync(record.DeviceId);

                        if (device == null)
                        {
                            continue;
                        }

                        Trace.TraceInformation(
                            "{0}{0}*** Processing Feedback Record ***{0}{0}DeviceId: {1}{0}OriginalMessageId: {2}{0}Result: {3}{0}{0}",
                            Console.Out.NewLine,
                            record.DeviceId,
                            record.OriginalMessageId,
                            record.StatusCode);

                        existingCommand =
                            CommandHistorySchemaHelper.GetCommandHistoryItemOrDefault(
                                device,
                                record.OriginalMessageId);

                        if (existingCommand == null)
                        {
                            continue;
                        }

                        updatedTime = record.EnqueuedTimeUtc;
                        if (updatedTime == default(DateTime))
                        {
                            updatedTime = batch.EnqueuedTime;
                        }

                        if (updatedTime == default(DateTime))
                        {
                            updatedTime = DateTime.UtcNow;
                        }

                        existingCommand.UpdatedTime = updatedTime;

                        existingCommand.Result = record.StatusCode.ToString();

                        if (record.StatusCode == FeedbackStatusCode.Success)
                        {
                            existingCommand.ErrorMessage = string.Empty;
                        }

                        CommandHistorySchemaHelper.UpdateCommandHistoryItem(
                            device,
                            existingCommand);

                        await _deviceLogic.UpdateDeviceAsync(device);
                    }

                    await batchReceiver.CompleteAsync(batch);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(
                    "Error in MessageFeedbackProcessor.RunProcess, Exception: {0}",
                    ex.Message);
            }

            if (serviceClient != null)
            {
                await serviceClient.CloseAsync();
            }

            _isRunning = false;
        }