| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 | using Aitex.Common.Util;using Aitex.Core.RT.ConfigCenter;using Aitex.Core.RT.DataCenter;using Aitex.Core.RT.Log;using Aitex.Core.Util;using PunkHPX8_Core;using MECF.Framework.Common.CommonData;using MECF.Framework.Common.CommonData.PowerSupplier;using MECF.Framework.Common.Device.PowerSupplier;using MECF.Framework.Common.Device.Wago;using MECF.Framework.Common.Net;using MECF.Framework.Common.Utilities;using MECF.Framework.Simulator.Core.Driver;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Security.Cryptography.X509Certificates;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Timers;using System.Xml.Linq;namespace PunkHPX8_Simulator.Devices{    public class PowerSupplierSocketSimulator : SocketDeviceSimulator    {        private const short POWER_CONTROL_ADDRESS = 0x0113;        private const short STEP_PERIOD_ADDRESS = 0x1400;        private const short STEP_PERIOD_START_ADDRESS = 0x1640;        private const short GOLD_CURRENT_SETTING_ADDRESS = 0x6102;        private const short GOLD_STEP_PERIOD_ADDRESS = 0x77D0;        private const short GOLD_STEP_PERIOD_START_ADDRESS = 0x7C50;        private const short POWER_GRADE = 0x0106;        /// <summary>        /// 电源状态(00-cv输出,01-cc输出)        /// </summary>        private const short POWER_STATUS_ADDRESS = 0x0200;        Dictionary<int, PowerSupplierData> _powerSupplierDic = new Dictionary<int, PowerSupplierData>();        private IByteTransform byteTransform = new BigEndianByteTransformBase();        private List<PowerSupplierStepPeriodData> _powerSupplierDatas = new List<PowerSupplierStepPeriodData>();        private byte _startStep = 1;        private byte _endStep = 255;        private byte _cycle = 1;        private byte _currentStep = 0;        private int _currentLength = 0;        private DateTime _currentTime = DateTime.Now;        private bool _isGoldPower = false;        private int _currentSetScale= 1000;        private int _highSetScale=1000;        private int _middleSetScale=100000;        private int _lowSetScale=1000000;        private int _currentPowerGrade = 2;//默认是高档        private System.Timers.Timer _timer;        /// <summary>        /// 记录每次设置的电流值        /// </summary>        private byte[] _powerSupplierSetPoint = new byte[4];        public PowerSupplierSocketSimulator(int port):base(port)        {            PowerSupplierData powerSupplierData1 = new PowerSupplierData();            powerSupplierData1.Current = 0;            powerSupplierData1.CurrentSetting = 0;            powerSupplierData1.Voltage = 560;            _powerSupplierDic[1] = powerSupplierData1;            PowerSupplierData powerSupplierData2 = new PowerSupplierData();            powerSupplierData2.Current = 0;            powerSupplierData2.Voltage = 2000;            _powerSupplierDic[2] = powerSupplierData2;            PowerSupplierData powerSupplierData3 = new PowerSupplierData();            powerSupplierData3.Current = 0;            powerSupplierData3.Voltage = 3000;            _powerSupplierDic[3] = powerSupplierData3;            PowerSupplierData powerSupplierData4 = new PowerSupplierData();            powerSupplierData4.Current = 0;            powerSupplierData4.Voltage = 4000;            _powerSupplierDic[4] = powerSupplierData4;            _timer = new System.Timers.Timer();            _timer.Interval = 1000;            _timer.Elapsed += Timer_Elapsed;            InitializeParamater(port);        }        private void InitializeParamater(int port)        {            try            {                string oldXmlPath = PathManager.GetCfgDir();                string newXmlPath = oldXmlPath.Replace("PunkHPX8_Simulator", "PunkHPX8_RT") + "Devices\\PowerSupplierCfg-Simulator.xml";                PowerSupplierDeviceConfigCfg cfg = CustomXmlSerializer.Deserialize<PowerSupplierDeviceConfigCfg>(new FileInfo(newXmlPath));                if (cfg != null)                {                    foreach (PowerSupplierDeviceConfig config in cfg.PowerSupplierDeviceConfigs)                    {                        if(config.Port != port)                        {                            continue;                        }                        else                        {                            foreach (PowerSupplierDevice device in config.Devices)                            {                                if (device != null)                                {                                    if (config.Type == 1) //金槽电源                                    {                                        _currentSetScale = 1000;                                    }                                    else                                    {                                        _highSetScale = device.HighGradeCurrentSetScale;                                        _middleSetScale = device.MiddleGradeCurrentSetScale;                                        _lowSetScale = device.LowGradeCurrentSetScale;                                    }                                }                                break;                            }                            break;                        }                     }                }            }            catch            {                LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, "PowerSupplier", "Load power supplier xml failed");            }        }        private void Timer_Elapsed(object sender, ElapsedEventArgs e)        {            if(_currentStep>=_cycle*_endStep)            {                _timer.Stop();            }            if(DateTime.Now.Subtract(_currentTime).TotalSeconds>=_currentLength)            {                _currentStep++;                if (_currentStep >= _cycle * _endStep)                {                    _powerSupplierDic[1].Current = 0;                    _timer.Stop();                    return;                }                _currentTime = DateTime.Now;                _currentLength = (int)(_powerSupplierDatas[_currentStep].Hour * 3600 + _powerSupplierDatas[_currentStep].Minute * 60 +                    _powerSupplierDatas[_currentStep].Second);                if (_isGoldPower)                {                    _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[_currentStep].Current);                }                else                {                    _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[_currentStep].Current);                }            }        }        protected override void ProcessUnsplitMessage(byte[] data)        {            short flag = byteTransform.TransInt16(data, 0);            byte channel = data[6];            byte command = data[7];            if(!_powerSupplierDic.ContainsKey(channel))            {                OnWriteMessage(CreateError(flag,channel, command, 0x02));                return;            }            if (command == 0x03)//读取            {                short startAddress = byteTransform.TransInt16(data, 8);                short registerCount = byteTransform.TransInt16(data, 10);                if (startAddress == 0x201)                {                    byte[] bytes = new byte[2*registerCount];                    Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].Voltage), 0, bytes, 0, 4);                    Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].Current), 0, bytes, 4, 4);                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));                    return;                }                else if (startAddress == 0x0110)                {                    byte[] bytes = new byte[2];                    bytes[0] = 0;                    bytes[1] = _powerSupplierDic[channel].Enabled?(byte)1:(byte)0;                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));                    return;                }                else if (startAddress == POWER_STATUS_ADDRESS)                {                    byte[] bytes = new byte[2];                    bytes[0] = 0;                    bytes[1] = (byte)_powerSupplierDic[channel].PowerStatus;                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));                    return;                }                else if (startAddress == 0x0101)                {                    byte[] bytes = new byte[2];                    Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].CurrentSetting), 0, bytes, 0, bytes.Length);                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));                    return;                }                else if(startAddress == GOLD_CURRENT_SETTING_ADDRESS)                {                    _isGoldPower = true;                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, _powerSupplierSetPoint));                    return;                }                else if (startAddress == 0X80)                {                    byte[] bytes = new byte[2];                    bytes[0] = 0;                    bytes[1] = _powerSupplierDic[channel].OutputSwitchControl;                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));                    return;                }                else if (startAddress == 0x0111)                {                    byte[] bytes = new byte[2];                    bytes[0] = 0;                    bytes[1] = (byte)_powerSupplierDic[channel].RunModel;                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));                    return;                }                else if (startAddress == POWER_CONTROL_ADDRESS)                {                    byte[] bytes = new byte[2];                    bytes[0] = 0;                    bytes[1] = (byte)_powerSupplierDic[channel].PowerControl;                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));                    return;                }                else if(startAddress == POWER_GRADE)                {                    byte[] bytes = new byte[2];                    bytes[0] = 0;                    bytes[1] = (byte)_powerSupplierDic[channel].PowerGrade;                    OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));                    return;                }                else                {                    OnWriteMessage(CreateError(flag, channel, command, 03));                }            }            else if (command == 0x06)//设置            {                short startAddress = byteTransform.TransInt16(data, 8);                short value = byteTransform.TransInt16(data, 10);                if(startAddress==0x0110)                {                    UpdateChannelEnable(flag, channel, command, startAddress, data[11]);                    if (_powerSupplierDic[channel].Enabled)                    {                        if (_powerSupplierDic[channel].RunModel == 1)                        {                            if (_isGoldPower)                            {                                int count = byteTransform.TransInt32(_powerSupplierSetPoint, 0);                                _powerSupplierDic[channel].Current = count / _currentSetScale;                            }                            else                            {                                 _powerSupplierDic[channel].Current = _powerSupplierDic[channel].CurrentSetting;                             }                        }                    }                    else                    {                        _powerSupplierDic[channel].Current = 0;                    }                }                else if (startAddress == 0x0101)                {                    UpdateChannelCurrent(flag,channel, command, startAddress, (ushort)value);                }                else if (startAddress == POWER_CONTROL_ADDRESS)                {                    UpdateChannelPowerControl(flag, channel, command, startAddress, data[11]);                }                else if(startAddress== 0x0111)                {                    UpdateChannelRunModel(flag, channel, command, startAddress, data[11]);                }                else if(startAddress == POWER_GRADE)                {                    UpdateChannelGrade(flag, channel, command, startAddress, data[11]);                    if (data[11] == 2)                    {                        _currentPowerGrade = 2;                        _currentSetScale = _highSetScale;                    }                    else if(_highSetScale == 1)                    {                        _currentPowerGrade = 1;                        _currentSetScale = _middleSetScale;                    }                    else                    {                        _currentPowerGrade = 0;                        _currentSetScale = _lowSetScale;                    }                }                else                {                    byte[] errorByt = CreateError(flag, channel, command, 03);                    OnWriteMessage(errorByt);                    return;                }            }            else if(command==0x10) //写多个寄存器            {                short startAddress = byteTransform.TransInt16(data, 8);                short length = byteTransform.TransInt16(data, 10);                if (startAddress == STEP_PERIOD_ADDRESS)                {                    _powerSupplierDatas.Clear();                    int listCount = length / 6;                    for(int i=0;i<listCount;i++)                    {                        PowerSupplierStepPeriodData powerSupplierData = new PowerSupplierStepPeriodData();                        powerSupplierData.Voltage = byteTransform.TransInt16(data, 13 + i * 12);                        powerSupplierData.Current = byteTransform.TransInt16(data, 15 + i * 12);                        powerSupplierData.Hour = byteTransform.TransUInt16(data, 17 + i * 12);                        powerSupplierData.Minute = byteTransform.TransUInt16(data, 19 + i * 12);                        powerSupplierData.Second = byteTransform.TransUInt16(data, 21 + i * 12);                        powerSupplierData.Microsecond = byteTransform.TransUInt16(data, 23 + i * 12);                        _powerSupplierDatas.Add(powerSupplierData);                    }                    OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));                }                else if(startAddress==STEP_PERIOD_START_ADDRESS)                {                    _startStep = (byte)byteTransform.TransInt16(data,13);                    _endStep = (byte)byteTransform.TransInt16(data, 15);                    _cycle = (byte)byteTransform.TransInt16(data, 17);                    _currentLength = (_powerSupplierDatas[0].Hour * 3600 + _powerSupplierDatas[0].Minute * 60 +                        _powerSupplierDatas[0].Second);                    _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[0].Current);                    _currentTime = DateTime.Now;                    _currentStep = 0;                    _timer.Start();                    OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));                }                else if(startAddress == GOLD_CURRENT_SETTING_ADDRESS) //设置电流 32位变量                {                    _isGoldPower = true;                    byte[] vauleData = new byte[4];   //0,1是高位,2,3是低位                    Array.Copy(data, 13, vauleData, 0, 4);                    _powerSupplierSetPoint = vauleData;                    int count = byteTransform.TransInt32(vauleData, 0);                    UpdateChannelGoldCurrent(flag, channel, command, (ushort)count);                }                else if(startAddress == GOLD_STEP_PERIOD_ADDRESS) //设置步阶数据  32位变量                {                    _powerSupplierDatas.Clear();                    int listCount = length / 12;                    for (int i = 0; i < listCount; i++)                    {                        PowerSupplierStepPeriodData powerSupplierData = new PowerSupplierStepPeriodData();                        powerSupplierData.Voltage = byteTransform.TransInt32(data, 13 + i * 24);                        powerSupplierData.Current = byteTransform.TransInt32(data, 17 + i * 24) / _currentSetScale;                        powerSupplierData.Hour = byteTransform.TransUInt16(data, 23 + i * 24);                        powerSupplierData.Minute = byteTransform.TransUInt16(data, 27 + i * 24);                        powerSupplierData.Second = byteTransform.TransUInt16(data, 31 + i * 24);                        powerSupplierData.Microsecond = byteTransform.TransUInt16(data, 35 + i * 24);                        _powerSupplierDatas.Add(powerSupplierData);                    }                    OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));                }                else if (startAddress == GOLD_STEP_PERIOD_START_ADDRESS) //启动步阶数据 32位变量                {                    _startStep = (byte)byteTransform.TransInt32(data, 13);                    _endStep = (byte)byteTransform.TransInt32(data, 17);                    _cycle = (byte)byteTransform.TransInt32(data, 21);                    _currentLength = (_powerSupplierDatas[0].Hour * 3600 + _powerSupplierDatas[0].Minute * 60 +                        _powerSupplierDatas[0].Second);                    _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[0].Current);                    _currentTime = DateTime.Now;                    _currentStep = 0;                    _timer.Start();                    OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));                }            }            else            {                OnWriteMessage(CreateError(flag, channel, command, 0x01));            }        }        private byte[] CreateReadResponse(short flag,byte channel,byte command,short registerCount, byte[] values)        {            byte[] bytes = new byte[3 + values.Length + 6];            Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);            bytes[2] = 0x00;            bytes[3] = 0x00;            short dataLength=(short)(3+ values.Length);            Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);            bytes[6] = channel;            bytes[7] = command;            bytes[8] = (byte)(2 * registerCount);            Array.Copy(values,0,bytes, 9, values.Length);            return bytes;        }        private void UpdateChannelCurrent(short flag,byte channel, byte command, short startAddress, ushort value)        {            _powerSupplierDic[channel].CurrentSetting = value;            OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, (short)value));        }        private void UpdateChannelGoldCurrent(short flag, byte channel, byte command, ushort count)        {            _powerSupplierDic[channel].CurrentSetting = count;            OnWriteMessage(CreateMultiWriteResponse(flag, channel, command, 2));        }        private void UpdateChannelEnabled(short flag,byte channel, byte command, short startAddress, short value)        {            _powerSupplierDic[channel].OutputSwitchControl = (byte)value;            OnWriteMessage(CreateWriteResponse(flag,channel, command, startAddress, value));        }        private void UpdateChannelPowerControl(short flag, byte channel, byte command, short startAddress, short value)        {            _powerSupplierDic[channel].PowerControl = (byte)value;            OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));        }        private void UpdateChannelRunModel(short flag, byte channel, byte command, short startAddress, short value)        {            _powerSupplierDic[channel].RunModel = (byte)value;            OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));        }        private void UpdateChannelGrade(short flag, byte channel, byte command, short startAddress, short value)        {            _powerSupplierDic[channel].PowerGrade = (byte)value;            OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));        }        private void UpdateChannelEnable(short flag, byte channel, byte command, short startAddress, short value)        {            _powerSupplierDic[channel].Enabled = value == 1;            OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));        }        private byte[] CreateWriteResponse(short flag,byte channel,byte command, short startAddress,short value)        {            byte[] byt = new byte[12];            Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);             byt[2] = 0x00;            byt[3] = 0x00;            byt[4] = 0x00;            byt[5] = 0x06;            byt[6] = channel;            byt[7] = command;            byte[] addressByt= byteTransform.GetBytes(startAddress);            Array.Copy(addressByt, 0, byt, 8, 2);            byte[] valueByt=byteTransform.GetBytes(value);            Array.Copy(valueByt, 0, byt, 10, 2);            return byt;        }        private byte[] CreateMultiWriteResponse(short flag, byte channel, byte command,byte registerCount)        {            byte[] byt = new byte[12];            Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);            byt[2] = 0x00;            byt[3] = 0x00;            byt[4] = 0x00;            byt[5] = 0x06;            byt[6] = channel;            byt[7] = command;            byt[8] = 0x00;            byt[9] = registerCount;            //byte[] addressByt = byteTransform.GetBytes(startAddress);            //Array.Copy(addressByt, 0, byt, 8, 2);            //byte[] valueByt = byteTransform.GetBytes(count);            //Array.Copy(valueByt, 0, byt, 10, 2);            return byt;        }        private byte[] CreateError(short flag,byte channel,byte command,byte error)        {            byte[] byt = new byte[9];            Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);            byt[2] = 0x00;            byt[3] = 0x00;            byt[4] = 0x00;            byt[5] = 0x03;            byt[6]=channel;            byt[7]=(byte)(command|0x80);            byt[8] = error;            return byt;        }    }    internal class PowerSupplierData    {        private short _voltageSetting;        private ushort _currentSetting;        private byte _outputSwitchControl;        private int _voltage;        private int _current;        private short _powerStatus = (int)PowerStatusEnum.CC;        private short _powerControl=(int)PowerControlEnum.Remote;        private short _runModel = (int)PowerRunModelEnum.Normal;        private short _powerGrade = (int)PowerGradeEnum.High;        /// <summary>        /// 可用性        /// </summary>        private bool _enabled;        public short VoltageSetting { get { return _voltageSetting; } set { _voltageSetting = value; } }        public ushort CurrentSetting { get { return _currentSetting; } set { _currentSetting = value;  } }        public byte OutputSwitchControl { get { return _outputSwitchControl; } set { _outputSwitchControl = value; } }        public int Voltage { get { return _voltage; } set { _voltage = value;  } }        public int Current { get { return _current; } set { _current = value;  } }        public short PowerStatus { get { return _powerStatus; } set { _powerStatus = value; } }        public short PowerControl { get { return _powerControl; } set { _powerControl = value; } }        public short RunModel { get { return _runModel; } set { _runModel = value; } }                public short PowerGrade { get { return _powerGrade; } set { _powerGrade = value; } }        public bool Enabled { get { return _enabled; } set { _enabled = value; } }    }    public class PowerSupplierStepPeriodData     {        #region 内部变量        private double _voltage;        private double _current;        private ushort _hour;        private ushort _minute;        private ushort _second;        private ushort _microsecond;        #endregion        #region 属性        public double Voltage { get { return _voltage; } set { _voltage = value;  } }        public double Current { get { return _current; } set { _current = value; } }        public ushort Hour { get { return _hour; } set { _hour = value; } }        public ushort Minute { get { return _minute; } set { _minute = value; } }        public ushort Second { get { return _second; } set { _second = value; } }        public ushort Microsecond { get { return _microsecond; } set { _microsecond = value; } }        #endregion    }}
 |