Share via


Devices.CreateOrUpdateAsync Method

Definition

Create a device entity.

public virtual System.Threading.Tasks.Task<Azure.Response> CreateOrUpdateAsync (string sensorPartnerId, string deviceId, Azure.Core.RequestContent content, Azure.RequestContext context = default);
abstract member CreateOrUpdateAsync : string * string * Azure.Core.RequestContent * Azure.RequestContext -> System.Threading.Tasks.Task<Azure.Response>
override this.CreateOrUpdateAsync : string * string * Azure.Core.RequestContent * Azure.RequestContext -> System.Threading.Tasks.Task<Azure.Response>
Public Overridable Function CreateOrUpdateAsync (sensorPartnerId As String, deviceId As String, content As RequestContent, Optional context As RequestContext = Nothing) As Task(Of Response)

Parameters

sensorPartnerId
String

Id of the sensor partner.

deviceId
String

Id of the device resource.

content
RequestContent

The content to send as the body of the request. Details of the request body schema are in the Remarks section below.

context
RequestContext

The request context, which can override default behaviors of the client pipeline on a per-call basis.

Returns

The response returned from the service. Details of the response body schema are in the Remarks section below.

Exceptions

sensorPartnerId, deviceId or content is null.

sensorPartnerId or deviceId is an empty string, and was expected to be non-empty.

Service returned a non-success status code.

Examples

This sample shows how to call CreateOrUpdateAsync with required parameters and parse the result.

var credential = new DefaultAzureCredential();
var client = new FarmBeatsClient(credential).GetDevicesClient(<2022-11-01-preview>);

var data = new {};

Response response = await client.CreateOrUpdateAsync("<sensorPartnerId>", "<deviceId>", RequestContent.Create(data));

JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.ToString());

This sample shows how to call CreateOrUpdateAsync with all parameters and request content, and how to parse the result.

var credential = new DefaultAzureCredential();
var client = new FarmBeatsClient(credential).GetDevicesClient(<2022-11-01-preview>);

var data = new {
    deviceDataModelId = "<deviceDataModelId>",
    integrationId = "<integrationId>",
    type = "<type>",
    hardwareId = "<hardwareId>",
    reportingIntervalInSeconds = 1234,
    parentDeviceId = "<parentDeviceId>",
    location = new {
        latitude = 123.45d,
        longitude = 123.45d,
    },
    status = "<status>",
    name = "<name>",
    description = "<description>",
    properties = new {
        key = new {},
    },
};

Response response = await client.CreateOrUpdateAsync("<sensorPartnerId>", "<deviceId>", RequestContent.Create(data));

JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("deviceDataModelId").ToString());
Console.WriteLine(result.GetProperty("integrationId").ToString());
Console.WriteLine(result.GetProperty("type").ToString());
Console.WriteLine(result.GetProperty("hardwareId").ToString());
Console.WriteLine(result.GetProperty("reportingIntervalInSeconds").ToString());
Console.WriteLine(result.GetProperty("parentDeviceId").ToString());
Console.WriteLine(result.GetProperty("location").GetProperty("latitude").ToString());
Console.WriteLine(result.GetProperty("location").GetProperty("longitude").ToString());
Console.WriteLine(result.GetProperty("sensorPartnerId").ToString());
Console.WriteLine(result.GetProperty("id").ToString());
Console.WriteLine(result.GetProperty("status").ToString());
Console.WriteLine(result.GetProperty("createdDateTime").ToString());
Console.WriteLine(result.GetProperty("modifiedDateTime").ToString());
Console.WriteLine(result.GetProperty("eTag").ToString());
Console.WriteLine(result.GetProperty("name").ToString());
Console.WriteLine(result.GetProperty("description").ToString());
Console.WriteLine(result.GetProperty("createdBy").ToString());
Console.WriteLine(result.GetProperty("modifiedBy").ToString());
Console.WriteLine(result.GetProperty("properties").GetProperty("<test>").ToString());

Remarks

Below is the JSON schema for the request and response payloads.

Request Body:

Schema for Device:

{
  deviceDataModelId: string, # Optional. Id of the associated device data model.
  integrationId: string, # Optional. Integration id for the device.
  type: string, # Optional. Type of device.
  hardwareId: string, # Optional. Device hardwareId.
  reportingIntervalInSeconds: number, # Optional. Interval at which the device sends data in seconds.
  parentDeviceId: string, # Optional. Parent device Id for this device.
  location: {
    latitude: number, # Required. Latitude of the location.
    longitude: number, # Required. Longitude of the location.
  }, # Optional. Location model class.
  sensorPartnerId: string, # Optional. Id of the associated sensor partner.
  id: string, # Optional. Id of the resource.
  status: string, # Optional. Status of the resource.
  createdDateTime: string (ISO 8601 Format), # Optional. Date-time when resource was created, sample format: yyyy-MM-ddTHH:mm:ssZ.
  modifiedDateTime: string (ISO 8601 Format), # Optional. Date-time when resource was last modified, sample format: yyyy-MM-ddTHH:mm:ssZ.
  eTag: string, # Optional. The ETag value to implement optimistic concurrency.
  name: string, # Optional. Name to identify resource.
  description: string, # Optional. Textual description of the resource.
  createdBy: string, # Optional. Created by user/tenant id.
  modifiedBy: string, # Optional. Modified by user/tenant id.
  properties: Dictionary<string, any>, # Optional. A collection of key value pairs that belongs to the resource.
Each pair must not have a key greater than 50 characters
and must not have a value greater than 150 characters.
Note: A maximum of 25 key value pairs can be provided for a resource and
only string, numeral and datetime (yyyy-MM-ddTHH:mm:ssZ) values are supported.
}

Response Body:

Schema for Device:

{
  deviceDataModelId: string, # Optional. Id of the associated device data model.
  integrationId: string, # Optional. Integration id for the device.
  type: string, # Optional. Type of device.
  hardwareId: string, # Optional. Device hardwareId.
  reportingIntervalInSeconds: number, # Optional. Interval at which the device sends data in seconds.
  parentDeviceId: string, # Optional. Parent device Id for this device.
  location: {
    latitude: number, # Required. Latitude of the location.
    longitude: number, # Required. Longitude of the location.
  }, # Optional. Location model class.
  sensorPartnerId: string, # Optional. Id of the associated sensor partner.
  id: string, # Optional. Id of the resource.
  status: string, # Optional. Status of the resource.
  createdDateTime: string (ISO 8601 Format), # Optional. Date-time when resource was created, sample format: yyyy-MM-ddTHH:mm:ssZ.
  modifiedDateTime: string (ISO 8601 Format), # Optional. Date-time when resource was last modified, sample format: yyyy-MM-ddTHH:mm:ssZ.
  eTag: string, # Optional. The ETag value to implement optimistic concurrency.
  name: string, # Optional. Name to identify resource.
  description: string, # Optional. Textual description of the resource.
  createdBy: string, # Optional. Created by user/tenant id.
  modifiedBy: string, # Optional. Modified by user/tenant id.
  properties: Dictionary<string, any>, # Optional. A collection of key value pairs that belongs to the resource.
Each pair must not have a key greater than 50 characters
and must not have a value greater than 150 characters.
Note: A maximum of 25 key value pairs can be provided for a resource and
only string, numeral and datetime (yyyy-MM-ddTHH:mm:ssZ) values are supported.
}

Applies to