Example #1
0
        /// <summary>
        /// Saves a List of program Files to a folder (creates the folder if needed).
        /// </summary>
        /// <param name="program"></param>
        /// <param name="folderPath"></param>
        /// <param name="encoding"></param>
        /// <param name="logger"></param>
        /// <returns></returns>
        internal static bool SaveProgramToFolder(RobotProgram program, string folderPath, RobotLogger logger)
        {
            // Create a subfolder within folderPath
            string programFolderPath = "";

            try
            {
                programFolderPath = Path.Combine(folderPath, Utilities.Strings.SafeProgramName(program.Name + "_Program"));
            }
            catch (Exception ex)
            {
                logger.Error("Badly formatted folder path: " + folderPath);
                logger.Debug(ex);
                return(false);
            }

            // Check if directory exists, and create it otherwise
            try
            {
                if (Directory.Exists(programFolderPath))
                {
                    logger.Debug("Found existing folder on " + programFolderPath + ", deleting it...");
                    EmptyDirectory(programFolderPath, logger);
                }
                else
                {
                    DirectoryInfo di = Directory.CreateDirectory(programFolderPath);
                    logger.Debug("Created folder " + programFolderPath);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Could not create folder " + programFolderPath);
                logger.Debug(ex);
                return(false);
            }

            // More sanity
            if (!IsDirectoryWritable(programFolderPath))
            {
                logger.Error("Cannot write to folder " + programFolderPath);
                return(false);
            }

            // Write each file
            bool success = true;

            foreach (var file in program.Files)
            {
                string fullPath = Path.Combine(programFolderPath, file.Name + "." + file.Extension);
                success = success && SaveStringListToFile(file.Lines, fullPath, file.Encoding, logger);
            }

            return(success);
        }
Example #2
0
        public override Joints GetCurrentJoints()
        {
            if (_rsBridge != null && _rsBridge.Connected)
            {
                var jnt = _rsBridge.GetCurrentJoints();
                logger.Debug($"CurrentJoints: {jnt}");
                return(jnt);
            }

            return(this._tcpManager.initAx);
        }
Example #3
0
 /// <summary>
 /// Saves a List of strings to a file.
 /// </summary>
 /// <param name="lines"></param>
 /// <param name="filepath"></param>
 /// <param name="encoding"></param>
 /// <param name="logger"></param>
 /// <returns></returns>
 internal static bool SaveStringListToFile(List <string> lines, string filepath, Encoding encoding, RobotLogger logger)
 {
     try
     {
         System.IO.File.WriteAllLines(filepath, lines, encoding);
         logger.Debug($"Saved content to file \"{filepath}\"");
         return(true);
     }
     catch (Exception ex)
     {
         logger.Error("Could not save content to file \"{filepath}\"");
         logger.Debug(ex);
     }
     return(false);
 }
Example #4
0
        /// <summary>
        /// Removes all files and directories in a folder, keeping the folder.
        /// From: https://www.techiedelight.com/delete-all-files-sub-directories-csharp/
        /// </summary>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        internal static bool EmptyDirectory(string folderPath, RobotLogger logger)
        {
            DirectoryInfo di = new DirectoryInfo(folderPath);

            try
            {
                foreach (FileInfo file in di.GetFiles())
                {
                    file.Delete();
                }

                foreach (DirectoryInfo dir in di.GetDirectories())
                {
                    dir.Delete(true);
                }
            }
            catch (Exception ex)
            {
                logger.Error($"Could not delete files in \"{folderPath}\"");
                logger.Debug(ex);
                return(false);
            }

            return(true);
        }
Example #5
0
        internal bool Connect()
        {
            try
            {
                _clientSocket = new TcpClient();
                _clientSocket.Connect(this._ip, this._port);
                _clientStatus                   = TCPConnectionStatus.Connected;
                _clientNetworkStream            = _clientSocket.GetStream();
                _clientSocket.ReceiveBufferSize = 1024;
                _clientSocket.SendBufferSize    = 1024;

                _sendingThread = new Thread(SendingMethod);
                _sendingThread.IsBackground = true;
                _sendingThread.Start();

                _receivingThread = new Thread(ReceivingMethod);
                _receivingThread.IsBackground = true;
                _receivingThread.Start();

                if (!WaitForInitialization())
                {
                    logger.Error("Timeout when waiting for initialization data from the controller");
                    Disconnect();
                    return(false);
                }

                // During YuMi development I was having a really weird problem: if a monitor is running, I cannot connect to another driver in the same unit...
                // So, for the time being, let's make monitoring an explicit process with its own API?
                if (TryConnectMonitor())
                {
                    // Establish a MotionCursor on `Control`
                    this._parentDriver.parentControl.InitializeMotionCursor();
                    this._motionCursor = this._parentDriver.parentControl.MotionCursor;
                }

                return(_clientSocket.Connected);
            }
            catch (Exception ex)
            {
                logger.Debug(ex);
                //throw new Exception("ERROR: could not establish TCP connection");
                Disconnect();
            }

            return(false);
        }
Example #6
0
 public override void DebugDump()
 {
     logger.Debug("Nothing to debug for a Driver in Offline mode");
 }
Example #7
0
        /// <summary>
        /// Searches the network for a robot controller and establishes a connection with the specified one by position.
        /// Performs no LogOn actions or similar.
        /// </summary>
        /// <returns></returns>
        private bool LoadController(int controllerID)
        {
            // Scan the network and hookup to the specified controller
            bool success = false;

            // This is specific to ABB, should become abstracted at some point...
            logger.Verbose("Scanning the network for controllers...");
            NetworkScanner scanner = new NetworkScanner();

            ControllerInfo[] controllers = scanner.GetControllers();
            if (controllers.Length > 0)
            {
                int cId = controllerID > controllers.Length ? controllers.Length - 1 :
                          controllerID < 0 ? 0 : controllerID;
                controller = ControllerFactory.CreateFrom(controllers[cId]);
                if (controller != null)
                {
                    //isConnected = true;
                    logger.Verbose($"Found controller {controller.SystemName} on {controller.Name}");
                    success = true;

                    logger.Debug(controller);
                    //Console.WriteLine(controller.RobotWare);
                    //Console.WriteLine(controller.RobotWareVersion);

                    //try
                    //{
                    //    this.robotWare = this.controller.RobotWare;
                    //    this.robotWareOptions = this.robotWare.Options;

                    //    this._hasMultiTasking = HasMultiTaskOption(this.robotWareOptions);
                    //    this._hasEGM = HasEGMOption(this.robotWareOptions);
                    //}
                    //catch
                    //{
                    //    Console.WriteLine("Could not access ROBOTWARE options");
                    //}
                }
                else
                {
                    logger.Debug("Could not connect to controller...");
                }
            }
            else
            {
                logger.Debug("No controllers found on the network");
            }

            //if (!success)
            //{
            //    Disconnect();
            //    //throw new Exception("ERROR: could not LoadController()");
            //}

            return(success);
        }
        internal bool Connect()
        {
            try
            {
                _clientSocket = new TcpClient();
                _clientSocket.Connect(this._robotIP, this._robotPort);
                ClientSocketStatus              = TCPConnectionStatus.Connected;
                _clientNetworkStream            = _clientSocket.GetStream();
                _clientSocket.ReceiveBufferSize = 2048;
                _clientSocket.SendBufferSize    = 1024;

                //// We don't need a sending thread to the client anymore, since the driver script will only be uplaoded once.
                //_clientSendingThread = new Thread(ClientSendingMethod);
                //_clientSendingThread.IsBackground = true;
                //_clientSendingThread.Start();

                _clientReceivingThread = new Thread(ClientReceivingMethod);
                _clientReceivingThread.IsBackground = true;
                _clientReceivingThread.Start();

                if (!Net.Net.GetLocalIPAddressInNetwork(_robotIP, "255.255.255.0", out _serverIP))
                {
                    throw new Exception("ERROR: Could not figure out local IP");
                }
                logger.Debug("Machina local IP: " + _serverIP);

                _serverSocket = new TcpListener(IPAddress.Parse(_serverIP), _serverPort);
                _serverSocket.Start();

                _isServerListeningRunning           = true;
                _serverListeningThread              = new Thread(ServerReceivingMethod);
                _serverListeningThread.IsBackground = true;
                _serverListeningThread.Start();

                string drScript = LoadDriverScript();
                if (!UploadScriptToDevice(drScript, false))
                {
                    logger.Error("Could not upload driver to robot");
                    Disconnect();
                    return(false);
                }

                if (!WaitForInitialization())
                {
                    logger.Error("Timeout when waiting for initialization data from the controller");
                    Disconnect();
                    return(false);
                }

                return(_clientSocket.Connected);
            }
            catch (Exception ex)
            {
                logger.Error("Something went wrong trying to connect to robot...");
                logger.Debug(ex);
                Disconnect();
                return(false);
            }

            //return false;
        }