| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 | using Aitex.Core.RT.DataCenter;using CyberX8_Core;using MECF.Framework.Common.CommonData;using MECF.Framework.Common.CommonData.PowerSupplier;using MECF.Framework.Common.Net;using MECF.Framework.Common.Utilities;using MECF.Framework.Simulator.Core.Driver;using System;using System.Collections.Generic;using System.Linq;using System.Security.Cryptography.X509Certificates;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Timers;namespace CyberX8_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;        /// <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 System.Timers.Timer _timer;        public PowerSupplierSocketSimulator(int port):base(port)        {            PowerSupplierData powerSupplierData1 = new PowerSupplierData();            powerSupplierData1.Current = 0;            powerSupplierData1.Voltage = 56000;            _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;        }        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);                _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[_currentStep].Current*10);            }        }        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 == 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                {                    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)                        {                            _powerSupplierDic[channel].Current = _powerSupplierDic[channel].CurrentSetting * 10;                        }                    }                    else                    {                        _powerSupplierDic[channel].Current = 0;                    }                }                else if (startAddress == 0x0101)                {                    UpdateChannelCurrent(flag,channel, command, startAddress, 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                {                    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*10);                    _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, short value)        {            _powerSupplierDic[channel].CurrentSetting = value;            OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));        }        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 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, short startAddress, short count)        {            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(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 short _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;        /// <summary>        /// 可用性        /// </summary>        private bool _enabled;        public short VoltageSetting { get { return _voltageSetting; } set { _voltageSetting = value; } }        public short 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 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    }}
 |