[성현모] DeviceManager, Factory 리팩토링, DIO, Scanner 추가
This commit is contained in:
40
eCIAv2.Library/Config/eCIAConfig.cs
Normal file
40
eCIAv2.Library/Config/eCIAConfig.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using eCIAv2.Library.Devices.Scanner;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Config
|
||||
{
|
||||
public class eCIAConfig
|
||||
{
|
||||
public Dictionary<int, eCIAChConfig> Configs { get; set; } = new Dictionary<int, eCIAChConfig>();
|
||||
}
|
||||
|
||||
public class eCIAChConfig
|
||||
{
|
||||
public short CH { get; set; }
|
||||
|
||||
public Device Device { get; set; } = new Device();
|
||||
}
|
||||
|
||||
public class Device
|
||||
{
|
||||
public List<DIO> DIOs { get; set; } = new List<DIO>();
|
||||
public List<Scanner> Scanners { get; set; } = new List<Scanner>();
|
||||
}
|
||||
|
||||
|
||||
//device
|
||||
public class DIO
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class Scanner
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Port { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
28
eCIAv2.Library/Config/eCIAv2Config.json
Normal file
28
eCIAv2.Library/Config/eCIAv2Config.json
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"Configs": {
|
||||
"1": {
|
||||
"CH": 1,
|
||||
"Device": {
|
||||
"DIOs": [
|
||||
{
|
||||
"Name": "NMF1"
|
||||
}
|
||||
],
|
||||
"Scanners": [
|
||||
{
|
||||
"Name": "Cognex1",
|
||||
"Port": "COM1"
|
||||
},
|
||||
{
|
||||
"Name": "Cognex2",
|
||||
"Port": "COM2"
|
||||
},
|
||||
{
|
||||
"Name": "Zebra1",
|
||||
"Port": "COM3"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
eCIAv2.Library/Devices/DIIO/IDIO.cs
Normal file
15
eCIAv2.Library/Devices/DIIO/IDIO.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using eCIAv2.Library.Config;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Devices.DIIO
|
||||
{
|
||||
public interface IDIO : IDevice
|
||||
{
|
||||
public string GetDI();
|
||||
public string SetDO();
|
||||
}
|
||||
}
|
||||
23
eCIAv2.Library/Devices/DIIO/NMF.cs
Normal file
23
eCIAv2.Library/Devices/DIIO/NMF.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Devices.DIIO
|
||||
{
|
||||
public class NMF : IDIO
|
||||
{
|
||||
public string DeviceName { get; set; }
|
||||
|
||||
public string GetDI()
|
||||
{
|
||||
return "NMF DI";
|
||||
}
|
||||
|
||||
public string SetDO()
|
||||
{
|
||||
return "NMF DO";
|
||||
}
|
||||
}
|
||||
}
|
||||
24
eCIAv2.Library/Devices/DeviceFactory.cs
Normal file
24
eCIAv2.Library/Devices/DeviceFactory.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Devices
|
||||
{
|
||||
public class DeviceFactory
|
||||
{
|
||||
private readonly IServiceProvider _provider;
|
||||
|
||||
public DeviceFactory(IServiceProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public T Create<T>()
|
||||
{
|
||||
return _provider.GetRequiredService<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
46
eCIAv2.Library/Devices/DeviceManager.cs
Normal file
46
eCIAv2.Library/Devices/DeviceManager.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using eCIAv2.Library.Config;
|
||||
using eCIAv2.Library.Devices.DIIO;
|
||||
using eCIAv2.Library.Devices.Scanner;
|
||||
using eCIAv2.Library.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Devices
|
||||
{
|
||||
public class DeviceManager
|
||||
{
|
||||
private readonly Dictionary<string, IDevice> _devices = new();
|
||||
|
||||
public DeviceManager(DeviceFactory factory, ConfigService config)
|
||||
{
|
||||
foreach (var dicConfig in config.GetConfig().Configs)
|
||||
{
|
||||
//scanner
|
||||
foreach(var device in dicConfig.Value.Device.Scanners)
|
||||
{
|
||||
var scanner = factory.Create<IScanner>();
|
||||
scanner.DeviceName = device.Name;
|
||||
|
||||
_devices.Add(device.Name, scanner);
|
||||
}
|
||||
|
||||
//dio
|
||||
foreach (var device in dicConfig.Value.Device.DIOs)
|
||||
{
|
||||
var dio = factory.Create<IDIO>();
|
||||
dio.DeviceName = device.Name;
|
||||
|
||||
_devices.Add(device.Name, dio);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public T GetDevice<T>(string name) where T : IDevice
|
||||
{
|
||||
return (T)_devices[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
13
eCIAv2.Library/Devices/IDevice.cs
Normal file
13
eCIAv2.Library/Devices/IDevice.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Devices
|
||||
{
|
||||
public interface IDevice
|
||||
{
|
||||
public string DeviceName { get; set; }
|
||||
}
|
||||
}
|
||||
23
eCIAv2.Library/Devices/Scanner/Cognex.cs
Normal file
23
eCIAv2.Library/Devices/Scanner/Cognex.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Devices.Scanner
|
||||
{
|
||||
public class Cognex : IScanner
|
||||
{
|
||||
public string DeviceName { get; set; } = string.Empty;
|
||||
|
||||
public Action<string> OnRecvData()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string Trigger()
|
||||
{
|
||||
return DeviceName;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
eCIAv2.Library/Devices/Scanner/IScanner.cs
Normal file
14
eCIAv2.Library/Devices/Scanner/IScanner.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Devices.Scanner
|
||||
{
|
||||
public interface IScanner : IDevice
|
||||
{
|
||||
public string Trigger();
|
||||
public Action<string> OnRecvData();
|
||||
}
|
||||
}
|
||||
23
eCIAv2.Library/Devices/Scanner/Zebra.cs
Normal file
23
eCIAv2.Library/Devices/Scanner/Zebra.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Devices.Scanner
|
||||
{
|
||||
public class Zebra : IScanner
|
||||
{
|
||||
public string DeviceName { get; set; } = string.Empty;
|
||||
|
||||
public Action<string> OnRecvData()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string Trigger()
|
||||
{
|
||||
return DeviceName;
|
||||
}
|
||||
}
|
||||
}
|
||||
52
eCIAv2.Library/Services/ConfigService.cs
Normal file
52
eCIAv2.Library/Services/ConfigService.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using eCIAv2.Library.Config;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.Library.Services
|
||||
{
|
||||
public class ConfigService
|
||||
{
|
||||
public string ConfigPath { get; private set; } = string.Empty;
|
||||
|
||||
public eCIAConfig Config = new eCIAConfig();
|
||||
|
||||
public eCIAConfig LoadConfig(string configPath)
|
||||
{
|
||||
ConfigPath = configPath;
|
||||
if(File.Exists(configPath) == true)
|
||||
{
|
||||
var configJson = File.ReadAllText(ConfigPath);
|
||||
Config = JsonConvert.DeserializeObject<eCIAConfig>(configJson);
|
||||
|
||||
LogXnet.WriteLine($"Config read Success.", LogXLabel.INFO);
|
||||
|
||||
return Config;
|
||||
}
|
||||
|
||||
LogXnet.WriteLine($"Config not exist.", LogXLabel.Error);
|
||||
LogXnet.WriteLine($"ConfigPath:{ConfigPath}", LogXLabel.Error);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public eCIAConfig GetConfig()
|
||||
{
|
||||
return Config;
|
||||
}
|
||||
|
||||
public eCIAChConfig GetConfigByCh(int ch)
|
||||
{
|
||||
if(Config?.Configs?.TryGetValue(ch, out var config) == true)
|
||||
{
|
||||
return config;
|
||||
}
|
||||
|
||||
LogXnet.WriteLine($"Config CH {ch} not exist.", LogXLabel.Error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,11 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="SystemX.Core">
|
||||
<HintPath>..\DLL\SystemX.Core.dll</HintPath>
|
||||
@ -13,6 +18,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Config\eCIAv2Config.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Config\LogXnetConfig.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
|
||||
@ -1,4 +1,8 @@
|
||||
using eCIAv2.Library.Devices;
|
||||
using eCIAv2.Library.Devices.DIIO;
|
||||
using eCIAv2.Library.Devices.Scanner;
|
||||
using eCIAv2.WindowsApp.ViewModels;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace eCIAv2.WindowsApp
|
||||
{
|
||||
@ -10,8 +14,6 @@ namespace eCIAv2.WindowsApp
|
||||
{
|
||||
InitializeComponent();
|
||||
_vm = vm;
|
||||
|
||||
var ss= _vm.LoadConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,12 @@
|
||||
using eCIAv2.WindowsApp.Services;
|
||||
using eCIAv2.Library.Config;
|
||||
using eCIAv2.Library.Devices;
|
||||
using eCIAv2.Library.Devices.DIIO;
|
||||
using eCIAv2.Library.Devices.Scanner;
|
||||
using eCIAv2.Library.Services;
|
||||
using eCIAv2.WindowsApp.ViewModels;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace eCIAv2.WindowsApp
|
||||
{
|
||||
@ -15,9 +20,9 @@ namespace eCIAv2.WindowsApp
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
//LogXnet
|
||||
string configDir = @$"{Application.StartupPath}Config";
|
||||
//raed log4net configs
|
||||
|
||||
//LogXnet
|
||||
if (LogXnet.ReadConfig(@$"{configDir}/LogXnetConfig.json") == true)
|
||||
{
|
||||
LogXnet.WriteLine("LogXnet Init Success");
|
||||
@ -31,18 +36,32 @@ namespace eCIAv2.WindowsApp
|
||||
AppHost = Host.CreateDefaultBuilder()
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
//device
|
||||
services.AddTransient<IScanner,Zebra>();
|
||||
services.AddTransient<IScanner,Cognex>();
|
||||
services.AddTransient<IDIO,NMF>();
|
||||
|
||||
//device factory
|
||||
services.AddSingleton<DeviceFactory>();
|
||||
//device manager
|
||||
services.AddSingleton<DeviceManager>();
|
||||
|
||||
// services
|
||||
services.AddSingleton<ConfigService>();
|
||||
|
||||
//viewmodels
|
||||
services.AddTransient<MainFromViewModel>();
|
||||
|
||||
// forms
|
||||
services.AddTransient<MainForm>();
|
||||
}).Build();
|
||||
|
||||
ApplicationConfiguration.Initialize();
|
||||
var configService = AppHost.Services.GetRequiredService<ConfigService>();
|
||||
var config = configService.LoadConfig(@$"{configDir}/eCIAv2Config.json");
|
||||
|
||||
//config <20><><EFBFBD><EFBFBD> üũ
|
||||
if (config == null)
|
||||
return;
|
||||
|
||||
ApplicationConfiguration.Initialize();
|
||||
var form = AppHost.Services.GetRequiredService<MainForm>();
|
||||
Application.Run(form);
|
||||
}
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace eCIAv2.WindowsApp.Services
|
||||
{
|
||||
public class ConfigService
|
||||
{
|
||||
public string LoadConfig()
|
||||
{
|
||||
return "loadConfig";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,8 @@
|
||||
using eCIAv2.WindowsApp.Services;
|
||||
using eCIAv2.Library.Config;
|
||||
using eCIAv2.Library.Devices;
|
||||
using eCIAv2.Library.Devices.DIIO;
|
||||
using eCIAv2.Library.Devices.Scanner;
|
||||
using eCIAv2.Library.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -10,15 +14,18 @@ namespace eCIAv2.WindowsApp.ViewModels
|
||||
public class MainFromViewModel
|
||||
{
|
||||
private readonly ConfigService _configService;
|
||||
private readonly DeviceManager _deviceManager;
|
||||
|
||||
public MainFromViewModel(ConfigService configService)
|
||||
public MainFromViewModel(ConfigService configService, DeviceManager deviceManager)
|
||||
{
|
||||
_configService = configService;
|
||||
_deviceManager = deviceManager;
|
||||
}
|
||||
|
||||
public string LoadConfig()
|
||||
public void TriggerCognex()
|
||||
{
|
||||
return _configService.LoadConfig();
|
||||
IScanner cognex = _deviceManager.GetDevice<IScanner>("Cognex1");
|
||||
var sss = cognex.Trigger();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,8 +30,4 @@
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user