Skip to content
This repository was archived by the owner on Jul 29, 2021. It is now read-only.

Commit dfe4670

Browse files
Merge pull request #93 from Azure/develop_hans_noterminal
changes for V2.1.2
2 parents 1e675e6 + 945c6e7 commit dfe4670

File tree

5 files changed

+70
-28
lines changed

5 files changed

+70
-28
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ The complete usage of the application can be shown using the `--help` command li
140140
--vc, --verboseconsole=VALUE
141141
the output of publisher is shown on the console.
142142
Default: False
143+
--ns, --noshutdown=VALUE
144+
publisher could not be stopped by pressing a key
145+
on the console, but will run forever.
146+
Default: False
143147
--ih, --iothubprotocol=VALUE
144148
the protocol to use for communication with Azure
145149
IoTHub (allowed values: Amqp, Http1, Amqp_

src/Diagnostics.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public static async Task ShowDiagnosticsInfoAsync(CancellationToken ct)
6161
{
6262
while (true)
6363
{
64-
try
64+
if (ct.IsCancellationRequested)
6565
{
66+
return;
67+
}
6668

69+
try
70+
{
6771
await Task.Delay((int)_diagnosticsInterval * 1000, ct);
68-
if (ct.IsCancellationRequested)
69-
{
70-
return;
71-
}
7272

7373
Trace("==========================================================================");
7474
Trace($"OpcPublisher status @ {System.DateTime.UtcNow} (started @ {PublisherStartTime})");

src/IotHubMessaging.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private async Task MonitoredItemsProcessor(CancellationToken ct)
313313
// if we are in shutdown do not wait, else wait infinite if send interval is not set
314314
millisecondsTillNextSend = ct.IsCancellationRequested ? 0 : Timeout.Infinite;
315315
}
316-
bool gotItem = _monitoredItemsDataQueue.TryTake(out jsonMessage, (int)millisecondsTillNextSend);
316+
bool gotItem = _monitoredItemsDataQueue.TryTake(out jsonMessage, (int)millisecondsTillNextSend, ct);
317317

318318
// the two commandline parameter --ms (message size) and --si (send interval) control when data is sent to IoTHub
319319
// pls see detailed comments on performance and memory consumption at https://github.com/Azure/iot-edge-opc-publisher
@@ -426,7 +426,11 @@ private async Task MonitoredItemsProcessor(CancellationToken ct)
426426
}
427427
catch (Exception e)
428428
{
429-
Trace(e, "Error while processing monitored item messages.");
429+
if (!(e is OperationCanceledException))
430+
{
431+
432+
Trace(e, "Error while processing monitored item messages.");
433+
}
430434
}
431435
}
432436
}

src/Program.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public async static Task MainAsync(string[] args)
8989
{ "di|diagnosticsinterval=", $"shows publisher diagnostic info at the specified interval in seconds. 0 disables diagnostic output.\nDefault: {DiagnosticsInterval}", (uint u) => DiagnosticsInterval = u },
9090

9191
{ "vc|verboseconsole=", $"the output of publisher is shown on the console.\nDefault: {VerboseConsole}", (bool b) => VerboseConsole = b },
92+
93+
{ "ns|noshutdown=", $"publisher could not be stopped by pressing a key on the console, but will run forever.\nDefault: {_noShutdown}", (bool b) => _noShutdown = b },
9294

9395
// IoTHub specific options
9496
{ "ih|iothubprotocol=", $"the protocol to use for communication with Azure IoTHub (allowed values: {string.Join(", ", Enum.GetNames(IotHubProtocol.GetType()))}).\nDefault: {Enum.GetName(IotHubProtocol.GetType(), IotHubProtocol)}",
@@ -432,11 +434,29 @@ public async static Task MainAsync(string[] args)
432434
// stop on user request
433435
WriteLine("");
434436
WriteLine("");
435-
WriteLine("Publisher is running. Press ENTER to quit.");
437+
if (_noShutdown)
438+
{
439+
// wait forever if asked to do so
440+
WriteLine("Publisher is running infinite...");
441+
await Task.Delay(Timeout.Infinite);
442+
}
443+
else
444+
{
445+
WriteLine("Publisher is running. Press any key to quit.");
446+
try
447+
{
448+
ReadKey(true);
449+
}
450+
catch
451+
{
452+
// wait forever if there is no console
453+
WriteLine("There is no console. Publisher is running infinite...");
454+
await Task.Delay(Timeout.Infinite);
455+
}
456+
}
436457
WriteLine("");
437458
WriteLine("");
438-
ReadLine();
439-
ShutdownTokenSource.Cancel();
459+
ShutdownTokenSource.Cancel();
440460
WriteLine("Publisher is shutting down...");
441461

442462
// Wait for session connector completion
@@ -507,7 +527,11 @@ public static async Task SessionConnectorAsync(CancellationToken ct)
507527
{
508528
Trace(e, $"Failed to connect and monitor a disconnected server. {(e.InnerException != null ? e.InnerException.Message : "")}");
509529
}
510-
await Task.Delay(_publisherSessionConnectWaitSec * 1000, ct);
530+
try
531+
{
532+
await Task.Delay(_publisherSessionConnectWaitSec * 1000, ct);
533+
}
534+
catch { }
511535
if (ct.IsCancellationRequested)
512536
{
513537
return;
@@ -645,5 +669,6 @@ private static void PrintSessionStatus(Session session, string reason)
645669
private static PublisherServer _publisherServer;
646670
private static bool _opcTraceInitialized = false;
647671
private static int _publisherSessionConnectWaitSec = 10;
672+
private static bool _noShutdown = false;
648673
}
649674
}

src/PublisherNodeConfiguration.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,31 +147,40 @@ public static async Task<bool> ReadConfigAsync()
147147
Trace("Publishing node configuration file path read from environment.");
148148
_publisherNodeConfigurationFilename = Environment.GetEnvironmentVariable("_GW_PNFP");
149149
}
150+
Trace($"The name of the configuration file for published nodes is: {_publisherNodeConfigurationFilename}");
150151

151-
Trace($"Attempting to load nodes file from: {_publisherNodeConfigurationFilename}");
152-
_configurationFileEntries = JsonConvert.DeserializeObject<List<PublisherConfigurationFileEntry>>(File.ReadAllText(_publisherNodeConfigurationFilename));
153-
Trace($"Loaded {_configurationFileEntries.Count} config file entry/entries.");
154-
155-
foreach (var publisherConfigFileEntry in _configurationFileEntries)
152+
// if the file exists, read it, if not just continue
153+
if (File.Exists(_publisherNodeConfigurationFilename))
156154
{
157-
if (publisherConfigFileEntry.NodeId == null)
155+
Trace($"Attemtping to load node configuration from: {_publisherNodeConfigurationFilename}");
156+
_configurationFileEntries = JsonConvert.DeserializeObject<List<PublisherConfigurationFileEntry>>(File.ReadAllText(_publisherNodeConfigurationFilename));
157+
Trace($"Loaded {_configurationFileEntries.Count} config file entry/entries.");
158+
159+
foreach (var publisherConfigFileEntry in _configurationFileEntries)
158160
{
159-
// new node configuration syntax.
160-
foreach (var opcNode in publisherConfigFileEntry.OpcNodes)
161+
if (publisherConfigFileEntry.NodeId == null)
161162
{
162-
ExpandedNodeId expandedNodeId = ExpandedNodeId.Parse(opcNode.ExpandedNodeId);
163-
_nodePublishingConfiguration.Add(new NodePublishingConfiguration(expandedNodeId, publisherConfigFileEntry.EndpointUri, opcNode.OpcSamplingInterval ?? OpcSamplingInterval, opcNode.OpcPublishingInterval ?? OpcPublishingInterval));
163+
// new node configuration syntax.
164+
foreach (var opcNode in publisherConfigFileEntry.OpcNodes)
165+
{
166+
ExpandedNodeId expandedNodeId = ExpandedNodeId.Parse(opcNode.ExpandedNodeId);
167+
_nodePublishingConfiguration.Add(new NodePublishingConfiguration(expandedNodeId, publisherConfigFileEntry.EndpointUri, opcNode.OpcSamplingInterval ?? OpcSamplingInterval, opcNode.OpcPublishingInterval ?? OpcPublishingInterval));
168+
}
164169
}
165-
}
166-
else
167-
{
168-
// NodeId (ns=) format node configuration syntax using default sampling and publishing interval.
169-
_nodePublishingConfiguration.Add(new NodePublishingConfiguration(publisherConfigFileEntry.NodeId, publisherConfigFileEntry.EndpointUri, OpcSamplingInterval, OpcPublishingInterval));
170-
// give user a warning that the syntax is obsolete
171-
Trace($"Please update the syntax of the configuration file and use ExpandedNodeId instead of NodeId property name for node with identifier '{publisherConfigFileEntry.NodeId.ToString()}' on EndpointUrl '{publisherConfigFileEntry.EndpointUri.AbsoluteUri}'.");
170+
else
171+
{
172+
// NodeId (ns=) format node configuration syntax using default sampling and publishing interval.
173+
_nodePublishingConfiguration.Add(new NodePublishingConfiguration(publisherConfigFileEntry.NodeId, publisherConfigFileEntry.EndpointUri, OpcSamplingInterval, OpcPublishingInterval));
174+
// give user a warning that the syntax is obsolete
175+
Trace($"Please update the syntax of the configuration file and use ExpandedNodeId instead of NodeId property name for node with identifier '{publisherConfigFileEntry.NodeId.ToString()}' on EndpointUrl '{publisherConfigFileEntry.EndpointUri.AbsoluteUri}'.");
172176

177+
}
173178
}
174179
}
180+
else
181+
{
182+
Trace($"The node configuration file '{_publisherNodeConfigurationFilename}' does not exist. Starting up and wait for remote configuration requests.");
183+
}
175184
}
176185
catch (Exception e)
177186
{

0 commit comments

Comments
 (0)