
using FANUC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static fanuc采集.ConDevice;

namespace HttpServer
{
    public class ConDevice2
    {
        private string remotePath = "//CNC_MEM/USER/PATH1/";
        public ushort h = 0;
        public ConDevice2(string ip, string port)
        {
            Fanuc.cnc_allclibhndl3(ip, Convert.ToUInt16(port), 1, out h);
        }
        public void freehand()
        {
            Focas1.cnc_freelibhndl(h);
        } 

        public string MainProg()
        {

            Focas1.ODBPRO pnum = new Focas1.ODBPRO();
            int ret = Focas1.cnc_rdprgnum(h, pnum);
            if (ret == Fanuc.EW_OK)
            {

                return pnum.data.ToString().PadLeft(4, '0');
            }
            else
            {
                return "error";
            }

        }

        public string Status()
        {

            Focas1.ODBST cncStatus = new Focas1.ODBST();
            string[] str = new string[3];
            int ret = Focas1.cnc_statinfo(h, cncStatus);
            if (ret == Fanuc.EW_OK)
            {

                return cncStatus.run.ToString();
            }
            else
            {
                return "error";
            }

        }
        public string Mode()
        {

            Focas1.ODBST cncStatus = new Focas1.ODBST();
            string[] str = new string[3];
            int ret = Focas1.cnc_statinfo(h, cncStatus);
            if (ret == Fanuc.EW_OK)
            {

                return cncStatus.aut.ToString();
            }
            else
            {
                return "error";
            }
        }




        public string EMG()
        {

            Focas1.IODBPMC0 iodbpmc0 = new Focas1.IODBPMC0();
            int ret = Focas1.pmc_rdpmcrng(h, 0, 0, 7, 12, 14, iodbpmc0);
            if (ret == Fanuc.EW_OK)
            {

                byte[] data = iodbpmc0.cdata;
                string value = Convert.ToString(data[1], 16).ToString().PadLeft(2, '0').ToUpper();
                string str = Convert.ToString(Convert.ToInt32(value.ToString().Substring(0, 1)), 2).PadLeft(4, '0').Substring(3, 1);
                if (str == "1")
                    return "1";
                else
                    return "0";
            }
            else
            {
                return "error";
            }

        }



        public string ActFeed()
        {
            //主轴进给率
            Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
            int ret = Focas1.pmc_rdpmcrng(h, 0, 1, 12, 13, 10, iodbpmco);
            if (ret == Fanuc.EW_OK)
            {                
                return (100 - (iodbpmco.cdata[0] - 155)).ToString();
            }
            else
            {
                return "error";
            }

        }

        public string spindleMagnification()
        {
            //主轴倍率           
            Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
            int ret = Focas1.pmc_rdpmcrng(h, 0, 1, 30, 31, 10, iodbpmco);
            if (ret == Fanuc.EW_OK)
            {
                return iodbpmco.cdata[0].ToString();
            }
            else
            {
                return "error";
            }

        }

        public string SpindleLoad()
        {
            //主轴负载
            String loaderX = this.ServoLoadX();
            String loaderY = this.ServoLoadY();
            String loaderZ = this.ServoLoadZ();
            if (loaderX == "error" || loaderY == "error" || loaderZ == "error") return "error";
            return loaderX + "/" + loaderY + "/" + loaderZ;

        }


        public string ActSpindle()
        {
            //主轴转数 pmc_rdpmcrng(hFanuc, 1, 2, 22, 25, 8 + 1 * 4, iodbpmco);
            //主轴实际转数
            Focas1.ODBACT pindle = new Focas1.ODBACT();

            int ret = Focas1.cnc_acts(h, pindle);
            if (ret == Fanuc.EW_OK)
            {
                return pindle.data.ToString();
            }
            else
            {
                return "error";
            }

        }




        public string ServoLoadX()
        {

            Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
            short a = 6;//伺服轴的数量
            int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
            if (ret == Fanuc.EW_OK)
            {

                return sv.svload1.data.ToString();
            }
            else
            {
                return "error";
            }

        }

        public string ServoLoadY()
        {

            Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
            short a = 6;//伺服轴的数量
            int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
            if (ret == Fanuc.EW_OK)
            {

                return sv.svload2.data.ToString();
            }
            else
            {
                return "error";
            }

        }
        public string ServoLoadZ()
        {

            Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
            short a = 6;//伺服轴的数量
            int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
            if (ret == Fanuc.EW_OK)
            {

                return sv.svload3.data.ToString();
            }
            else
            {
                return "error";
            }

        }


        public string PowerOnTime()
        {

            Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
            int ret = Focas1.cnc_rdparam(h, 6750, 0, 8, iodbpsd);
            if (ret == Fanuc.EW_OK)
            {

                return iodbpsd.ldata.ToString();
            }
            else
            {
                return "error";
            }

        }



        public string AccumulateCuttingTime()
        {

            Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
            int ret = Focas1.cnc_rdparam(h, 6754, 0, 8, iodbpsd);
            if (ret == Fanuc.EW_OK)
            {

                return iodbpsd.ldata.ToString();
            }
            else
            {
                return "error";
            }



        }



        public string CuttingTimePerCycle()
        {

            Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
            int min = 0;
            int sec = 0;
            int ret = Focas1.cnc_rdparam(h, 6757, 0, 8, iodbpsd);//秒
            if (ret == Focas1.EW_OK)
            {
                sec = iodbpsd.ldata;
                ret = Focas1.cnc_rdparam(h, 6758, 0, 8, iodbpsd);//分
                if (ret == Focas1.EW_OK)
                {
                    min = iodbpsd.ldata;

                    return (min * 60 * 1000 + sec).ToString();
                }
                else
                { return "error"; }

            }
            else { return "error"; }




        }





        public string WorkTime()
        {

            Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
            int ret = Focas1.cnc_rdparam(h, 6752, -1, 8, iodbpsd);
            if (ret == Fanuc.EW_OK)
            {

                return iodbpsd.ldata.ToString();
            }
            else
            {
                return "error";
            }



        }
        public string Part_Count()
        {

            Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
            int ret = Focas1.cnc_rdparam(h, 6712, 0, 8, iodbpsd);
            if (ret == Fanuc.EW_OK)
            {

                return iodbpsd.ldata.ToString();
            }
            else
            {
                return "error";
            }


        }


        public string ToolNo()
        {

            Focas1.ODBM odb = new Focas1.ODBM();

            int ret = Focas1.cnc_rdmacro(h, 523, 10, odb);
            if (ret == Fanuc.EW_OK)
            {

                return (odb.mcr_val * Math.Pow(10, -odb.dec_val)).ToString();
            }
            else
            {
                return "error";
            }
        }

        public string ToolLife(string IP, string port)
        {
            ushort h = 0;
            int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
            List<Tool> tools = new List<Tool>();
            Tool toolEntity = new Tool();
            if (ret == Fanuc.EW_OK)
            {
                string[] grp_nos = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
                foreach (string _grp_no in grp_nos)
                {
                    short grp_no = short.Parse(_grp_no);
                    short num = short.Parse("10");
                    Focas1.IODBTLGRP iodbtlgrp = new Focas1.IODBTLGRP();

                    ret = Focas1.cnc_rdtlgrp(h, grp_no, ref num, iodbtlgrp);

                    Console.WriteLine("=============================group_no====" + grp_no + "=====================");
                    Console.WriteLine(num);
                    //此组第一把刀的信息
                    Console.WriteLine(iodbtlgrp.data1.ntool);
                    Console.WriteLine(iodbtlgrp.data1.nfree);
                    Console.WriteLine(iodbtlgrp.data1.life);
                    Console.WriteLine(iodbtlgrp.data1.count);
                    Console.WriteLine(iodbtlgrp.data1.use_tool);
                    Console.WriteLine(iodbtlgrp.data1.opt_grpno);
                    Console.WriteLine(iodbtlgrp.data1.life_rest);
                    Console.WriteLine(iodbtlgrp.data1.rest_sig);
                    //以此类推 此组第二把刀的信息
                    Console.WriteLine(iodbtlgrp.data2.ntool);
                    switch (iodbtlgrp.data1.count_type)
                    {
                        case 0:
                            Console.WriteLine("count型");
                            break;
                        case 1:
                            Console.WriteLine("minute型");
                            break;
                    }
                }
                    var targetLifes = from t in tools select t.targetLife.ToString();
                return string.Join(",", targetLifes.ToArray());
            }
            else { return "error"; }

        }

            public string ToolLife()
        {            
            Fanuc.ODBTLIFE5 tool = new Focas1.ODBTLIFE5();//读取刀片组的序号
            Fanuc.ODBTLIFE2 tool1 = new Focas1.ODBTLIFE2();//读取刀片组的全部数量
            Fanuc.ODBTLIFE3 tool2 = new Focas1.ODBTLIFE3();//刀具的数量
            Fanuc.ODBTG btg = new Focas1.ODBTG();
            Fanuc.ODBUSEGRP grp = new Focas1.ODBUSEGRP();
            int m = 2; 
            Fanuc.cnc_rdgrpid2(h, m, tool);
            int group_numer = tool.data;
            int all = Fanuc.cnc_rdngrp(h, tool1);//刀片组的全部数量
            short b = Convert.ToInt16(group_numer);
            int ret = Fanuc.cnc_rdntool(h, b, tool2);//刀具的数量            
            ret = Fanuc.cnc_rdlife(h, b, tool2);//刀具寿命
            ret = Fanuc.cnc_rdcount(h, b, tool2);//刀具计时器
            ret = Fanuc.cnc_rdtoolgrp(h, 2, 20 + 20 * 1, btg);//根据刀组号读出所有信息，很重要；

           //int ret   =Fanuc.cnc_rdtlusegrp(h, grp);//读出正在使用的到组号；

            if (ret == Fanuc.EW_OK)
            {
                return tool2.data.ToString();
            }
            else if(ret == (short)Fanuc.focas_ret.EW_NOOPT)
            {
                return "0";
            }
            else
            {
                return "error";
            }

        }

        public List<AlmInfo> AlmMsg( out string IsAlarm)
        {
            List<AlmInfo> stateinfo = new List<AlmInfo>();
            IsAlarm = "0";
            ushort h = 0;

            short b = -1;//哪一类报警，-1所有报警
            short num = 11;//设大一些没关系
            Focas1.ODBALMMSG odbalmmsg2 = new Focas1.ODBALMMSG();
            int ret = Focas1.cnc_rdalmmsg(h, b, ref num, odbalmmsg2);
            System.Type type = odbalmmsg2.GetType();
            if (ret == 0)
            {
                if (num > 0)
                {
                    IsAlarm = "1";
                    for (int i = 0; i <= num - 1; i++)
                    {
                        string str1 = "msg";
                        str1 = str1 + (i + 1);
                        object obj = type.GetField(str1).GetValue(odbalmmsg2);
                        //if(obj.)
                        System.Type type1 = obj.GetType();
                        if (type1.GetField("alm_msg").GetValue(obj).ToString() != "")
                        {
                            AlmInfo s = new AlmInfo();
                            s.no = Convert.ToInt32(type1.GetField("alm_no").GetValue(obj).ToString()).ToString();
                            s.msg = type1.GetField("alm_msg").GetValue(obj).ToString();
                            stateinfo.Add(s);
                        }
                    }

                    return stateinfo;
                }
                else
                {
                    IsAlarm = "0";
                       AlmInfo s2 = new AlmInfo();
                    s2.no = "0";
                    s2.msg = "无报警";
                    stateinfo.Add(s2);


                    return stateinfo;
                }
            }
            else
            {
                IsAlarm = "0";
                AlmInfo s3 = new AlmInfo();
                s3.no = "0";
                s3.msg = "无报警";
                stateinfo.Add(s3);
                return stateinfo;
            }

        }

        //履历信息
        public List<AlmInfo> operationAlarm()
        {
            List<AlmInfo> stateinfo = new List<AlmInfo>();
           string IsAlarm = "0";
            //停止日志Stop sampling
            var ret = Focas1.cnc_stopophis(h);

            ushort s_no = 1;
            ushort e_no = 10;
            ushort length = 700;

            Focas1.ODBOMHIS2 odbomhis2 = new Focas1.ODBOMHIS2();

            ret = Focas1.cnc_rdomhistry2(h, s_no, e_no, length, odbomhis2);

            //System.Type type = odbomhis2.GetType();
            ushort num  = odbomhis2.e_no;
            if (ret == 0)
            {
                if (num > 0)
                {
                    Focas1.OPM_HIS opm_his =  odbomhis2.opm_his;
                    System.Type type = opm_his.GetType();
                    for (int i = 1; i <= num ; i++)
                    {
                       
                        string str1 = "data";

                        str1 = str1 + i ;
                        object obj = type.GetField(str1).GetValue(opm_his);
                        System.Type dataType = obj.GetType();
 
                      
                        if (dataType.GetField("alm_msg").GetValue(obj).ToString() != "")
                        {
                            AlmInfo s = new AlmInfo();
                            s.no = Convert.ToInt32(dataType.GetField("om_no").GetValue(obj).ToString()).ToString();
                            s.msg = dataType.GetField("alm_msg").GetValue(obj).ToString();
                            stateinfo.Add(s);

                        }
                    }
                }
               
            }
            //else
            //{
            //    IsAlarm = "0";
            //    AlmInfo s2 = new AlmInfo();
            //    s2.no = "0";
            //    s2.msg = "无报警";
            //    stateinfo.Add(s2);


            //    return stateinfo;
            //}
            return stateinfo;

        }
        //履历
        public List<AlmInfo> rdalmhistry5()
        {
            List<AlmInfo> stateinfo = new List<AlmInfo>();
            string IsAlarm = "0";
            //停止日志Stop sampling
            var ret = Focas1.cnc_stopophis(h);

            ushort s_no = 1;
            ushort e_no = 10;
            ushort length = 700;

            Focas1.ODBAHIS5 almHis5 = new Focas1.ODBAHIS5();

            ret = Focas1.cnc_rdalmhistry5(h, s_no, e_no, length, almHis5);
 
            ushort num = almHis5.e_no;
            if (ret == 0)
            {
                if (num > 0)
                {
                    Focas1.ALM_HIS5 opm_his = almHis5.alm_his;
                    System.Type type = opm_his.GetType();
                    for (int i = 1; i <= num; i++)
                    {

                        string str1 = "data";

                        str1 = str1 + i;
                        object obj = type.GetField(str1).GetValue(opm_his);
                        System.Type dataType = obj.GetType();


                        if (dataType.GetField("alm_msg").GetValue(obj).ToString() != "")
                        {
                            AlmInfo s = new AlmInfo();
                            s.no = Convert.ToInt32(dataType.GetField("om_no").GetValue(obj).ToString()).ToString();
                            s.msg = dataType.GetField("alm_msg").GetValue(obj).ToString();
                            stateinfo.Add(s);

                        }
                    }
                }
                
            }
            //else
            //{
            //    IsAlarm = "0";
            //    AlmInfo s2 = new AlmInfo();
            //    s2.no = "0";
            //    s2.msg = "无报警";
            //    stateinfo.Add(s2);


            //    return stateinfo;
            //}
            return stateinfo;
        }
    } 

 
}