using Misc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace FLY.Modbus
{
    /// <summary>
    /// 加载通讯列表用
    /// </summary>
    public class PLCGroup
    {
        public class PLCDevice
        {
            /// <summary>
            /// PLC 名称 用于报警时，能有提示
            /// </summary>
            public string PlcName;

            [JsonConverter(typeof(IPEndPointJsonConverter))]
            public IPEndPoint EP;
        }

        public class PLCVariable
        {
            /// <summary>
            /// 设备序号
            /// </summary>
            public int DeviceIndex;
            /// <summary>
            /// 值为 0,1,3,4 ;对应modbus寄存器的区号
            /// </summary>
            public string Mode;
            /// <summary>
            /// 区号的地址
            /// </summary>
            public int Addr;
            /// <summary>
            /// 类型 只有 bool 才对应 bool， 其它数值类型 都转为 float
            /// </summary>
            public string Type;
            /// <summary>
            /// 放大倍数
            /// </summary>
            public double Scale;
            /// <summary>
            /// 属性拥有者名称
            /// </summary>
            public string OwnerName;
            /// <summary>
            /// 属性名称
            /// </summary>
            public string PropertyName;

            /// <summary>
            /// 原始寄存器地址
            /// </summary>
            public string RegAddr;

            public override string ToString()
            {
                return $"{OwnerName}.{PropertyName} ({RegAddr})=dev{DeviceIndex}[{Mode}]({Addr})";
            }
        }

        /// <summary>
        /// 版本
        /// </summary>
        public string Version;

        /// <summary>
        /// 设备列表
        /// </summary>
        public List<PLCDevice> Devices = new List<PLCDevice>();

        /// <summary>
        /// 寄存器列表
        /// </summary>
        public List<PLCVariable> Variables = new List<PLCVariable>();
    }
}
