Skip to content

Commit

Permalink
change how the data is transferred from backend to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
tmathura committed Aug 31, 2020
1 parent eced6a8 commit c7352b1
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Spacearr.Common.ComputerDrive.Interfaces;
using Spacearr.Common.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Spacearr.Common.Command.Implementations.Commands
Expand All @@ -20,28 +21,26 @@ public GetComputerDrivesCommand(IComputerDrives computerDrives)
/// Returns all the computer hard disks.
/// </summary>
/// <returns>Returns a list of ComputerDriveModels serialized as Json</returns>
public async Task<string> Execute()
public async Task<List<string>> Execute()
{
var computerDrives = new List<ComputerDriveModel>();
var commandData = new List<string>();

if (_computerDrives.GetComputerDrives().Count > 0)
{
foreach (var computerDrive in _computerDrives.GetComputerDrives())
commandData.AddRange(_computerDrives.GetComputerDrives().Select(computerDrive => JsonConvert.SerializeObject(new ComputerDriveModel
{
computerDrives.Add(new ComputerDriveModel
{
Name = computerDrive.Name,
RootDirectory = computerDrive.RootDirectory,
VolumeLabel = computerDrive.VolumeLabel,
DriveFormat = computerDrive.DriveFormat,
DriveType = computerDrive.DriveType,
IsReady = computerDrive.IsReady,
TotalFreeSpace = computerDrive.TotalFreeSpace,
TotalSize = computerDrive.TotalSize
});
}
Name = computerDrive.Name,
RootDirectory = computerDrive.RootDirectory,
VolumeLabel = computerDrive.VolumeLabel,
DriveFormat = computerDrive.DriveFormat,
DriveType = computerDrive.DriveType,
IsReady = computerDrive.IsReady,
TotalFreeSpace = computerDrive.TotalFreeSpace,
TotalSize = computerDrive.TotalSize
})));
}

return await Task.FromResult(JsonConvert.SerializeObject(computerDrives));
return await Task.FromResult(commandData);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;
using Spacearr.Common.Command.Interfaces;
using Spacearr.Common.Models;
using Spacearr.Common.Services.Interfaces;
Expand All @@ -22,15 +23,18 @@ public GetWorkerServiceVersionCommand(IFileService fileService)
/// Returns the Worker Service version.
/// </summary>
/// <returns>Returns a WorkerServiceVersionModel</returns>
public async Task<string> Execute()
public async Task<List<string>> Execute()
{
var commandData = new List<string>();
var parentDirectory = Directory.GetParent(_fileService.GetUpdateAppStorageFolderPath());
var currentAppPath = Path.GetFullPath(parentDirectory?.FullName ?? string.Empty);
var currentApp = Path.Combine(currentAppPath, $"{AppName}.dll");
var assemblyName = AssemblyName.GetAssemblyName(currentApp);
var workerServiceVersion = new WorkerServiceVersionModel { Version = assemblyName.Version};

return await Task.FromResult(JsonConvert.SerializeObject(workerServiceVersion));
var workerServiceVersion = new WorkerServiceVersionModel { Version = assemblyName.Version };
commandData.Add(JsonConvert.SerializeObject(workerServiceVersion));

return await Task.FromResult(commandData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public LowSpaceCommand(IConfiguration configuration, ILogger logger, IComputerDr
/// Returns all the computers hard disk that are low, the low value is determined by the _lowSpaceGBValue.
/// </summary>
/// <returns>string.Empty</returns>
public async Task<string> Execute()
public async Task<List<string>> Execute()
{
var notificationList = new List<string>();

Expand Down Expand Up @@ -69,7 +69,7 @@ public async Task<string> Execute()
}
}

return string.Empty;
return new List<string>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Spacearr.Common.Command.Interfaces;
using System.Collections.Generic;
using Spacearr.Common.Command.Interfaces;
using Spacearr.Common.Logger.Interfaces;
using Spacearr.Common.Models;
using System.Threading.Tasks;
Expand All @@ -20,7 +21,7 @@ public SaveFirebasePushNotificationTokenCommand(ILogger logger, FirebasePushNoti
/// Save the firebase push notification token.
/// </summary>
/// <returns>string.Empty</returns>
public async Task<string> Execute()
public async Task<List<string>> Execute()
{
var firebasePushNotificationDevice = await _logger.GetFirebasePushNotificationDeviceAsync(_firebasePushNotificationDevice.DeviceId);
if (firebasePushNotificationDevice == null)
Expand All @@ -33,7 +34,7 @@ public async Task<string> Execute()
await _logger.UpdateFirebasePushNotificationDeviceAsync(firebasePushNotificationDevice);
}

return string.Empty;
return new List<string>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Spacearr.Common.Logger.Interfaces;
using Spacearr.Common.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.ServiceProcess;
Expand Down Expand Up @@ -31,7 +32,7 @@ public UpdateCommand(ILogger logger, IUpdateService updateService, IDownloadServ
/// Update app.
/// </summary>
/// <returns>string.Empty</returns>
public async Task<string> Execute()
public async Task<List<string>> Execute()
{
var parentDirectory = Directory.GetParent(_fileService.GetUpdateAppStorageFolderPath()).Parent;
var currentAppPath = Path.GetFullPath(parentDirectory?.FullName ?? string.Empty);
Expand Down Expand Up @@ -68,7 +69,7 @@ public async Task<string> Execute()
await _logger.LogErrorAsync(ex.Message, ex.StackTrace);
}
}
return string.Empty;
return new List<string>();
}
}
}
5 changes: 3 additions & 2 deletions Spacearr.Common/Command/Implementations/Invoker.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Spacearr.Common.Command.Interfaces;
using System.Collections.Generic;
using Spacearr.Common.Command.Interfaces;
using System.Threading.Tasks;

namespace Spacearr.Common.Command.Implementations
{
public class Invoker : IInvoker
{
public Invoker() { }
public async Task<string> Invoke(ICommand command)
public async Task<List<string>> Invoke(ICommand command)
{
return await command.Execute();
}
Expand Down
5 changes: 3 additions & 2 deletions Spacearr.Common/Command/Interfaces/ICommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Spacearr.Common.Command.Interfaces
{
public interface ICommand
{
Task<string> Execute();
Task<List<string>> Execute();
}
}
5 changes: 3 additions & 2 deletions Spacearr.Common/Command/Interfaces/IInvoker.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Spacearr.Common.Command.Interfaces
{
public interface IInvoker
{
Task<string> Invoke(ICommand command);
Task<List<string>> Invoke(ICommand command);
}
}
11 changes: 8 additions & 3 deletions Spacearr.Pusher.API/IPusher.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Spacearr.Pusher.API
{
public interface IPusher
{
string ReturnData { get; }
TimeSpan TimeLimit { get; }
bool CommandCompleted { get; }
List<string> ReturnData { get; }

/// <summary>
/// Connect the get computer drives command receiver.
Expand Down Expand Up @@ -60,11 +64,12 @@ public interface IPusher
/// <param name="channelName">The channel name to connect to</param>
/// <param name="eventName">The event name to connect to</param>
/// <param name="message">The message to send</param>
/// <param name="isFinalMessage">The it is the final message to send</param>
/// <param name="appId">The Pusher app id</param>
/// <param name="key">The Pusher key</param>
/// <param name="secret">The Pusher secret</param>
/// <param name="cluster">The Pusher cluster</param>
/// <returns></returns>
Task SendMessage(string channelName, string eventName, string message, string appId, string key, string secret, string cluster);
Task SendMessage(string channelName, string eventName, string message, bool isFinalMessage, string appId, string key, string secret, string cluster);
}
}
1 change: 1 addition & 0 deletions Spacearr.Pusher.API/Models/PusherReceiveMessageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public class PusherReceiveMessageModel
{
public string Message { get; set; }
public bool IsFinalMessage { get; set; }
}
}
25 changes: 20 additions & 5 deletions Spacearr.Pusher.API/Pusher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Spacearr.Common.Logger.Interfaces;
using Spacearr.Pusher.API.Receivers.Interfaces;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Spacearr.Pusher.API
Expand All @@ -14,7 +15,9 @@ public class Pusher : IPusher
private readonly IGetComputerDrivesCommandReceiver _getComputerDrivesCommandReceiver;
private readonly ISaveFirebasePushNotificationTokenCommandReceiver _saveFirebasePushNotificationTokenCommandReceiver;
private readonly IGetWorkerServiceVersionCommandReceiver _getWorkerServiceVersionCommandReceiver;
public string ReturnData => _workerServiceReceiver?.ReturnData;
public TimeSpan TimeLimit => _workerServiceReceiver.TimeLimit;
public bool CommandCompleted => _workerServiceReceiver.CommandCompleted;
public List<string> ReturnData => _workerServiceReceiver?.ReturnData;

public Pusher(ILogger logger, IWorkerServiceReceiver workerServiceReceiver)
{
Expand Down Expand Up @@ -101,18 +104,19 @@ public async Task WorkerServiceReceiverDisconnect()
/// <param name="channelName">The channel name to connect to</param>
/// <param name="eventName">The event name to connect to</param>
/// <param name="message">The message to send</param>
/// <param name="isFinalMessage">The it is the final message to send</param>
/// <param name="appId">The Pusher app id</param>
/// <param name="key">The Pusher key</param>
/// <param name="secret">The Pusher secret</param>
/// <param name="cluster">The Pusher cluster</param>
/// <returns></returns>
public async Task SendMessage(string channelName, string eventName, string message, string appId, string key, string secret, string cluster)
public async Task SendMessage(string channelName, string eventName, string message, bool isFinalMessage, string appId, string key, string secret, string cluster)
{
if (!string.IsNullOrWhiteSpace(appId) && !string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(secret) && !string.IsNullOrWhiteSpace(cluster))
{
var pusherSend = new PusherServer.Pusher(appId, key, secret, new PusherServer.PusherOptions { Cluster = cluster });

await pusherSend.TriggerAsync(channelName, eventName, new { message });
await pusherSend.TriggerAsync(channelName, eventName, new { Message = message, IsFinalMessage = isFinalMessage });
}
else
{
Expand All @@ -123,13 +127,24 @@ public async Task SendMessage(string channelName, string eventName, string messa
/// <summary>
/// Command to send a message to the Pusher Pub/Sub to a specific channel and event.
/// </summary>
/// <param name="command">The command to execute</param>
/// <param name="channelName">The channel name to connect to</param>
/// <param name="eventName">The event name to connect to</param>
/// <param name="appId">The Pusher app id</param>
/// <param name="key">The Pusher key</param>
/// <param name="secret">The Pusher secret</param>
/// <param name="cluster">The Pusher cluster</param>
/// <returns></returns>
public async void ExecuteCommand(ICommand command, string channelName, string eventName, string appId, string key, string secret, string cluster)
{
try
{
var json = await _invoker.Invoke(command);
await SendMessage(channelName, eventName, json, appId, key, secret, cluster);
var jsonList = await _invoker.Invoke(command);

for (var i = 0; i < jsonList.Count; i++)
{
await SendMessage(channelName, eventName, jsonList[i], i == jsonList.Count-1, appId, key, secret, cluster);
}
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
using Spacearr.Pusher.API.Models;
using Spacearr.Pusher.API.Receivers.Interfaces;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Spacearr.Pusher.API.Receivers.Implementations
{
public class WorkerServiceReceiver : IWorkerServiceReceiver
{
private readonly ILogger _logger;
public string ReturnData { get; set; }
public TimeSpan TimeLimit { get; set; }
public bool CommandCompleted { get; set; }
public List<string> ReturnData { get; set; }
private PusherClient.Pusher _pusherReceive;

public WorkerServiceReceiver(ILogger logger)
Expand All @@ -32,19 +35,28 @@ public async Task Connect(string channelNameReceive, string eventNameReceive, st
{
try
{
ReturnData = null;
CommandCompleted = false;
ReturnData = new List<string>();
_pusherReceive = null;

if (!string.IsNullOrWhiteSpace(appId) && !string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(secret) && !string.IsNullOrWhiteSpace(cluster))
{
_pusherReceive = new PusherClient.Pusher(key, new PusherClient.PusherOptions { Cluster = cluster });

TimeLimit = new TimeSpan(0, 0, 10);
var myChannel = await _pusherReceive.SubscribeAsync(channelNameReceive);
myChannel.Bind(eventNameReceive, (dynamic data) =>
{
PusherReceiveMessageObjectModel pusherReceiveMessageObject = JsonConvert.DeserializeObject<PusherReceiveMessageObjectModel>(data.ToString());
var pusherReceiveMessage = JsonConvert.DeserializeObject<PusherReceiveMessageModel>(pusherReceiveMessageObject.Data);
ReturnData = pusherReceiveMessage.Message;
CommandCompleted = pusherReceiveMessage.IsFinalMessage;
ReturnData.Add(pusherReceiveMessage.Message);
if (!pusherReceiveMessage.IsFinalMessage)
{
TimeLimit = TimeLimit.Add(new TimeSpan(0, 0, 10));
}
});

await _pusherReceive.ConnectAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Spacearr.Pusher.API.Receivers.Interfaces
{
public interface IWorkerServiceReceiver
{
string ReturnData { get; set; }
TimeSpan TimeLimit { get; set; }
bool CommandCompleted { get; set; }
List<string> ReturnData { get; set; }

/// <summary>
/// Connect the worker service receiver to the Pusher Pub/Sub to a specific channel and event.
Expand Down
Loading

0 comments on commit c7352b1

Please sign in to comment.