-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoTHubModuleService.cs
99 lines (87 loc) · 3.27 KB
/
IoTHubModuleService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ReSharper disable GCSuppressFinalizeForTypeWithoutDestructor
namespace Atc.Azure.IoT.Services.IoTHub;
/// <summary>
/// Provides services for direct interaction with IoT devices and modules through Azure IoT Hub, enabling the invocation of direct methods on devices.
/// This service facilitates communication between the cloud and IoT devices by sending direct method requests and processing the responses.
/// </summary>
public sealed partial class IoTHubModuleService : ServiceBase, IIoTHubModuleService, IDisposable
{
private ServiceClient? serviceClient;
public IoTHubModuleService(
ILoggerFactory loggerFactory,
IotHubOptions options)
{
logger = loggerFactory.CreateLogger<IoTHubModuleService>();
ValidateAndAssign(options.ConnectionString, Assign);
}
public async Task<MethodResultModel> CallMethod(
string deviceId,
string moduleId,
MethodParameterModel parameters,
CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrEmpty(deviceId);
ArgumentException.ThrowIfNullOrEmpty(moduleId);
ArgumentNullException.ThrowIfNull(parameters);
try
{
var methodInfo = new CloudToDeviceMethod(parameters.Name);
methodInfo.SetPayloadJson(parameters.JsonPayload);
var result = await (string.IsNullOrEmpty(moduleId)
? serviceClient!.InvokeDeviceMethodAsync(deviceId, methodInfo, cancellationToken)
: serviceClient!.InvokeDeviceMethodAsync(deviceId, moduleId, methodInfo, cancellationToken));
return new MethodResultModel(
Status: result.Status,
JsonPayload: result.GetPayloadAsJson());
}
catch (DeviceNotFoundException ex)
{
LogMethodCallFailedDeviceNotFound(
deviceId,
parameters.Name,
parameters.JsonPayload,
ex.GetLastInnerMessage());
return new MethodResultModel(
Status: StatusCodes.Status404NotFound,
JsonPayload: $"{{\"message\":\"Device not found by id '{deviceId}'\"}}");
}
catch (Exception ex)
{
LogMethodCallFailed(
deviceId,
parameters.Name,
parameters.JsonPayload,
ex.GetLastInnerMessage());
return new MethodResultModel(
Status: StatusCodes.Status500InternalServerError,
JsonPayload: JsonSerializer.Serialize(new MethodResultErrorModel(
Status: StatusCodes.Status500InternalServerError,
Title: nameof(HttpStatusCode.InternalServerError),
Detail: ex.GetLastInnerMessage())));
}
}
protected override void Assign(
string connectionString)
{
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(
bool disposing)
{
if (!disposing)
{
return;
}
if (serviceClient is null)
{
return;
}
serviceClient.Dispose();
serviceClient = null;
}
}