private void afterProgStateChanged(object sender, EventArgs e)
        {
            DateTime now = new DateTime();

            now = DateTime.Now;
            JDMonitorRealTimeModel mode = sender as JDMonitorRealTimeModel;

            using (Materiel_NoScan_V2Entities CIE = new  Materiel_NoScan_V2Entities())
            {
                using (System.Data.Entity.DbContextTransaction mytran = CIE.Database.BeginTransaction())
                {
                    try
                    {
                        var StateID = CIE.JDJS_WMS_Device_ProgState_Info.Where(r => r.CncID == mode.ID && r.EndTime == null);
                        if (StateID.Count() < 1)
                        {
                            JDJS_WMS_Device_ProgState_Info state = new JDJS_WMS_Device_ProgState_Info()
                            {
                                CncID     = mode.ID,
                                ProgState = mode.ProgState,
                                StartTime = now
                            };
                            CIE.JDJS_WMS_Device_ProgState_Info.Add(state);
                            CIE.SaveChanges();
                        }
                        else
                        {
                            foreach (var item in StateID)
                            {
                                item.EndTime = now;
                            }
                            JDJS_WMS_Device_ProgState_Info state = new JDJS_WMS_Device_ProgState_Info()
                            {
                                CncID     = mode.ID,
                                ProgState = mode.ProgState,
                                StartTime = now
                            };
                            CIE.JDJS_WMS_Device_ProgState_Info.Add(state);
                            CIE.SaveChanges();
                        }
                        var alarmstate = CIE.JDJS_WMS_Device_Alarm_History_Table.Where(r => r.CncID == mode.ID && r.EndTime == null);
                        foreach (var item in alarmstate)
                        {
                            item.EndTime = now;
                        }
                        CIE.SaveChanges();


                        mytran.Commit();
                        CIE.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        if (!Directory.Exists("log"))
                        {
                            Directory.CreateDirectory("log");
                        }
                        using (StreamWriter write = new StreamWriter("log/" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + ".log", true))
                        {
                            write.WriteLine(DateTime.Now.ToString() + " " + ex);
                        }
                        mytran.Rollback();
                    }
                }
            }
        }
        public bool CncDataSave(Dictionary <int, IMonitorRealTimeModel> models)
        {
            using (Materiel_NoScan_V2Entities CIE = new Materiel_NoScan_V2Entities())
            {
                using (System.Data.Entity.DbContextTransaction mytran = CIE.Database.BeginTransaction())
                {
                    try
                    {
                        foreach (var mode in models.ToArray())
                        {
                            var device = CIE.JDJS_WMS_Device_Info.Where(r => r.ID == mode.Key);
                            if (device.Count() > 0)
                            {
                                JDMonitorRealTimeModel mo = mode.Value as JDMonitorRealTimeModel;
                                //保存实时数据
                                if (!(mo.MachCoord == null))
                                {
                                    var id = CIE.JDJS_WMS_Device_RealTime_Data.Where(r => r.CncID == mode.Key);
                                    if (id.Count() < 1)
                                    {
                                        JDJS_WMS_Device_RealTime_Data jDMachRealTimeData = new JDJS_WMS_Device_RealTime_Data()
                                        {
                                            CncID        = mode.Key,
                                            MachCoord_X  = mo.MachCoord[0],
                                            MachCoord_Y  = mo.MachCoord[1],
                                            MachCoord_Z  = mo.MachCoord[2],
                                            MachCoord_A  = mo.MachCoord[3],
                                            MachCoord_B  = mo.MachCoord[4],
                                            MachCoord_C  = mo.MachCoord[5],
                                            AbsCoord_X   = mo.AbsCoord[0],
                                            AbsCoord_Y   = mo.AbsCoord[1],
                                            AbsCoord_Z   = mo.AbsCoord[2],
                                            AbsCoord_A   = mo.AbsCoord[3],
                                            AbsCoord_B   = mo.AbsCoord[4],
                                            AbsCoord_C   = mo.AbsCoord[5],
                                            RelCoord_X   = mo.RelCoord[0],
                                            RelCoord_Y   = mo.RelCoord[1],
                                            RelCoord_Z   = mo.RelCoord[2],
                                            RelCoord_A   = mo.RelCoord[3],
                                            RelCoord_B   = mo.RelCoord[4],
                                            RelCoord_C   = mo.RelCoord[5],
                                            ProgState    = mo.ProgState,
                                            CurrWCoord   = mo.CurrWCoord,
                                            FeedRate     = mo.FeedRate,
                                            SpindleSpeed = mo.SpindleSpeed,
                                            ToolNo       = mo.ToolNO,
                                            MachTime     = mo.MachTime,
                                            CurrO        = mo.CurrO,
                                            CurrOMainO   = mo.CurrMainO,
                                            CountNum     = mo.WorkCount,
                                            SpindleRate  = mo.Rates[0],
                                            Rate         = mo.Rates[1],
                                            SInfo_A      = mo.Sinfo[0],
                                            SInfo_NM     = mo.Sinfo[1],
                                            SInfo_W      = mo.Sinfo[2],
                                            CurLineNo    = mo.CurLineNo[0],
                                            //CurLineText =mo.CurrLineText ,
                                            Time_Y    = mo.time[0],
                                            Time_M    = mo.time[1],
                                            Time_D    = mo.time[2],
                                            Time_H    = mo.time[3],
                                            Time_MIT  = mo.time[4],
                                            Time_S    = mo.time[5],
                                            Time_zone = mo.time[6],
                                            Mode      = mo.Mode
                                        };
                                        CIE.JDJS_WMS_Device_RealTime_Data.Add(jDMachRealTimeData);
                                        //CIE.SaveChanges();
                                    }
                                    else//已存在则修改
                                    {
                                        foreach (var item in id)
                                        {
                                            item.MachCoord_X  = mo.MachCoord[0];
                                            item.MachCoord_Y  = mo.MachCoord[1];
                                            item.MachCoord_Z  = mo.MachCoord[2];
                                            item.MachCoord_A  = mo.MachCoord[3];
                                            item.MachCoord_B  = mo.MachCoord[4];
                                            item.MachCoord_C  = mo.MachCoord[5];
                                            item.AbsCoord_X   = mo.AbsCoord[0];
                                            item.AbsCoord_Y   = mo.AbsCoord[1];
                                            item.AbsCoord_Z   = mo.AbsCoord[2];
                                            item.AbsCoord_A   = mo.AbsCoord[3];
                                            item.AbsCoord_B   = mo.AbsCoord[4];
                                            item.AbsCoord_C   = mo.AbsCoord[5];
                                            item.RelCoord_X   = mo.RelCoord[0];
                                            item.RelCoord_Y   = mo.RelCoord[1];
                                            item.RelCoord_Z   = mo.RelCoord[2];
                                            item.RelCoord_A   = mo.RelCoord[3];
                                            item.RelCoord_B   = mo.RelCoord[4];
                                            item.RelCoord_C   = mo.RelCoord[5];
                                            item.ProgState    = mo.ProgState;
                                            item.CurrWCoord   = mo.CurrWCoord;
                                            item.FeedRate     = mo.FeedRate;
                                            item.SpindleSpeed = mo.SpindleSpeed;
                                            item.ToolNo       = mo.ToolNO;
                                            item.MachTime     = mo.MachTime;
                                            item.CurrO        = mo.CurrO;
                                            item.CurrOMainO   = mo.CurrMainO;
                                            item.CountNum     = mo.WorkCount;
                                            item.SpindleRate  = mo.Rates[0];
                                            item.Rate         = mo.Rates[1];
                                            item.SInfo_A      = mo.Sinfo[0];
                                            item.SInfo_NM     = mo.Sinfo[1];
                                            item.SInfo_W      = mo.Sinfo[2];
                                            item.CurLineNo    = mo.CurLineNo[0];
                                            item.CurLineText  = mo.CurrLineText;
                                            item.Time_Y       = mo.time[0];
                                            item.Time_M       = mo.time[1];
                                            item.Time_D       = mo.time[2];
                                            item.Time_H       = mo.time[3];
                                            item.Time_MIT     = mo.time[4];
                                            item.Time_S       = mo.time[5];
                                            item.Time_zone    = mo.time[6];
                                            item.Mode         = mo.Mode;
                                            //CIE.SaveChanges();
                                        }
                                    }
                                }
                                else
                                {
                                    var id = CIE.JDJS_WMS_Device_RealTime_Data.Where(r => r.CncID == mode.Key);
                                    if (id.Count() < 1)
                                    {
                                        JDJS_WMS_Device_RealTime_Data jDMachRealTimeData = new JDJS_WMS_Device_RealTime_Data()
                                        {
                                            CncID        = mode.Key,
                                            MachCoord_X  = -1,
                                            MachCoord_Y  = -1,
                                            MachCoord_Z  = -1,
                                            MachCoord_A  = -1,
                                            MachCoord_B  = -1,
                                            MachCoord_C  = -1,
                                            AbsCoord_X   = -1,
                                            AbsCoord_Y   = -1,
                                            AbsCoord_Z   = -1,
                                            AbsCoord_A   = -1,
                                            AbsCoord_B   = -1,
                                            AbsCoord_C   = -1,
                                            RelCoord_X   = -1,
                                            RelCoord_Y   = -1,
                                            RelCoord_Z   = -1,
                                            RelCoord_A   = -1,
                                            RelCoord_B   = -1,
                                            RelCoord_C   = -1,
                                            ProgState    = -1,
                                            CurrWCoord   = -1,
                                            FeedRate     = -1,
                                            SpindleSpeed = -1,
                                            ToolNo       = -1,
                                            MachTime     = -1,
                                            CurrO        = -1,
                                            CurrOMainO   = -1,
                                            CountNum     = -1,
                                            SpindleRate  = -1,
                                            Rate         = -1,
                                            SInfo_A      = -1,
                                            SInfo_NM     = -1,
                                            SInfo_W      = -1,
                                            CurLineNo    = -1,
                                            //CurLineText =mo.CurrLineText ,
                                            Time_Y    = -1,
                                            Time_M    = -1,
                                            Time_D    = -1,
                                            Time_H    = -1,
                                            Time_MIT  = -1,
                                            Time_S    = -1,
                                            Time_zone = -1,
                                            Mode      = -1
                                        };
                                        CIE.JDJS_WMS_Device_RealTime_Data.Add(jDMachRealTimeData);
                                        //CIE.SaveChanges();
                                    }
                                    else//已存在则修改
                                    {
                                        foreach (var item in id)
                                        {
                                            item.MachCoord_X  = -1;
                                            item.MachCoord_Y  = -1;
                                            item.MachCoord_Z  = -1;
                                            item.MachCoord_A  = -1;
                                            item.MachCoord_B  = -1;
                                            item.MachCoord_C  = -1;
                                            item.AbsCoord_X   = -1;
                                            item.AbsCoord_Y   = -1;
                                            item.AbsCoord_Z   = -1;
                                            item.AbsCoord_A   = -1;
                                            item.AbsCoord_B   = -1;
                                            item.AbsCoord_C   = -1;
                                            item.RelCoord_X   = -1;
                                            item.RelCoord_Y   = -1;
                                            item.RelCoord_Z   = -1;
                                            item.RelCoord_A   = -1;
                                            item.RelCoord_B   = -1;
                                            item.RelCoord_C   = -1;
                                            item.ProgState    = -1;
                                            item.CurrWCoord   = -1;
                                            item.FeedRate     = -1;
                                            item.SpindleSpeed = -1;
                                            item.ToolNo       = -1;
                                            item.MachTime     = -1;
                                            item.CurrO        = -1;
                                            item.CurrOMainO   = -1;
                                            item.CountNum     = -1;
                                            item.SpindleRate  = -1;
                                            item.Rate         = -1;
                                            item.SInfo_A      = -1;
                                            item.SInfo_NM     = -1;
                                            item.SInfo_W      = -1;
                                            item.CurLineNo    = -1;
                                            //item.CurLineText = -1;
                                            item.Time_Y    = -1;
                                            item.Time_M    = -1;
                                            item.Time_D    = -1;
                                            item.Time_H    = -1;
                                            item.Time_MIT  = -1;
                                            item.Time_S    = -1;
                                            item.Time_zone = -1;
                                            item.Mode      = -1;
                                            //CIE.SaveChanges();
                                        }
                                    }
                                }
                            }
                        }
                        CIE.SaveChanges();
                        mytran.Commit();

                        return(true);
                    }
                    catch (Exception ex)
                    {
                        if (!Directory.Exists("log"))
                        {
                            Directory.CreateDirectory("log");
                        }
                        using (StreamWriter write = new StreamWriter("log/" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + ".log", true))
                        {
                            write.WriteLine(DateTime.Now.ToString() + " " + ex);
                        }
                        mytran.Rollback();
                        return(false);
                    }
                }
            }
        }
        public List <CncInfo> GetCncInfo()
        {
            List <CncInfo> infos = new List <CncInfo>();

            try
            {
                using (Materiel_NoScan_V2Entities model = new Materiel_NoScan_V2Entities())
                {
                    var dataList = from cnc in model.JDJS_WMS_Device_Info
                                   join type in model.JDJS_WMS_Device_Type_Info on cnc.MachType equals type.ID
                                   join brand in model.JDJS_WMS_Device_Brand_Info on type.BrandID equals brand.ID
                                   select new
                    {
                        cnc.ID,
                        cnc.IP,
                        cnc.MachNum,
                        cnc.MachState,
                        cnc.MachType,
                        cnc.Position,
                        type.Type,
                        type.BrandID,
                        brand.Brand
                    };
                    if (dataList.Count() > 0)
                    {
                        foreach (var item in dataList)
                        {
                            switch (item.BrandID)//判断机床品牌
                            {
                            case 1:
                                JDInfo jd = new JDInfo();
                                jd.ID        = item.ID;
                                jd.MachNum   = item.MachNum;
                                jd.MachBrand = item.Brand;
                                jd.MachType  = item.Type;
                                jd.IP        = item.IP;
                                jd.MachState = item.MachState;
                                infos.Add(jd);
                                break;

                            case 2:
                                break;

                            case 3:
                                break;

                            case 4:
                                break;

                            case 5:
                                break;

                            case 6:
                                break;
                            }
                        }
                    }
                }

                return(infos);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Exemple #4
0
        public override bool GetData(IMonitorRealTimeModel model)
        {
            try
            {
                JDMonitorRealTimeModel monitorRealTimeModel = model as JDMonitorRealTimeModel;

                //机床坐标
                double[] machCorrd = new double[6];
                double[] absCorrd  = new double[6];
                double[] relCorrd  = new double[6];
                if (Function.GetMachPos(ptr, machCorrd, absCorrd, relCorrd))
                {
                    monitorRealTimeModel.MachCoord = machCorrd;
                    monitorRealTimeModel.AbsCoord  = absCorrd;
                    monitorRealTimeModel.RelCoord  = relCorrd;
                }
                else
                {
                    return(false);
                }
                //机床程序状态
                int progState = -1;
                int almInfo   = -1;
                if (Function.GetMachAlmInfo(ptr, ref almInfo))
                {
                    if (almInfo == 0)
                    {
                        if (Function.GetProgState(ptr, ref progState))
                        {
                            monitorRealTimeModel.ProgState = progState;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        progState = 4;
                        monitorRealTimeModel.ProgState = progState;
                        int   errCount    = -1;
                        int[] errCodeList = new int[50];
                        byte[,] errDescList = new byte[50, 128];
                        List <string> errDesc = new List <string>();

                        if (Function.GetCncErrList(ptr, ref errCount, errCodeList, errDescList))
                        {
                            byte[] buff = new byte[128];
                            for (int i = 0; i < 50; i++)
                            {
                                for (int j = 0; j < 128; j++)
                                {
                                    buff[j] = errDescList[i, j];
                                }
                                string str = Encoding.Default.GetString(buff).Replace("\0", "").Replace("\r", "").Replace("\n", "");
                                errDesc.Add(str);
                            }
                            {
                                using (Materiel_NoScan_V2Entities cie = new Materiel_NoScan_V2Entities())
                                {
                                    var alarminfo = cie.JDJS_WMS_Device_Alarm_History_Table.Where(r => r.CncID == this.cncInfo.ID && r.EndTime == null);
                                    if (alarminfo.Count() < 1)
                                    {
                                        for (int i = 0; i < errCount; i++)
                                        {
                                            JDJS_WMS_Device_Alarm_History_Table alarms = new JDJS_WMS_Device_Alarm_History_Table()
                                            {
                                                CncID     = this.cncInfo.ID,
                                                ErrCode   = errCodeList[i].ToString(),
                                                ErrDesc   = errDesc[i],
                                                StartTime = DateTime.Now,
                                            };
                                            cie.JDJS_WMS_Device_Alarm_History_Table.Add(alarms);
                                        }
                                        cie.SaveChanges();
                                    }
                                }
                            }
                        }
                        else
                        {
                            errCount = -1; errCodeList = null; errDescList = null;
                        }
                        monitorRealTimeModel.ErrCount    = errCount;
                        monitorRealTimeModel.ErrCodeList = errCodeList;
                        monitorRealTimeModel.ErrDescList = errDesc.ToArray();
                    }
                }
                else
                {
                    return(false);
                }
                //机床模态
                int   currwCorrd   = -1;
                float feedrate     = -1;
                int   spindlespeed = -1;
                int   toolno       = -1;
                float machTime     = -1;
                int   curro        = -1;
                int   curroMaino   = -1;
                if (Function.GetBasicModalInfo(ptr, ref currwCorrd, ref feedrate, ref spindlespeed, ref toolno, ref machTime, ref curro, ref curroMaino))
                {
                    monitorRealTimeModel.CurrWCoord   = currwCorrd;
                    monitorRealTimeModel.FeedRate     = feedrate;
                    monitorRealTimeModel.SpindleSpeed = spindlespeed;
                    monitorRealTimeModel.ToolNO       = toolno;
                    monitorRealTimeModel.MachTime     = machTime;
                    monitorRealTimeModel.CurrO        = curro;
                    monitorRealTimeModel.CurrMainO    = curroMaino;
                }
                else
                {
                    return(false);
                }

                //机床时间信息
                double[] times = new double[3];
                if (Function.GetTime(ptr, times))
                {
                    //model.Times = times;
                    monitorRealTimeModel.Times = times;
                }
                else
                {
                    return(false);
                }
                //工件计数
                int count = -1;
                if (Function.GetMachinedWorkpieceCount(ptr, ref count))
                {
                    monitorRealTimeModel.WorkCount = count;
                }
                else
                {
                    return(false);
                }
                //倍率信息
                int[] rates = new int[2];
                if (Function.GetRate(ptr, rates))
                {
                    monitorRealTimeModel.Rates = rates;
                }
                else
                {
                    return(false);
                }
                //主轴信息
                double[] sinfo = new double[3];
                if (Function.GetSpindleInfo(ptr, sinfo))
                {
                    monitorRealTimeModel.Sinfo = sinfo;
                }
                else
                {
                    return(false);
                }
                //当前运行行号
                int[] curlineno = new int[3];
                if (Function.GetCurLineNo(ptr, curlineno))
                {
                    monitorRealTimeModel.CurLineNo = curlineno;
                }
                else
                {
                    return(false);
                }
                //当前运行行文本
                StringBuilder curlinetext = new StringBuilder();
                curlinetext.Capacity = 200000;
                if (Function.GetCurrLineText(ptr, curlinetext))
                {
                    monitorRealTimeModel.CurrLineText = curlinetext.ToString();
                }
                else
                {
                    return(false);
                }

                //机床系统时间
                int[] time = new int[7];
                if (Function.GetMachTime(ptr, time))
                {
                    //model.time = time;
                    monitorRealTimeModel.time = time;
                }
                else
                {
                    return(false);
                }
                //当前xyzabc剩余量没有相关函数
                //当前工作模式 获取机床的操作模式:
                //1、回参考点是128
                //2、打再寸动上是32
                //3、又打到回参考点之后又是128
                //3、打到手轮上,是64
                //4、程序运行 是 2
                //5、MDI是4
                //6、编辑是1
                //7、点动是16
                int modell = -1;
                int mode   = -1;
                if (Function.GetOprationMode(ptr, ref mode))
                {
                    switch (mode)
                    {
                    case 1:
                        modell = 1;    //编辑
                        break;

                    case 2:
                        modell = 2;    //程序运行
                        break;

                    case 4:
                        modell = 3;    //mdi
                        break;

                    case 8:
                        modell = 8;    //程序自动远程运行
                        break;

                    case 16:
                        modell = 4;    //点动
                        break;

                    case 32:
                        modell = 5;    //寸动
                        break;

                    case 64:
                        modell = 6;    //手轮
                        break;

                    case 128:
                        modell = 7;    //回参考点
                        break;

                    case 256:
                        modell = 9;
                        break;
                    }
                    monitorRealTimeModel.Mode = modell;
                }
                else
                {
                    return(false);
                }
                model = monitorRealTimeModel as IMonitorRealTimeModel;
                return(true);
            }
            catch (Exception ex)
            {
                if (!Directory.Exists("log"))
                {
                    Directory.CreateDirectory("log");
                }
                using (StreamWriter write = new StreamWriter("log/" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + ".log", true))
                {
                    write.WriteLine(DateTime.Now.ToString() + " " + ex);
                }
                return(false);
            }
        }