using HighWayIot.Log4net;
using HighWayIot.Plc.PlcEntity;
using HighWayIot.Plc.PlcHelper;
using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Rfid;
using HighWayIot.Rfid.Entity;
using HighWayIot.TouchSocket;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using Timer = System.Threading.Timer;

namespace HighWayIot.Winform.Business
{
    /// <summary>
    /// 工位RFID识别业务
    /// </summary>
    public class WorkStationBusiness
    {
        /// <summary>
        /// 日志
        /// </summary>
        private LogHelper _logHelper = LogHelper.Instance;

        /// <summary>
        /// PLC
        /// </summary>
        private WorkStationHelper _workStationHelper = new WorkStationHelper();

        /// <summary>
        /// 读写器服务类
        /// </summary>
        private ZxReaderSettingService _readerService = ZxReaderSettingService.Instance;

        /// <summary>
        /// 读写器实体类
        /// </summary>
        private List<ZxReaderSettingEntity> _readerSetting;

        /// <summary>
        /// RFID数据分析
        /// </summary>
        private RfidDataAnalyse _RfidDataAnalyse = new RfidDataAnalyse();

        /// <summary>
        /// TCP客户端
        /// </summary>
        private TouchSocketTcpClient _touchSocketTcpClient = TouchSocketTcpClient.Instance;

        /// <summary>
        /// TCP客户端工厂
        /// </summary>
        private TCPClientFactory tcpClientFactory = new TCPClientFactory();

        /// <summary>
        /// 刷新器
        /// </summary>
        private Timer timer;

        /// <summary>
        /// 是否全部连接成功
        /// </summary>
        private bool IsAllConnected = false;

        public WorkStationBusiness()
        {
            _readerSetting = _readerService.GetReaderInfos();
            this.CreateAllRFIDClient();
            _touchSocketTcpClient.GetMessageAction += tcpClientFactory.ReciveDataRoute;
            timer = new Timer(new System.Threading.TimerCallback(SingalMonitor), null, 0, 1000);
        }

        /// <summary>
        /// 创建所有RFID客户端
        /// </summary>
        public void CreateAllRFIDClient()
        {
            foreach (var setting in _readerSetting)
            {
                Task.Run(() =>
                {
                    _touchSocketTcpClient.CreateTcpClient(setting.RfidIp, "20108");
                    _touchSocketTcpClient.Send(setting.RfidIp, _RfidDataAnalyse.SendBFH(3));
                });
            }

            IsAllConnected = true;
        }

        /// <summary>
        /// 自动监视 获取PLC信号
        /// </summary>
        /// <param name="o"></param>
        public void SingalMonitor(object o)
        {
            bool[] singals = _workStationHelper.ReadStationSingal();

            if(singals == null)
            {
                _logHelper.Error("工位监视PLC读取信号失败");
                return;
            }

            for (int i = 1; i <= singals.Length; i++)
            {
                if (singals[i - 1] == true)
                {
                    SendRFIDSingal(i);
                }
            }
        }
        
        /// <summary>
        /// 向指定RFID读写器发送读信号
        /// </summary>
        public void SendRFIDSingal(int no)
        {
            if (IsAllConnected == true)
            {
                ZxReaderSettingEntity setting = null;
                try
                {
                    setting = _readerSetting.FirstOrDefault(x => x.WorkstationNo == no.ToString());
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.Error($"[{no}] 工位读写器信息获取失败!", ex);
                }

                if (setting == null)
                {
                    LogHelper.Instance.Error($"找不到第[{no}]个读写器");
                    return;
                }

                bool result = _touchSocketTcpClient.Send(setting.RfidIp, _RfidDataAnalyse.Send02H(1000));
              
                if(result == false)
                {
                    _logHelper.Error($"[{no}] 工位 [{setting.RfidIp}] 读写器指令发送失败!");
                }
            }
        }
    }
}
