using BPASmartClient.Modbus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.PLC
{
    /// <summary>
    /// PLC读取参数
    /// </summary>
    internal class PLCReadParameter
    {
        /// <summary>
        /// 读取类型
        /// </summary>
        internal CommandType CmdType { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        internal string Address { get; set; }
        /// <summary>
        /// 长度
        /// </summary>
        internal ushort Length { get; set; }

        /// <summary>
        /// 配置转模型
        /// </summary>
        /// <param name="content">M,M0.3,3;M,M100.0,16;M,M235.0,1;M,M102,7;M,M103,6;VW,VW372,1</param>
        /// <returns>读取参数模型</returns>
        public static List<PLCReadParameter> DeSerialize(string content)
        {
            List<PLCReadParameter> result = new List<PLCReadParameter>();
            var wholeTemp = content.Split(';');
            foreach (var temp in wholeTemp)
            {
                if (temp.Contains(","))
                {
                    var subTemp = temp.Split(',');
                    if (null != subTemp && subTemp.Length == 3)
                    {
                        PLCReadParameter parameter = new PLCReadParameter();
                        switch (subTemp[0])
                        {
                            case "M":
                                parameter.CmdType = CommandType.Coils;
                                break;
                            case "VW":
                                parameter.CmdType = CommandType.HoldingRegisters;
                                break;
                        }
                        parameter.Address = subTemp[1];
                        parameter.Length = ushort.Parse(subTemp[2]);
                        result.Add(parameter);
                    }
                }
            }
            return result;
        }
    }
}
