Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EDP - Added functionality missing in Hajk3 from Hajk2 #861

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions backend/EdpConn/EdpConn.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// #define USE_REF_TO_EDP // Uncomment to enable EDP integration
//#define USE_REF_TO_EDP // Uncomment to enable EDP integration

using System;
using System.Collections.Generic;
using log4net;

#if USE_REF_TO_EDP
using EDP.GIS.Kubb.Connector.EDP;
Expand All @@ -11,9 +12,11 @@

/// <summary>
/// Denna klass implementerar kommunikationen med EDP.
/// Vill man kompilera backend utan några beroenden alls till EDP gör följande:
/// 1. Kommentera raden #define USE_REF_TO_EDP överst i denna fil
/// 2. Man kan nu även ta bort referenserna till:
/// Vill man kompilera backend med kommuniaktion till EDP gör följande:
/// 1. Ta bort kommentarstecknen på raden //#define USE_REF_TO_EDP överst i denna fil
///
/// Skulle man ha problem med att NuGet inte kan hämta referenserna, trots att man har kommenterat översta raden,
/// kan man ta bort referenserna till:
/// - EDP.GIS.Kubb.Connector.Common
/// - EDP.GIS.Kubb.Connector.EDP
/// - Microsoft.AspNet.SignalR.Client
Expand All @@ -40,6 +43,10 @@ public ImplEdpConnectorPublic(string user, string organisation, string client, s
{
_implEdpConnector = new ImplEdpConnector(user, organisation, client, serverUrl);
}
public void Disconnect()
{
_implEdpConnector.Disconnect();
}

public void SetRealEstateIdentifiersToSend(List<RealEstateIdentifierPublic> newList)
{
Expand All @@ -62,12 +69,21 @@ public void SetRealEstateIdentifiersToSend(List<RealEstateIdentifierPublic> newL

class ImplEdpConnector : EdpConnector
{
ILog _log = LogManager.GetLogger(typeof(ImplEdpConnector));
string _user;

public ImplEdpConnector(string user, string organisation, string client, string serverUrl)
: base(new HubConnectionFactory(), new HubProxyFactory(), user, organisation, client, serverUrl)
{
_user = user;
OpenConnection();
}

public void Disconnect()
{
this.HubConnection.Stop();
}

public List<RealEstateIdentifier> RealEstateIdentifiersToSend = null;

// public List<Coordinate> CoordinatesToSend = null; // not implemented
Expand All @@ -90,6 +106,7 @@ public override void HandleAskingForCoordinates()

public override void HandleAskingForRealEstateIdentifiers()
{
_log.DebugFormat("ImplEdpConnector.HandleAskingForRealEstateIdentifiers called for user '{0}'.", _user);
if (RealEstateIdentifiersToSend != null)
SendRealEstateIdentifiers(RealEstateIdentifiersToSend);
}
Expand All @@ -107,6 +124,8 @@ public ImplEdpConnectorPublic(string user, string organisation, string client, s
{
}

public void Disconnect() { }

public void SetRealEstateIdentifiersToSend(List<RealEstateIdentifierPublic> newList)
{
}
Expand Down
5 changes: 5 additions & 0 deletions backend/EdpConn/EdpConn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@
<Reference Include="EDP.GIS.Kubb.Connector.EDP, Version=0.8.1.16, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\EDP.GIS.Kubb.Connector.Edp.0.8.1\lib\net45\EDP.GIS.Kubb.Connector.EDP.dll</HintPath>
</Reference>
<Reference Include="log4net, Version=2.0.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.11\lib\net45\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.SignalR.Client, Version=2.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.SignalR.Client.2.2.2\lib\net45\Microsoft.AspNet.SignalR.Client.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
1 change: 1 addition & 0 deletions backend/EdpConn/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EDP.GIS.Kubb.Connector.Edp" version="0.8.1" targetFramework="net452" />
<package id="log4net" version="2.0.11" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.Client" version="2.2.2" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
</packages>
62 changes: 35 additions & 27 deletions backend/mapservice/Controllers/EdpController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ namespace MapService.Controllers
public class EdpController : Controller
{
ILog _log = LogManager.GetLogger(typeof(EdpController));
static private Dictionary<string, ImplEdpConnectorPublic> _dictEdpConnections = new Dictionary<string, ImplEdpConnectorPublic>();
static private Dictionary<string, ImplEdpConnectorPublic> _dictEdpConnection = new Dictionary<string, ImplEdpConnectorPublic>();

public static void CloseEdpConnections()
{
ILog _log = LogManager.GetLogger(typeof(EdpController));
foreach (KeyValuePair<string, ImplEdpConnectorPublic> entry in _dictEdpConnection)
{
_log.DebugFormat("EdpController: Disconnecting user: {0}", entry.Key);
entry.Value.Disconnect();
}
_dictEdpConnection.Clear();
}

// json = [{"Fnr":"130121047","Fastbet":"BLÅKLINTEN 1"},{"Fnr":"130125494","Fastbet":"GETAKÄRR 4:1"},{"Fnr":"130127043","Fastbet":"GULMÅRAN 1"},{"Fnr":"130125494","Fastbet":"GETAKÄRR 4:1"},{"Fnr":"130125494","Fastbet":"GETAKÄRR 4:1"}]
[HttpPost]
Expand Down Expand Up @@ -67,39 +78,37 @@ public ActionResult SendRealEstateIdentifiers(string json)
userName = userName.Split('\\')[1];
}

// Save real estate identifiers for this user
if (_dictEdpConnections.ContainsKey(userName))
// Make sure we reconnect each time the user sends data to Edp
ImplEdpConnectorPublic edpCon = null;
if (_dictEdpConnection.TryGetValue(userName, out edpCon))
{
_log.DebugFormat("Found user '{0}' in queue.", userName);
_log.DebugFormat("Found user '{0}' in cache, disconnecting...", userName);

var edpCon = _dictEdpConnections[userName];
edpCon.SetRealEstateIdentifiersToSend(realEstateIdentifiersToSend);
edpCon.Disconnect();
_dictEdpConnection.Remove(userName);
}
else
{
_log.DebugFormat("Adding new user '{0}' to queue.", userName);

var edpUUID = ConfigurationManager.AppSettings["edpUUID"];
var edpClientName = ConfigurationManager.AppSettings["edpClientName"];
var edpServerUrl = ConfigurationManager.AppSettings["edpServerUrl"];
_log.DebugFormat("Adding user '{0}' to cache.", userName);

if (string.IsNullOrEmpty(edpUUID) || string.IsNullOrEmpty(edpClientName) || string.IsNullOrEmpty(edpServerUrl))
{
_log.Error("EDP is not configured in Web.config.");
}
else
{
_log.DebugFormat("Using config params, edpUUID: {0}, edpClientName: {1}, edpServerUrl: {2}", edpUUID, edpClientName, edpServerUrl);
var edpUUID = ConfigurationManager.AppSettings["edpUUID"];
var edpClientName = ConfigurationManager.AppSettings["edpClientName"];
var edpServerUrl = ConfigurationManager.AppSettings["edpServerUrl"];

if (string.IsNullOrEmpty(edpUUID) || string.IsNullOrEmpty(edpClientName) || string.IsNullOrEmpty(edpServerUrl))
{
_log.Error("EDP is not configured in Web.config.");
}
else
{
_log.DebugFormat("Using config params, edpUUID: {0}, edpClientName: {1}, edpServerUrl: {2}", edpUUID, edpClientName, edpServerUrl);

var edpCon = new ImplEdpConnectorPublic(userName, edpUUID, edpClientName, edpServerUrl);
edpCon = new ImplEdpConnectorPublic(userName, edpUUID, edpClientName, edpServerUrl);

edpCon.SetRealEstateIdentifiersToSend(realEstateIdentifiersToSend);
_dictEdpConnections.Add(userName, edpCon);
}
edpCon.SetRealEstateIdentifiersToSend(realEstateIdentifiersToSend);
_dictEdpConnection.Add(userName, edpCon);
}

var res = new HttpStatusCodeResult(HttpStatusCode.OK);
return res;
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
catch (Exception e)
{
Expand All @@ -117,8 +126,7 @@ public ActionResult SendCoordinates(string json)

// DO nothing in this version

var res = new HttpStatusCodeResult(HttpStatusCode.OK);
return res;
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
catch (Exception e)
{
Expand Down
5 changes: 5 additions & 0 deletions backend/mapservice/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ protected void Application_Start()
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

protected void Application_End()
{
Controllers.EdpController.CloseEdpConnections(); // This method does nothing if EDP integration is not used!
}
}
}