Revert "Some works for #124"

This reverts commit b5af648512.
This commit is contained in:
Namhyeon Go 2024-07-29 15:41:10 +09:00
parent f72db8bf2e
commit 95a7588726
4 changed files with 20 additions and 175 deletions

View File

@ -1,21 +1,20 @@
using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
namespace WelsonJS.Service
{
internal static class Program
{
/// <summary>
/// entry point
/// 해당 애플리케이션의 주 진입점입니다.
/// </summary>
static void Main(string[] args)
{
ServiceMain svc = new ServiceMain();
if (Environment.UserInteractive)
{
Console.WriteLine("Welcome to WelsonJS Scripting Service...");
Console.WriteLine("https://github.com/gnh1201/welsonjs");
ServiceMain svc = new ServiceMain();
svc.TestStartupAndStop(args);
}
else
@ -23,7 +22,7 @@ namespace WelsonJS.Service
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
svc
new ServiceMain()
};
ServiceBase.Run(ServicesToRun);
}

View File

@ -32,13 +32,13 @@ namespace WelsonJS.Service
{
public partial class ServiceMain : ServiceBase
{
private static List<Timer> timers;
private Timer timer;
private string workingDirectory;
private string scriptName;
private string scriptFilePath;
private string scriptText;
private string scriptName;
private ScriptControl scriptControl;
private readonly string logFilePath = Path.Combine(Path.GetTempPath(), "WelsonJS.Service.Log.txt");
private string logFilePath;
private readonly string appName = "WelsonJS";
public ServiceMain()
@ -46,18 +46,8 @@ namespace WelsonJS.Service
InitializeComponent();
// set the log file path
logFilePath = Path.Combine(Path.GetTempPath(), "WelsonJS.Service.Log.txt");
Log(appName + " Service Loaded");
// set timers
timers = new List<Timer>();
// set default timer
Timer timer = new Timer
{
Interval = 60000 // 1 minute
};
timer.Elapsed += OnElapsedTime;
timers.Add(timer);
}
internal void TestStartupAndStop(string[] args)
@ -138,22 +128,27 @@ namespace WelsonJS.Service
{
Log($"Script file not found: {scriptFilePath}");
}
// Start timers
timers.ForEach(timer => timer.Start());
// set interval
timer = new Timer();
timer.Interval = 60000; // 1 minute
timer.Elapsed += OnElapsedTime;
timer.Start();
Log(appName + " Service Started");
}
protected override void OnStop()
{
// Stop timers
timers.ForEach(timer => timer.Stop());
timer.Stop();
try
{
Log(DispatchServiceEvent(scriptName, "stop"));
scriptControl?.Reset();
if (scriptControl != null)
{
scriptControl.Reset();
}
}
catch (Exception ex)
{
@ -224,10 +219,5 @@ namespace WelsonJS.Service
return arguments;
}
public string GetWorkingDirectory()
{
return workingDirectory;
}
}
}

View File

@ -1,141 +0,0 @@
// this is prototype code
// https://github.com/gnh1201/welsonjs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using WelsonJS.Service;
public class TemplateMatching
{
public void Test(string workingDirectory)
{
string templateFolderPath = Path.Combine(workingDirectory, "assets/img/templates");
// Load all template images
List<Bitmap> templateImages = LoadTemplateImages(templateFolderPath);
// try template matching
List<(string FileName, int ScreenNumber, Point Location, double MaxCorrelation)> results =
CaptureAndMatchAllScreens(templateImages);
// print results
foreach (var result in results)
{
Console.WriteLine($"Template: {result.FileName}, Screen: {result.ScreenNumber}, " +
$"Location: (x: {result.Location.X}, y: {result.Location.Y}), " +
$"Max Correlation: {result.MaxCorrelation}");
}
}
public static List<Bitmap> LoadTemplateImages(string folderPath)
{
var templates = new List<Bitmap>();
var files = Directory.GetFiles(folderPath, "*.png");
foreach (var file in files)
{
templates.Add(new Bitmap(file));
}
return templates;
}
public static List<(string FileName, int ScreenNumber, Point Location, double MaxCorrelation)>
CaptureAndMatchAllScreens(List<Bitmap> templateImages)
{
var results = new List<(string FileName, int ScreenNumber, Point Location, double MaxCorrelation)>();
for (int i = 0; i < Screen.AllScreens.Length; i++)
{
Screen screen = Screen.AllScreens[i];
Bitmap mainImage = CaptureScreen(screen);
foreach (var templateImage in templateImages)
{
Point matchLocation = FindTemplate(mainImage, templateImage, out double maxCorrelation);
string templateFileName = templateImage.Tag as string;
results.Add((templateFileName, i, matchLocation, maxCorrelation));
}
}
return results;
}
public static Bitmap CaptureScreen(Screen screen)
{
Rectangle screenSize = screen.Bounds;
Bitmap bitmap = new Bitmap(screenSize.Width, screenSize.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(screenSize.Left, screenSize.Top, 0, 0, screenSize.Size);
}
return bitmap;
}
public static Point FindTemplate(Bitmap mainImage, Bitmap templateImage, out double maxCorrelation)
{
int mainWidth = mainImage.Width;
int mainHeight = mainImage.Height;
int templateWidth = templateImage.Width;
int templateHeight = templateImage.Height;
Point bestMatch = Point.Empty;
maxCorrelation = double.MinValue;
for (int x = 0; x <= mainWidth - templateWidth; x++)
{
for (int y = 0; y <= mainHeight - templateHeight; y++)
{
double correlation = CalculateCorrelation(mainImage, templateImage, x, y);
if (correlation > maxCorrelation)
{
maxCorrelation = correlation;
bestMatch = new Point(x, y);
}
}
}
return bestMatch;
}
private static double CalculateCorrelation(Bitmap mainImage, Bitmap templateImage, int offsetX, int offsetY)
{
int templateWidth = templateImage.Width;
int templateHeight = templateImage.Height;
double sumTemplate = 0;
double sumTemplateSquare = 0;
double sumMain = 0;
double sumMainSquare = 0;
double sumProduct = 0;
for (int x = 0; x < templateWidth; x++)
{
for (int y = 0; y < templateHeight; y++)
{
Color mainPixel = mainImage.GetPixel(x + offsetX, y + offsetY);
Color templatePixel = templateImage.GetPixel(x, y);
double mainGray = (mainPixel.R + mainPixel.G + mainPixel.B) / 3.0;
double templateGray = (templatePixel.R + templatePixel.G + templatePixel.B) / 3.0;
sumTemplate += templateGray;
sumTemplateSquare += templateGray * templateGray;
sumMain += mainGray;
sumMainSquare += mainGray * mainGray;
sumProduct += mainGray * templateGray;
}
}
int numPixels = templateWidth * templateHeight;
double numerator = (numPixels * sumProduct) - (sumMain * sumTemplate);
double denominator = Math.Sqrt(((numPixels * sumMainSquare) - (sumMain * sumMain)) * ((numPixels * sumTemplateSquare) - (sumTemplate * sumTemplate)));
return denominator == 0 ? 0 : numerator / denominator;
}
}

View File

@ -83,10 +83,8 @@
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
@ -104,7 +102,6 @@
<DependentUpon>ProjectInstaller.cs</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TemplateMatching.cs" />
</ItemGroup>
<ItemGroup>
<COMReference Include="MSScriptControl">