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
{
///
/// ���ò�������ʵ���ࡣ
/// �̰߳�ȫ���ڲ�ʹ��˽�����ֶ� ���������ڴ漯�ϵIJ������ʡ�
/// �־û����Լ��ϵ��Ļ�д�ص���Ӧ�������ļ���·�����ڲ���̬�� ���壩��
///
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 Cameras { get; set; }
private ObservableCollection Robots { get; set; }
private ObservableCollection Feeders { get; set; }
// ��˿�����������ϣ�����
private ObservableCollection ScrewFeeders { get; set; }
private ObservableCollection Plcs { get; set; }
private BgTcpIP BgCommunicate { get; set; }
private BgModbusTcp BgModbusCommunicate { get; set; }
private ObservableCollection FeederClearanceTasks { get; set; }
// ��Դ����������
private ObservableCollection LightControllers { get; set; }
// Device info - 支持多设备
private List _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;
///
/// ע��ϵͳ���ݿ����ѡ�������ڼ�¼��˿���α���ȼ�¼��
///
///
public void SetSystemDatabaseService(ISystemDatabaseService svc)
{
lock (_sync)
{
_systemDatabaseService = svc;
}
}
///
/// ��ʼ��һ���µ� ʵ����
/// ���캯�������Զ����������ļ��������ڹ����ڼ���� I/O ���������ɵ��� �� ��ʽ���ء�
///
public ConfigService()
{
//LoadAll();
}
///
/// ͬ���������������ļ����ڴ棨����Ӧ�ļ������ڣ�������Ĭ�Ͽռ���/����д���ļ�����
/// �̰߳�ȫ�������ڲ��Թ�����Դʹ�� ������
///
public void LoadAll()
{
lock (_sync)
{
// ��ȡ�����ã�����ļ������ڣ��� DeviceConfig Ĭ��ֵ
if (File.Exists(ConfigPaths.CamerasConfigurationPath))
Cameras = FileHelper.ReadJsonFile>(ConfigPaths.CamerasConfigurationPath);
else
{
Cameras = new ObservableCollection();
FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
}
if (File.Exists(ConfigPaths.RobotsConfigurationPath))
Robots = FileHelper.ReadJsonFile>(ConfigPaths.RobotsConfigurationPath);
else
{
Robots = new ObservableCollection();
FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
}
if (File.Exists(ConfigPaths.FeedersConfigurationPath))
Feeders = FileHelper.ReadJsonFile>(ConfigPaths.FeedersConfigurationPath);
else
{
Feeders = new ObservableCollection();
FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
}
// load screw feeders (material) configuration
if (File.Exists(ConfigPaths.ScrewFeedersConfigurationPath))
ScrewFeeders = FileHelper.ReadJsonFile>(ConfigPaths.ScrewFeedersConfigurationPath);
else
{
ScrewFeeders = new ObservableCollection();
FileHelper.WriteJsonFile(ScrewFeeders, ConfigPaths.ScrewFeedersConfigurationPath);
}
if (File.Exists(ConfigPaths.PlcConfigurationPath))
Plcs = FileHelper.ReadJsonFile>(ConfigPaths.PlcConfigurationPath);
else
{
Plcs = new ObservableCollection();
FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
}
if (File.Exists(ConfigPaths.BgTcpIpConfigurationPath))
BgCommunicate = FileHelper.ReadJsonFile(ConfigPaths.BgTcpIpConfigurationPath);
else
{
BgCommunicate = new BgTcpIP();
FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath);
}
if (File.Exists(ConfigPaths.BgModbusTcpConfigurationPath))
BgModbusCommunicate = FileHelper.ReadJsonFile(ConfigPaths.BgModbusTcpConfigurationPath);
else
{
BgModbusCommunicate = new BgModbusTcp();
FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath);
}
if (!File.Exists(ConfigPaths.FeederClearanceTasksConfigurationPath))
{
FeederClearanceTasks = new ObservableCollection();
FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
}
else
{
// ��ȡ Feeder�������� ����
FeederClearanceTasks = FileHelper.ReadJsonFile>(ConfigPaths.FeederClearanceTasksConfigurationPath);
}
// ��ȡ��Դ����������
if (!File.Exists(ConfigPaths.LightControllersConfigurationPath))
{
LightControllers = new ObservableCollection();
FileHelper.WriteJsonFile(LightControllers, ConfigPaths.LightControllersConfigurationPath);
}
else
{
LightControllers = FileHelper.ReadJsonFile>(ConfigPaths.LightControllersConfigurationPath);
}
// load plc addresses
var plcAddressPath = "..//Config//PlcAddressConfiguration.cfg";
if (File.Exists(plcAddressPath))
PlcAddressConfig = FileHelper.ReadJsonFile(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(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>(ConfigPaths.DeviceInfoConfigurationPath);
}
catch
{
// 兼容旧版单个 DeviceInfo,将其转换为列表
var singleDevice = FileHelper.ReadJsonFile(ConfigPaths.DeviceInfoConfigurationPath);
if (singleDevice != null)
{
singleDevice.DeviceNo = 0;
_deviceInfoList = new List { singleDevice };
}
else
{
_deviceInfoList = new List { new DeviceInfo { DeviceNo = 0 } };
}
FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
}
}
else
{
_deviceInfoList = new List { new DeviceInfo { DeviceNo = 0 } };
FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
}
// load card reader configuration
if (File.Exists(ConfigPaths.CardReaderConfigurationPath))
_cardReaderConfig = FileHelper.ReadJsonFile(ConfigPaths.CardReaderConfigurationPath);
else
{
_cardReaderConfig = new SerialPortConfig();
FileHelper.WriteJsonFile(_cardReaderConfig, ConfigPaths.CardReaderConfigurationPath);
}
}
}
///
/// �첽ִ�� ������
///
/// ��ʾ�첽���ز���������
public Task LoadAllAsync()
{
return Task.Run(() => LoadAll());
}
///
/// ���ڴ��е���������д�ص���Ӧ�����ļ���
/// �̰߳�ȫ�������ڲ���д�����ʹ�� ������
///
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);
}
}
///
/// �첽ִ�� ������
///
/// ��ʾ�첽�������������
public Task SaveAllAsync()
{
return Task.Run(() => SaveAll());
}
#region Cameras
///
/// ��ȡ����������õ�ֻ�����ϡ�
/// �̰߳�ȫ�����ڲ������Ա�֤������ȡ��һ���ԡ�
///
/// ������õ�ֻ�����ϡ�
public IReadOnlyCollection GetAllCameras()
{
lock (_sync) { return Cameras.ToList().AsReadOnly(); }
}
///
/// ���ݱ�ʶ��ȡ�������������Ϣ��
///
/// ����� Guid ��ʶ��
/// �ҵ��ض�Ӧ�� ������ null��
public CameraInfo GetCamera(Guid id)
{
lock (_sync) { return Cameras.FirstOrDefault(c => c.Id == id); }
}
///
/// ���������������á�
/// - ������ Ϊ null ���� null��
/// - �� Id �Ѵ������滻�������Ӳ����������� CameraNo ��š�
/// ����������Ὣ������ϳ־û��������ļ���
///
/// Ҫ���ӻ���µ�������á�
/// �����ӻ���µ� ������Ϊ null ʱ���� null��
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;
}
///
/// ���ݱ�ʶ�Ƴ�������ò����±��ʣ������� CameraNo��
/// ����������Ὣ������ϳ־û��������ļ���
///
/// Ҫ�Ƴ������ Guid��
/// ���ҵ����ɹ��Ƴ����� true������ false��
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;
}
///
/// ����Ƿ����ָ�� Id ��������á�
///
/// ����� Guid��
/// ���ڷ��� true������ false��
public bool ContainsCamera(Guid id) { lock (_sync) { return Cameras.Any(c => c.Id == id); } }
#endregion
#region DeviceInfo
///
/// 获取所有设备信息配置列表
///
/// 设备信息列表的只读集合
public IReadOnlyCollection GetAllDeviceInfos()
{
lock (_sync)
{
if (_deviceInfoList == null)
{
if (File.Exists(ConfigPaths.DeviceInfoConfigurationPath))
{
try
{
_deviceInfoList = FileHelper.ReadJsonFile>(ConfigPaths.DeviceInfoConfigurationPath);
}
catch
{
// 兼容旧版单个 DeviceInfo
var singleDevice = FileHelper.ReadJsonFile(ConfigPaths.DeviceInfoConfigurationPath);
if (singleDevice != null)
{
singleDevice.DeviceNo = 0;
_deviceInfoList = new List { singleDevice };
}
else
{
_deviceInfoList = new List { new DeviceInfo { DeviceNo = 0 } };
}
FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
}
}
else
{
_deviceInfoList = new List { new DeviceInfo { DeviceNo = 0 } };
FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
}
}
return _deviceInfoList.AsReadOnly();
}
}
///
/// 根据设备编号获取设备信息(兼容旧API,返回第一个设备)
///
/// 设备信息,若不存在则创建默认设备
public DeviceInfo GetDeviceInfo()
{
return GetDeviceInfo(0);
}
///
/// 根据设备编号获取设备信息
///
/// 设备编号
/// 设备信息,若不存在则返回 null
public DeviceInfo GetDeviceInfo(int deviceNo)
{
lock (_sync)
{
var allDevices = GetAllDeviceInfos();
return allDevices.FirstOrDefault(d => d.DeviceNo == deviceNo);
}
}
///
/// 添加或更新设备信息
///
/// 设备信息对象
public void SaveDeviceInfo(DeviceInfo deviceInfo)
{
if (deviceInfo == null) return;
lock (_sync)
{
// 确保列表已初始化
if (_deviceInfoList == null)
{
_deviceInfoList = new List();
}
// 查找是否已存在相同设备编号的设备
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);
}
}
///
/// 保存所有设备信息列表
///
/// 设备信息列表
public void SaveAllDeviceInfos(IEnumerable deviceInfoList)
{
if (deviceInfoList == null) return;
lock (_sync)
{
_deviceInfoList = deviceInfoList.OrderBy(d => d.DeviceNo).ToList();
FileHelper.WriteJsonFile(_deviceInfoList, ConfigPaths.DeviceInfoConfigurationPath);
}
}
///
/// 添加新设备
///
/// 新添加的设备
public DeviceInfo AddDeviceInfo()
{
lock (_sync)
{
if (_deviceInfoList == null)
{
_deviceInfoList = new List();
}
// 计算新设备编号
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;
}
}
///
/// 删除设备
///
/// 设备编号
/// 是否删除成功
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
///
/// ��ȡ���л��������õ�ֻ�����ϡ�
///
/// ���������õ�ֻ�����ϡ�
public IReadOnlyCollection GetAllRobots() { lock (_sync) { return Robots.ToList().AsReadOnly(); } }
///
/// ���ݱ�ʶ��ȡ�������������á�
///
/// �����˵� Guid ��ʶ��
/// �ҵ��ض�Ӧ�� ������ null��
public RobotInfo GetRobot(Guid id) { lock (_sync) { return Robots.FirstOrDefault(r => r.Id == id); } }
///
/// ��������»��������ã�������־û��������ļ���
/// ����ʱΪ�����˷��������� RobotNo ��š�
///
/// Ҫ���ӻ���µĻ��������á�
/// �����ӻ���µ� ������Ϊ null ʱ���� null��
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;
}
///
/// ���ݱ�ʶ�Ƴ����������ò����±��ʣ������ˡ�
/// ������־û������ļ���
///
/// Ҫ�Ƴ��Ļ����� Guid��
/// �Ƴ��ɹ����� true������ false��
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;
}
///
/// �ж��Ƿ����ָ�� Id �Ļ��������á�
///
/// �����˵� Guid��
/// ���ڷ��� true������ false��
public bool ContainsRobot(Guid id) { lock (_sync) { return Robots.Any(r => r.Id == id); } }
///
/// ��ָ�������� Id�IJ������벢�־û����������á�
///
/// ������Ψһ��ʶ����
/// �������롣
/// ���³ɹ����� true���Ҳ��������˷��� false��
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;
}
}
}
///
/// ��ȡָ�������� Id �IJ������롣
///
/// ������ Guid��
/// ���ҵ��ض�Ӧ�IJ������룬����Ĭ��ֵ 1.0��
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
///
/// ��ȡ���й��������õ�ֻ�����ϡ�
///
/// ���������õ�ֻ�����ϡ�
public IReadOnlyCollection GetAllFeeders() { lock (_sync) { return Feeders.ToList().AsReadOnly(); } }
///
/// ���ݱ�ʶ��ȡ�������������á�
///
/// �������� Guid ��ʶ��
/// �ҵ��ض�Ӧ�� ������ null��
public FeederInfo GetFeeder(Guid id) { lock (_sync) { return Feeders.FirstOrDefault(f => f.Id == id); } }
///
/// ��������¹��������ã�������־û��������ļ���
/// ����ʱΪ���������������� FeederNo ��š�
///
/// Ҫ���ӻ���µĹ��������á�
/// �����ӻ���µ� ������Ϊ null ʱ���� null��
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;
}
///
/// ���ݱ�ʶ�Ƴ����������ò����±��ʣ�������
/// ������־û������ļ���
///
/// Ҫ�Ƴ��Ĺ����� Guid��
/// �Ƴ��ɹ����� true������ false��
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;
}
///
/// �ж��Ƿ����ָ�� Id �Ĺ��������á�
///
/// ������ Guid��
/// ���ڷ��� true������ false��
public bool ContainsFeeder(Guid id) { lock (_sync) { return Feeders.Any(f => f.Id == id); } }
#endregion
#region PLC
///
/// ��ȡ���� PLC ���õ�ֻ�����ϡ�
///
/// PLC ���õ�ֻ�����ϡ�
public IReadOnlyCollection GetAllPlcs() { lock (_sync) { return Plcs.ToList().AsReadOnly(); } }
///
/// ���ݱ�ʶ��ȡ���� PLC ���á�
///
/// PLC �� Guid ��ʶ��
/// ƥ��� ʵ�����Ҳ������� null��
public PlcInfo GetPlc(Guid id) { lock (_sync) { return Plcs.FirstOrDefault(p => p.Id == id); } }
///
/// ��������� PLC ���ã�������־û��������ļ�������ʱ�������� PlcNo ��š�
///
/// Ҫ���ӻ���µ� PLC ���á�
/// �����ӻ���µ� ������Ϊ null ʱ���� null��
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;
}
///
/// ���ݱ�ʶ�Ƴ� PLC ���ò����±��ʣ�� PLC��
/// ������־û������ļ���
///
/// Ҫ�Ƴ��� PLC Guid��
/// �Ƴ��ɹ����� true������ false��
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;
}
///
/// �ж��Ƿ����ָ�� Id �� PLC ���á�
///
/// PLC �� Guid��
/// ���ڷ��� true������ false��
public bool ContainsPlc(Guid id) { lock (_sync) { return Plcs.Any(p => p.Id == id); } }
#endregion
#region BgTcp
///
/// ��ȡ BG TCP/IP ͨ�����ã��ڴ������Ϊ null����
///
/// ��ǰ�� ���ö���
public BgTcpIP GetBgTcp() { lock (_sync) { return BgCommunicate; } }
///
/// ���� BG TCP/IP ͨ�����ã��������ڴ�������Զ��־û�����
/// ����־û������ ��
///
/// �µ� ���ö���
public void SetBgTcp(BgTcpIP cfg) { lock (_sync) { BgCommunicate = cfg; } }
///
/// ����ǰ�ڴ��е� BG TCP/IP ���ó־û��������ļ���
///
public void SaveBgTcp() { lock (_sync) { FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath); } }
#endregion
#region BgModbus
///
/// ��ȡ BG Modbus TCP ͨ�����ã��ڴ����
///
/// ��ǰ�� ���ö���
public BgModbusTcp GetBgModbusTcp() { lock (_sync) { return BgModbusCommunicate; } }
///
/// ���� BG Modbus TCP ͨ�����ã��������ڴ�������Զ��־û�����
///
/// �µ� ���ö���
public void SetBgModbusTcp(BgModbusTcp cfg) { lock (_sync) { BgModbusCommunicate = cfg; } }
///
/// ����ǰ�ڴ��е� BG Modbus TCP ���ó־û��������ļ���
///
public void SaveBgModbusTcp() { lock (_sync) { FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath); } }
#endregion
#region CTQData
///
/// 获取CTQ信息配置
///
public CTQ_Data GetCTQDataInfo()
{
lock (_sync)
{
if (CtqData == null)
{
if (File.Exists(ConfigPaths.DataConfigurationPath))
CtqData = FileHelper.ReadJsonFile(ConfigPaths.DataConfigurationPath);
else
{
CtqData = new CTQ_Data();
FileHelper.WriteJsonFile(CtqData, ConfigPaths.DataConfigurationPath);
}
}
return CtqData;
}
}
///
/// 保存CTQ信息
///
public void SaveCTQDataInfo(CTQ_Data cd)
{
if (cd == null) return;
lock (_sync)
{
CtqData = cd;
FileHelper.WriteJsonFile(CtqData, ConfigPaths.DataConfigurationPath);
}
}
#endregion
#region ϵͳ����
///
/// ��ȡϵͳ���ã��������ļ���ȡ����
/// �̰߳�ȫ�������ڲ������Ա�֤��ȡһ���ԡ�
///
/// ��ȡ���� �������ļ���������Ĭ�ϵ���ʵ����
public SystemConfiguration GetSystemConfiguration()
{
lock (_sync)
{
var config = new SystemConfiguration();
if (File.Exists(ConfigPaths.SystemConfigurationPath))
{
config = FileHelper.ReadJsonFile(ConfigPaths.SystemConfigurationPath);
}
FileHelper.WriteJsonFile(config, ConfigPaths.SystemConfigurationPath);
return config;
}
}
///
/// ����ϵͳ���õ������ļ����Ḳ�������ļ�����
///
/// Ҫ�����ϵͳ���ö���
public void SaveSystemConfiguration(SystemConfiguration systemConfiguration)
{
lock (_sync)
{
FileHelper.WriteJsonFile(systemConfiguration, ConfigPaths.SystemConfigurationPath);
}
}
///
/// ��ȡ��ǰ����
///
///
public Language GetCurrentLanguage()
{
lock (_sync)
{
var config = GetSystemConfiguration();
return config.CurrentLanguage;
}
}
///
/// ���õ�ǰ����
///
///
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��������
///
/// ��ȡ�������������ֻ�����ϡ�
///
/// ���������ֻ�����ϡ�
public IReadOnlyCollection GetAllClearanceTasks() { lock (_sync) { return FeederClearanceTasks.ToList().AsReadOnly(); } }
///
/// ���������������������ʱΪ������������ TaskCode����ɺ�־û������ļ���
///
/// Ҫ���ӻ���µ������������
/// �����ӻ���µ� ������Ϊ null ʱ���� null��
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;
}
///
/// ���ݱ�ʶ�Ƴ��������־û������ļ���
///
/// Ҫ�Ƴ����������� Guid��
/// ���ҵ����Ƴ����� true������ false��
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;
}
}
///
/// ��������������־û��ռ��ϵ������ļ���
///
public void ClearAllClearanceTasks()
{
lock (_sync)
{
FeederClearanceTasks.Clear();
}
FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
}
///
/// �ж��Ƿ����ָ�� Id ����������
///
/// �������� Guid��
/// ���ڷ��� true������ false��
public bool ContainsClearanceTask(Guid id)
{
lock (_sync) { return FeederClearanceTasks.Any(t => t.Id == id); }
}
///
/// ���ݱ�ʶ��ȡ��������������Ϣ��
///
/// �������� Guid��
/// ƥ��� ���Ҳ������� null��
public FeederClearWork GetClearanceTask(Guid id)
{
lock (_sync) { return FeederClearanceTasks.FirstOrDefault(t => t.Id == id); }
}
///
/// �ж��Ƿ����ָ�������ŵ���������
///
/// �����ţ�TaskCode����
/// ���ڷ��� true������ false��
public bool ContainsClearanceTaskByNumber(int taskNumber)
{
lock (_sync) { return FeederClearanceTasks.Any(t => t.TaskCode == taskNumber); }
}
///
/// ���������Ż�ȡ��������������Ϣ��
///
/// �����ţ�TaskCode����
/// ƥ��� ���Ҳ������� null��
public FeederClearWork GetClearanceTaskByNumber(int taskNumber)
{
lock (_sync) { return FeederClearanceTasks.FirstOrDefault(t => t.TaskCode == taskNumber); }
}
#endregion
#region Light controllers and channels
///
/// ��ȡ���й�Դ���������õ�ֻ�����ϡ�
/// �̰߳�ȫ�����ڲ�ʹ�� ���������Ա�֤�������ʰ�ȫ��
///
/// ����ǰ�ڴ��д洢˳�ص�ֻ�� ���ϡ�
public IReadOnlyCollection GetAllLightControllers()
{
lock (_sync) { return LightControllers.ToList().AsReadOnly(); }
}
///
/// ���ݿ�������ʶ��ȡ������Դ���������á�
/// �̰߳�ȫ���÷������ڴ漯�Ͻ���ֻ�����ʲ����ڲ�������
///
/// ��������������ʶ��Id����
/// ƥ��� ʵ�����Ҳ����� null��
public LightControllerConfig GetLightController(int id)
{
lock (_sync) { return LightControllers.FirstOrDefault(c => c.Id == id); }
}
///
/// ��������¹�Դ���������á�
/// - �� Ϊ null ���� null��
/// - ����ʱ�� Id ƥ��������滻��
/// - ����ʱ���������Ŀ����� Id����Ϊ�������ڵ�ÿ��ͨ������ȫ��ͨ����ţ�GlobalIndex����
/// �̰߳�ȫ�����ڲ�ʹ�� �������ϡ�
///
/// Ҫ���ӻ���µ� ʵ����
/// �����ӻ���µ� ʵ��������Ϊ null ʱ���� null��
/// ��Ҫ����/���µĿ��������������������������ظ�ʱ�׳���
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;
}
///
/// ���� Id �Ƴ���Դ���������ò���ʣ����������±�ţ��� 1 ��ʼ������ţ���
/// ������־û������ļ���
///
/// Ҫ�Ƴ��Ŀ����� Id��
/// ����ɹ��Ƴ����� true������ false������δ�ҵ��� Id����
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;
}
///
/// �ж��Ƿ����ָ�� Id �Ĺ�Դ��������
///
/// ������ Id��
/// ���ڷ��� true������ false��
public bool ContainsLightController(int id) { lock (_sync) { return LightControllers.Any(c => c.Id == id); } }
///
/// ��ȡ���й�Դͨ����ֻ�����ϣ��� GlobalIndex ���ء�
///
/// �� GlobalIndex ����� ֻ�����ϡ�
public IReadOnlyCollection GetAllLightChannels()
{
lock (_sync) { return LightControllers.SelectMany(c => c.ChannelConfigs).OrderBy(ch => ch.GlobalIndex).ToList().AsReadOnly(); }
}
///
/// ����ȫ��ͨ��������ȡ����ͨ�����á�
///
/// ͨ����ȫ��������GlobalIndex����
/// ƥ��� ʵ�����Ҳ����� null��
public ChannelConfig GetLightChannelByGlobalIndex(int globalIndex)
{
lock (_sync) { return LightControllers.SelectMany(c => c.ChannelConfigs).FirstOrDefault(ch => ch.GlobalIndex == globalIndex); }
}
///
/// �ж��Ƿ����ָ��ȫ��ͨ��������ͨ�����á�
///
/// ȫ��ͨ��������
/// ���ڷ��� true������ false��
public bool ContainsLightChannelByGlobalIndex(int globalIndex)
{
lock (_sync) { return LightControllers.SelectMany(c => c.ChannelConfigs).Any(ch => ch.GlobalIndex == globalIndex); }
}
///
/// ����ָ��ȫ��ͨ����Ĭ�����ȣ�DefaultBrightness�������־û���Դ���������á�
///
/// Ŀ��ͨ����ȫ��������GlobalIndex����
/// Ҫ���õ�����ֵ��
/// ���³ɹ����� true���Ҳ�����Ӧͨ������ false��
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)
///
/// ��ȡ�������ã����豸����
///
/// ��ǰ�� ����δ������ null��
public ScrewDriverConfig GetScrewDriverConfig()
{
lock (_sync) { return _screwDriverConfig; }
}
///
/// ���ò��־û��������ã����豸����
///
/// �µ����ö�����Ϊ�ա�
public void SetScrewDriverConfig(ScrewDriverConfig cfg)
{
if (cfg == null) throw new ArgumentNullException(nameof(cfg));
lock (_sync)
{
_screwDriverConfig = cfg;
FileHelper.WriteJsonFile(_screwDriverConfig, ConfigPaths.ScrewDriverConfigurationPath);
}
}
///
/// ������ǰ�ڴ��еĵ������ñ��浽�ļ���
///
public void SaveScrewDriverConfig()
{
lock (_sync)
{
if (_screwDriverConfig != null)
FileHelper.WriteJsonFile(_screwDriverConfig, ConfigPaths.ScrewDriverConfigurationPath);
}
}
#endregion
#region Screw Feeders (material)
///
/// ��ȡ������˿���������õ�ֻ�����ϡ�
///
public IReadOnlyCollection GetAllScrewFeeders() { lock (_sync) { return ScrewFeeders.ToList().AsReadOnly(); } }
///
/// ���� Id ��ȡ������˿���������á�
///
public ScrewFeederInfo GetScrewFeeder(Guid id) { lock (_sync) { return ScrewFeeders.FirstOrDefault(s => s.Id == id); } }
///
/// �����������˿���������á�
/// ����ʱ��δָ�� FeederNumber �����Ϊ��ǰ����+1��
///
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;
}
///
/// �Ƴ���˿���������á�
///
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 ScrewFeederLowLevelAlarmTriggered;
///
/// ��ȡָ����������ŵ�ʣ����˿�������Ҳ������� -1��
///
public int GetRemainingCountByFeederNumber(int feederNumber)
{
lock (_sync)
{
var f = ScrewFeeders.FirstOrDefault(s => s.FeederNumber == feederNumber);
if (f == null) return -1;
return f.RemainingCount;
}
}
///
/// ��ȡָ����������ŵĵ�������״̬���Ҳ������� false��
///
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;
}
}
}
///
/// Ϊָ����������ŵݼ�ָ�����������������㷵�� -1
///
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;
}
}
///
/// ���ָ����������ŵ���˿��Ϣ�����κ��ÿգ�ʣ��������0��
///
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);
}
}
///
/// Ϊָ������������������κţ�����ԭ��ʣ������������һ����PackSize��
///
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
}
}
///
/// ���ʣ����˿�Ƿ�
///
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 �û���¼ˢ����
///
/// ��ȡˢ��������
///
///
public SerialPortConfig GetCardReaderConfig()
{
lock (_sync)
{
if (_cardReaderConfig != null)
return _cardReaderConfig;
if (File.Exists(ConfigPaths.CardReaderConfigurationPath))
{
try
{
_cardReaderConfig = FileHelper.ReadJsonFile(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;
}
}
///
/// ����ˢ��������
///
///
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(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
///
/// �ͷ���Դ�����浱ǰ�ڴ��е��������õ��ļ���
///
public void Dispose()
{
SaveAll();
GC.SuppressFinalize(this);
}
}
}