| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705 |
- using Cognex.VisionPro;
- using Cognex.VisionPro.ToolBlock;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Data; // for ISystemDatabaseService (if used)
- using TeamAAS_VP.Enums;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Models.Calibration;
- using TeamAAS_VP.Models.Feeder;
- using TeamAAS_VP.Models.Lights;
- using TeamAAS_VP.Models.PLC;
- using TeamAAS_VP.Models.ScrewDriver;
- using TeamAAS_VP.Models.Torque;
- using TeamAAS_VP.Resources.Languages;
- using WPFLocalizeExtension.Engine;
- namespace TeamAAS_VP.Services
- {
- /// <summary>
- /// ���ò�������ʵ���ࡣ
- /// �̰߳�ȫ���ڲ�ʹ��˽�����ֶ� <see cref="_sync"/> ���������ڴ漯�ϵIJ������ʡ�
- /// �־û����Լ��ϵ��Ļ�д�ص���Ӧ�������ļ���·�����ڲ���̬�� <see cref="ConfigPaths"/> ���壩��
- /// </summary>
- public class ConfigService : IConfigService
- {
- private static class ConfigPaths
- {
- // ��·�����й����ڱ��࣬��ǰʹ������ FilePath ֵ�Ա��ּ���
- public static readonly string CamerasConfigurationPath = "..//Config//CameraConfiguration.cfg";
- public static readonly string RobotsConfigurationPath = "..//Config//RobotsConfiguration.cfg";
- public static readonly string FeedersConfigurationPath = "..//Config//FeedersConfiguration.cfg";
- public static readonly string PlcConfigurationPath = "..//Config//PlcConfiguration.cfg";
- public static readonly string BgTcpIpConfigurationPath = "..//Config//BgTcpIpConfiguration.cfg";
- public static readonly string BgModbusTcpConfigurationPath = "..//Config//BgModbusTcpConfiguration.cfg";
- public static readonly string SystemConfigurationPath = "..//Config//SystemConfiguration.cfg";
- public static readonly string DeviceInfoConfigurationPath = "..//Config//DeviceInfo.cfg";
- // Feeder������������·��
- public static readonly string FeederClearanceTasksConfigurationPath = "..//Config//FeederClearanceTasksConfiguration.cfg";
- // ��Դ����������·��
- public static readonly string LightControllersConfigurationPath = "..//Config//LightControllersConfiguration.cfg";
- // ��������·�������豸��
- public static readonly string ScrewDriverConfigurationPath = "..//Config//ScrewDriverConfiguration.cfg";
- // ��˿�����������ϣ�����·��
- public static readonly string ScrewFeedersConfigurationPath = "..//Config//ScrewFeedersConfiguration.cfg";
- // ˢ��������·��
- public static readonly string CardReaderConfigurationPath = "..//Config//CardReaderConfiguration.cfg";
- // ��������·��
- public static readonly string TorqueReaderConfigurationPath = "..//Config//TorqueReaderConfiguration.cfg";
- // ������������·��
- public static readonly string DataConfigurationPath = "..//Config//DataConfiguration.cfg";
- }
- private readonly object _sync = new object();
- private ObservableCollection<CameraInfo> Cameras { get; set; }
- private ObservableCollection<RobotInfo> Robots { get; set; }
- private ObservableCollection<FeederInfo> Feeders { get; set; }
- // ��˿�����������ϣ�����
- private ObservableCollection<ScrewFeederInfo> ScrewFeeders { get; set; }
- private ObservableCollection<PlcInfo> Plcs { get; set; }
- private BgTcpIP BgCommunicate { get; set; }
- private BgModbusTcp BgModbusCommunicate { get; set; }
- private ObservableCollection<FeederClearWork> FeederClearanceTasks { get; set; }
- // ��Դ����������
- private ObservableCollection<LightControllerConfig> LightControllers { get; set; }
- // Device info - 支持多设备
- private List<DeviceInfo> _deviceInfoList;
- public CTQ_Data CtqData { get; set; }
- private PlcAddressConfig PlcAddressConfig { get; set; }
- // ������������
- private TeamAAS_VP.Models.ScrewDriver.ScrewDriverConfig _screwDriverConfig;
- public TeamAAS_VP.Models.ScrewDriver.ScrewDriverConfig ScrewDriverConfig { get { lock (_sync) { return _screwDriverConfig; } } }
- private ISystemDatabaseService _systemDatabaseService;
- // ˢ��������
- private SerialPortConfig _cardReaderConfig;
- private TorqueSerialPortConfig _torqueReaderConfig;
- /// <summary>
- /// ע��ϵͳ���ݿ����ѡ�������ڼ�¼��˿���α���ȼ�¼��
- /// </summary>
- /// <param name="svc"></param>
- public void SetSystemDatabaseService(ISystemDatabaseService svc)
- {
- lock (_sync)
- {
- _systemDatabaseService = svc;
- }
- }
- /// <summary>
- /// ��ʼ��һ���µ� <see cref="ConfigService"/> ʵ����
- /// ���캯�������Զ����������ļ��������ڹ����ڼ���� I/O ���������ɵ��� <see cref="LoadAll"/> �� <see cref="LoadAllAsync"/> ��ʽ���ء�
- /// </summary>
- public ConfigService()
- {
- //LoadAll();
- }
- /// <summary>
- /// ͬ���������������ļ����ڴ棨����Ӧ�ļ������ڣ�������Ĭ�Ͽռ���/����д���ļ�����
- /// �̰߳�ȫ�������ڲ��Թ�����Դʹ�� <see cref="_sync"/> ������
- /// </summary>
- public void LoadAll()
- {
- lock (_sync)
- {
- // ��ȡ�����ã�����ļ������ڣ��� DeviceConfig Ĭ��ֵ
- if (File.Exists(ConfigPaths.CamerasConfigurationPath))
- Cameras = FileHelper.ReadJsonFile<ObservableCollection<CameraInfo>>(ConfigPaths.CamerasConfigurationPath);
- else
- {
- Cameras = new ObservableCollection<CameraInfo>();
- FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
- }
- if (File.Exists(ConfigPaths.RobotsConfigurationPath))
- Robots = FileHelper.ReadJsonFile<ObservableCollection<RobotInfo>>(ConfigPaths.RobotsConfigurationPath);
- else
- {
- Robots = new ObservableCollection<RobotInfo>();
- FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
- }
- if (File.Exists(ConfigPaths.FeedersConfigurationPath))
- Feeders = FileHelper.ReadJsonFile<ObservableCollection<FeederInfo>>(ConfigPaths.FeedersConfigurationPath);
- else
- {
- Feeders = new ObservableCollection<FeederInfo>();
- FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
- }
- // load screw feeders (material) configuration
- if (File.Exists(ConfigPaths.ScrewFeedersConfigurationPath))
- ScrewFeeders = FileHelper.ReadJsonFile<ObservableCollection<ScrewFeederInfo>>(ConfigPaths.ScrewFeedersConfigurationPath);
- else
- {
- ScrewFeeders = new ObservableCollection<ScrewFeederInfo>();
- FileHelper.WriteJsonFile(ScrewFeeders, ConfigPaths.ScrewFeedersConfigurationPath);
- }
- if (File.Exists(ConfigPaths.PlcConfigurationPath))
- Plcs = FileHelper.ReadJsonFile<ObservableCollection<PlcInfo>>(ConfigPaths.PlcConfigurationPath);
- else
- {
- Plcs = new ObservableCollection<PlcInfo>();
- FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
- }
- if (File.Exists(ConfigPaths.BgTcpIpConfigurationPath))
- BgCommunicate = FileHelper.ReadJsonFile<BgTcpIP>(ConfigPaths.BgTcpIpConfigurationPath);
- else
- {
- BgCommunicate = new BgTcpIP();
- FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath);
- }
- if (File.Exists(ConfigPaths.BgModbusTcpConfigurationPath))
- BgModbusCommunicate = FileHelper.ReadJsonFile<BgModbusTcp>(ConfigPaths.BgModbusTcpConfigurationPath);
- else
- {
- BgModbusCommunicate = new BgModbusTcp();
- FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath);
- }
- if (!File.Exists(ConfigPaths.FeederClearanceTasksConfigurationPath))
- {
- FeederClearanceTasks = new ObservableCollection<FeederClearWork>();
- FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
- }
- else
- {
- // ��ȡ Feeder�������� ����
- FeederClearanceTasks = FileHelper.ReadJsonFile<ObservableCollection<FeederClearWork>>(ConfigPaths.FeederClearanceTasksConfigurationPath);
- }
- // ��ȡ��Դ����������
- if (!File.Exists(ConfigPaths.LightControllersConfigurationPath))
- {
- LightControllers = new ObservableCollection<LightControllerConfig>();
- FileHelper.WriteJsonFile(LightControllers, ConfigPaths.LightControllersConfigurationPath);
- }
- else
- {
- LightControllers = FileHelper.ReadJsonFile<ObservableCollection<LightControllerConfig>>(ConfigPaths.LightControllersConfigurationPath);
- }
- // load plc addresses
- var plcAddressPath = "..//Config//PlcAddressConfiguration.cfg";
- if (File.Exists(plcAddressPath))
- PlcAddressConfig = FileHelper.ReadJsonFile<PlcAddressConfig>(plcAddressPath);
- else
- {
- PlcAddressConfig = new PlcAddressConfig();
- PlcAddressConfig.SetDefaultValue();
- FileHelper.WriteJsonFile(PlcAddressConfig, plcAddressPath);
- }
- // load screw driver config (single device)
- if (File.Exists(ConfigPaths.ScrewDriverConfigurationPath))
- _screwDriverConfig = FileHelper.ReadJsonFile<TeamAAS_VP.Models.ScrewDriver.ScrewDriverConfig>(ConfigPaths.ScrewDriverConfigurationPath);
- else
- {
- _screwDriverConfig = new TeamAAS_VP.Models.ScrewDriver.ScrewDriverConfig();
- FileHelper.WriteJsonFile(_screwDriverConfig, ConfigPaths.ScrewDriverConfigurationPath);
- }
- // load device info list (支持多设备)
- if (File.Exists(ConfigPaths.DeviceInfoConfigurationPath))
- {
- try
- {
- // 尝试作为列表读取
- _deviceInfoList = FileHelper.ReadJsonFile<List<DeviceInfo>>(ConfigPaths.DeviceInfoConfigurationPath);
- }
- catch
- {
- // 兼容旧版单个 DeviceInfo,将其转换为列表
- var singleDevice = FileHelper.ReadJsonFile<DeviceInfo>(ConfigPaths.DeviceInfoConfigurationPath);
- if (singleDevice != null)
- {
- singleDevice.DeviceNo = 0;
- _deviceInfoList = new List<DeviceInfo> { singleDevice };
- }
- else
- {
- _deviceInfoList = new List<DeviceInfo> { new DeviceInfo { DeviceNo = 0 } };
- }
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- }
- }
- else
- {
- _deviceInfoList = new List<DeviceInfo> { new DeviceInfo { DeviceNo = 0 } };
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- }
- // load card reader configuration
- if (File.Exists(ConfigPaths.CardReaderConfigurationPath))
- _cardReaderConfig = FileHelper.ReadJsonFile<SerialPortConfig>(ConfigPaths.CardReaderConfigurationPath);
- else
- {
- _cardReaderConfig = new SerialPortConfig();
- FileHelper.WriteJsonFile(_cardReaderConfig, ConfigPaths.CardReaderConfigurationPath);
- }
- if (File.Exists(ConfigPaths.DataConfigurationPath))
- CtqData = FileHelper.ReadJsonFile<CTQ_Data>(ConfigPaths.DataConfigurationPath);
- else
- {
- CtqData = new CTQ_Data();
- FileHelper.WriteJsonFile(CtqData, ConfigPaths.DataConfigurationPath);
- }
- }
- }
- /// <summary>
- /// �첽ִ�� <see cref="LoadAll"/> ������
- /// </summary>
- /// <returns>��ʾ�첽���ز���������</returns>
- public Task LoadAllAsync()
- {
- return Task.Run(() => LoadAll());
- }
- /// <summary>
- /// ���ڴ��е���������д�ص���Ӧ�����ļ���
- /// �̰߳�ȫ�������ڲ���д�����ʹ�� <see cref="_sync"/> ������
- /// </summary>
- public void SaveAll()
- {
- lock (_sync)
- {
- FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
- FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
- FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
- FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
- FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath);
- FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath);
- FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
- FileHelper.WriteJsonFile(LightControllers, ConfigPaths.LightControllersConfigurationPath);
- // save screw driver config
- if (_screwDriverConfig != null)
- FileHelper.WriteJsonFile(_screwDriverConfig, ConfigPaths.ScrewDriverConfigurationPath);
- // save screw feeders
- if (ScrewFeeders != null)
- FileHelper.WriteJsonFile(ScrewFeeders, ConfigPaths.ScrewFeedersConfigurationPath);
- // save plc addresses
- //FileHelper.WriteJsonFile(PlcAddressConfig, "..//Config//PlcAddressConfiguration.cfg");
- // save device info list (支持多设备)
- if (_deviceInfoList != null)
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- // save card reader config
- if (_cardReaderConfig != null)
- FileHelper.WriteJsonFile(_cardReaderConfig, ConfigPaths.CardReaderConfigurationPath);
- if (CtqData != null)
- FileHelper.WriteJsonFile(CtqData, ConfigPaths.DataConfigurationPath);
- }
- }
- /// <summary>
- /// �첽ִ�� <see cref="SaveAll"/> ������
- /// </summary>
- /// <returns>��ʾ�첽�������������</returns>
- public Task SaveAllAsync()
- {
- return Task.Run(() => SaveAll());
- }
- #region Cameras
- /// <summary>
- /// ��ȡ����������õ�ֻ�����ϡ�
- /// �̰߳�ȫ�����ڲ������Ա�֤������ȡ��һ���ԡ�
- /// </summary>
- /// <returns>������õ�ֻ�����ϡ�</returns>
- public IReadOnlyCollection<CameraInfo> GetAllCameras()
- {
- lock (_sync) { return Cameras.ToList().AsReadOnly(); }
- }
- /// <summary>
- /// ���ݱ�ʶ��ȡ�������������Ϣ��
- /// </summary>
- /// <param name="id">����� Guid ��ʶ��</param>
- /// <returns>�ҵ��ض�Ӧ�� <see cref="CameraInfo"/>������ <c>null</c>��</returns>
- public CameraInfo GetCamera(Guid id)
- {
- lock (_sync) { return Cameras.FirstOrDefault(c => c.Id == id); }
- }
- /// <summary>
- /// ���������������á�
- /// - ������ <paramref name="camera"/> Ϊ <c>null</c> ���� <c>null</c>��
- /// - �� Id �Ѵ������滻�������Ӳ����������� CameraNo ��š�
- /// ����������Ὣ������ϳ־û��������ļ���
- /// </summary>
- /// <param name="camera">Ҫ���ӻ���µ�������á�</param>
- /// <returns>�����ӻ���µ� <see cref="CameraInfo"/>������Ϊ <c>null</c> ʱ���� <c>null</c>��</returns>
- public CameraInfo AddOrUpdateCamera(CameraInfo camera)
- {
- if (camera == null) return null;
- lock (_sync)
- {
- var exist = Cameras.FirstOrDefault(c => c.Id == camera.Id);
- if (exist != null)
- {
- var idx = Cameras.IndexOf(exist);
- Cameras[idx] = camera;
- }
- else
- {
- camera.CameraNo = Cameras.Count + 1;
- Cameras.Add(camera);
- }
- }
- FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
- return camera;
- }
- /// <summary>
- /// ���ݱ�ʶ�Ƴ�������ò����±��ʣ������� CameraNo��
- /// ����������Ὣ������ϳ־û��������ļ���
- /// </summary>
- /// <param name="id">Ҫ�Ƴ������ Guid��</param>
- /// <returns>���ҵ����ɹ��Ƴ����� <c>true</c>������ <c>false</c>��</returns>
- public bool RemoveCamera(Guid id)
- {
- bool result = false;
- lock (_sync)
- {
- var exist = Cameras.FirstOrDefault(c => c.Id == id);
- if (exist != null) result = Cameras.Remove(exist);
- //���±��
- int no = 1;
- //�����������CameraNo����
- var sortedCameras = Cameras.OrderBy(c => c.CameraNo).ToList();
- foreach (var cam in sortedCameras)
- {
- cam.CameraNo = no;
- no++;
- }
- Cameras.Clear();
- foreach (var cam in sortedCameras)
- {
- Cameras.Add(cam);
- }
- }
- FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
- return result;
- }
- /// <summary>
- /// ����Ƿ����ָ�� Id ��������á�
- /// </summary>
- /// <param name="id">����� Guid��</param>
- /// <returns>���ڷ��� <c>true</c>������ <c>false</c>��</returns>
- public bool ContainsCamera(Guid id) { lock (_sync) { return Cameras.Any(c => c.Id == id); } }
- #endregion
- #region DeviceInfo
- /// <summary>
- /// 获取所有设备信息配置列表
- /// </summary>
- /// <returns>设备信息列表的只读集合</returns>
- public IReadOnlyCollection<DeviceInfo> GetAllDeviceInfos()
- {
- lock (_sync)
- {
- if (_deviceInfoList == null)
- {
- if (File.Exists(ConfigPaths.DeviceInfoConfigurationPath))
- {
- try
- {
- _deviceInfoList = FileHelper.ReadJsonFile<List<DeviceInfo>>(ConfigPaths.DeviceInfoConfigurationPath);
- }
- catch
- {
- // 兼容旧版单个 DeviceInfo
- var singleDevice = FileHelper.ReadJsonFile<DeviceInfo>(ConfigPaths.DeviceInfoConfigurationPath);
- if (singleDevice != null)
- {
- singleDevice.DeviceNo = 0;
- _deviceInfoList = new List<DeviceInfo> { singleDevice };
- }
- else
- {
- _deviceInfoList = new List<DeviceInfo> { new DeviceInfo { DeviceNo = 0 } };
- }
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- }
- }
- else
- {
- _deviceInfoList = new List<DeviceInfo> { new DeviceInfo { DeviceNo = 0 } };
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- }
- }
- return _deviceInfoList.AsReadOnly();
- }
- }
- /// <summary>
- /// 根据设备编号获取设备信息(兼容旧API,返回第一个设备)
- /// </summary>
- /// <returns>设备信息,若不存在则创建默认设备</returns>
- public DeviceInfo GetDeviceInfo()
- {
- return GetDeviceInfo(0);
- }
- /// <summary>
- /// 根据设备编号获取设备信息
- /// </summary>
- /// <param name="deviceNo">设备编号</param>
- /// <returns>设备信息,若不存在则返回 null</returns>
- public DeviceInfo GetDeviceInfo(int deviceNo)
- {
- lock (_sync)
- {
- var allDevices = GetAllDeviceInfos();
- return allDevices.FirstOrDefault(d => d.DeviceNo == deviceNo);
- }
- }
- /// <summary>
- /// 添加或更新设备信息
- /// </summary>
- /// <param name="deviceInfo">设备信息对象</param>
- public void SaveDeviceInfo(DeviceInfo deviceInfo)
- {
- if (deviceInfo == null) return;
- lock (_sync)
- {
- // 确保列表已初始化
- if (_deviceInfoList == null)
- {
- _deviceInfoList = new List<DeviceInfo>();
- }
- // 查找是否已存在相同设备编号的设备
- var existing = _deviceInfoList.FirstOrDefault(d => d.DeviceNo == deviceInfo.DeviceNo);
- if (existing != null)
- {
- // 更新现有设备
- var index = _deviceInfoList.IndexOf(existing);
- _deviceInfoList[index] = deviceInfo;
- }
- else
- {
- // 添加新设备
- _deviceInfoList.Add(deviceInfo);
- }
- // 按设备编号排序
- _deviceInfoList = _deviceInfoList.OrderBy(d => d.DeviceNo).ToList();
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- }
- }
- /// <summary>
- /// 保存所有设备信息列表
- /// </summary>
- /// <param name="deviceInfoList">设备信息列表</param>
- public void SaveAllDeviceInfos(IEnumerable<DeviceInfo> deviceInfoList)
- {
- if (deviceInfoList == null) return;
- lock (_sync)
- {
- _deviceInfoList = deviceInfoList.OrderBy(d => d.DeviceNo).ToList();
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- }
- }
- /// <summary>
- /// 添加新设备
- /// </summary>
- /// <returns>新添加的设备</returns>
- public DeviceInfo AddDeviceInfo()
- {
- lock (_sync)
- {
- if (_deviceInfoList == null)
- {
- _deviceInfoList = new List<DeviceInfo>();
- }
- // 计算新设备编号
- int newDeviceNo = _deviceInfoList.Count > 0 ? _deviceInfoList.Max(d => d.DeviceNo) + 1 : 0;
- var newDevice = new DeviceInfo { DeviceNo = newDeviceNo };
- _deviceInfoList.Add(newDevice);
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- return newDevice;
- }
- }
- /// <summary>
- /// 删除设备
- /// </summary>
- /// <param name="deviceNo">设备编号</param>
- /// <returns>是否删除成功</returns>
- public bool RemoveDeviceInfo(int deviceNo)
- {
- lock (_sync)
- {
- if (_deviceInfoList == null) return false;
- var device = _deviceInfoList.FirstOrDefault(d => d.DeviceNo == deviceNo);
- if (device != null)
- {
- _deviceInfoList.Remove(device);
- FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
- return true;
- }
- return false;
- }
- }
- #endregion
- #region CTQData
- /// <summary>
- /// ��ȡCTQ��Ϣ����
- /// </summary>
- public CTQ_Data GetCTQDataInfo()
- {
- lock (_sync)
- {
- if (CtqData == null)
- {
- if (File.Exists(ConfigPaths.DataConfigurationPath))
- CtqData = FileHelper.ReadJsonFile<CTQ_Data>(ConfigPaths.DataConfigurationPath);
- else
- {
- CtqData = new CTQ_Data();
- FileHelper.WriteJsonFile(CtqData, ConfigPaths.DataConfigurationPath);
- }
- }
- return CtqData;
- }
- }
- /// <summary>
- /// ����CTQ��Ϣ
- /// </summary>
- public void SaveCTQDataInfo(CTQ_Data cd)
- {
- if (cd == null) return;
- lock (_sync)
- {
- CtqData = cd;
- FileHelper.WriteJsonFile(CtqData, ConfigPaths.DataConfigurationPath);
- }
- }
- #endregion
- #region Robots
- /// <summary>
- /// ��ȡ���л��������õ�ֻ�����ϡ�
- /// </summary>
- /// <returns>���������õ�ֻ�����ϡ�</returns>
- public IReadOnlyCollection<RobotInfo> GetAllRobots() { lock (_sync) { return Robots.ToList().AsReadOnly(); } }
- /// <summary>
- /// ���ݱ�ʶ��ȡ�������������á�
- /// </summary>
- /// <param name="id">�����˵� Guid ��ʶ��</param>
- /// <returns>�ҵ��ض�Ӧ�� <see cref="RobotInfo"/>������ <c>null</c>��</returns>
- public RobotInfo GetRobot(Guid id) { lock (_sync) { return Robots.FirstOrDefault(r => r.Id == id); } }
- /// <summary>
- /// ��������»��������ã�������־û��������ļ���
- /// ����ʱΪ�����˷��������� RobotNo ��š�
- /// </summary>
- /// <param name="robot">Ҫ���ӻ���µĻ��������á�</param>
- /// <returns>�����ӻ���µ� <see cref="RobotInfo"/>������Ϊ <c>null</c> ʱ���� <c>null</c>��</returns>
- public RobotInfo AddOrUpdateRobot(RobotInfo robot)
- {
- if (robot == null) return null;
- lock (_sync)
- {
- var exist = Robots.FirstOrDefault(r => r.Id == robot.Id);
- if (exist != null)
- {
- var idx = Robots.IndexOf(exist);
- Robots[idx] = robot;
- }
- else
- {
- robot.RobotNo = Robots.Count + 1;
- Robots.Add(robot);
- }
- }
- FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
- return robot;
- }
- /// <summary>
- /// ���ݱ�ʶ�Ƴ����������ò����±��ʣ������ˡ�
- /// ������־û������ļ���
- /// </summary>
- /// <param name="id">Ҫ�Ƴ��Ļ����� Guid��</param>
- /// <returns>�Ƴ��ɹ����� <c>true</c>������ <c>false</c>��</returns>
- public bool RemoveRobot(Guid id)
- {
- bool result = false;
- lock (_sync)
- {
- var e = Robots.FirstOrDefault(r => r.Id == id);
- if (e != null)
- result = Robots.Remove(e);
- //���±��
- int no = 1;
- //�����л����˰�RobotNo����
- var sortedRobots = Robots.OrderBy(r => r.RobotNo).ToList();
- foreach (var r in sortedRobots)
- {
- r.RobotNo = no;
- no++;
- }
- Robots.Clear();
- foreach (var r in sortedRobots)
- {
- Robots.Add(r);
- }
- }
- FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
- return result;
- }
- /// <summary>
- /// �ж��Ƿ����ָ�� Id �Ļ��������á�
- /// </summary>
- /// <param name="id">�����˵� Guid��</param>
- /// <returns>���ڷ��� <c>true</c>������ <c>false</c>��</returns>
- public bool ContainsRobot(Guid id) { lock (_sync) { return Robots.Any(r => r.Id == id); } }
- /// <summary>
- /// ��ָ�������� Id�IJ������벢�־û����������á�
- /// </summary>
- /// <param name="id">������Ψһ��ʶ����</param>
- /// <param name="stepDistance">�������롣</param>
- /// <returns>���³ɹ����� <c>true</c>���Ҳ��������˷��� <c>false</c>��</returns>
- public bool UpdateRobotStepDistance(Guid id, double stepDistance)
- {
- lock (_sync)
- {
- var robot = Robots.FirstOrDefault(r => r.Id == id);
- if (robot != null)
- {
- robot.StepDistance = stepDistance;
- FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- /// <summary>
- /// ��ȡָ�������� Id �IJ������롣
- /// </summary>
- /// <param name="id">������ Guid��</param>
- /// <returns>���ҵ��ض�Ӧ�IJ������룬����Ĭ��ֵ 1.0��</returns>
- public double GetRobotStepDistance(Guid id)
- {
- lock (_sync)
- {
- var robot = Robots.FirstOrDefault(r => r.Id == id);
- if (robot != null)
- {
- return robot.StepDistance;
- }
- else
- {
- return 1.0;
- }
- }
- }
- #endregion
- #region Feeders
- /// <summary>
- /// ��ȡ���й��������õ�ֻ�����ϡ�
- /// </summary>
- /// <returns>���������õ�ֻ�����ϡ�</returns>
- public IReadOnlyCollection<FeederInfo> GetAllFeeders() { lock (_sync) { return Feeders.ToList().AsReadOnly(); } }
- /// <summary>
- /// ���ݱ�ʶ��ȡ�������������á�
- /// </summary>
- /// <param name="id">�������� Guid ��ʶ��</param>
- /// <returns>�ҵ��ض�Ӧ�� <see cref="FeederInfo"/>������ <c>null</c>��</returns>
- public FeederInfo GetFeeder(Guid id) { lock (_sync) { return Feeders.FirstOrDefault(f => f.Id == id); } }
- /// <summary>
- /// ��������¹��������ã�������־û��������ļ���
- /// ����ʱΪ���������������� FeederNo ��š�
- /// </summary>
- /// <param name="feeder">Ҫ���ӻ���µĹ��������á�</param>
- /// <returns>�����ӻ���µ� <see cref="FeederInfo"/>������Ϊ <c>null</c> ʱ���� <c>null</c>��</returns>
- public FeederInfo AddOrUpdateFeeder(FeederInfo feeder)
- {
- if (feeder == null) return null;
- lock (_sync)
- {
- var exist = Feeders.FirstOrDefault(f => f.Id == feeder.Id);
- if (exist != null)
- {
- var idx = Feeders.IndexOf(exist);
- Feeders[idx] = feeder;
- }
- else
- {
- feeder.FeederNo = Feeders.Count + 1;
- Feeders.Add(feeder);
- }
- }
- FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
- return feeder;
- }
- /// <summary>
- /// ���ݱ�ʶ�Ƴ����������ò����±��ʣ�������
- /// ������־û������ļ���
- /// </summary>
- /// <param name="id">Ҫ�Ƴ��Ĺ����� Guid��</param>
- /// <returns>�Ƴ��ɹ����� <c>true</c>������ <c>false</c>��</returns>
- public bool RemoveFeeder(Guid id)
- {
- bool result = false;
- lock (_sync)
- {
- var e = Feeders.FirstOrDefault(f => f.Id == id);
- if (e != null) result = Feeders.Remove(e);
- //���±��
- int no = 1;
- //�����������FeederNo����
- var sortedFeeders = Feeders.OrderBy(f => f.FeederNo).ToList();
- foreach (var f in sortedFeeders)
- {
- f.FeederNo = no;
- no++;
- }
- Feeders.Clear();
- foreach (var f in sortedFeeders)
- {
- Feeders.Add(f);
- }
- }
- FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
- return result;
- }
- /// <summary>
- /// �ж��Ƿ����ָ�� Id �Ĺ��������á�
- /// </summary>
- /// <param name="id">������ Guid��</param>
- /// <returns>���ڷ��� <c>true</c>������ <c>false</c>��</returns>
- public bool ContainsFeeder(Guid id) { lock (_sync) { return Feeders.Any(f => f.Id == id); } }
- #endregion
- #region PLC
- /// <summary>
- /// ��ȡ���� PLC ���õ�ֻ�����ϡ�
- /// </summary>
- /// <returns>PLC ���õ�ֻ�����ϡ�</returns>
- public IReadOnlyCollection<PlcInfo> GetAllPlcs() { lock (_sync) { return Plcs.ToList().AsReadOnly(); } }
- /// <summary>
- /// ���ݱ�ʶ��ȡ���� PLC ���á�
- /// </summary>
- /// <param name="id">PLC �� Guid ��ʶ��</param>
- /// <returns>ƥ��� <see cref="PlcInfo"/> ʵ�����Ҳ������� <c>null</c>��</returns>
- public PlcInfo GetPlc(Guid id) { lock (_sync) { return Plcs.FirstOrDefault(p => p.Id == id); } }
- /// <summary>
- /// ��������� PLC ���ã�������־û��������ļ�������ʱ�������� PlcNo ��š�
- /// </summary>
- /// <param name="plc">Ҫ���ӻ���µ� PLC ���á�</param>
- /// <returns>�����ӻ���µ� <see cref="PlcInfo"/>������Ϊ <c>null</c> ʱ���� <c>null</c>��</returns>
- public PlcInfo AddOrUpdatePlc(PlcInfo plc)
- {
- if (plc == null) return null;
- lock (_sync)
- {
- var exist = Plcs.FirstOrDefault(p => p.Id == plc.Id);
- if (exist != null)
- {
- var idx = Plcs.IndexOf(exist);
- Plcs[idx] = plc;
- }
- else
- {
- plc.PlcNo = Plcs.Count + 1;
- Plcs.Add(plc);
- }
- }
- FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
- return plc;
- }
- /// <summary>
- /// ���ݱ�ʶ�Ƴ� PLC ���ò����±��ʣ�� PLC��
- /// ������־û������ļ���
- /// </summary>
- /// <param name="id">Ҫ�Ƴ��� PLC Guid��</param>
- /// <returns>�Ƴ��ɹ����� <c>true</c>������ <c>false</c>��</returns>
- public bool RemovePlc(Guid id)
- {
- bool result = false;
- lock (_sync)
- {
- var e = Plcs.FirstOrDefault(p => p.Id == id);
- if (e != null) result = Plcs.Remove(e);
- //���±��
- int no = 1;
- //������PLC��PlcNo����
- var sortedPlcs = Plcs.OrderBy(p => p.PlcNo).ToList();
- foreach (var p in sortedPlcs)
- {
- p.PlcNo = no;
- no++;
- }
- Plcs.Clear();
- foreach (var p in sortedPlcs)
- {
- Plcs.Add(p);
- }
- }
- FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
- return result;
- }
- /// <summary>
- /// �ж��Ƿ����ָ�� Id �� PLC ���á�
- /// </summary>
- /// <param name="id">PLC �� Guid��</param>
- /// <returns>���ڷ��� <c>true</c>������ <c>false</c>��</returns>
- public bool ContainsPlc(Guid id) { lock (_sync) { return Plcs.Any(p => p.Id == id); } }
- #endregion
- #region BgTcp
- /// <summary>
- /// ��ȡ BG TCP/IP ͨ�����ã��ڴ������Ϊ null����
- /// </summary>
- /// <returns>��ǰ�� <see cref="BgTcpIP"/> ���ö���</returns>
- public BgTcpIP GetBgTcp() { lock (_sync) { return BgCommunicate; } }
- /// <summary>
- /// ���� BG TCP/IP ͨ�����ã��������ڴ�������Զ��־û�����
- /// ����־û������ <see cref="SaveBgTcp"/>��
- /// </summary>
- /// <param name="cfg">�µ� <see cref="BgTcpIP"/> ���ö���</param>
- public void SetBgTcp(BgTcpIP cfg) { lock (_sync) { BgCommunicate = cfg; } }
- /// <summary>
- /// ����ǰ�ڴ��е� BG TCP/IP ���ó־û��������ļ���
- /// </summary>
- public void SaveBgTcp() { lock (_sync) { FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath); } }
- #endregion
- #region BgModbus
- /// <summary>
- /// ��ȡ BG Modbus TCP ͨ�����ã��ڴ����
- /// </summary>
- /// <returns>��ǰ�� <see cref="BgModbusTcp"/> ���ö���</returns>
- public BgModbusTcp GetBgModbusTcp() { lock (_sync) { return BgModbusCommunicate; } }
- /// <summary>
- /// ���� BG Modbus TCP ͨ�����ã��������ڴ�������Զ��־û�����
- /// </summary>
- /// <param name="cfg">�µ� <see cref="BgModbusTcp"/> ���ö���</param>
- public void SetBgModbusTcp(BgModbusTcp cfg) { lock (_sync) { BgModbusCommunicate = cfg; } }
- /// <summary>
- /// ����ǰ�ڴ��е� BG Modbus TCP ���ó־û��������ļ���
- /// </summary>
- public void SaveBgModbusTcp() { lock (_sync) { FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath); } }
- #endregion
- #region ϵͳ����
- /// <summary>
- /// ��ȡϵͳ���ã��������ļ���ȡ����
- /// �̰߳�ȫ�������ڲ������Ա�֤��ȡһ���ԡ�
- /// </summary>
- /// <returns>��ȡ���� <see cref="SystemConfiguration"/> �������ļ���������Ĭ�ϵ���ʵ����</returns>
- public SystemConfiguration GetSystemConfiguration()
- {
- lock (_sync)
- {
- var config = new SystemConfiguration();
- if (File.Exists(ConfigPaths.SystemConfigurationPath))
- {
- config = FileHelper.ReadJsonFile<SystemConfiguration>(ConfigPaths.SystemConfigurationPath);
- }
- return config;
- }
- }
- /// <summary>
- /// ����ϵͳ���õ������ļ����Ḳ�������ļ�����
- /// </summary>
- /// <param name="systemConfiguration">Ҫ�����ϵͳ���ö���</param>
- public void SaveSystemConfiguration(SystemConfiguration systemConfiguration)
- {
- lock (_sync)
- {
- FileHelper.WriteJsonFile(systemConfiguration, ConfigPaths.SystemConfigurationPath);
- }
- }
- /// <summary>
- /// ��ȡ��ǰ����
- /// </summary>
- /// <returns></returns>
- public Language GetCurrentLanguage()
- {
- lock (_sync)
- {
- var config = GetSystemConfiguration();
- return config.CurrentLanguage;
- }
- }
- /// <summary>
- /// ���õ�ǰ����
- /// </summary>
- /// <param name="language"></param>
- public void SetCurrentLanguage(Language language)
- {
- lock (_sync)
- {
- var config = GetSystemConfiguration();
- config.CurrentLanguage = language;
- if (language == Enums.Language.ChineseSimplified)
- {
- var culture = new CultureInfo("zh-CN");
- App.Current.Dispatcher.Thread.CurrentCulture = culture;
- App.Current.Dispatcher.Thread.CurrentUICulture = culture;
- LocalizeDictionary.Instance.Culture = culture;
- Lang.Culture = culture;
- System.Windows.Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Microsoft YaHei");
- }
- else if (language == Enums.Language.ChineseTraditional)
- {
- var culture = new CultureInfo("zh-TW");
- App.Current.Dispatcher.Thread.CurrentCulture = culture;
- App.Current.Dispatcher.Thread.CurrentUICulture = culture;
- LocalizeDictionary.Instance.Culture = culture;
- Lang.Culture = culture;
- System.Windows.Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Microsoft YaHei");
- }
- else if (language == Enums.Language.English)
- {
- var culture = new CultureInfo("en");
- App.Current.Dispatcher.Thread.CurrentCulture = culture;
- App.Current.Dispatcher.Thread.CurrentUICulture = culture;
- LocalizeDictionary.Instance.Culture = culture;
- Lang.Culture = culture;
- System.Windows.Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Segoe UI");
- }
- else if (language == Enums.Language.Japanese)
- {
- var culture = new CultureInfo("ja-JP");
- App.Current.Dispatcher.Thread.CurrentCulture = culture;
- App.Current.Dispatcher.Thread.CurrentUICulture = culture;
- LocalizeDictionary.Instance.Culture = culture;
- Lang.Culture = culture;
- System.Windows.Application.Current.Resources["DefaultFont"] = new System.Windows.Media.FontFamily("Segoe UI");
- }
- SaveSystemConfiguration(config);
- }
- }
- #endregion
- #region Feeder��������
- /// <summary>
- /// ��ȡ�������������ֻ�����ϡ�
- /// </summary>
- /// <returns>���������ֻ�����ϡ�</returns>
- public IReadOnlyCollection<FeederClearWork> GetAllClearanceTasks() { lock (_sync) { return FeederClearanceTasks.ToList().AsReadOnly(); } }
- /// <summary>
- /// ���������������������ʱΪ������������ TaskCode����ɺ�־û������ļ���
- /// </summary>
- /// <param name="task">Ҫ���ӻ���µ������������</param>
- /// <returns>�����ӻ���µ� <see cref="FeederClearWork"/>������Ϊ <c>null</c> ʱ���� <c>null</c>��</returns>
- public FeederClearWork AddOrUpdateClearanceTask(FeederClearWork task)
- {
- if (task == null) return null;
- lock (_sync)
- {
- var exist = FeederClearanceTasks.FirstOrDefault(t => t.Id == task.Id);
- if (exist != null)
- {
- var idx = FeederClearanceTasks.IndexOf(exist);
- FeederClearanceTasks[idx] = task;
- }
- else
- {
- // ����������Ϊ��ǰ����ż�1
- task.TaskCode = FeederClearanceTasks.Count > 0 ? FeederClearanceTasks.Max(t => t.TaskCode) + 1 : 1;
- FeederClearanceTasks.Add(task);
- }
- }
- FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
- return task;
- }
- /// <summary>
- /// ���ݱ�ʶ�Ƴ��������־û������ļ���
- /// </summary>
- /// <param name="id">Ҫ�Ƴ����������� Guid��</param>
- /// <returns>���ҵ����Ƴ����� <c>true</c>������ <c>false</c>��</returns>
- public bool RemoveClearanceTask(Guid id)
- {
- lock (_sync)
- {
- var exist = FeederClearanceTasks.FirstOrDefault(t => t.Id == id);
- if (exist != null)
- {
- FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
- return FeederClearanceTasks.Remove(exist);
- }
- else
- return false;
- }
- }
- /// <summary>
- /// ��������������־û��ռ��ϵ������ļ���
- /// </summary>
- public void ClearAllClearanceTasks()
- {
- lock (_sync)
- {
- FeederClearanceTasks.Clear();
- }
- FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
- }
- /// <summary>
- /// �ж��Ƿ����ָ�� Id ����������
- /// </summary>
- /// <param name="id">�������� Guid��</param>
- /// <returns>���ڷ��� <c>true</c>������ <c>false</c>��</returns>
- public bool ContainsClearanceTask(Guid id)
- {
- lock (_sync) { return FeederClearanceTasks.Any(t => t.Id == id); }
- }
- /// <summary>
- /// ���ݱ�ʶ��ȡ��������������Ϣ��
- /// </summary>
- /// <param name="id">�������� Guid��</param>
- /// <returns>ƥ��� <see cref="FeederClearWork"/>���Ҳ������� <c>null</c>��</returns>
- public FeederClearWork GetClearanceTask(Guid id)
- {
- lock (_sync) { return FeederClearanceTasks.FirstOrDefault(t => t.Id == id); }
- }
- /// <summary>
- /// �ж��Ƿ����ָ�������ŵ���������
- /// </summary>
- /// <param name="taskNumber">�����ţ�TaskCode����</param>
- /// <returns>���ڷ��� <c>true</c>������ <c>false</c>��</returns>
- public bool ContainsClearanceTaskByNumber(int taskNumber)
- {
- lock (_sync) { return FeederClearanceTasks.Any(t => t.TaskCode == taskNumber); }
- }
- /// <summary>
- /// ���������Ż�ȡ��������������Ϣ��
- /// </summary>
- /// <param name="taskNumber">�����ţ�TaskCode����</param>
- /// <returns>ƥ��� <see cref="FeederClearWork"/>���Ҳ������� <c>null</c>��</returns>
- public FeederClearWork GetClearanceTaskByNumber(int taskNumber)
- {
- lock (_sync) { return FeederClearanceTasks.FirstOrDefault(t => t.TaskCode == taskNumber); }
- }
- #endregion
- #region Light controllers and channels
- /// <summary>
- /// ��ȡ���й�Դ���������õ�ֻ�����ϡ�
- /// �̰߳�ȫ�����ڲ�ʹ�� <see cref="_sync"/> ���������Ա�֤�������ʰ�ȫ��
- /// </summary>
- /// <returns>����ǰ�ڴ��д洢˳�ص�ֻ�� <see cref="LightControllerConfig"/> ���ϡ�</returns>
- public IReadOnlyCollection<LightControllerConfig> GetAllLightControllers()
- {
- lock (_sync) { return LightControllers.ToList().AsReadOnly(); }
- }
- /// <summary>
- /// ���ݿ�������ʶ��ȡ������Դ���������á�
- /// �̰߳�ȫ���÷������ڴ漯�Ͻ���ֻ�����ʲ����ڲ�������
- /// </summary>
- /// <param name="id">��������������ʶ��Id����</param>
- /// <returns>ƥ��� <see cref="LightControllerConfig"/> ʵ�����Ҳ����� <c>null</c>��</returns>
- public LightControllerConfig GetLightController(int id)
- {
- lock (_sync) { return LightControllers.FirstOrDefault(c => c.Id == id); }
- }
- /// <summary>
- /// ��������¹�Դ���������á�
- /// - �� <paramref name="controller"/> Ϊ <c>null</c> ���� <c>null</c>��
- /// - ����ʱ�� Id ƥ��������滻��
- /// - ����ʱ���������Ŀ����� Id����Ϊ�������ڵ�ÿ��ͨ������ȫ��ͨ����ţ�GlobalIndex����
- /// �̰߳�ȫ�����ڲ�ʹ�� <see cref="_sync"/> �������ϡ�
- /// </summary>
- /// <param name="controller">Ҫ���ӻ���µ� <see cref="LightControllerConfig"/> ʵ����</param>
- /// <returns>�����ӻ���µ� <see cref="LightControllerConfig"/> ʵ��������Ϊ <c>null</c> ʱ���� <c>null</c>��</returns>
- /// <exception cref="ArgumentException">��Ҫ����/���µĿ��������������������������ظ�ʱ�׳���</exception>
- public LightControllerConfig AddOrUpdateLightController(LightControllerConfig controller)
- {
- if (controller == null) return null;
- lock (_sync)
- {
- // ���Ʋ����������������ظ���ͬ������ͬ Id ��������
- if (LightControllers.Any(c => c.Name == controller.Name && c.Id != controller.Id))
- throw new ArgumentException("Controller name must be unique");
- var exist = LightControllers.FirstOrDefault(c => c.Id == controller.Id);
- if (exist != null)
- {
- // �������п��������������滻��
- var idx = LightControllers.IndexOf(exist);
- LightControllers[idx] = controller;
- }
- else
- {
- // ����������
- // ������������Ϊ��ǰ��� Id + 1��������Ϊ����Ϊ 1��
- controller.Id = LightControllers.Count > 0 ? LightControllers.Max(c => c.Id) + 1 : 1;
- // ������һ��ȫ��ͨ���������������Ѵ��ڿ�������ͨ�� GlobalIndex ��ȡ���ֵ����1
- int nextGlobal = 0;
- if (LightControllers.SelectMany(c => c.ChannelConfigs).Any())
- {
- nextGlobal = LightControllers.SelectMany(c => c.ChannelConfigs).Max(ch => ch.GlobalIndex) + 1;
- }
- // ����������������ͨ������ͨ���� GlobalIndex ��Ч��<0����������ͨ����ͻ�������·���
- for (int i = 0; i < controller.ChannelConfigs.Count; i++)
- {
- var ch = controller.ChannelConfigs[i];
- if (ch.GlobalIndex < 0 || LightControllers.SelectMany(c => c.ChannelConfigs).Any(existingCh => existingCh.GlobalIndex == ch.GlobalIndex))
- {
- ch.GlobalIndex = nextGlobal;
- nextGlobal++;
- }
- }
- LightControllers.Add(controller);
- }
- }
- // �־û����º�Ĺ�Դ����������
- FileHelper.WriteJsonFile(LightControllers, ConfigPaths.LightControllersConfigurationPath);
- return controller;
- }
- /// <summary>
- /// ���� Id �Ƴ���Դ���������ò���ʣ����������±�ţ��� 1 ��ʼ������ţ���
- /// ������־û������ļ���
- /// </summary>
- /// <param name="id">Ҫ�Ƴ��Ŀ����� Id��</param>
- /// <returns>����ɹ��Ƴ����� <c>true</c>������ <c>false</c>������δ�ҵ��� Id����</returns>
- public bool RemoveLightController(int id)
- {
- bool result = false;
- lock (_sync)
- {
- var e = LightControllers.FirstOrDefault(c => c.Id == id);
- if (e != null)
- result = LightControllers.Remove(e);
- // ���°� Id ���� 1 ��ʼ���±�ţ��Ա���������
- int no = 1;
- var sorted = LightControllers.OrderBy(c => c.Id).ToList();
- foreach (var c in sorted)
- {
- c.Id = no;
- no++;
- }
- LightControllers.Clear();
- foreach (var c in sorted)
- {
- LightControllers.Add(c);
- }
- }
- FileHelper.WriteJsonFile(LightControllers, ConfigPaths.LightControllersConfigurationPath);
- return result;
- }
- /// <summary>
- /// �ж��Ƿ����ָ�� Id �Ĺ�Դ��������
- /// </summary>
- /// <param name="id">������ Id��</param>
- /// <returns>���ڷ��� <c>true</c>������ <c>false</c>��</returns>
- public bool ContainsLightController(int id) { lock (_sync) { return LightControllers.Any(c => c.Id == id); } }
- /// <summary>
- /// ��ȡ���й�Դͨ����ֻ�����ϣ��� GlobalIndex ���ء�
- /// </summary>
- /// <returns>�� GlobalIndex ����� <see cref="ChannelConfig"/> ֻ�����ϡ�</returns>
- public IReadOnlyCollection<ChannelConfig> GetAllLightChannels()
- {
- lock (_sync) { return LightControllers.SelectMany(c => c.ChannelConfigs).OrderBy(ch => ch.GlobalIndex).ToList().AsReadOnly(); }
- }
- /// <summary>
- /// ����ȫ��ͨ��������ȡ����ͨ�����á�
- /// </summary>
- /// <param name="globalIndex">ͨ����ȫ��������GlobalIndex����</param>
- /// <returns>ƥ��� <see cref="ChannelConfig"/> ʵ�����Ҳ����� <c>null</c>��</returns>
- public ChannelConfig GetLightChannelByGlobalIndex(int globalIndex)
- {
- lock (_sync) { return LightControllers.SelectMany(c => c.ChannelConfigs).FirstOrDefault(ch => ch.GlobalIndex == globalIndex); }
- }
- /// <summary>
- /// �ж��Ƿ����ָ��ȫ��ͨ��������ͨ�����á�
- /// </summary>
- /// <param name="globalIndex">ȫ��ͨ��������</param>
- /// <returns>���ڷ��� <c>true</c>������ <c>false</c>��</returns>
- public bool ContainsLightChannelByGlobalIndex(int globalIndex)
- {
- lock (_sync) { return LightControllers.SelectMany(c => c.ChannelConfigs).Any(ch => ch.GlobalIndex == globalIndex); }
- }
- /// <summary>
- /// ����ָ��ȫ��ͨ����Ĭ�����ȣ�DefaultBrightness�������־û���Դ���������á�
- /// </summary>
- /// <param name="globalIndex">Ŀ��ͨ����ȫ��������GlobalIndex����</param>
- /// <param name="brightness">Ҫ���õ�����ֵ��</param>
- /// <returns>���³ɹ����� <c>true</c>���Ҳ�����Ӧͨ������ <c>false</c>��</returns>
- public bool UpdateChannelDefaultBrightness(int globalIndex, int brightness)
- {
- lock (_sync)
- {
- var ch = LightControllers.SelectMany(c => c.ChannelConfigs).FirstOrDefault(x => x.GlobalIndex == globalIndex);
- if (ch == null) return false;
- ch.DefaultBrightness = brightness;
- FileHelper.WriteJsonFile(LightControllers, ConfigPaths.LightControllersConfigurationPath);
- return true;
- }
- }
- #endregion
- #region Plc Addresses
- public PlcAddressConfig GetPlcAddresses() { lock (_sync) { return PlcAddressConfig; } }
- #endregion
- #region ScrewDriver config (single device)
- /// <summary>
- /// ��ȡ�������ã����豸����
- /// </summary>
- /// <returns>��ǰ�� <see cref="ScrewDriverConfig"/>����δ������ null��</returns>
- public ScrewDriverConfig GetScrewDriverConfig()
- {
- lock (_sync) { return _screwDriverConfig; }
- }
- /// <summary>
- /// ���ò��־û��������ã����豸����
- /// </summary>
- /// <param name="cfg">�µ����ö�����Ϊ�ա�</param>
- public void SetScrewDriverConfig(ScrewDriverConfig cfg)
- {
- if (cfg == null) throw new ArgumentNullException(nameof(cfg));
- lock (_sync)
- {
- _screwDriverConfig = cfg;
- FileHelper.WriteJsonFile(_screwDriverConfig, ConfigPaths.ScrewDriverConfigurationPath);
- }
- }
- /// <summary>
- /// ������ǰ�ڴ��еĵ������ñ��浽�ļ���
- /// </summary>
- public void SaveScrewDriverConfig()
- {
- lock (_sync)
- {
- if (_screwDriverConfig != null)
- FileHelper.WriteJsonFile(_screwDriverConfig, ConfigPaths.ScrewDriverConfigurationPath);
- }
- }
- #endregion
- #region Screw Feeders (material)
- /// <summary>
- /// ��ȡ������˿���������õ�ֻ�����ϡ�
- /// </summary>
- public IReadOnlyCollection<ScrewFeederInfo> GetAllScrewFeeders() { lock (_sync) { return ScrewFeeders.ToList().AsReadOnly(); } }
- /// <summary>
- /// ���� Id ��ȡ������˿���������á�
- /// </summary>
- public ScrewFeederInfo GetScrewFeeder(Guid id) { lock (_sync) { return ScrewFeeders.FirstOrDefault(s => s.Id == id); } }
- /// <summary>
- /// �����������˿���������á�
- /// ����ʱ��δָ�� FeederNumber �����Ϊ��ǰ����+1��
- /// </summary>
- public ScrewFeederInfo AddOrUpdateScrewFeeder(ScrewFeederInfo feeder)
- {
- if (feeder == null) return null;
- lock (_sync)
- {
- var exist = ScrewFeeders.FirstOrDefault(f => f.Id == feeder.Id);
- if (exist != null)
- {
- var idx = ScrewFeeders.IndexOf(exist);
- ScrewFeeders[idx] = feeder;
- }
- else
- {
- if (feeder.FeederNumber <= 0) feeder.FeederNumber = ScrewFeeders.Count + 1;
- ScrewFeeders.Add(feeder);
- }
- FileHelper.WriteJsonFile(ScrewFeeders, ConfigPaths.ScrewFeedersConfigurationPath);
- }
- return feeder;
- }
- /// <summary>
- /// �Ƴ���˿���������á�
- /// </summary>
- public bool RemoveScrewFeeder(Guid id)
- {
- bool result = false;
- lock (_sync)
- {
- var e = ScrewFeeders.FirstOrDefault(s => s.Id == id);
- if (e != null) result = ScrewFeeders.Remove(e);
- }
- FileHelper.WriteJsonFile(ScrewFeeders, ConfigPaths.ScrewFeedersConfigurationPath);
- return result;
- }
- public bool ContainsScrewFeeder(Guid id) { lock (_sync) { return ScrewFeeders.Any(s => s.Id == id); } }
- // event for low level alarm
- public event Action<ScrewFeederInfo> ScrewFeederLowLevelAlarmTriggered;
- /// <summary>
- /// ��ȡָ����������ŵ�ʣ����˿�������Ҳ������� -1��
- /// </summary>
- public int GetRemainingCountByFeederNumber(int feederNumber)
- {
- lock (_sync)
- {
- var f = ScrewFeeders.FirstOrDefault(s => s.FeederNumber == feederNumber);
- if (f == null) return -1;
- return f.RemainingCount;
- }
- }
- /// <summary>
- /// ��ȡָ����������ŵĵ�������״̬���Ҳ������� false��
- /// </summary>
- public bool GetLowLevelAlarmStatusByFeederNumber(int feederNumber)
- {
- lock (_sync)
- {
- var f = ScrewFeeders.FirstOrDefault(s => s.FeederNumber == feederNumber);
- if (f == null) return false;
- return f.IsLowLevelAlarmActive;
- }
- }
- // helper: check and raise low level alarm when necessary
- private void CheckAndTriggerLowLevelAlarm(ScrewFeederInfo f)
- {
- if (f == null) return;
- if (f.LowLevelAlarmEnabled && f.RemainingCount <= f.LowLevelThreshold)
- {
- if (!f.IsLowLevelAlarmActive)
- {
- f.IsLowLevelAlarmActive = true;
- try
- {
- ScrewFeederLowLevelAlarmTriggered?.Invoke(f.Clone());
- }
- catch { }
- }
- }
- else
- {
- if (f.IsLowLevelAlarmActive)
- {
- f.IsLowLevelAlarmActive = false;
- }
- }
- }
- /// <summary>
- /// Ϊָ����������ŵݼ�ָ�����������������㷵�� -1
- /// </summary>
- public int DecrementScrewCountByFeederNumber(int feederNumber, int decrement = 1)
- {
- lock (_sync)
- {
- var f = ScrewFeeders.FirstOrDefault(s => s.FeederNumber == feederNumber);
- if (f == null) return -1;
- if (decrement <= 0) decrement = 1;
- if (f.RemainingCount < decrement) return -1;
- f.RemainingCount -= decrement;
- // check alarm
- CheckAndTriggerLowLevelAlarm(f);
- FileHelper.WriteJsonFile(ScrewFeeders, ConfigPaths.ScrewFeedersConfigurationPath);
- return f.RemainingCount;
- }
- }
- /// <summary>
- /// ���ָ����������ŵ���˿��Ϣ�����κ��ÿգ�ʣ��������0��
- /// </summary>
- public void ClearScrewFeederByNumber(int feederNumber)
- {
- lock (_sync)
- {
- var f = ScrewFeeders.FirstOrDefault(s => s.FeederNumber == feederNumber);
- if (f == null) return;
- f.BatchNumber = string.Empty;
- f.RemainingCount = 0;
- // check alarm
- CheckAndTriggerLowLevelAlarm(f);
- FileHelper.WriteJsonFile(ScrewFeeders, ConfigPaths.ScrewFeedersConfigurationPath);
- }
- }
- /// <summary>
- /// Ϊָ������������������κţ�����ԭ��ʣ������������һ����PackSize��
- /// </summary>
- public void SetBatchForFeederNumber(int feederNumber, string batchNumber)
- {
- if (string.IsNullOrWhiteSpace(batchNumber)) return;
- //��ȡ��ǰ�IJ���Ա�û���
- string operatorName = _systemDatabaseService.GetCurrentUser()?.UserName;
- // capture values under lock, then record to DB outside lock
- ScrewFeederInfo target = null;
- int remainingAfter = 0;
- int packSize = 0;
- string feederName = null;
- int previousCount = 0;
- lock (_sync)
- {
- var f = ScrewFeeders.FirstOrDefault(s => s.FeederNumber == feederNumber);
- if (f == null) return;
- f.BatchNumber = batchNumber;
- previousCount = f.RemainingCount;
- f.RemainingCount += Math.Max(0, f.PackSize);
- // prepare capture values
- target = f;
- remainingAfter = f.RemainingCount;
- packSize = f.PackSize;
- feederName = f.Name;
- // check alarm
- CheckAndTriggerLowLevelAlarm(f);
- FileHelper.WriteJsonFile(ScrewFeeders, ConfigPaths.ScrewFeedersConfigurationPath);
- }
- try
- {
- var record = new ScrewFeederBatchRecord
- {
- BatchNumber = batchNumber,
- PackSize = packSize,
- ChangeTime = DateTime.UtcNow,
- RemainingCount = remainingAfter,
- FeederNumber = feederNumber,
- FeederName = feederName,
- PreviousCount = previousCount,
- OperatorName = operatorName,
- };
- // try get operator name from system database service if available
- try
- {
- var user = _systemDatabaseService?.GetCurrentUser();
- if (user != null)
- record.OperatorName = user.UserName;
- }
- catch
- {
- // ignore getting user failure
- }
- try
- {
- if (_systemDatabaseService != null)
- {
- _ = _systemDatabaseService.RecordScrewFeederBatchAsync(record);
- }
- }
- catch
- {
- // �������ݿ�д�����¼��������־
- }
- }
- catch
- {
- // ignore any error creating/recording the batch record
- }
- }
- /// <summary>
- /// ���ʣ����˿�Ƿ�
- /// </summary>
- public bool CheckAlarm(int feederNumber)
- {
- lock (_sync)
- {
- var f = ScrewFeeders.FirstOrDefault(s => s.FeederNumber == feederNumber);
- if (f == null) return false;
- // check alarm
- CheckAndTriggerLowLevelAlarm(f);
- return f.IsLowLevelAlarmActive;
- }
- }
- #endregion
- #region �û���¼ˢ����
- /// <summary>
- /// ��ȡˢ��������
- /// </summary>
- /// <returns></returns>
- public SerialPortConfig GetCardReaderConfig()
- {
- lock (_sync)
- {
- if (_cardReaderConfig != null)
- return _cardReaderConfig;
- if (File.Exists(ConfigPaths.CardReaderConfigurationPath))
- {
- try
- {
- _cardReaderConfig = FileHelper.ReadJsonFile<SerialPortConfig>(ConfigPaths.CardReaderConfigurationPath);
- }
- catch
- {
- // ����ȡʧ�ܣ�����Ĭ�����ò������ļ�������Ŀ���������ò���һ�£�
- _cardReaderConfig = new SerialPortConfig();
- try { FileHelper.WriteJsonFile(_cardReaderConfig, ConfigPaths.CardReaderConfigurationPath); } catch { }
- }
- }
- else
- {
- _cardReaderConfig = new SerialPortConfig();
- try { FileHelper.WriteJsonFile(_cardReaderConfig, ConfigPaths.CardReaderConfigurationPath); } catch { }
- }
- return _cardReaderConfig;
- }
- }
- /// <summary>
- /// ����ˢ��������
- /// </summary>
- /// <param name="config"></param>
- public void SaveCardReaderConfig(SerialPortConfig config)
- {
- if (config == null) return;
- lock (_sync)
- {
- _cardReaderConfig = config;
- try
- {
- FileHelper.WriteJsonFile(_cardReaderConfig, ConfigPaths.CardReaderConfigurationPath);
- }
- catch
- {
- // ���Գ־û���������Ŀ���������淽��һ�£���������Ҫʱ������־��¼
- }
- }
- }
- #endregion
- #region ��ȡ��������
- public void SaveTorqueReaderConfig(TorqueSerialPortConfig config)
- {
- if (config == null) return;
- lock (_sync)
- {
- _torqueReaderConfig = config;
- try
- {
- FileHelper.WriteJsonFile(_torqueReaderConfig, ConfigPaths.TorqueReaderConfigurationPath);
- }
- catch
- {
- // ���Գ־û���������Ŀ���������淽��һ�£���������Ҫʱ������־��¼
- }
- }
- }
- public TorqueSerialPortConfig GetTorqueReaderConfig()
- {
- lock (_sync)
- {
- if (_torqueReaderConfig != null)
- return _torqueReaderConfig;
- if (File.Exists(ConfigPaths.TorqueReaderConfigurationPath))
- {
- try
- {
- _torqueReaderConfig = FileHelper.ReadJsonFile<TorqueSerialPortConfig>(ConfigPaths.TorqueReaderConfigurationPath);
- }
- catch
- {
- // ����ȡʧ�ܣ�����Ĭ�����ò������ļ�������Ŀ���������ò���һ�£�
- _torqueReaderConfig = new TorqueSerialPortConfig();
- try { FileHelper.WriteJsonFile(_torqueReaderConfig, ConfigPaths.TorqueReaderConfigurationPath); } catch { }
- }
- }
- else
- {
- _torqueReaderConfig = new TorqueSerialPortConfig();
- try { FileHelper.WriteJsonFile(_torqueReaderConfig, ConfigPaths.TorqueReaderConfigurationPath); } catch { }
- }
- return _torqueReaderConfig;
- }
- }
- #endregion
- /// <summary>
- /// �ͷ���Դ�����浱ǰ�ڴ��е��������õ��ļ���
- /// </summary>
- public void Dispose()
- {
- SaveAll();
- GC.SuppressFinalize(this);
- }
- }
- }
|