/// <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); }
/// <summary> /// Performs all necessary actions to establish a connection to a real/virtual device, /// including connecting to the controller, loggin in, checking required states, etc. /// </summary> /// <param name="deviceId"></param> public override bool ConnectToDevice(int deviceId) { if (this.parentControl.connectionMode != ConnectionType.Machina) { logger.Error("Can only connect to ConnectToDevice(int deviceId) in ConnectionType.Machina mode"); return(false); } // 1. use RSmanager to connecto to devide // load the streaming module // 2. if successful, use the fetched address to initialize the TCP server // 3. release mastership right away to make the program more solid? if (!_rsBridge.Connect(deviceId)) { logger.Error("Could not connect automatically to device"); return(false); } // If on 'stream' mode, set up stream connection flow if (this.parentControl.ControlMode == ControlType.Stream || this.parentControl.ControlMode == ControlType.Online) { if (!_rsBridge.SetupStreamingMode()) { logger.Error("Could not initialize Streaming Mode in the controller"); return(false); } } else { // if on Execute mode on _rsBridge, do nothing (programs will be uploaded in batch) logger.Error("Control mode " + this.parentControl.ControlMode + " not supported"); return(false); } this.IP = _rsBridge.IP; this.Port = _rsBridge.Port; if (!this.ConnectToDevice(this.IP, this.Port)) { logger.Error("Could not establish TCP connection to the controller"); return(false); } DebugDump(); return(true); }
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 (!Machina.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(); LoadDriverScript(); UploadScriptToDevice(_driverScript, false); return(_clientSocket.Connected); } catch (Exception ex) { logger.Error("Something went wrong trying to connect to robot..."); logger.Debug(ex); throw new Exception(); } //return false; }
/// <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); }
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); }
/// <summary> /// Retrieves the main task from the ABB controller, typically 'T_ROB1'. /// </summary> /// <returns></returns> private bool LoadMainTask() { if (controller == null) { logger.Debug("Cannot retrieve main task: no controller available"); return(false); } tMainTask = controller.Rapid.GetTask("T_ROB1"); if (tMainTask == null) { logger.Error("Could not retrieve task T_ROB1 from the controller"); // Quick workaround for Yumis to get the left hand if (!LoadFirstTask()) { return(false); } } logger.Debug("Retrieved task " + tMainTask.Name); return(true); }
/// <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); }
internal bool Disconnect() { if (_clientSocket != null) { // Upload an empty script to stop the running program string emptyScript = LoadEmptyScript(); if (!UploadScriptToDevice(emptyScript, false)) { Logger.Error("Could not load empty script to robot"); } try { ClientSocketStatus = TCPConnectionStatus.Disconnected; _clientSocket.Client.Disconnect(false); _clientSocket.Close(); if (_clientNetworkStream != null) { _clientNetworkStream.Dispose(); } _isServerListeningRunning = false; // TESTING _robotSocket.Close(); _robotSocket.Dispose(); _robotSocket = null; _serverSocket.Stop(); _serverSocket = null; return(true); } catch (Exception ex) { logger.Error("Something went wrong on disconnection:"); logger.Error(ex); return(false); } } return(false); }