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
// https://github.com/gnh1201/welsonjs
using DeviceId;
using Grpc.Core;
using Grpc.Net.Client;
using System;
using System.Management;
using System.ServiceProcess;
using System.Threading.Tasks;
using WelsonJS.GrpcService;
@ -15,29 +15,39 @@ namespace WelsonJS.Service
private GrpcChannel channel;
private ServiceMain parent;
private string deviceId;
private string serverAddress;
public MessageReceiver(ServiceBase parent, string workingDirectory)
{
this.parent = (ServiceMain)parent;
// Read the device ID on this computer
deviceId = new DeviceIdBuilder()
.OnWindows(windows => windows.AddWindowsDeviceId())
.ToString();
deviceId = GetSystemUUID();
// Read configuration from settings.ini
try
{
// Get the GRPC server URL from settings
string grpcServerAddress = this.parent.GetSettingsFileHandler().Read("GRPC_HOST", "Service");
// Set the GRPC channel
channel = GrpcChannel.ForAddress(grpcServerAddress);
serverAddress = this.parent.GetSettingsFileHandler().Read("GRPC_HOST", "Service");
}
catch (Exception ex)
{
this.parent.Log(ex.Message);
channel = null;
serverAddress = 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)
{
Task.Run(() => GetTask());
parent.Log("GRPC Message Receiver Started");
}
else
{
parent.Log("Not Initializd GRPC channel");
}
}
private async Task GetTask()
{
parent.Log("Use the device ID: " + deviceId);
try
{
var client = new MessageController.MessageControllerClient(channel);
@ -79,5 +91,25 @@ namespace WelsonJS.Service
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;
}
}
else
{
Log($"Configuration file not found: {settingsFilePath}");
}
// set script name
if (string.IsNullOrEmpty(scriptName))
{
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
@ -215,16 +219,14 @@ namespace WelsonJS.Service
Log("File Event Monitor is Disabled");
}
// Start GRPC based message receiver
// Start GRPC Message Receiver
if (!disabledMessageReceiver) {
MessageReceiver receiver = new MessageReceiver(this, workingDirectory);
receiver.Start();
Log("GRPC Message Receiver Started");
}
else
{
Log("GRPC Message Reciver is Disabled");
Log("GRPC Message Receiver is Disabled");
}
// Start all the registered timers

View File

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