ABBRobotManager.cs 11.2 KB
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.IOSystemDomain;
using ABB.Robotics.Controllers.RapidDomain;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ABBRobotTest
{
    internal class ABBRobotManager
    {

        internal static string DI_Start = "di_start";//启动程序
        internal static string DI_Stop = "di_stop";//停止程序
        internal static string DO_ES = "do_ES";//急停状态
        internal static string DO_IsRun= "do_IsRun";//机器人程序运行状态
        internal static string Do_AutoOn = "do_AutoOn";//是否自动状态

        private static NetworkScanner scanner = null;
        // private Controller controller = null;
        private static Task[] tasks = null;
        private static NetworkWatcher networkwatcher = null;
        internal static Dictionary<string, Controller> controllerMap = new Dictionary<string, Controller>();
        internal static Dictionary<string, ControllerInfo> controllerInfoMap = new Dictionary<string, ControllerInfo>();

        internal delegate void ControllerAddDelegate(ControllerInfo controller);
        internal static  event ControllerAddDelegate ControllerAddEvent;



        internal static   void LoadController()
        {
            controllerMap = new Dictionary<string, Controller>();
            controllerInfoMap = new Dictionary<string, ControllerInfo>();
            scanner = new NetworkScanner();
            scanner.Scan();
            ControllerInfoCollection controllers = scanner.Controllers;
            int i = 0;
            foreach (ControllerInfo controllerInfo in controllers)
            {
                AddControllerInfo(controllerInfo);
                i++; 
            }

            networkwatcher = new NetworkWatcher(scanner.Controllers);
            networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
            networkwatcher.Lost += new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
            networkwatcher.EnableRaisingEvents = true;
        }
        private static bool AddControllerInfo(ControllerInfo controllerInfo)
        {
            try
            {
                Controller con = ControllerFactory.CreateFrom(controllerInfo);
                con.Logon(UserInfo.DefaultUser);
                ControllerState state = con.State;
                string ip = con.IPAddress.ToString();
                RemoveController(ip);
                controllerMap.Add(ip, con);
                // controller = con;
                controllerInfoMap.Add(ip, controllerInfo);
                LogUtil.info("成功添加机器人【" + ip + "】");
                return true;
            }catch(Exception ex)
            {
                LogUtil.error("添加机器人【" + controllerInfo.IPAddress.ToString() + "】失败:" + ex.ToString());
            }return false;
        }
        private static void RemoveController(string ip)
        {
            if (controllerInfoMap.ContainsKey(ip))
            {
                controllerInfoMap.Remove(ip);
            }
            if (controllerMap.ContainsKey(ip))
            {
                controllerMap.Remove(ip);
            }
        }
        private static void HandleLostEvent(object sender, NetworkWatcherEventArgs e)
        { 
        }

        private static void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)
        {
            //Invoke(new
            //EventHandler<NetworkWatcherEventArgs>(AddControllerToListView),
            //new Object[] {  null, e });
            AddControllerToListView(null, e);
        }

        private static void Invoke(EventHandler<NetworkWatcherEventArgs> eventHandler, object[] v)
        { 
        }

        private static void AddControllerToListView(object sender, NetworkWatcherEventArgs e)
        {
            ControllerInfo controllerInfo = e.Controller;
            if (controllerInfo != null)
            {
                if (AddControllerInfo(controllerInfo))
                {
                    ControllerAddEvent?.Invoke(controllerInfo);
                }
            }
        }
        private void StartRobotByTask(string ip)
        {
            try
            {
                Controller controller = GetControllerByIP(ip);
                if (controller != null && controller.OperatingMode == ControllerOperatingMode.Auto)
                { 
                    tasks = controller.Rapid.GetTasks();
                    using (Mastership m = Mastership.Request(controller.Rapid))
                    {
                        tasks[0].SetProgramPointer("aaa", "main");
                        controller.State = ControllerState.MotorsOn;
                        controller.Rapid.Start();
                        //Perform operation
                        //tasks[0].Start();

                        m.Dispose();
                    } 
                }
                else
                {
                    Console.WriteLine("Automatic mode is required to start execution from a remote client.");
                }
            }
            catch (System.InvalidOperationException ex)
            {
                Console.WriteLine("Mastership is held by another client." + ex.Message);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Unexpected error occurred: " + ex.Message);
            }
        }
        private void StopRobotByTask(string ip)
        {
            try
            {
                Controller controller = GetControllerByIP(ip);
                if (controller != null && controller.OperatingMode == ControllerOperatingMode.Auto)
                {

                    tasks = controller.Rapid.GetTasks();
                    using (Mastership m = Mastership.Request(controller.Rapid))
                    {
                        //tasks[0].SetProgramPointer("aaa", "main");
                        //controller.State = ControllerState.MotorsOn;
                        //controller.Rapid.Start();
                        //Perform operation
                        //tasks[0].Start();
                        controller.Rapid.Stop();
                        m.Dispose();
                    }
                }
                else
                {
                    Console.WriteLine("Automatic mode is required to start execution from a remote client.");
                }
            }
            catch (System.InvalidOperationException ex)
            {
                Console.WriteLine("Mastership is held by another client." + ex.Message);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Unexpected error occurred: " + ex.Message);
            }
        }

        internal static void StartRobotBySingle(string ip,string signalName = "di_start",bool isReset=true )
        {
            if (isReset)
            {
                StopRobotBySingle(ip);
            }

            SetRobotSignal(ip, signalName); 
            Thread.Sleep(100); 
            ResetRobotSignal(ip, signalName);
        }

        internal static void StopRobotBySingle(string ip,string signalName = "di_stop")
        {
            SetRobotSignal(ip, signalName); 
            Thread.Sleep(100); 
            ResetRobotSignal(ip, signalName);
        }

        internal static void SetRobotSignal(string ip, string name)
        {
            try
            {
                Controller controller = GetControllerByIP(ip);
                if (controller != null)
                {
                    // string name = comboBox1.Text;
                    Signal singalValue = controller.IOSystem.GetSignal(name);
                    if (singalValue == null)
                    {
                        LogUtil.error("ABBRobot[" + ip + "] GetSignal [" + name + "] =null");
                    }
                    else
                    {
                        DigitalSignal digitalSig = (DigitalSignal)singalValue;
                        digitalSig.Set();
                    }
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("ABBRobot[" + ip + "]  SetRobotSignal  [" + name + "] Error:" + ex.ToString());
            }
        }

        internal static void ResetRobotSignal(string ip, string name)
        {
            try
            {
                Controller controller = GetControllerByIP(ip);
                if (controller != null)
                {
                    //string name = comboBox1.Text;
                    Signal singalValue = controller.IOSystem.GetSignal(name);
                    if (singalValue == null)
                    {
                        LogUtil.error("ABBRobot[" + ip + "] GetSignal [" + name + "]= null");
                    }
                    else
                    {
                        DigitalSignal digitalSig = (DigitalSignal)singalValue;
                        digitalSig.Reset();
                    }
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("ABBRobot[" + ip + "]  ResetRobotSignal  [" + name + "] Error:" + ex.ToString());
            }
        }
        internal static int GetSingalState(string ip, string name = "do_ES")
        {
            try
            {
                Controller controller = GetControllerByIP(ip);
                if (controller != null)
                {
                    //string name = comboBox1.Text;
                    Signal singalValue = controller.IOSystem.GetSignal(name);
                    if (singalValue == null)
                    {
                        LogUtil.error("ABBRobot[" + ip + "] GetSingal  [" + name + "]= null");
                    }
                    else
                    {
                        DigitalSignal digitalSig = (DigitalSignal)singalValue;
                        return (int)digitalSig.Value;
                    }
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("ABBRobot[" + ip + "]  GetSingalState  [" + name + "] Error:" + ex.ToString());
            }
            return 0;
        }
        internal  static string GetRobotState(string ip) 
        {
            try
            {
                Controller controller = GetControllerByIP(ip);
                if (controller != null)
                {
                    ControllerState state = controller.State;
                    return state.ToString();
                }
            }catch(Exception ex)
            {
                LogUtil.error("ABBRobot[" + ip + "] GetRobotState" +"error:"+ex.ToString());
            }
            return "";
        }

        internal static Controller GetControllerByIP(string ip)
        {
            if (controllerMap.ContainsKey(ip))
            {
                return controllerMap[ip];
            }
            LogUtil.error("ABBRobot [" + ip + "] GetControllerByIP= null");
            return null;
        }


        internal static void DisposeControllers()
        {
            foreach (Controller con in controllerMap.Values)
            {
                con.Logoff();
                con.Dispose();
            }
            controllerMap.Clear();
        } 
    }
}