/// <summary>
        /// Update the device description.
        /// </summary>
        private void UpdateDevice(string deviceName, string description)
        {
            DevicePatch devicePatch = new DevicePatch();

            devicePatch.DeviceDescription = description;

            this.Client.Devices.Update(
                deviceName,
                devicePatch,
                this.ResourceGroupName,
                this.ManagerName);
        }
Example #2
0
        /// <summary>
        /// Apply the patch to the given device
        /// </summary>
        /// <param name="device"></param>
        /// <param name="patch"></param>
        /// <returns></returns>

        public static Device Patch(this Device device, DevicePatch patch)
        {
            var deviceAfterUpdate = device.Client.Devices.Patch(
                device.Name,
                patch,
                device.ResourceGroupName,
                device.ManagerName);

            deviceAfterUpdate.SetBaseResourceValues(
                device.Client,
                device.ResourceGroupName,
                device.ManagerName);

            return(deviceAfterUpdate);
        }
Example #3
0
        public void TestDevicePatch()
        {
            var devices = Helpers.CheckAndGetDevicesByStatus(this, DeviceStatus.ReadyToSetup, 1);

            Assert.True(devices != null && devices.Any(), "No devices found");

            // Get the first device
            Device device = devices.First();

            Assert.True(string.IsNullOrEmpty(device.DeviceDescription), "Device description is not null");

            string      newDescriptionPrefix = "NewDescription";
            string      newDescription       = newDescriptionPrefix + DateTime.Now.ToString();
            DevicePatch patchInput           = new DevicePatch(newDescription);

            var deviceAfterUpdate = device.Patch(patchInput);

            Assert.True(
                deviceAfterUpdate.DeviceDescription.StartsWith(
                    newDescriptionPrefix,
                    StringComparison.CurrentCultureIgnoreCase),
                "The device update failed");
        }
Example #4
0
 /// <summary>
 /// Patches the device.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='deviceName'>
 /// The device name
 /// </param>
 /// <param name='parameters'>
 /// Patch representation of the device.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The resource group name
 /// </param>
 /// <param name='managerName'>
 /// The manager name
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <Device> UpdateAsync(this IDevicesOperations operations, string deviceName, DevicePatch parameters, string resourceGroupName, string managerName, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.UpdateWithHttpMessagesAsync(deviceName, parameters, resourceGroupName, managerName, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Example #5
0
 /// <summary>
 /// Patches the device.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='deviceName'>
 /// The device name
 /// </param>
 /// <param name='parameters'>
 /// Patch representation of the device.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The resource group name
 /// </param>
 /// <param name='managerName'>
 /// The manager name
 /// </param>
 public static Device Update(this IDevicesOperations operations, string deviceName, DevicePatch parameters, string resourceGroupName, string managerName)
 {
     return(operations.UpdateAsync(deviceName, parameters, resourceGroupName, managerName).GetAwaiter().GetResult());
 }
        /// <summary>
        /// Updates a single Device with one or more values
        /// </summary>
        /// <param name="updatedDevice">The new data for the Device you wish to update</param>
        /// <returns>Returns a result indicating if the operation succeeded</returns>
        public async Task <Result> UpdateDevice(DevicePatch updatedDevice)
        {
            try
            {
                using (var con = new Npgsql.NpgsqlConnection(settings.Connection.DatabaseConnectionString))
                {
                    var sqlPatchOperations = new StringBuilder();
                    var obj            = updatedDevice;
                    var operationCount = 0;

                    if (obj.UserId != null)
                    {
                        sqlPatchOperations.AppendLine(obj.UserId.Operation == OperationKind.Remove
                            ? "userId = NULL,"
                            : "userId = @UserId,"
                                                      );
                        operationCount++;
                    }
                    if (obj.Token != null)
                    {
                        sqlPatchOperations.AppendLine(obj.Token.Operation == OperationKind.Remove
                            ? "deviceToken = NULL,"
                            : "deviceToken = @Token,"
                                                      );
                        operationCount++;
                    }
                    if (obj.Platform != null)
                    {
                        sqlPatchOperations.AppendLine(obj.Platform.Operation == OperationKind.Remove
                            ? "platform = NULL,"
                            : "platform = @Platform,"
                                                      );
                        operationCount++;
                    }
                    if (obj.UpdateTimestamp != null)
                    {
                        sqlPatchOperations.AppendLine(obj.UpdateTimestamp.Operation == OperationKind.Remove
                            ? "updated = NULL,"
                            : "updated = @UpdateTimestamp,"
                                                      );
                        operationCount++;
                    }

                    var patchOperations = sqlPatchOperations.ToString();

                    if (operationCount > 0)
                    {
                        // Remove final ", " from StringBuilder to ensure query is valid
                        patchOperations = patchOperations.TrimEnd(System.Environment.NewLine.ToCharArray());
                        patchOperations = patchOperations.TrimEnd(',');
                    }

                    await con.ExecuteAsync($"UPDATE \"UserDevice\" SET {patchOperations} WHERE deviceId = @ResourceId", new {
                        ResourceId      = obj.ResourceId,
                        UserId          = (int)(obj.UserId == default ? default : obj.UserId.Value),
                        Token           = (string)(obj.Token == default ? default : obj.Token.Value),
                        Platform        = (int)(obj.Platform == default ? default : obj.Platform.Value),
                        UpdateTimestamp = (DateTime)(obj.UpdateTimestamp == default ? default : obj.UpdateTimestamp.Value)
                    }).ConfigureAwait(false);

                    return(Result.Ok());
                }