using IMCS.Device.FanucBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YG.Device;
using static IMCS.Device.FanucBase.Focas1;
namespace IMCS.Device
{
    public class Fanuc : Focas1
    {
        //下载程序  5-27
        //开始
        private static short dwnstart(ushort handle, short type)
        {
            return Fanuc.cnc_dwnstart3(handle, type);
        }
        //结束
        private static short dwnend(ushort handle)
        {
            return Fanuc.cnc_dwnend3(handle);
        }
        //下载
        private static short dwnload(ushort handle, ref int datalength, string data)
        {
            //开始下载程序  datalength将会被返回,实际的输出的字符数量
            return Fanuc.cnc_download3(handle, ref datalength, data);
        }
        //获取详细的错误信息
        private static short getdtailerr(ushort handle, Fanuc.ODBERR odberr)
        {
            return Fanuc.cnc_getdtailerr(handle, odberr);
        }
        //下载程序的入口点
        /// 
        /// 向CNC下载指定类型的程序
        /// 
        /// 句柄
        /// 程序类型
        /// 程序的内容
        /// 保存返回错误信息的详细内容,为null不返回
        /// 错误码
        public static short download(ushort handle, short type, string data, Fanuc.ODBERR odberr)
        {
            int datalength = data.Length;
            short ret = dwnstart(handle, type);
            if (ret == 0)
            {
                int olddata = datalength;
                while (true)
                {
                    ret = dwnload(handle, ref datalength, data);
                    //说明缓存已满或为空,继续尝试
                    if (ret == (short)Fanuc.focas_ret.EW_BUFFER)
                    {
                        continue;
                    }
                    if (ret == Fanuc.EW_OK)
                    {
                        //说明当前下载完成,temp记录剩余下载量
                        int temp = olddata - datalength;
                        if (temp <= 0)
                        {
                            break;
                        }
                        else
                        {
                            data = data.Substring(datalength, temp);
                            datalength = temp; olddata = temp;
                        }
                    }
                    else
                    {
                        //下载出现错误,解析出具体的错误信息
                        if (odberr != null)
                        {
                            getdtailerr(handle, odberr);
                        }
                        //下载出错
                        break;
                    }
                }
                //判断是哪里出的问题
                if (ret == 0)
                {
                    ret = dwnend(handle);
                    //结束下载出错
                    return ret;
                }
                else
                {
                    dwnend(handle);
                    //下载出错
                    return ret;
                }
            }
            else
            {
                dwnend(handle);
                //开始下载出错
                return ret;
            }
        }
        //下载程序  5-27
        //上传程序 5-28
        //开始
        private static short upstart(ushort handle, short type, int startno, int endno)
        {
            return cnc_upstart3(handle, type, startno, endno);
        }
        //上传
        private static short uplod(ushort handle, int length, char[] databuf)
        {
            return cnc_upload3(handle, ref length, databuf);
        }
        //结束
        private static short upend(ushort handle)
        {
            return cnc_upend3(handle);
        }
        //上传程序的入口
        /// 
        /// 根据程序号读取指定程序
        /// 
        /// 句柄
        /// 类型
        /// 程序号
        /// 详细错误内容,null不返回
        /// 返回的程序内容
        /// 错误码
        public static short upload(ushort handle, short type, int no, ref string data, Fanuc.ODBERR odberr)
        {
            int startno = no; int endno = no;
            int length = 256; char[] databuf = new char[256];
            short ret = upstart(handle, type, startno, endno);
            if (ret == Fanuc.EW_OK)
            {
                string temp = "";
                while (true)
                {
                    //上传
                    ret = uplod(handle, length, databuf);
                    temp = new string(databuf);
                    int one = temp.Length;
                    if (ret == (short)Fanuc.focas_ret.EW_BUFFER)
                    {
                        continue;
                    }
                    if (ret == Fanuc.EW_OK)
                    {
                        temp = temp.Replace("\0", "");
                        data += temp;
                        string lastchar = temp.Substring(temp.Length - 1, 1);
                        if (lastchar == "%")
                        {
                            break;
                        }
                    }
                    else
                    {
                        //下载出现错误,解析出具体的错误信息
                        if (odberr != null)
                        {
                            getdtailerr(handle, odberr);
                        }
                        //下载出错
                        break;
                    }
                }
                //判断是哪里出的问题
                if (ret == 0)
                {
                    ret = upend(handle);
                    //结束上传出错
                    return ret;
                }
                else
                {
                    upend(handle);
                    //上传出错
                    return ret;
                }
            }
            else
            {
                //开始出错
                upend(handle);
                return ret;
            }
        }
        //上传程序 5-28
        //根据alm_grp 编号 返回 提示内容 简
        public static string getalmgrp(short no)
        {
            string ret = "";
            switch (no)
            {
                case 0:
                    ret = "SW";
                    break;
                case 1:
                    ret = "PW";
                    break;
                case 2:
                    ret = "IO";
                    break;
                case 3:
                    ret = "PS";
                    break;
                case 4:
                    ret = "OT";
                    break;
                case 5:
                    ret = "OH";
                    break;
                case 6:
                    ret = "SV";
                    break;
                case 7:
                    ret = "SR";
                    break;
                case 8:
                    ret = "MC";
                    break;
                case 9:
                    ret = "SP";
                    break;
                case 10:
                    ret = "DS";
                    break;
                case 11:
                    ret = "IE";
                    break;
                case 12:
                    ret = "BG";
                    break;
                case 13:
                    ret = "SN";
                    break;
                case 14:
                    ret = "reserved";
                    break;
                case 15:
                    ret = "EX";
                    break;
                case 19:
                    ret = "PC";
                    break;
                default:
                    ret = "Not used";
                    break;
            }
            return ret;
        }
        //根据alm_grp 编号 返回 提示内容 简
        //2016-6-2 根据地址码和地址号,返回完整的显示信息
        public static string getpmcadd(short a, ushort b)
        {
            string tempa = "";
            switch (a)
            {
                case 0:
                    tempa = "G";
                    break;
                case 1:
                    tempa = "F";
                    break;
                case 2:
                    tempa = "Y";
                    break;
                case 3:
                    tempa = "X";
                    break;
                case 4:
                    tempa = "A";
                    break;
                case 5:
                    tempa = "R";
                    break;
                case 6:
                    tempa = "T";
                    break;
                case 7:
                    tempa = "K";
                    break;
                case 8:
                    tempa = "C";
                    break;
                case 9:
                    tempa = "D";
                    break;
                default:
                    tempa = "n";
                    break;
            }
            string tempb = b.ToString().PadLeft(4, '0');
            return tempa + tempb;
        }
        //2016-6-2 根据地址码和地址号,返回完整的显示信息
    }
    /// 
    /// 发那科
    /// 
    public class DeviceFanucHandle
    {
        public bool IsOn { get; set; }
        public ushort h;
        public DeviceFanucHandle(string ip, int port, int outtime = 10)
        {
            int ret = Fanuc.cnc_allclibhndl3(ip, Convert.ToUInt16(port), outtime, out h);
            if (ret == Fanuc.EW_OK)
            {
                IsOn = true;
                YG.Log.Instance.WriteLogAdd($"Fanuc连接成功--->{DateTime.Now}");
            }
            else
            {
                IsOn = false;
                YG.Log.Instance.WriteLogAdd($"Fanuc连接失败--->{DateTime.Now}");
            }
        }
        /// 
        /// 关闭连接
        /// 
        /// 
        public bool Fanuc_Close()
        {
            bool result = false;
            if (IsOn)
            {
                int ret = Fanuc.cnc_freelibhndl(h);
                if (ret == Fanuc.EW_OK)
                {
                    YG.Log.Instance.WriteLogAdd("断开连接成功!");
                    result = true;
                }
                else
                {
                    YG.Log.Instance.WriteLogAdd($"{ret}--->断开连接失败-->>{DateTime.Now}");
                }
            }
            return result;
        }
       /// 
       /// 读取正在执行的程序名称
       /// 
       /// 
        public bool Fanuc_ReadCurrentProc()
        {
            bool result = false;
            ODBEXEPRG oDBEXEPRG = new ODBEXEPRG();
            short ret = Fanuc.cnc_exeprgname(h, oDBEXEPRG);
            if (ret == Fanuc.EW_OK)
            {
                YG.Log.Instance.WriteLogAdd("成功读取当前正在执行的程序名称!");
                result = true;
            }
            else
            {
                YG.Log.Instance.WriteLogAdd($"{ret}--->读取当前正在执行的程序名称失败-->>{DateTime.Now}");
            }
            return result;
        }
        /// 
        /// 读取当前机床刀具信息
        /// 
        /// 
        public bool Fanuc_ReadTool()
        {
            bool result = false;
            get_tool_life();
            return result;
        }
        /// 
        /// 读取获得刀片的有关信息
        /// 
        private void get_tool_life()//
        {
            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);
            Fanuc.cnc_rdntool(h, b, tool2);//刀具的数量
            Fanuc.cnc_rdlife(h, b, tool2);//刀具寿命
            Fanuc.cnc_rdcount(h, b, tool2);//刀具计时器
            Fanuc.cnc_rdtoolgrp(h, 2, 20 + 20 * 1, btg);//根据刀组号读出所有信息,很重要;
            Fanuc.cnc_rdtlusegrp(h, grp);//读出正在使用的到组号;
           System.Console.WriteLine("刀片组号:" + group_numer);
           System.Console.WriteLine("type:" + tool.type);
           System.Console.WriteLine("刀片组的全部数量" + all);
           System.Console.WriteLine("刀片号:" + tool2.data);
           System.Console.WriteLine("刀片组号;" + group_numer + "   寿命:" + tool2.data);
           System.Console.WriteLine("刀片组号;" + group_numer + "   寿命计时:" + tool2.data);
        }
     
    }
}