/// <summary> /// List nodes on endpoint /// </summary> private static async Task ListNodesAsync(IIoTHubConfig config, ILogger logger, string deviceId, string moduleId, string endpointUrl, CancellationToken ct) { if (string.IsNullOrEmpty(endpointUrl)) { throw new ArgumentNullException(nameof(endpointUrl)); } var client = new IoTHubTwinMethodClient(CreateClient(config, logger), logger); while (!ct.IsCancellationRequested) { await Task.Delay(TimeSpan.FromSeconds(5), ct); try { var content = new GetNodesRequestModel { EndpointUrl = endpointUrl }; var result = await client.CallMethodAsync(deviceId, moduleId, "GetConfiguredNodesOnEndpoint", JsonConvertEx.SerializeObject(content), null, ct); var response = JsonConvertEx.DeserializeObject <GetNodesResponseModel>(result); logger.Information("Published nodes: {@response}", response); } catch (Exception ex) { logger.Verbose(ex, "Failed to list published nodes."); } } }
/// <summary> /// Configure publishing of a particular node /// </summary> private static async Task PublishNodesAsync(IIoTHubConfig config, ILogger logger, string deviceId, string moduleId, string endpointUrl, string nodeId, bool publish, CancellationToken ct) { if (string.IsNullOrEmpty(endpointUrl)) { throw new ArgumentNullException(nameof(endpointUrl)); } if (string.IsNullOrEmpty(nodeId)) { throw new ArgumentNullException(nameof(nodeId)); } var client = new IoTHubTwinMethodClient(CreateClient(config, logger), logger); while (!ct.IsCancellationRequested) { try { logger.Information("Start publishing {nodeId}...", nodeId); var content = new PublishNodesRequestModel { EndpointUrl = endpointUrl, UseSecurity = true, OpcNodes = new List <PublisherNodeModel> { new PublisherNodeModel { Id = nodeId, OpcPublishingInterval = 1000, OpcSamplingInterval = 1000 } } }; var result = await client.CallMethodAsync(deviceId, moduleId, publish? "PublishNodes" : "UnpublishNodes", JsonConvertEx.SerializeObject(content), null, ct); logger.Information("... started"); break; } catch (Exception ex) { logger.Verbose(ex, "Failed to configure publishing."); // Wait a bit await Task.Delay(TimeSpan.FromSeconds(2), ct); } } }
/// <summary> /// Ping continously /// </summary> private static async Task PingAsync(IIoTHubConfig config, ILogger logger, string deviceId, string moduleId, CancellationToken ct) { var serializer = new NewtonSoftJsonSerializer(); var client = new IoTHubTwinMethodClient(CreateClient(config, logger), logger); logger.Information("Starting echo thread"); var found = false; for (var index = 0; !ct.IsCancellationRequested; index++) { try { var message = serializer.SerializePretty(new { Index = index, Started = DateTime.UtcNow }); logger.Debug("Sending ECHO {Index}... ", index); var result = await client.CallMethodAsync(deviceId, moduleId, "Echo_V1", message, null, ct); found = true; try { var returned = serializer.Parse(result); logger.Debug("... received back ECHO {Index} - took {Passed}.", returned["Index"], DateTime.UtcNow - ((DateTime)returned["Started"])); } catch (Exception e) { logger.Error(e, "Bad result for ECHO {Index}: {result} ", index, result); } } catch (Exception ex) { if (!found && (ex is ResourceNotFoundException)) { logger.Debug("Waiting for module to connect..."); continue; // Initial startup ... } logger.Information(ex, "Failed to send ECHO {Index}.", index); } } logger.Information("Echo thread completed"); }
/// <summary> /// Stop publishing /// </summary> private static async Task StopPublishAsync(IIoTHubConfig config, ILogger logger, string deviceId, string moduleId, CancellationToken ct) { var client = new IoTHubTwinMethodClient(CreateClient(config, logger), logger); while (!ct.IsCancellationRequested) { try { logger.Information("Stop publishing..."); var result = await client.CallMethodAsync(deviceId, moduleId, "StopPublish_V1", "", null, ct); logger.Information("... stopped"); break; } catch (Exception ex) { logger.Verbose(ex, "Failed to stop publishing"); } } }