First code for live profit
This commit is contained in:
@@ -64,6 +64,7 @@
|
||||
<Compile Include="FrontendServices.cs" />
|
||||
<Compile Include="FrontendLoop.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SocketServerExtensionMethods.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
|
||||
+57
-38
@@ -9,11 +9,14 @@ using Mono.Unix;
|
||||
using Mono.Unix.Native;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Swarmops.Common.Enums;
|
||||
using Swarmops.Logic.Communications;
|
||||
using Swarmops.Logic.Communications.Payload;
|
||||
using Swarmops.Common.ExtensionMethods;
|
||||
using Swarmops.Logic.Security;
|
||||
using Swarmops.Logic.Structure;
|
||||
using Swarmops.Logic.Support;
|
||||
using Swarmops.Logic.Financial;
|
||||
using WebSocketSharp;
|
||||
using WebSocketSharp.Server;
|
||||
using ErrorEventArgs = WebSocketSharp.ErrorEventArgs;
|
||||
@@ -213,6 +216,18 @@ namespace Swarmops.Frontend.Socket
|
||||
{
|
||||
SystemSettings.HeartbeatFrontend = DateTime.UtcNow.ToUnix();
|
||||
|
||||
// Check for backend heartbeat
|
||||
|
||||
if (_lastBackendHeartBeat.AddSeconds(5) < DateTime.UtcNow)
|
||||
{
|
||||
RaiseAlarm(FrontendMalfunctions.BackendHeartbeatLost);
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearAlarm(FrontendMalfunctions.BackendHeartbeatLost);
|
||||
}
|
||||
|
||||
|
||||
if (_isSandbox)
|
||||
{
|
||||
_sandboxDummy1 += new Random().Next(10) - 3;
|
||||
@@ -224,42 +239,6 @@ namespace Swarmops.Frontend.Socket
|
||||
data1["Profit"] = _sandboxDummy2.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
_socketServer.WebSocketServices.Broadcast(data1.ToString());
|
||||
|
||||
// Check for backend heartbeat
|
||||
|
||||
if (_lastBackendHeartBeat.AddSeconds(5) < DateTime.UtcNow)
|
||||
{
|
||||
RaiseAlarm(FrontendMalfunctions.BackendHeartbeatLost);
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearAlarm(FrontendMalfunctions.BackendHeartbeatLost);
|
||||
}
|
||||
|
||||
/*
|
||||
JArray malfunctionsArray = new JArray();
|
||||
for (int loop = 0; loop < _malfunctionTestCounter; loop++)
|
||||
{
|
||||
if (loop < _malfunctionTestCounter - 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
JObject malfunctionObject = new JObject();
|
||||
malfunctionObject["Id"] = "Mal" + loop.ToString();
|
||||
malfunctionObject["Text"] = malfunctionStrings[loop] + " (Test code, ignore - and let's make this a multiline point to test flow)";
|
||||
malfunctionsArray.Add(malfunctionObject);
|
||||
}
|
||||
|
||||
if (++_malfunctionTestCounter > 4)
|
||||
{
|
||||
_malfunctionTestCounter = 0;
|
||||
}
|
||||
|
||||
JObject data2 = new JObject();
|
||||
data2["MessageType"] = "Malfunctions";
|
||||
data2["MalfunctionsList"] = malfunctionsArray;
|
||||
_socketServer.WebSocketServices.Broadcast(data2.ToString());*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,8 +270,6 @@ namespace Swarmops.Frontend.Socket
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void OnBackendMessage(object sender, MessageEventArgs args)
|
||||
{
|
||||
JObject json = JObject.Parse(args.Data);
|
||||
@@ -312,6 +289,47 @@ namespace Swarmops.Frontend.Socket
|
||||
{
|
||||
_socketServer.WebSocketServices.Broadcast(args.Data); // send unfiltered to all sessions, for now
|
||||
}
|
||||
|
||||
if (messageType == "RecalculateOrganizationProfitLoss")
|
||||
{
|
||||
// An organization's annual profit changed - recalculate and broadcast to those who
|
||||
// are displaying it
|
||||
|
||||
int organizationId = Int32.Parse((string) json["OrganizationId"]);
|
||||
Organization organization = Organization.FromIdentity(organizationId);
|
||||
|
||||
RecalculateOrganizationProfitLoss(organization);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void RecalculateOrganizationProfitLoss(Organization organization)
|
||||
{
|
||||
Int64 annualProfitLossCents = GetOrganizationProfitLossCents(organization);
|
||||
|
||||
JObject json = new JObject();
|
||||
json["messageType"] = "AnnualProfitLossCents";
|
||||
json["profitLossCents"] = annualProfitLossCents.ToString();
|
||||
json["organizationId"] = organization.Identity;
|
||||
|
||||
BroadcastToOrganization(organization, json);
|
||||
}
|
||||
|
||||
|
||||
public static Int64 GetOrganizationProfitLossCents(Organization organization)
|
||||
{
|
||||
FinancialAccounts allPLAccounts = FinancialAccounts.ForOrganization(organization,
|
||||
FinancialAccountType.Result);
|
||||
DateTime thisYearStart = new DateTime(DateTime.UtcNow.Year, 1, 1);
|
||||
DateTime thisYearEnd = new DateTime(thisYearStart.Year + 1, 1, 1);
|
||||
|
||||
return allPLAccounts.GetDeltaCents(thisYearStart, thisYearEnd);
|
||||
}
|
||||
|
||||
|
||||
public static void BroadcastToOrganization(Organization organization, JObject message)
|
||||
{
|
||||
_socketServer.WebSocketServices.BroadcastToOrganization(organization, message);
|
||||
}
|
||||
|
||||
public static void OnBackendOpen(object sender, EventArgs args)
|
||||
@@ -397,6 +415,7 @@ namespace Swarmops.Frontend.Socket
|
||||
|
||||
private static Dictionary<FrontendMalfunctions,bool> _activeAlarms;
|
||||
|
||||
internal static WebSocketServer SocketServer { get { return _socketServer; } }
|
||||
|
||||
// --------------------------------- LEGACY CODE BELOW THIS MARK -------------------------------
|
||||
|
||||
|
||||
@@ -60,9 +60,18 @@ namespace Swarmops.Frontend.Socket
|
||||
|
||||
_authority = Authority.FromEncryptedXml (authBase64);
|
||||
|
||||
Console.WriteLine(" - - authenticated: " + _authority.Person.Canonical);
|
||||
Console.WriteLine(" - - authenticated: " + this._authority.Person.Canonical);
|
||||
|
||||
this.Sessions.RegisterSession(this, this._authority);
|
||||
|
||||
base.OnOpen();
|
||||
|
||||
JObject json = new JObject();
|
||||
json ["messageType"] = json["messageType"] = "AnnualProfitLossCents";
|
||||
json["profitLossCents"] = FrontendLoop.GetOrganizationProfitLossCents(this._authority.Organization).ToString();
|
||||
json["organizationId"] = this._authority.Organization.Identity;
|
||||
|
||||
this.Send(json.ToString());
|
||||
}
|
||||
|
||||
protected override void OnClose (CloseEventArgs e)
|
||||
@@ -70,9 +79,13 @@ namespace Swarmops.Frontend.Socket
|
||||
Console.WriteLine(" - client closed");
|
||||
base.OnClose (e);
|
||||
|
||||
this.Sessions.UnregisterSession(this);
|
||||
|
||||
// Sessions.Broadcast("{\"messageType\":\"EditorCount\"," + String.Format("\"editorCount\":\"{0}\"", Sessions.ActiveIDs.ToArray().Length) + '}');
|
||||
}
|
||||
|
||||
public Authority Authority { get { return this._authority; } }
|
||||
|
||||
private Authority _authority = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Swarmops.Logic.Security;
|
||||
using Swarmops.Logic.Structure;
|
||||
using WebSocketSharp;
|
||||
using WebSocketSharp.Server;
|
||||
|
||||
namespace Swarmops.Frontend.Socket
|
||||
{
|
||||
public static class SocketServerExtensionMethods
|
||||
{
|
||||
static SocketServerExtensionMethods()
|
||||
{
|
||||
_sessionAuthorityLookup = new Dictionary<string, Authority>();
|
||||
_sessionLookup = new Dictionary<string, IWebSocketSession>();
|
||||
}
|
||||
|
||||
public static void BroadcastToOrganization(this WebSocketSharp.Server.WebSocketServiceManager extendedObject,
|
||||
Organization organization, JObject jsonMessage)
|
||||
{
|
||||
lock (_sessionLookup)
|
||||
{
|
||||
foreach (string key in _sessionAuthorityLookup.Keys)
|
||||
{
|
||||
IWebSocketSession session = _sessionLookup[key];
|
||||
if (session.State == WebSocketState.Open) // protect against open/close race condition
|
||||
{
|
||||
if (_sessionAuthorityLookup.ContainsKey(key) &&
|
||||
_sessionAuthorityLookup[key].Organization.Identity == organization.Identity)
|
||||
{
|
||||
session.Context.WebSocket.Send(jsonMessage.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void RegisterSession(this WebSocketSessionManager extendedObject, IWebSocketSession session, Authority authority)
|
||||
{
|
||||
lock (_sessionLookup)
|
||||
{
|
||||
_sessionLookup[session.ID] = session;
|
||||
_sessionAuthorityLookup[session.ID] = authority;
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnregisterSession(this WebSocketSessionManager extendedObject, IWebSocketSession session)
|
||||
{
|
||||
lock (_sessionLookup)
|
||||
{
|
||||
string id = session.ID;
|
||||
if (_sessionLookup.ContainsKey(id))
|
||||
{
|
||||
_sessionLookup.Remove(id);
|
||||
}
|
||||
if (_sessionAuthorityLookup.ContainsKey(id))
|
||||
{
|
||||
_sessionLookup.Remove(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<string, Authority> _sessionAuthorityLookup;
|
||||
private static Dictionary<string, IWebSocketSession> _sessionLookup;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user