JAKABean.cs 7.4 KB
using log4net;
using System;
using System.Threading;
using static JAKA.JKTYPE;

namespace JAKA
{
    public partial class JAKABean
    {
        private static ILog log;
        /// <summary>
        /// 机器人句柄
        /// </summary>
        private int _handle = 0;
        public string IP { get; private set; }
        public string RemoteEndPoint { get; set; } = "";
        private string progFileName { get; set; } = "lizhen1011";
        /// <summary>
        /// 机器人数据
        /// </summary>
        public RobotData RobotData;
        bool initok = false;
        public JAKABean(string robotIp, string logname = "jaka")
        {
            IP = robotIp;
            log = LogManager.GetLogger(logname);
            RobotData = new RobotData();
            try
            {
                initok = CreateHandler(robotIp);
                SetStatusDataUpdateTimeInterval(100);
            }
            catch (Exception ex)
            {
                log.Error("Init JAKABean", ex);
            }
        }
        public void Start()
        {
            if (initok)
            {
                // Load(progFileName);
                started = true;
                updateThread = new System.Threading.Thread(UpdateData);
                updateThread.IsBackground = true;
                updateThread.Start();
                //updateTimer.Enabled = true;
                //updateTimer.Interval = 300;
                //updateTimer.Elapsed += UpdateTimer_Elapsed; ;
                //updateTimer.AutoReset = true;
            }
        }
        object updateLoc = new object();
        private void UpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (Monitor.TryEnter(updateLoc))
            {
                try
                {
                    JKTYPE.ErrorCode error = new JKTYPE.ErrorCode();
                    if (GetRobotStatus(out robotData.RobotStatus))//GetRobotState(out RobotData.RobotState) &&
                    {
                        if (robotData.RobotStatus.errcode != 0)
                        {
                            if (GetLastError(ref error))
                            {
                                robotData.Errorinfo = $"运行异常:【{error.code}】【{string.Join("", error.message)}】";
                                if (!err.Equals(robotData.Errorinfo))
                                {
                                    err = robotData.Errorinfo;
                                    log.Error(err);
                                }
                            }
                            else
                            {
                                robotData.Errorinfo = $"运行异常:{robotData.RobotStatus.errcode}";
                            }
                        }
                        else
                            robotData.Errorinfo = "运行正常";
                        GetProgramState(out robotData.ProgramState);
                        if (!RobotData.Equals(robotData))
                        {
                            RobotData.RobotStatus = robotData.RobotStatus;
                            RobotData.Errorinfo = robotData.Errorinfo;
                            RobotData.ProgramState = robotData.ProgramState;
                            robotData.Online = RobotData.Online;
                            robotData.CurCmd = RobotData.CurCmd;
                            robotData.RecvMsg = RobotData.RecvMsg;
                            log.Info($"Update Robot Data:【{RobotData.CurCmd}】【{RobotData.RecvMsg}】【{RobotData.Errorinfo}】");
                            if (robotData != null)
                                UpdateDataEvent?.Invoke(robotData);
                        }

                    }
                }
                catch (Exception ex)
                {
                    log.Error("UpdateData", ex);
                }
                finally
                {
                    Monitor.Exit(updateLoc);
                }
            }

        }

        System.Timers.Timer updateTimer = new System.Timers.Timer();
        ~JAKABean()
        {
            started = false;
            if (initok)
                DestroyHandler();
        }
        #region 更新数据信息
        System.Threading.Thread updateThread;
        bool started = false;
        public delegate void UpdateEventHandler(RobotData robotData);
        event UpdateEventHandler UpdateDataEvent;
        /// <summary>
        /// 刷新频率
        /// </summary>
        public int ScanRate = 1000;
        RobotData robotData = new RobotData();
        string err = "";
        private void UpdateData()
        {
            while (started)
            {
                try
                {
                    JKTYPE.ErrorCode error = new JKTYPE.ErrorCode();
                    if (GetRobotStatus(out robotData.RobotStatus))//GetRobotState(out RobotData.RobotState) &&
                    {
                        if (robotData.RobotStatus.errcode != 0)
                        {
                            if (GetLastError(ref error))
                            {
                                robotData.Errorinfo = $"运行异常:【{error.code}】【{string.Join("", error.message)}】";
                                if (!err.Equals(robotData.Errorinfo))
                                {
                                    err = robotData.Errorinfo;
                                    log.Error(err);
                                }
                            }
                            else
                            {
                                robotData.Errorinfo = $"运行异常:{robotData.RobotStatus.errcode}";
                            }
                        }
                        else
                            robotData.Errorinfo = "运行正常";
                        GetProgramState(out robotData.ProgramState);
                        if (!RobotData.Equals(robotData))
                        {
                            RobotData.RobotStatus = robotData.RobotStatus;
                            RobotData.Errorinfo = robotData.Errorinfo;
                            RobotData.ProgramState = robotData.ProgramState;
                            robotData.Online = RobotData.Online;
                            robotData.CurCmd = RobotData.CurCmd;
                            robotData.RecvMsg = RobotData.RecvMsg;
                            log.Info($"Update Robot Data:【{RobotData.CurCmd}】【{RobotData.RecvMsg}】【{RobotData.Errorinfo}】");
                            if (robotData != null)
                                UpdateDataEvent?.Invoke(robotData);
                        }

                    }
                }
                catch (Exception ex)
                {
                    log.Error("UpdateData", ex);
                }
                System.Threading.Thread.Sleep(ScanRate);
            }

        }

        public void RegisterUpdataEvent(UpdateEventHandler eventHandler)
        {
            UpdateDataEvent += eventHandler;
        }
        #endregion
        #region 日志打印
        private void DebugLog(string txt)
        {
            log.Debug($"[{IP}][{_handle}] {txt}]");
        }
        private void ErrorLog(string txt, Exception exception = null)
        {
            log.Error($"[{IP}][{_handle}] {txt}]", exception);
        }
        private void InfoLog(string txt)
        {
            log.Info($"[{IP}][{_handle}] {txt}]");
        }
        #endregion

    }
}