using CFX;
using HslCommunication;
using Sln.Iot.CFX.CFXBusiness;
using Sln.Iot.CFX.CFXConnect;
using Sln.Iot.CFX.Event;
using Sln.Iot.PLC;
using Sln.Iot.Repository.service;
using Sln.Iot.Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sln.Iot.Business
{
    public class RFID05Business
    {
        private Timer _timer;

        private readonly PLCConnect _plc = PLCConnect.Instance;

        private readonly SerilogHelper _log = SerilogHelper.Instance;

        private TrayBindingService trayBindingService = TrayBindingService.Instance;
        private OvenPlcDataService ovenPlcDataService = OvenPlcDataService.Instance;
        private VacuumInjectionPlcDataService vacuumInjectionPlcDataService = VacuumInjectionPlcDataService.Instance;

        private UnitsProcessPLCDataGetBusiness unitsProcessPLCDataGetBusiness = UnitsProcessPLCDataGetBusiness.Instance;

        private CFXUnitProcessedDataGet cFXUnitProcessedDataGet = CFXUnitProcessedDataGet.Instance;

        private WorkStageStartedEvent workStageStartedEvent = new WorkStageStartedEvent();
        private WorkStageCompletedEvent workStageCompletedEvent = new WorkStageCompletedEvent();
        private UnitsProcessedEvent unitsProcessedEvent = new UnitsProcessedEvent();
        private CFXConnect1 connect1 = CFXConnect1.Instance;
        private CFXConnect2 connect2 = CFXConnect2.Instance;
        private CFXConnect3 connect3 = CFXConnect3.Instance;

        public RFID05Business()
        {
            _timer = new Timer(TimerCallback, null, 0, 2000);
        }

        /// <summary>
        /// RFID05胶机2真空箱后数据处理流程
        /// </summary>
        /// <param name="state"></param>
        public void TimerCallback(object? state)
        {
            try
            {
                // 定时任务逻辑
                //读取信号
                OperateResult<short> signalRes = _plc.ReadInt16(_plc.DeltaInstance2, "D802");
                //成功验证
                if (signalRes.IsSuccess)
                {
                    short signalValue = signalRes.Content;
                    //如果有读取信号
                    if (signalValue == 5001)
                    {
                        //读取托盘码
                        OperateResult<byte[]> trayBytesResult = _plc.ReadBytes(_plc.DeltaInstance2, "D5020", 10);
                        //成功验证
                        if (trayBytesResult.IsSuccess)
                        {
                            //转换托盘吗
                            string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
                            _log.Info("RFID05" + traycode);

                            //sql更新
                            //取出work唯一guid(tid)
                            string stid = trayBindingService.TidGet(traycode);
                            Guid.TryParse(stid, out Guid tid);

                            bool res = trayBindingService.UpDateTime(DateTime.Now.ToString(), traycode, "4");
                            if (!res)
                            {
                                _log.Error("胶机2真空箱后数据库写入异常");
                            }

                            //写入完成信号
                            res = _plc.PlcWrite(_plc.DeltaInstance2, "D802", 5002, DataTypeEnum.UInt16).IsSuccess;
                            if (!res)
                            {
                                _log.Error("胶机2真空箱后PLC写入完成信号异常");
                            }

                            //CFX
                            Task.Run(() =>
                            {
                                //真空箱2工段完成事件
                                connect1.PublishEvent(new CFXEnvelope(workStageCompletedEvent.Handle(tid, "VacuumInjection2", 2)));
                                _log.Info($"真空箱2工段完成, {tid}");
                                //固化炉工段开启
                                connect3.PublishEvent(new CFXEnvelope(workStageStartedEvent.Handle(tid, "CureOven", 3)));
                                _log.Info($"固化炉工段开启, {tid}");

                                //真空箱获取并上传数据
                                var unitsData1 = vacuumInjectionPlcDataService.GetData(stid);
                                var unitsData2 = unitsProcessPLCDataGetBusiness.Vacuum2PlcDataGet();
                                unitsData1.GlueAmountSetValue2 = unitsData2.GlueAmountSetValue2;
                                unitsData1.GluePushSpeedSetValue2 = unitsData2.GluePushSpeedSetValue2;
                                unitsData1.BarrelA1TempActValue2 = unitsData2.BarrelA1TempActValue2;
                                unitsData1.BarrelA2TempActValue2 = unitsData2.BarrelA2TempActValue2;
                                unitsData1.BarrelB1TempActValue2 = unitsData2.BarrelB1TempActValue2;
                                unitsData1.BarrelB2TempActValue2 = unitsData2.BarrelB2TempActValue2;
                                unitsData1.PumpAPressureActValue2 = unitsData2.PumpAPressureActValue2;
                                unitsData1.PumpBPressureActValue2 = unitsData2.PumpBPressureActValue2;
                                unitsData1.VacuumDegreeActValue2 = unitsData2.VacuumDegreeActValue2;
                                unitsData1.PressureHoldTimeSetValue2 = unitsData2.PressureHoldTimeSetValue2;
                                connect1.PublishEvent(new CFXEnvelope(unitsProcessedEvent.Handle(tid, cFXUnitProcessedDataGet.ProcessDataGet1(unitsData1))));
                                _log.Info($"真空箱温度数据上传, " +
                                $"GlueAmountSetValue1:{unitsData1.GlueAmountSetValue1} " +
                                $"GluePushSpeedSetValue1:{unitsData1.GluePushSpeedSetValue1} " +
                                $"BarrelA1TempActValue1:{unitsData1.BarrelA1TempActValue1} " +
                                $"BarrelA2TempActValue1:{unitsData1.BarrelA2TempActValue1} " +
                                $"BarrelB1TempActValue1:{unitsData1.BarrelB1TempActValue1} " +
                                $"BarrelB2TempActValue1:{unitsData1.BarrelB2TempActValue1} " +
                                $"PumpAPressureActValue1:{unitsData1.PumpAPressureActValue1} " +
                                $"PumpBPressureActValue1:{unitsData1.PumpBPressureActValue1} " +
                                $"VacuumDegreeActValue1:{unitsData1.VacuumDegreeActValue1} " +
                                $"PressureHoldTimeSetValue1:{unitsData1.PressureHoldTimeSetValue1} " +
                                $"GlueAmountSetValue2:{unitsData1.GlueAmountSetValue2} " +
                                $"GluePushSpeedSetValue2:{unitsData1.GluePushSpeedSetValue2} " +
                                $"BarrelA1TempActValue2:{unitsData1.BarrelA1TempActValue2} " +
                                $"BarrelA2TempActValue2:{unitsData1.BarrelA2TempActValue2} " +
                                $"BarrelB1TempActValue2:{unitsData1.BarrelB1TempActValue2} " +
                                $"BarrelB2TempActValue2:{unitsData1.BarrelB2TempActValue2} " +
                                $"PumpAPressureActValue2:{unitsData1.PumpAPressureActValue2} " +
                                $"PumpBPressureActValue2:{unitsData1.PumpBPressureActValue2} " +
                                $"VacuumDegreeActValue2:{unitsData1.VacuumDegreeActValue2} " +
                                $"PressureHoldTimeSetValue2:{unitsData1.PressureHoldTimeSetValue2}");
                            });
                        }
                        //流程完成
                    }
                }
                else
                {
                    _log.Error("胶机2真空箱后PLC读取信号异常");
                }
            }
            catch (Exception ex)
            {
                _log.Error("RFID0业务出现异常", ex);
            }
            
        }
    }
}
