本文整理汇总了C#中Microsoft.Azure.Devices.Device类的典型用法代码示例。如果您正苦于以下问题:C# Device类的具体用法?C# Device怎么用?C# Device使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Device类属于Microsoft.Azure.Devices命名空间,在下文中一共展示了Device类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDeviceConnectionString
private String CreateDeviceConnectionString(Device device)
{
StringBuilder deviceConnectionString = new StringBuilder();
var hostName = String.Empty;
var tokenArray = iotHubConnectionString.Split(';');
for (int i = 0; i < tokenArray.Length; i++)
{
var keyValueArray = tokenArray[i].Split('=');
if (keyValueArray[0] == "HostName")
{
hostName = tokenArray[i] + ';';
break;
}
}
if (!String.IsNullOrWhiteSpace(hostName))
{
deviceConnectionString.Append(hostName);
deviceConnectionString.AppendFormat("DeviceId={0}", device.Id);
if (device.Authentication != null &&
device.Authentication.SymmetricKey != null)
{
deviceConnectionString.AppendFormat(";SharedAccessKey={0}", device.Authentication.SymmetricKey.PrimaryKey);
}
if (this.protocolGatewayHostName.Length > 0)
{
deviceConnectionString.AppendFormat(";GatewayHostName=ssl://{0}:8883", this.protocolGatewayHostName);
}
}
return deviceConnectionString.ToString();
}
示例2: DeviceAuthenticationGoodAuthConfigTest2
public async Task DeviceAuthenticationGoodAuthConfigTest2()
{
var deviceGoodAuthConfig = new Device("123")
{
ConnectionState = DeviceConnectionState.Connected,
Authentication = new AuthenticationMechanism()
{
SymmetricKey = null,
X509Thumbprint = new X509Thumbprint()
{
PrimaryThumbprint = "921BC9694ADEB8929D4F7FE4B9A3A6DE58B0790B",
SecondaryThumbprint = "921BC9694ADEB8929D4F7FE4B9A3A6DE58B0790B"
}
}
};
var restOpMock = new Mock<IHttpClientHelper>();
restOpMock.Setup(
restOp =>
restOp.PutAsync(It.IsAny<Uri>(), It.IsAny<Device>(), It.IsAny<PutOperationType>(),
It.IsAny<IDictionary<HttpStatusCode, Func<HttpResponseMessage, Task<Exception>>>>(),
It.IsAny<CancellationToken>())).ReturnsAsync(deviceGoodAuthConfig);
var registryManager = new HttpRegistryManager(restOpMock.Object, IotHubName);
await registryManager.AddDeviceAsync(deviceGoodAuthConfig);
}
示例3: AddDeviceAsync
public override Task<Device> AddDeviceAsync(Device device, CancellationToken cancellationToken)
{
this.EnsureInstanceNotClosed();
if (device == null)
{
throw new ArgumentNullException("device");
}
if (string.IsNullOrWhiteSpace(device.Id))
{
throw new ArgumentException(ApiResources.DeviceIdNotSet);
}
if (!string.IsNullOrEmpty(device.ETag))
{
throw new ArgumentException(ApiResources.ETagSetWhileRegisteringDevice);
}
// auto generate keys if not specified
if (device.Authentication == null)
{
device.Authentication = new AuthenticationMechanism();
}
ValidateDeviceAuthentication(device);
return this.httpClientHelper.PutAsync(GetRequestUri(device.Id), device, PutOperationType.CreateEntity, null, cancellationToken);
}
示例4: ExportImportDevice
/// <summary>
/// ctor which takes a Device object along with import mode
/// </summary>
/// <param name="device"></param>
/// <param name="importmode"></param>
public ExportImportDevice(Device device, ImportMode importmode)
{
this.Id = device.Id;
this.ETag = device.ETag;
this.ImportMode = importmode;
this.Status = device.Status;
this.StatusReason = device.StatusReason;
this.Authentication = device.Authentication;
}
示例5: RegisterDeviceAsyncTest
public async Task RegisterDeviceAsyncTest()
{
var deviceToReturn = new Device("123") { ConnectionState = DeviceConnectionState.Connected };
var restOpMock = new Mock<IHttpClientHelper>();
restOpMock.Setup(restOp => restOp.PutAsync(It.IsAny<Uri>(), It.IsAny<Device>(), It.IsAny<PutOperationType>(), It.IsAny<IDictionary<HttpStatusCode, Func<HttpResponseMessage, Task<Exception>>>>(), It.IsAny<CancellationToken>())).ReturnsAsync(deviceToReturn);
var registryManager = new HttpRegistryManager(restOpMock.Object, IotHubName);
var returnedDevice = await registryManager.AddDeviceAsync(deviceToReturn);
Assert.AreSame(deviceToReturn, returnedDevice);
restOpMock.VerifyAll();
}
示例6: RegisterDeviceAsync
private static async Task RegisterDeviceAsync()
{
try
{
device = await registryManager.AddDeviceAsync(new Device(deviceId));
}
catch (DeviceAlreadyExistsException)
{
device = await registryManager.GetDeviceAsync(deviceId);
}
deviceKey = device.Authentication.SymmetricKey.PrimaryKey;
}
示例7: GetDeviceAsyncTest
public async Task GetDeviceAsyncTest()
{
const string DeviceId = "123";
var deviceToReturn = new Device(DeviceId) { ConnectionState = DeviceConnectionState.Connected };
var restOpMock = new Mock<IHttpClientHelper>();
restOpMock.Setup(restOp => restOp.GetAsync<Device>(It.IsAny<Uri>(), It.IsAny<IDictionary<HttpStatusCode, Func<HttpResponseMessage, Task<Exception>>>>(), null, false, It.IsAny<CancellationToken>())).ReturnsAsync(deviceToReturn);
var registryManager = new HttpRegistryManager(restOpMock.Object, IotHubName);
var device = await registryManager.GetDeviceAsync(DeviceId);
Assert.AreSame(deviceToReturn, device);
restOpMock.VerifyAll();
}
示例8: DeviceCreatedForm
public DeviceCreatedForm(Device device)
{
InitializeComponent();
if (device.Authentication.SymmetricKey != null)
{
richTextBox.Text = $"ID={device.Id}\nPrimaryKey={device.Authentication.SymmetricKey.PrimaryKey}\nSecondaryKey={device.Authentication.SymmetricKey.SecondaryKey}";
}
else if (device.Authentication.X509Thumbprint != null)
{
richTextBox.Text = $"ID={device.Id}\nPrimaryThumbPrint={device.Authentication.X509Thumbprint.PrimaryThumbprint}\nSecondaryThumbPrint={device.Authentication.X509Thumbprint.SecondaryThumbprint}";
}
}
示例9: createButton_Click
private async void createButton_Click(object sender, EventArgs e)
{
try
{
if (String.IsNullOrEmpty(deviceIDTextBox.Text))
{
throw new ArgumentNullException("DeviceId cannot be empty!");
}
var device = new Device(deviceIDTextBox.Text);
device.Authentication = new AuthenticationMechanism();
if (keysRadioButton.Checked)
{
device.Authentication.SymmetricKey.PrimaryKey = primaryKeyTextBox.Text;
device.Authentication.SymmetricKey.SecondaryKey = secondaryKeyTextBox.Text;
}
else if (x509RadioButton.Checked)
{
device.Authentication.SymmetricKey = null;
device.Authentication.X509Thumbprint = new X509Thumbprint()
{
PrimaryThumbprint = primaryKeyTextBox.Text,
SecondaryThumbprint = secondaryKeyTextBox.Text
};
}
await registryManager.AddDeviceAsync(device);
var deviceCreated = new DeviceCreatedForm(device);
deviceCreated.ShowDialog();
this.Close();
}
catch (Exception ex)
{
using (new CenterDialog(this))
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
示例10: createButton_Click
private async void createButton_Click(object sender, EventArgs e)
{
try
{
Device device = new Device(deviceIDTextBox.Text);
await registryManager.AddDeviceAsync(device);
device = await registryManager.GetDeviceAsync(device.Id);
device.Authentication.SymmetricKey.PrimaryKey = primaryKeyTextBox.Text;
device.Authentication.SymmetricKey.SecondaryKey = secondaryKeyTextBox.Text;
device = await registryManager.UpdateDeviceAsync(device);
string deviceInfo = String.Format("ID={0}\nPrimaryKey={1}\nSecondaryKey={2}", device.Id, device.Authentication.SymmetricKey.PrimaryKey, device.Authentication.SymmetricKey.SecondaryKey);
DeviceCreatedForm deviceCreated = new DeviceCreatedForm(device.Id, device.Authentication.SymmetricKey.PrimaryKey, device.Authentication.SymmetricKey.SecondaryKey);
deviceCreated.ShowDialog();
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
示例11: AddDeviceAsync
/// <summary>
/// Register a new device with the system
/// </summary>
/// <param name="device">
/// The Device object to be registered.
/// </param>
/// <param name="cancellationToken">
/// The token which allows the the operation to be cancelled.
/// </param>
/// <returns>echoes back the Device object with the generated keys and etags</returns>
public abstract Task<Device> AddDeviceAsync(Device device, CancellationToken cancellationToken);
示例12: ValidateDeviceAuthentication
static void ValidateDeviceAuthentication(Device device)
{
if (device.Authentication.SymmetricKey != null)
{
// either both keys should be specified or neither once should be specified (in which case
// we will create both the keys in the service)
if (string.IsNullOrWhiteSpace(device.Authentication.SymmetricKey.PrimaryKey) ^ string.IsNullOrWhiteSpace(device.Authentication.SymmetricKey.SecondaryKey))
{
throw new ArgumentException(ApiResources.DeviceKeysInvalid);
}
}
}
示例13: RemoveDeviceAsync
public override Task RemoveDeviceAsync(Device device)
{
return this.RemoveDeviceAsync(device, CancellationToken.None);
}
示例14: RemoveDeviceAsync
/// <summary>
/// Deletes a previously registered device from the system.
/// </summary>
/// <param name="device">
/// The device to be deleted.
/// </param>
/// <param name="cancellationToken">
/// The token which allows the the operation to be cancelled.
/// </param>
public abstract Task RemoveDeviceAsync(Device device, CancellationToken cancellationToken);
示例15: DeleteDevices2AsyncWithDeviceIdNullTest
public async Task DeleteDevices2AsyncWithDeviceIdNullTest()
{
var goodDevice = new Device("123") { ConnectionState = DeviceConnectionState.Connected, ETag = "234" };
var badDevice = new Device();
var restOpMock = new Mock<IHttpClientHelper>();
var registryManager = new HttpRegistryManager(restOpMock.Object, IotHubName);
await registryManager.RemoveDevices2Async(new List<Device>() { goodDevice, badDevice });
Assert.Fail("DeleteDevices API did not throw exception when deviceId was null.");
}