using CFX.Structures.PressInsertion;
using CFX;
using HslCommunication;
using HslCommunication.Core;
using Serilog;
using Sln.Iot.CFX.CFXBusiness;
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.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

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

        private readonly PLCConnect _plc = PLCConnect.Instance;

        private readonly SerilogHelper _log = SerilogHelper.Instance;

        private ErrorAnalyseBusiness _errorAnalyse;
        private RecipeBusiness _recipeBusiness;

        public TestBusiness()
        {
            _errorAnalyse = new ErrorAnalyseBusiness();
            _recipeBusiness = new RecipeBusiness();
            //_timer = new Timer(TimerCallback, null, 0, 1000);
        }

        /// <summary>
        /// RFID01上料提升机数据处理流程业务刷新
        /// </summary>
        /// <param name="state"></param>
        public void TimerCallback(object? state)
        {
            try
            {
                UnitsProcessPLCDataGetBusiness unitsProcessPLCDataGetBusiness = UnitsProcessPLCDataGetBusiness.Instance;

                var data1 = unitsProcessPLCDataGetBusiness.Vacuum1PlcDataGet();
                var data2 = unitsProcessPLCDataGetBusiness.Vacuum2PlcDataGet();
                var data3 = unitsProcessPLCDataGetBusiness.PreHeatOvenPlcDataGet();
                var data4 = unitsProcessPLCDataGetBusiness.PreCureOvenPlcDataGet();
                var data5 = unitsProcessPLCDataGetBusiness.CureOvenPlcDataGet();

                data1.GlueAmountSetValue2 = data2.GlueAmountSetValue2;
                data1.GluePushSpeedSetValue2 = data2.GluePushSpeedSetValue2;
                data1.BarrelA1TempActValue2 = data2.BarrelA1TempActValue2;
                data1.BarrelA2TempActValue2 = data2.BarrelA2TempActValue2;
                data1.BarrelB1TempActValue2 = data2.BarrelB1TempActValue2;
                data1.BarrelB2TempActValue2 = data2.BarrelB2TempActValue2;
                data1.PumpAPressureActValue2 = data2.PumpAPressureActValue2;
                data1.PumpBPressureActValue2 = data2.PumpBPressureActValue2;
                data1.VacuumDegreeActValue2 = data2.VacuumDegreeActValue2;
                data1.PressureHoldTimeSetValue2 = data2.PressureHoldTimeSetValue2;
                _log.Info($"真空箱温度数据上传,\n " +
                $"GlueAmountSetValue1:{data1.GlueAmountSetValue1}\n " +
                $"GluePushSpeedSetValue1:{data1.GluePushSpeedSetValue1}\n " +
                $"BarrelA1TempActValue1:{data1.BarrelA1TempActValue1}\n " +
                $"BarrelA2TempActValue1:{data1.BarrelA2TempActValue1}\n " +
                $"BarrelB1TempActValue1:{data1.BarrelB1TempActValue1}\n " +
                $"BarrelB2TempActValue1:{data1.BarrelB2TempActValue1}\n " +
                $"PumpAPressureActValue1:{data1.PumpAPressureActValue1}\n " +
                $"PumpBPressureActValue1:{data1.PumpBPressureActValue1}\n " +
                $"VacuumDegreeActValue1:{data1.VacuumDegreeActValue1}\n " +
                $"PressureHoldTimeSetValue1:{data1.PressureHoldTimeSetValue1}\n " +
                $"GlueAmountSetValue2:{data1.GlueAmountSetValue2}\n " +
                $"GluePushSpeedSetValue2:{data1.GluePushSpeedSetValue2}\n " +
                $"BarrelA1TempActValue2:{data1.BarrelA1TempActValue2}\n " +
                $"BarrelA2TempActValue2:{data1.BarrelA2TempActValue2}\n " +
                $"BarrelB1TempActValue2:{data1.BarrelB1TempActValue2}\n " +
                $"BarrelB2TempActValue2:{data1.BarrelB2TempActValue2}\n " +
                $"PumpAPressureActValue2:{data1.PumpAPressureActValue2}\n " +
                $"PumpBPressureActValue2:{data1.PumpBPressureActValue2}\n " +
                $"VacuumDegreeActValue2:{data1.VacuumDegreeActValue2}\n " +
                $"PressureHoldTimeSetValue2:{data1.PressureHoldTimeSetValue2}");

                _log.Info($"隧道烤箱温度数据上传,\n " +
                $"PreheatOvenTempActValue:{data3}\n " +
                $"PreCureOven1TempActValue:{data4.PreCureOven1TempActValue}\n " +
                $"PreCureOven2TempActValue:{data4.PreCureOven2TempActValue}\n " +
                $"PreCureOven3TempActValue:{data4.PreCureOven3TempActValue}\n " +
                $"PreCureOven4TempActValue:{data4.PreCureOven4TempActValue}\n " +
                $"CureOven1TempActValue:{data5.CureOven1TempActValue}\n " +
                $"CureOven2TempActValue:{data5.CureOven2TempActValue}\n " +
                $"CureOven3TempActValue:{data5.CureOven3TempActValue}\n " +
                $"CureOven4TempActValue:{data5.CureOven4TempActValue} ");

                ////var res = _plc.ReadBytes(_plc.DeltaInstance0, "D200", 9);
                ////var bytes = res.Content.Reverse().ToArray();
                ////ushort[] shorts = new ushort[9];
                ////if (bytes != null && bytes.Length != 0)
                ////{
                ////    shorts[0] = BitConverter.ToUInt16(bytes, 0);
                ////    shorts[1] = BitConverter.ToUInt16(bytes, 2);
                ////    shorts[2] = BitConverter.ToUInt16(bytes, 4);
                ////    shorts[3] = BitConverter.ToUInt16(bytes, 6);
                ////    shorts[4] = BitConverter.ToUInt16(bytes, 8);
                ////    shorts[5] = BitConverter.ToUInt16(bytes, 10);
                ////    shorts[6] = BitConverter.ToUInt16(bytes, 12);
                ////    shorts[7] = BitConverter.ToUInt16(bytes, 14);
                ////    shorts[8] = BitConverter.ToUInt16(bytes, 16);
                ////}

                ////for (int i = 0; i < 9; i++)
                ////{
                ////    _log.Info($"D20{i * 2}数值[{shorts[i].ToString()}]");
                ////}

                ////读取托盘码和产品码
                //OperateResult<byte[]> trayBytesResult = _plc.ReadBytes(_plc.DeltaInstance3, "D5000", 10);
                //OperateResult<byte[]> pordBytesResult = _plc.ReadBytes(_plc.DeltaInstance3, "D8000", 60);

                ////转换托盘码
                //string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
                //_log.Info("RFID读数" + traycode);
                //byte[] prodBytes = pordBytesResult.Content;
                //string[] prodcode = new string[6];
                ////分割转换产品码
                //for (int i = 0; i < 6; i++)
                //{
                //    prodcode[i] = Encoding.ASCII.GetString(AscIIReverseTostring(prodBytes[(i * 20)..(i * 20 + 20)]));
                //    _log.Info($"扫码枪读数{i + 1}是{prodcode[i]}");
                //}
            }
            catch (Exception ex)
            {
                _log.Error("隧道炉温度读取失败", ex);
            }



        }

        public byte[] AscIIReverseTostring(byte[] bytes)
        {
            if (bytes.Length % 2 != 0)
            {
                return new byte[0];
            }

            for (int i = 0; i < bytes.Length / 2; i++)
            {
                byte temp = bytes[i * 2];
                bytes[i * 2] = bytes[i * 2 + 1];
                bytes[i * 2 + 1] = temp;
            }

            return bytes;
        }
    }
}
