Skip to content

Commit

Permalink
Removed duplicate session cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
barnstee committed Aug 29, 2024
1 parent 5778f7e commit acc6df9
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 293 deletions.
75 changes: 11 additions & 64 deletions Controllers/BrowserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Opc.Ua.Cloud.Publisher.Controllers
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Opc.Ua.Cloud.Publisher;
using Opc.Ua.Cloud.Publisher.Interfaces;
using Opc.Ua.Cloud.Publisher.Models;
using System;
Expand All @@ -18,24 +17,21 @@ public class BrowserController : Controller
{
private readonly IUAApplication _app;
private readonly IUAClient _client;
private readonly OpcSessionHelper _helper;
private readonly ILogger _logger;

private SessionModel _session;

public BrowserController(OpcSessionHelper helper, IUAApplication app, IUAClient client, ILoggerFactory loggerFactory)
public BrowserController(IUAApplication app, IUAClient client, ILoggerFactory loggerFactory)
{
_app = app;
_client = client;
_helper = helper;
_logger = loggerFactory.CreateLogger("BrowserController");
_session = new();
}

[HttpPost]
public IActionResult DownloadUACert()
{
_session.SessionId = HttpContext.Session.Id;
_session.EndpointUrl = HttpContext.Session.GetString("EndpointUrl");

try
Expand All @@ -52,13 +48,10 @@ public IActionResult DownloadUACert()
[HttpGet]
public ActionResult Index()
{
_session.SessionId = HttpContext.Session.Id;
_session.EndpointUrl = HttpContext.Session.GetString("EndpointUrl");

OpcSessionCacheData entry = null;
if (_helper.OpcSessionCache.TryGetValue(_session.SessionId, out entry))
if (!string.IsNullOrEmpty(_session.EndpointUrl))
{
_session.EndpointUrl = entry.EndpointURL;
return View("Browse", _session);
}

Expand All @@ -68,7 +61,6 @@ public ActionResult Index()
[HttpPost]
public ActionResult UserPassword(string endpointUrl)
{
_session.SessionId = HttpContext.Session.Id;
_session.EndpointUrl = endpointUrl;

HttpContext.Session.SetString("EndpointUrl", endpointUrl);
Expand All @@ -77,70 +69,36 @@ public ActionResult UserPassword(string endpointUrl)
}

[HttpPost]
public async Task<ActionResult> ConnectAsync(string username, string password)
public ActionResult ConnectAsync(string username, string password)
{
_session.SessionId = HttpContext.Session.Id;
_session.EndpointUrl = HttpContext.Session.GetString("EndpointUrl");

_session.UserName = username;
_session.Password = password;

Client.Session session = null;
try
{
session = await _helper.GetSessionAsync(_session.SessionId, _session.EndpointUrl, _session.UserName, _session.Password).ConfigureAwait(false);
}
catch (Exception ex)
{
_session.StatusMessage = ex.Message;
return View("Index", _session);
}

if (session == null)
{
_session.StatusMessage = "Unable to create session!";
return View("Index", _session);
}
else
{
_session.StatusMessage = "Connected to: " + _session.EndpointUrl;
return View("Browse", _session);
}
_session.StatusMessage = "Connected to: " + _session.EndpointUrl;

return View("Browse", _session);
}

[HttpPost]
public ActionResult Disconnect()
{
_session.SessionId = HttpContext.Session.Id;
_session.EndpointUrl = HttpContext.Session.GetString("EndpointUrl");

try
{
_helper.Disconnect(_session.SessionId);
}
catch (Exception)
{
// do nothing
}

return View("Index", _session);
}

[HttpPost]
public async Task<ActionResult> GeneratePN()
{
_session.SessionId = HttpContext.Session.Id;
_session.EndpointUrl = HttpContext.Session.GetString("EndpointUrl");

try
{
Client.Session session = await _helper.GetSessionAsync(_session.SessionId, _session.EndpointUrl, _session.UserName, _session.Password).ConfigureAwait(false);

List<UANodeInformation> results = await _client.BrowseVariableNodesResursivelyAsync(session, null).ConfigureAwait(false);
List<UANodeInformation> results = await _client.BrowseVariableNodesResursivelyAsync(_session.EndpointUrl, _session.UserName, _session.Password, null).ConfigureAwait(false);

PublishNodesInterfaceModel model = new()
{
EndpointUrl = session.Endpoint.EndpointUrl,
EndpointUrl = _session.EndpointUrl,
OpcNodes = new List<VariableModel>()
};

Expand Down Expand Up @@ -175,14 +133,11 @@ public async Task<ActionResult> GeneratePN()
[HttpPost]
public async Task<ActionResult> GenerateCSV()
{
_session.SessionId = HttpContext.Session.Id;
_session.EndpointUrl = HttpContext.Session.GetString("EndpointUrl");

try
{
Client.Session session = await _helper.GetSessionAsync(_session.SessionId, _session.EndpointUrl, _session.UserName, _session.Password).ConfigureAwait(false);

List<UANodeInformation> results = await _client.BrowseVariableNodesResursivelyAsync(session, null).ConfigureAwait(false);
List<UANodeInformation> results = await _client.BrowseVariableNodesResursivelyAsync(_session.EndpointUrl, _session.UserName, _session.Password, null).ConfigureAwait(false);

string content = "Endpoint,ApplicationUri,ExpandedNodeId,DisplayName,Type,VariableCurrentValue,VariableType,Parent,References\r\n";
foreach (UANodeInformation nodeInfo in results)
Expand Down Expand Up @@ -221,20 +176,12 @@ public async Task<ActionResult> GenerateCSV()
[HttpPost]
public async Task<ActionResult> PushCert()
{
_session.SessionId = HttpContext.Session.Id;
_session.EndpointUrl = HttpContext.Session.GetString("EndpointUrl");

try
{
OpcSessionCacheData entry;
if (_helper.OpcSessionCache.TryGetValue(_session.SessionId, out entry))
{
if (entry.OPCSession != null)
{
await _client.GDSServerPush(_session.EndpointUrl, entry.Username, entry.Password).ConfigureAwait(false);
}
}

await _client.GDSServerPush(_session.EndpointUrl, _session.UserName, _session.Password).ConfigureAwait(false);

_session.StatusMessage = "New certificate and trust list pushed successfully to server!";
}
catch (Exception ex)
Expand Down
11 changes: 7 additions & 4 deletions Interfaces/IUAClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

namespace Opc.Ua.Cloud.Publisher.Interfaces
{
using Opc.Ua.Client;
using Opc.Ua.Cloud.Publisher.Models;
using System;
using System.Collections.Generic;
Expand All @@ -10,6 +9,12 @@ namespace Opc.Ua.Cloud.Publisher.Interfaces

public interface IUAClient : IDisposable
{
Task<ReferenceDescriptionCollection> Browse(string endpointUrl, string username, string password, BrowseDescription nodeToBrowse, bool throwOnError);

Task<List<UANodeInformation>> BrowseVariableNodesResursivelyAsync(string endpointUrl, string username, string password, NodeId nodeId);

string ReadNode(string endpointUrl, string username, string password, ref string nodeId);

Task<string> PublishNodeAsync(NodePublishingModel nodeToPublish, CancellationToken cancellationToken = default);

void UnpublishNode(NodePublishingModel nodeToUnpublish);
Expand All @@ -19,9 +24,7 @@ public interface IUAClient : IDisposable
IEnumerable<PublishNodesInterfaceModel> GetPublishedNodes();

Task GDSServerPush(string endpointURL, string adminUsername, string adminPassword);

Task<List<UANodeInformation>> BrowseVariableNodesResursivelyAsync(Session session, NodeId nodeId);


Task WoTConUpload(string endpoint, string username, string password, byte[] bytes, string assetName);
}
}
2 changes: 0 additions & 2 deletions Models/SessionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ namespace Opc.Ua.Cloud.Publisher.Models
{
public class SessionModel
{
public string SessionId { get; set; }

public string EndpointUrl { get; set; }

public string UserName { get; set; }
Expand Down
168 changes: 0 additions & 168 deletions OpcSessionHelper.cs

This file was deleted.

Loading

0 comments on commit acc6df9

Please sign in to comment.