using MesWork;
using Newtonsoft.Json;
using OpcBasic;
using System;
using System.Collections.Concurrent;

namespace DC_A95
{
    public class PLC_R
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="opCode">工位号</param>
        /// <param name="partCode">上线缸体码</param>
        /// <param name="palletNo">托盘号</param>
        /// <returns></returns>
        public static bool Read_LineSet(string opCode, out string partCode, out int palletNo)
        {
            partCode = MesWorkForm.ReadPLC(120, opCode).ToString();
            palletNo = Convert.ToInt32(MesWorkForm.ReadPLC(80, opCode));
            return true;
        }
    
        public static string GetString_CleanGarbled(string inputStr)
        {
            return inputStr.Replace("\0", "").Replace("\n", "").Replace("\r", "").Replace(" ", "");
        }


        public static ConcurrentDictionary<string, TagFormat> ReadPLC_OpName(string opName)
        {
            if (MesWorkForm.IsDemoMesServer == 1)
            {
                return JsonConvert.DeserializeObject<
                    ConcurrentDictionary<string, TagFormat>
                    >(JsonConvert.SerializeObject(
                        PlcLinkForm.ReadPLC_GroupData(opName)
                    ));
            }
            else
            {
                try
                {
                    PlcLinkForm.GetDbAndMaxLengthByOpName(opName, out string tagStartAdr, out ushort tagAdrListMaxLength);
                    return JsonConvert.DeserializeObject<
                        ConcurrentDictionary<string, TagFormat>
                        >(JsonConvert.SerializeObject(PlcLinkForm.ReadQualityDataList(
                             opName
                            , tagStartAdr
                            , tagAdrListMaxLength
                            )
                        ));
                }
                catch (Exception err)
                {
                    return null;
                }

            }


        }


    }
}
