This commit is contained in:
Namhyeon Go 2024-08-20 13:42:44 +09:00
parent 1fa024546a
commit ed9e1a6d05
3 changed files with 62 additions and 17 deletions

View File

@ -1,9 +1,9 @@
// MessageReceiver.cs // MessageReceiver.cs
// https://github.com/gnh1201/welsonjs // https://github.com/gnh1201/welsonjs
using DeviceId;
using Grpc.Core; using Grpc.Core;
using Grpc.Net.Client; using Grpc.Net.Client;
using System; using System;
using System.Management;
using System.ServiceProcess; using System.ServiceProcess;
using System.Threading.Tasks; using System.Threading.Tasks;
using WelsonJS.GrpcService; using WelsonJS.GrpcService;
@ -15,29 +15,39 @@ namespace WelsonJS.Service
private GrpcChannel channel; private GrpcChannel channel;
private ServiceMain parent; private ServiceMain parent;
private string deviceId; private string deviceId;
private string serverAddress;
public MessageReceiver(ServiceBase parent, string workingDirectory) public MessageReceiver(ServiceBase parent, string workingDirectory)
{ {
this.parent = (ServiceMain)parent; this.parent = (ServiceMain)parent;
// Read the device ID on this computer // Read the device ID on this computer
deviceId = new DeviceIdBuilder() deviceId = GetSystemUUID();
.OnWindows(windows => windows.AddWindowsDeviceId())
.ToString();
// Read configuration from settings.ini // Read configuration from settings.ini
try try
{ {
// Get the GRPC server URL from settings serverAddress = this.parent.GetSettingsFileHandler().Read("GRPC_HOST", "Service");
string grpcServerAddress = this.parent.GetSettingsFileHandler().Read("GRPC_HOST", "Service");
// Set the GRPC channel
channel = GrpcChannel.ForAddress(grpcServerAddress);
} }
catch (Exception ex) catch (Exception ex)
{ {
this.parent.Log(ex.Message); serverAddress = null;
channel = null; this.parent.Log($"Failed to read the server address: {ex.Message}");
}
// 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}");
}
} }
} }
@ -46,16 +56,18 @@ namespace WelsonJS.Service
if (channel != null) if (channel != null)
{ {
Task.Run(() => GetTask()); Task.Run(() => GetTask());
parent.Log("GRPC Message Receiver Started");
} }
else else
{ {
parent.Log("Not Initializd GRPC channel"); parent.Log("Not Initializd GRPC channel");
} }
} }
private async Task GetTask() private async Task GetTask()
{ {
parent.Log("Use the device ID: " + deviceId);
try try
{ {
var client = new MessageController.MessageControllerClient(channel); var client = new MessageController.MessageControllerClient(channel);
@ -79,5 +91,25 @@ namespace WelsonJS.Service
channel?.Dispose(); channel?.Dispose();
} }
} }
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}");
}
return string.Empty;
}
} }
} }

View File

@ -123,12 +123,16 @@ namespace WelsonJS.Service
settingsFileHandler = null; settingsFileHandler = null;
} }
} }
else
{
Log($"Configuration file not found: {settingsFilePath}");
}
// set script name // set script name
if (string.IsNullOrEmpty(scriptName)) if (string.IsNullOrEmpty(scriptName))
{ {
scriptName = "defaultService"; scriptName = "defaultService";
Log("Script name not provided. Using default value: " + scriptName); Log($"Script name not provided. Using default value: {scriptName}");
} }
// set path of the script // set path of the script
@ -215,16 +219,14 @@ namespace WelsonJS.Service
Log("File Event Monitor is Disabled"); Log("File Event Monitor is Disabled");
} }
// Start GRPC based message receiver // Start GRPC Message Receiver
if (!disabledMessageReceiver) { if (!disabledMessageReceiver) {
MessageReceiver receiver = new MessageReceiver(this, workingDirectory); MessageReceiver receiver = new MessageReceiver(this, workingDirectory);
receiver.Start(); receiver.Start();
Log("GRPC Message Receiver Started");
} }
else else
{ {
Log("GRPC Message Reciver is Disabled"); Log("GRPC Message Receiver is Disabled");
} }
// Start all the registered timers // Start all the registered timers

View File

@ -1,7 +1,13 @@
var SYS = require("lib/system");
function main(args) { function main(args) {
console.log("WelsonJS.Service required."); console.log("WelsonJS.Service required.");
} }
function getDeviceID() {
return SYS.getUUID();
}
function onServiceStart() { function onServiceStart() {
return "onServiceStart recevied"; return "onServiceStart recevied";
} }
@ -14,6 +20,10 @@ function onServiceElapsedTime() {
return "onServiceElapsedTime recevied"; return "onServiceElapsedTime recevied";
} }
function onMessageReceived() {
return "onMessageReceived recevied. " + args.join(', ');
}
function onServiceScreenTime(args) { function onServiceScreenTime(args) {
return "onServiceScreenTime recevied. " + args.join(', '); return "onServiceScreenTime recevied. " + args.join(', ');
} }
@ -27,6 +37,7 @@ function onFileRuleMatched(args) {
} }
exports.main = main; exports.main = main;
exports.getDeviceID = getDeviceID;
exports.onServiceStart = onServiceStart; exports.onServiceStart = onServiceStart;
exports.onServiceStop = onServiceStop; exports.onServiceStop = onServiceStop;
exports.onServiceElapsedTime = onServiceElapsedTime; exports.onServiceElapsedTime = onServiceElapsedTime;