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;
using static System.Runtime.CompilerServices.RuntimeHelpers;

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

        private readonly PLCConnect _plc = PLCConnect.Instance;

        private readonly SerilogHelper _log = SerilogHelper.Instance;
        private TrayBindingService trayBindingService = TrayBindingService.Instance;

        private ErrorAnalyseBusiness _errorAnalyse;
        private RecipeBusiness _recipeBusiness;

        public TestBusiness()
        {
            //_errorAnalyse = new ErrorAnalyseBusiness();
            //_recipeBusiness = new RecipeBusiness();
            _timer = new Timer(TimerCallback, null, 0, 1000);
            //sql更新
            //取出work唯一guid(tid)
            //var traycode = "M3\0\0\0\0\0\0\0\0\0\0";
            //traycode = traycode.Replace("\0", "");


            //bool res = trayBindingService.UpDateTime(DateTime.Now.ToString(), traycode, "1");
        }

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

                var data = unitsProcessPLCDataGetBusiness.VacuumPlcDataGet();
                _log.Info($"真空灌胶机数据 — " +
                    $"推胶速度设定值:{data.GluePushSpeedSetValue} " +
                    $"胶量设定值:[{data.GlueAmountSetValue1},{data.GlueAmountSetValue2},{data.GlueAmountSetValue3},{data.GlueAmountSetValue4},{data.GlueAmountSetValue5},{data.GlueAmountSetValue6},{data.GlueAmountSetValue7},{data.GlueAmountSetValue8},{data.GlueAmountSetValue9},{data.GlueAmountSetValue10}] " +
                    $"真空度设定值:{data.VacuumDegreeSetValue} 保压时长设定值:{data.PressureHoldTimeSetValue} " +
                    $"真空箱启用设定值:{data.VacuumIsUseSetValue} 总胶量设定值:{data.TotalGlueAmountSetValue} " +
                    $"A泵压力实际值:{data.PumpAPressureActValue} B泵压力实际值:{data.PumpBPressureActValue} " +
                    $"灌胶真空度实际值:{data.VacuumDegreeActValue} 配方数据:{data.FormulaData}");

                //var data1 = unitsProcessPLCDataGetBusiness.OvenPlcDataGet();
                //_log.Info($"隧道炉数据 — " +
                //    $"隧道炉1温度设定值:{data1.CureOven1SetValue} 隧道炉2温度设定值:{data1.CureOven2SetValue} " +
                //    $"隧道炉1温度实际值:{data1.CureOven1ActValue} 隧道炉2温度实际值:{data1.CureOven2ActValue}");
            }
            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;
        }
    }
}
