| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704 |
- 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);
- }
- }
- }
- /// <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);
- }
- }
- /// <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 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 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 ϵͳ����
- /// <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);
- }
- FileHelper.WriteJsonFile(config, 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");
- }
- else if (language == Enums.Language.Vietnamese)
- {
- var culture = new CultureInfo("vi");
- 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.Now,
- 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);
- }
- }
- }
|