using Aitex.Common.Util; using Aitex.Core.RT.ConfigCenter; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using MECF.Framework.Common.CommonData.PowerSupplier; using MECF.Framework.Common.Device.LinMot; using MECF.Framework.Common.Layout; using MECF.Framework.Common.TwinCat; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MECF.Framework.Common.Device.PowerSupplier { public class PowerSupplierDeviceConfigManager : Singleton { #region 常量 #endregion #region 内部变量 /// /// PowerSupplier对象字典(key-名称,value-PowerSupplier对象) /// private Dictionary _namePowerSupplierDic = new Dictionary(); /// /// Modbus通信对象字典(key-名称,value /// private Dictionary _nameModbusDeviceDic = new Dictionary(); /// /// 模块变量注册事件字典 /// private Dictionary _moduleVariableActionDic = new Dictionary(); /// /// 地址模块字典(key-母级模块,value-supplier device模块名称 /// private Dictionary _powerSupplierDeviceAddressModuleDic = new Dictionary(); #endregion /// /// 初始化 /// public void Initialize() { bool isSimulate = SC.GetValue("System.IsSimulatorMode"); string xmlPath = ""; try { if (isSimulate) { xmlPath = PathManager.GetCfgDir() + "Devices\\PowerSupplierCfg-Simulator.xml"; } else { xmlPath = PathManager.GetCfgDir() + "Devices\\PowerSupplierCfg.xml"; } PowerSupplierDeviceConfigCfg cfg = CustomXmlSerializer.Deserialize(new FileInfo(xmlPath)); if (cfg != null) { foreach (PowerSupplierDeviceConfig config in cfg.PowerSupplierDeviceConfigs) { PowerSupplierModbusDevice modbusDevice = new PowerSupplierModbusDevice(config.Name, config.IpAddress, config.Port); modbusDevice.ReceiveTimeout = config.RecvTimeout; modbusDevice.SendTimeout = config.SendTimeout; InitialDeviceConfig(config, modbusDevice); } } } catch { LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, "PowerSupplier", "Load power supplier xml failed"); } } /// /// 启动设备 /// /// public void StartConnectDevice(string name) { if(_nameModbusDeviceDic.ContainsKey(name)) { _nameModbusDeviceDic[name].Start(); } } /// /// 中止 /// /// public void Stop(string name) { if (_nameModbusDeviceDic.ContainsKey(name)) { _nameModbusDeviceDic[name].Stop(); } } /// /// 电源控制模式 /// /// public bool SetChannelPowerControl(string name,byte channel,byte remoteControl) { if (_nameModbusDeviceDic.ContainsKey(name)) { return _nameModbusDeviceDic[name].SetChannelPowerControl(channel,remoteControl); } return false; } /// /// 初始化设备 /// /// private void InitialDeviceConfig(PowerSupplierDeviceConfig config,PowerSupplierModbusDevice modbusDevice) { foreach (PowerSupplierDevice item in config.Devices) { _nameModbusDeviceDic[item.Name] = modbusDevice; _namePowerSupplierDic[item.Name] = item; _powerSupplierDeviceAddressModuleDic[$"{config.Name}.{item.Address}"] = item.Name; } } /// /// 获取PowerSupplier对象 /// /// /// public PowerSupplierDevice GetPowerSupplierDeviceByName(string name) { return _namePowerSupplierDic.ContainsKey(name) ? _namePowerSupplierDic[name] : null; } /// /// 获取当前电压电流 /// /// /// /// public void GetChannelCurrentAndVoltage(string name,byte channel) { if (_nameModbusDeviceDic.ContainsKey(name)) { _nameModbusDeviceDic[name].GetChannelVoltageAndCurrent(channel); } } /// /// 获取通道输出开关控制 /// /// /// /// public void GetChannelOutputSwitch(string name,byte channel) { if (_nameModbusDeviceDic.ContainsKey(name)) { _nameModbusDeviceDic[name].GetChannelOutput(channel); } } /// /// 获取通道电流设置数值 /// /// /// /// public void GetChannelCurrentSetting(string name, byte channel) { if (_nameModbusDeviceDic.ContainsKey(name)) { _nameModbusDeviceDic[name].GetChannelCurrentSetting(channel); } } /// /// 设置通道输出开关控制 /// /// /// /// public bool SetChannelOutputSwitchControl(string name, byte channel, bool enabled) { if (_nameModbusDeviceDic.ContainsKey(name)) { return _nameModbusDeviceDic[name].SetChannelOutputSwitchControl(channel, enabled); } return false; } /// /// 设置通道输出开关控制 /// /// /// public void GetChannelPowerControl(string name, byte channel) { if (_nameModbusDeviceDic.ContainsKey(name)) { _nameModbusDeviceDic[name].GetChannelPowerControl(channel); } } /// /// 设置通道输出开关控制 /// /// /// /// public bool SetChannelCurrent(string name, byte channel, ushort current) { if (_nameModbusDeviceDic.ContainsKey(name)) { return _nameModbusDeviceDic[name].SetCurrentValue(channel, current); } return false; } /// /// 启动步阶 /// /// /// /// public bool SetStepPeriod(string name,byte channel,List stepPeriodDatas,int scale) { if (_nameModbusDeviceDic.ContainsKey(name)) { return _nameModbusDeviceDic[name].SetStepPeriod(channel, stepPeriodDatas,scale); } return false; } /// /// 启动步阶 /// /// /// /// /// /// public bool StartStepPeriod(string name,byte channel,ushort startStep,ushort endStep,ushort cycle) { if (_nameModbusDeviceDic.ContainsKey(name)) { return _nameModbusDeviceDic[name].StartStepPeriod(channel,startStep,endStep, cycle); } return false; } /// /// 设置电源运行模式 /// /// /// /// public bool SetPowerRunmodelControl(string name,byte channel,byte model) { if(_nameModbusDeviceDic.ContainsKey(name)) { return _nameModbusDeviceDic[name].SetChannelPowerRunmodelControl(channel, model); } return false; } /// /// 设置电源运行模式 /// /// /// /// public void GetPowerRunmodelControl(string name, byte channel) { if (_nameModbusDeviceDic.ContainsKey(name)) { _nameModbusDeviceDic[name].GetChannelPowerRunModel(channel); } } /// /// 获取通道电源状态 /// /// /// /// public bool SetChannelPowerStatus(string name, byte channel,byte powerstatus) { if (_nameModbusDeviceDic.ContainsKey(name)) { return _nameModbusDeviceDic[name].SetChannelPowerStatus(channel,powerstatus); } return false; } /// /// 获取通道电源状态 /// /// /// /// public void GetChannelPowerStatus(string name, byte channel) { if (_nameModbusDeviceDic.ContainsKey(name)) { _nameModbusDeviceDic[name].GetChannelPowerStatus(channel); } } /// /// 注册变量数值发生变化回调 /// /// /// /// public void SubscribeModuleVariable(string moduleName, string variable, BeckhoffDelegate.OnUpdateModuleVariableValue onUpdateModuleVariableValue) { _moduleVariableActionDic[$"{moduleName}.{variable}"] = onUpdateModuleVariableValue; } /// /// 更新模块变量数值 /// /// /// /// /// public void UpdateModuleVariable(string module,byte channel,string variable,object value) { string itemModulaName = GetModuleNameByAddress(module, channel); if(!string.IsNullOrEmpty(itemModulaName)) { string key = $"{itemModulaName}.{variable}"; if(_moduleVariableActionDic.ContainsKey(key)) { _moduleVariableActionDic[key].Invoke(variable, value); } } } /// /// 获取模块连接状态 /// /// /// public bool GetModuleConnected(string module) { return _nameModbusDeviceDic.ContainsKey(module) ? _nameModbusDeviceDic[module].Connected : false; } /// /// 根据地址获取模块名称 /// /// /// /// public string GetModuleNameByAddress(string name, byte id) { if (_powerSupplierDeviceAddressModuleDic.ContainsKey($"{name}.{id}")) { return _powerSupplierDeviceAddressModuleDic[$"{name}.{id}"]; } return ""; } } }