using HighWayIot.Log4net;
using HighWayIot.Plc.PlcEntity;
using HslCommunication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HighWayIot.Plc.PlcHelper
{
    /// <summary>
    /// 中专信号点位读写（暂时米用）
    /// </summary>
    public class TransferSingal
    {
        /// <summary>
        /// 读取工位报表信号
        /// </summary>
        /// <returns></returns>
        public byte[] ReadStationReportSignal()
        {
            OperateResult<byte[]> operateResult = PlcConnect.ReadByte2("D3001", 30);
            //LogHelper.Instance.Info($"长度:{operateResult.Content.Length}");
            if (!operateResult.IsSuccess)
            {
                return null;
            }
            return operateResult.Content;
        }

        /// <summary>
        /// 复位工位小车结束信号
        /// </summary>
        /// <param name="No"></param>
        /// <returns></returns>
        public void WriteStationReportSignal(int No)
        {
            if(!PlcConnect.PlcWrite2($"D300{No}", 0, DataTypeEnum.UInt16).IsSuccess)
            {
                LogHelper.Instance.Error($"工位小车报表信号复位失败 No.{No}");
            }
        }

        /// <summary>
        /// 复位报警点位信号
        /// </summary>
        /// <param name="No"></param>
        public void WriteAlarmSignal()
        {
            if(!PlcConnect.PlcWrite2($"D3030", 0, DataTypeEnum.UInt16).IsSuccess)
            {
                LogHelper.Instance.Error($"小车报警复位失败");
            }
        }


        ///// <summary>
        ///// 工位贴合开始信号写入
        ///// </summary>
        ///// <param name="stationNo"></param>
        ///// <returns></returns>
        //public bool WriteStationStickSingal(int stationNo)
        //{
        //    bool result = false;

        //    switch (stationNo)
        //    {
        //        case 1:
        //            result = PlcConnect.PlcWrite2("B201", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        case 2:
        //            result = PlcConnect.PlcWrite2("B202", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        case 3:
        //            result = PlcConnect.PlcWrite2("B203", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        case 4:
        //            result = PlcConnect.PlcWrite2("B205", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        case 5:
        //            result = PlcConnect.PlcWrite2("B207", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        default:
        //            break;
        //    }

        //    return result;
        //}


        ///// <summary>
        ///// 工位贴合计数开始信号写入
        ///// </summary>
        ///// <param name="stationNo"></param>
        ///// <returns></returns>
        //public bool WriteStationStickCountSingal(int stationNo)
        //{
        //    bool result = false;

        //    switch (stationNo)
        //    {
        //        case 1:
        //            result = PlcConnect.PlcWrite2("B211", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        case 2:
        //            result = PlcConnect.PlcWrite2("B212", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        case 3:
        //            result = PlcConnect.PlcWrite2("B213", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        case 4:
        //            result = PlcConnect.PlcWrite2("B215", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        case 5:
        //            result = PlcConnect.PlcWrite2("B217", true, DataTypeEnum.Bool).IsSuccess;
        //            break;
        //        default:
        //            break;
        //    }

        //    return result;
        //}

        ///// <summary>
        ///// 读取小车就绪信号(开始贴合) 鼓到位信号读取（开始横裁信号）和贴合计数信号读取（横裁结束信号）
        ///// </summary>
        ///// <returns>第一个Byte数组是第二个Byte数组是开始横裁信号，第三个是结束横裁信号</returns>
        //public bool[][] ReadDrumReadyAndCountReadySignal()
        //{
        //    OperateResult<byte[]> operateResult = PlcConnect.ReadByte2("B980", 3);

        //    if (!operateResult.IsSuccess)
        //    {
        //        return null;
        //    }
        //    byte[] data = operateResult.Content;
        //    bool[][] result = new bool[3][];
        //    result[0] = new bool[9];
        //    result[1] = new bool[8];
        //    result[2] = new bool[8];
        //    //12345工位小车就绪
        //    for (int i = 0; i < 6; i++)
        //    {
        //        result[0][i] = data[0].GetBoolByIndex(i);
        //    }
        //    //345称重工位就绪
        //    for (int i = 0; i < 3; i++)
        //    {
        //        result[0][i + 6] = data[1].GetBoolByIndex(i);
        //    }
        //    //开始和结束横裁的信号
        //    for (int i = 0; i < 8; i++)
        //    {
        //        result[1][i + 1] = data[2].GetBoolByIndex(i);
        //        result[2][i + 1] = data[4].GetBoolByIndex(i);
        //    }

        //    return result;
        //}

        ///// <summary>
        ///// 监控中转信号
        ///// </summary>
        ///// <returns></returns>
        //public bool[][] ReadMonitorSingal()
        //{
        //    OperateResult<byte[]> operateResult = PlcConnect.ReadByte2("B9B1", 5);

        //    if (!operateResult.IsSuccess)
        //    {
        //        return null;
        //    }

        //    byte[] data = operateResult.Content;
        //    bool[][] result = new bool[5][];
        //    for (int i = 0; i < 5; i++)
        //    {
        //        result[i] = new bool[5];
        //        for (int j = 0; j < 5; j++)
        //        {
        //            result[i][j] = data[i * 2].GetBoolByIndex(j);
        //        }
        //    }

        //    return result;
        //}

        ///// <summary>
        ///// 监控工位配方信息
        ///// </summary>
        ///// <returns></returns>
        //public List<StationRecipeEntity> ReadStationRecipeInfo()
        //{
        //    OperateResult<byte[]> operateResult = PlcConnect.ReadByte2("W950", 80);

        //    if (!operateResult.IsSuccess)
        //    {
        //        return null;
        //    }

        //    byte[] data = operateResult.Content;
        //    List<StationRecipeEntity> resultList = new List<StationRecipeEntity>();

        //    for (int i = 0; i < 5; i++)
        //    {
        //        resultList.Add(new StationRecipeEntity()
        //        {
        //            StationRecipeDescripe = PlcConnect.MelsecInstance2.ByteTransform.TransString(data, (i * 16) + 0, 20, Encoding.ASCII),
        //            StationRecipeNo = PlcConnect.MelsecInstance2.ByteTransform.TransInt16(data, (i * 16) + 20),
        //            StationVehicleNo = PlcConnect.MelsecInstance2.ByteTransform.TransInt16(data, (i * 16) + 22),
        //            StationTireWeight = PlcConnect.MelsecInstance2.ByteTransform.TransSingle(data, (i * 16) + 24),
        //        });
        //    }

        //    return resultList;
        //}
    }
}
