Skip to content

Commit

Permalink
TouchPortalClient now calls event handler's OnClosedEvent() _before_ …
Browse files Browse the repository at this point in the history
…closing the socket, allowing plugin to send any final data as needed; Remove macro hack and implement try/catch around the SendCommand() calls to avoid duplicate validation; Log less verbosely on Info level.
  • Loading branch information
mpaperno committed Apr 18, 2022
1 parent dfcdd28 commit c4691d6
Showing 1 changed file with 59 additions and 69 deletions.
128 changes: 59 additions & 69 deletions TouchPortalSDK/Clients/TouchPortalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ bool ITouchPortalClient.Connect()
//Listen:
// set up message processing queue and task
_incommingMessages.Clear();
_logger?.LogInformation("Starting message processing queue task.");
_logger?.LogDebug("Starting message processing queue task.");
_incomingMessageTask = Task.Run(MessageHandlerTask);
_incomingMessageTask.ConfigureAwait(false);
// start socket reader thread
_logger?.LogInformation("Start Socket listener.");
_logger?.LogDebug("Start Socket listener.");
var listening = _touchPortalSocket.Listen();
if (!listening)
return false;
_logger?.LogInformation("Listener created.");
_logger?.LogDebug("Listener created.");

//Pair:
_logger?.LogInformation("Sending pair message.");
Expand All @@ -99,10 +99,10 @@ private void Close(string message, Exception exception = default)
{
_logger?.LogInformation(exception, $"Closing TouchPortal Plugin: '{message}'");

_touchPortalSocket?.CloseSocket();

_eventHandler.OnClosedEvent(message);

_touchPortalSocket?.CloseSocket();

_cts.Cancel();
if (_incomingMessageTask.Status == TaskStatus.Running && !_incomingMessageTask.Wait(2000))
_logger?.LogWarning("The incoming message processor task is hung!");
Expand All @@ -117,119 +117,109 @@ private void Close(string message, Exception exception = default)
/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.SettingUpdate(string name, string value)
{
if (string.IsNullOrWhiteSpace(name))
try {
return SendCommand(new SettingUpdateCommand(name, value));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "SettingUpdateCommand() validation failed.");
return false;

var command = new SettingUpdateCommand(name, value);

return SendCommand(command);
}
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.CreateState(string stateId, string desc, string defaultValue)
{
if (string.IsNullOrWhiteSpace(stateId) ||
string.IsNullOrWhiteSpace(desc))
try {
return SendCommand(new CreateStateCommand(stateId, desc, defaultValue));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "CreateStateCommand() validation failed.");
return false;

var command = new CreateStateCommand(stateId, desc, defaultValue);

return SendCommand(command);
}
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.RemoveState(string stateId)
{
if (string.IsNullOrWhiteSpace(stateId))
try {
return SendCommand(new RemoveStateCommand(stateId));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "RemoveStateCommand() validation failed.");
return false;

var command = new RemoveStateCommand(stateId);

return SendCommand(command);
}
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.StateUpdate(string stateId, string value)
{
if (string.IsNullOrWhiteSpace(stateId))
try {
return SendCommand(new StateUpdateCommand(stateId, value));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "StateUpdateCommand() validation failed.");
return false;

var command = new StateUpdateCommand(stateId, value);

return SendCommand(command);
}
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.ChoiceUpdate(string choiceId, string[] values, string instanceId)
{
if (string.IsNullOrWhiteSpace(choiceId))
try {
return SendCommand(new ChoiceUpdateCommand(choiceId, values, instanceId));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "ChoiceUpdateCommand() validation failed.");
return false;

var command = new ChoiceUpdateCommand(choiceId, values, instanceId);

return SendCommand(command);
}
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.UpdateActionData(string dataId, double minValue, double maxValue, ActionDataType dataType, string instanceId)
{
if (string.IsNullOrWhiteSpace(dataId))
try {
return SendCommand(new UpdateActionDataCommand(dataId, minValue, maxValue, dataType, instanceId));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "UpdateActionDataCommand() validation failed.");
return false;

var command = new UpdateActionDataCommand(dataId, minValue, maxValue, dataType, instanceId);

return SendCommand(command);
}
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.ShowNotification(string notificationId, string title, string message, NotificationOptions[] notificationOptions)
{
if (string.IsNullOrWhiteSpace(notificationId))
return false;

if (string.IsNullOrWhiteSpace(title))
return false;

if (string.IsNullOrWhiteSpace(message))
return false;

if (notificationOptions is null || notificationOptions.Length == 0)
try {
return SendCommand(new ShowNotificationCommand(notificationId, title, message, notificationOptions));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "ShowNotificationCommand() validation failed.");
return false;

var command = new ShowNotificationCommand(notificationId, title, message, notificationOptions);

return SendCommand(command);
}
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.ConnectorUpdate(string connectorId, int value)
{
if (string.IsNullOrWhiteSpace(connectorId))
return false;

if (value < 0 || value > 100)
return false;

var command = new ConnectorUpdateCommand(_eventHandler.PluginId, connectorId, value);

if (command.ConnectorId.Length > 200)
try {
return SendCommand(new ConnectorUpdateCommand(_eventHandler.PluginId, connectorId, value));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "ConnectorUpdateCommand() validation failed.");
return false;

return SendCommand(command);
}
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.ConnectorUpdateShort(string shortId, int value)
{
if (string.IsNullOrWhiteSpace(shortId))
return false;

if (value < 0 || value > 100)
try {
return SendCommand(new ConnectorUpdateShortCommand(_eventHandler.PluginId, shortId, value));
}
catch (ArgumentException e) {
_logger?.LogWarning(e, "ConnectorUpdateShortCommand() validation failed.");
return false;

var command = new ConnectorUpdateShortCommand(_eventHandler.PluginId, shortId, value);

return SendCommand(command);
}
}

public bool SendCommand<TCommand>(TCommand command, [CallerMemberName]string callerMemberName = "")
Expand Down

0 comments on commit c4691d6

Please sign in to comment.