welsonjs/WelsonJS.Toolkit/WelsonJS.Service/MessageReceiver.cs

130 lines
3.9 KiB
C#
Raw Normal View History

2024-08-19 08:04:50 +00:00
// MessageReceiver.cs
// https://github.com/gnh1201/welsonjs
using Grpc.Core;
using Grpc.Net.Client;
2024-08-20 00:25:50 +00:00
using System;
2024-08-20 04:42:44 +00:00
using System.Management;
2024-08-19 08:04:50 +00:00
using System.ServiceProcess;
using System.Threading.Tasks;
using WelsonJS.GrpcService;
namespace WelsonJS.Service
{
public class MessageReceiver
{
private GrpcChannel channel;
private ServiceMain parent;
private string deviceId;
2024-08-20 04:42:44 +00:00
private string serverAddress;
2024-08-19 08:04:50 +00:00
public MessageReceiver(ServiceBase parent, string workingDirectory)
{
this.parent = (ServiceMain)parent;
// Read the device ID on this computer
2024-08-20 04:42:44 +00:00
deviceId = GetSystemUUID();
2024-08-19 08:04:50 +00:00
// Read configuration from settings.ini
2024-08-20 07:03:27 +00:00
if (!String.IsNullOrEmpty(deviceId))
2024-08-19 08:04:50 +00:00
{
2024-08-20 07:03:27 +00:00
this.parent.Log($"Resolved the device ID: {deviceId}");
try
{
serverAddress = this.parent.GetSettingsFileHandler().Read("GRPC_HOST", "Service");
2024-08-22 07:59:47 +00:00
if (!String.IsNullOrEmpty(serverAddress))
{
throw new Exception("The server addresss is empty.");
}
2024-08-20 07:03:27 +00:00
}
catch (Exception ex)
{
2024-08-20 07:31:23 +00:00
serverAddress = "http://localhost:50051";
this.parent.Log($"Failed to read the server address. {ex.Message} Use default value: {serverAddress}");
2024-08-20 07:03:27 +00:00
}
2024-08-19 08:04:50 +00:00
}
2024-08-20 07:03:27 +00:00
else
2024-08-19 08:04:50 +00:00
{
2024-08-20 04:42:44 +00:00
serverAddress = null;
2024-08-20 07:03:27 +00:00
this.parent.Log($"Failed to resolve the device ID");
2024-08-20 04:42:44 +00:00
}
// Set the GRPC channel
if (serverAddress != null)
{
try
{
this.parent.Log($"Use the remote address: {serverAddress}");
channel = GrpcChannel.ForAddress(serverAddress);
}
catch (Exception ex)
{
channel = null;
this.parent.Log($"Failed to initialize the GRPC channel: {ex.Message}");
}
2024-08-19 08:04:50 +00:00
}
}
public void Start()
{
if (channel != null)
{
Task.Run(() => GetTask());
2024-08-20 04:42:44 +00:00
parent.Log("GRPC Message Receiver Started");
2024-08-19 08:04:50 +00:00
}
else
{
2024-08-20 07:03:27 +00:00
parent.Log("Failed to initalize the GRPC channel");
2024-08-19 08:04:50 +00:00
}
}
private async Task GetTask()
{
2024-08-20 04:42:44 +00:00
parent.Log("Use the device ID: " + deviceId);
2024-08-19 08:04:50 +00:00
try
{
var client = new MessageController.MessageControllerClient(channel);
var request = new MessageRequest {
ClientId = deviceId
};
var call = client.SendMessageStream(request);
while (await call.ResponseStream.MoveNext())
{
var response = call.ResponseStream.Current;
2024-08-20 10:37:10 +00:00
parent.Log($"< {response.Message}");
2024-08-20 00:25:50 +00:00
// dispatch to the script runtime
parent.DispatchServiceEvent("messageReceived", new string[] { response.Message });
2024-08-19 08:04:50 +00:00
}
}
finally
{
channel?.Dispose();
}
}
2024-08-20 04:42:44 +00:00
private string GetSystemUUID()
{
try
{
using (var searcher = new ManagementObjectSearcher("SELECT UUID FROM Win32_ComputerSystemProduct"))
{
foreach (var mo in searcher.Get())
{
return mo["UUID"].ToString();
}
}
}
catch (Exception ex)
{
parent.Log($"An error occurred while retrieving the system UUID: {ex.Message}");
}
2024-08-20 07:03:27 +00:00
return null;
2024-08-20 04:42:44 +00:00
}
2024-08-19 08:04:50 +00:00
}
}