41 DashboardClient::DashboardClient(
const std::string& host) : host_(host), port_(DASHBOARD_SERVER_PORT)
47 str.erase(str.find_last_not_of(chars) + 1);
54 URCL_LOG_ERROR(
"%s",
"Socket is already connected. Refusing to reconnect.");
66 TCPSocket::setReceiveTimeout(tv);
77 URCL_LOG_WARN(
"Did not receive dashboard bootup message although connection was established. This should not " 78 "happen, please contact the package maintainers. Retrying anyway...");
85 TCPSocket::setReceiveTimeout(tv);
101 size_t len = text.size();
102 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(text.c_str());
104 return TCPSocket::write(data, len, written);
109 std::stringstream result;
111 size_t read_chars = 99;
112 while (read_chars > 0)
114 if (!TCPSocket::read((uint8_t*)&character, 1, read_chars))
117 throw TimeoutException(
"Did not receive answer from dashboard server in time. Disconnecting from dashboard " 122 if (character ==
'\n')
132 std::string command = text;
133 if (text.back() !=
'\n')
135 command = text +
"\n";
137 std::string response =
"ERROR";
145 throw UrException(
"Failed to send request to dashboard server. Are you connected to the Dashboard Server?");
156 bool ret = std::regex_match(response, std::regex(expected));
159 throw UrException(
"Expected: " + expected +
", but received: " + response);
168 bool ret = std::regex_match(response, std::regex(expected));
171 throw UrException(
"Expected: " + expected +
", but received: " + response);
177 const std::chrono::duration<double> timeout)
179 const std::chrono::duration<double> wait_period = 100ms;
181 std::chrono::duration<double> time_done(0);
182 std::string response;
184 while (time_done < timeout)
190 if (std::regex_match(response, std::regex(expected)))
196 std::this_thread::sleep_for(wait_period);
197 time_done += wait_period;
200 URCL_LOG_WARN(
"Did not got the expected \"%s\" response within the timeout. Last response was: \"%s\"",
201 expected.c_str(), response.c_str());
206 const std::string& waitRequest,
const std::string& waitExpectedResponse,
207 const std::chrono::duration<double> timeout,
208 const std::chrono::duration<double> retry_period)
210 std::chrono::duration<double> time_done(0);
213 sendRequest(requestCommand, requestExpectedResponse);
214 time_done += retry_period;
216 if (
waitForReply(waitRequest, waitExpectedResponse, retry_period))
220 }
while (time_done < timeout);
233 return retryCommand(
"power on",
"Powering on",
"robotmode",
"Robotmode: IDLE", timeout);
245 return sendRequest(
"load " + program_file_name +
"",
"(?:Loading program: ).*(?:" + program_file_name +
").*") &&
246 waitForReply(
"programState",
"STOPPED " + program_file_name);
252 return sendRequest(
"load installation " + installation_file_name,
253 "(?:Loading installation: ).*(?:" + installation_file_name +
").*");
277 return sendRequest(
"close popup",
"closing popup");
283 return sendRequest(
"close safety popup",
"closing safety popup");
289 return sendRequest(
"restart safety",
"Restarting safety") &&
waitForReply(
"robotmode",
"Robotmode: POWER_OFF");
295 return sendRequest(
"unlock protective stop",
"Protective stop releasing");
313 return sendRequest(
"running",
"Program running: true");
319 return sendRequest(
"isProgramSaved",
"(?:true ).*");
326 bool ret = std::regex_match(response, std::regex(
"true"));
333 return sendRequest(
"popup " + popup_text,
"showing popup");
339 return sendRequest(
"addToLog " + log_text,
"Added log message");
344 std::string expected =
"(?:URSoftware ).*";
346 std::string version_string = polyscope_version.substr(polyscope_version.find(
" ") + 1,
347 polyscope_version.find(
" (") - polyscope_version.find(
" ") - 1);
349 return std::regex_match(polyscope_version, std::regex(expected));
355 std::string expected =
"(?:UR).*";
357 return std::regex_match(robot_model, std::regex(expected));
363 std::string expected =
"(?:20).*";
365 return std::regex_match(serial_number, std::regex(expected));
371 std::string expected =
"(?:Robotmode: ).*";
373 return std::regex_match(robot_mode, std::regex(expected));
379 std::string expected =
"(?:Loaded program: ).*";
381 return std::regex_match(loaded_program, std::regex(expected));
387 std::string expected =
"(?:Safetymode: ).*";
389 return std::regex_match(safety_mode, std::regex(expected));
395 std::string expected =
"(?:Safetystatus: ).*";
397 return std::regex_match(safety_status, std::regex(expected));
403 std::string expected =
"(?:).*";
405 return !std::regex_match(program_state, std::regex(
"(?:could not understand).*"));
411 std::string expected =
"(?:).*";
413 return !std::regex_match(operational_mode, std::regex(
"(?:could not understand).*"));
419 return sendRequest(
"set operational mode " + operational_mode,
420 "(?:Operational mode ).*(?:" + operational_mode +
").*");
426 return sendRequest(
"clear operational mode",
"(?:No longer controlling the operational mode. ).*");
432 return sendRequest(
"setUserRole " + user_role,
"(?:Setting user role: ).*");
438 std::string expected =
"(?:).*";
440 return !std::regex_match(user_role, std::regex(
"(?:could not understand).*"));
449 TCPSocket::setReceiveTimeout(tv);
450 bool ret =
sendRequest(
"generate flight report " + report_type,
"(?:Flight Report generated with id:).*");
452 TCPSocket::setReceiveTimeout(tv);
462 TCPSocket::setReceiveTimeout(tv);
463 bool ret =
sendRequest(
"generate support file " + dir_path,
"(?:Completed successfully:).*");
465 TCPSocket::setReceiveTimeout(tv);
472 return sendRequest(
"saveLog",
"Log saved to disk");
476 const std::string& required_call)
480 std::stringstream ss;
481 ss <<
"The dasboard call '" << required_call
482 <<
"' is only available on e-series robots, but you seem to be running version " <<
polyscope_version_;
488 std::stringstream ss;
489 ss <<
"The dasboard call '" << required_call
490 <<
"' is only available on pre-e-series robots (5.x.y), but you seem to be running version " 499 std::stringstream ss;
500 ss <<
"Polyscope version " <<
polyscope_version_ <<
" isn't recent enough to use dashboard call '" << required_call
void assertVersion(const std::string &e_series_min_ver, const std::string &cb3_min_ver, const std::string &required_call)
Makes sure that the dashboard_server's version is above the required version.
bool commandGetSerialNumber(std::string &serial_number)
Get Serial number.
std::string sendRequestString(const std::string &command, const std::string &expected)
Sends command and compare it with the expected answer.
bool commandSafetyMode(std::string &safety_mode)
Get Safety mode.
#define URCL_LOG_ERROR(...)
bool commandSetUserRole(const std::string &user_role)
Send Set user role command (Only available for CB3)
VersionInformation polyscope_version_
bool waitForReply(const std::string &command, const std::string &expected, std::chrono::duration< double > timeout=std::chrono::seconds(30))
brief Sends a command and wait until it returns the expected answer
bool commandIsProgramSaved()
Send "Is program saved" request command.
bool retryCommand(const std::string &requestCommand, const std::string &requestExpectedResponse, const std::string &waitRequest, const std::string &waitExpectedResponse, const std::chrono::duration< double > timeout, const std::chrono::duration< double > retry_period=std::chrono::seconds(1))
Keep Sending the requesting Command and wait until it returns the expected answer.
bool commandGetRobotModel(std::string &robot_model)
Get Robot model.
bool send(const std::string &text)
std::unique_ptr< timeval > recv_timeout_
bool commandUnlockProtectiveStop()
Send Unlock Protective stop popup command.
bool commandClosePopup()
Send Close popup command.
bool commandStop()
Send Stop program command.
bool commandGenerateSupportFile(const std::string &dir_path)
Send Generate support file command.
bool commandBrakeRelease()
Send Brake release command.
std::string sendAndReceive(const std::string &command)
Sends a command through the socket and waits for an answer.
bool commandPopup(const std::string &popup_text)
Send popup command.
bool commandQuit()
Send Quit command.
void rtrim(std::string &str, const std::string &chars="\\\")
bool commandClearOperationalMode()
Send Clear operational mode command.
bool sendRequest(const std::string &command, const std::string &expected)
Sends command and compare it with the expected answer.
bool commandShutdown()
Send Shutdown command.
bool commandLoadProgram(const std::string &program_file_name)
Send Load program command.
bool commandProgramState(std::string &program_state)
Get Program state.
SocketState getState()
Getter for the state of the socket.
#define URCL_LOG_DEBUG(...)
bool commandSafetyStatus(std::string &safety_status)
Get Safety status.
bool commandAddToLog(const std::string &log_text)
Send text to log.
bool connect()
Opens a connection to the dashboard server on the host as specified in the constructor.
bool commandSaveLog()
Flush the polyscope log to the log_history.txt file.
bool commandLoadInstallation(const std::string &installation_file_name)
Send Load installation command.
#define URCL_LOG_WARN(...)
bool commandPowerOn(const std::chrono::duration< double > timeout=std::chrono::seconds(300))
Send Power on command.
bool commandPowerOff()
Send Power off command.
bool commandRunning()
Send Running command.
bool commandRobotMode(std::string &robot_mode)
Get Robot mode.
A specialized exception representing that communication to the tool is not possible.
bool commandGetLoadedProgram(std::string &loaded_program)
Get Loaded Program.
Socket is connected and ready to use.
void disconnect()
Makes sure no connection to the dashboard server is held inside the object.
bool commandIsInRemoteControl()
Send "Is in remote control" query command.
bool commandPause()
Send Pause program command.
bool commandRestartSafety()
Send Restart Safety command.
bool commandPlay()
Send Play program command.
bool commandGenerateFlightReport(const std::string &report_type)
Send Generate flight report command.
bool commandSetOperationalMode(const std::string &operational_mode)
Send Set operational mode command (Only available for e-series)
bool commandGetUserRole(std::string &user_role)
Send Get user role command (Only available for CB3)
bool commandGetOperationalMode(std::string &operational_mode)
Get Operational mode.
#define URCL_LOG_INFO(...)
bool commandCloseSafetyPopup()
Send Close safety popup command.
Our base class for exceptions. Specialized exceptions should inherit from those.
bool commandPolyscopeVersion(std::string &polyscope_version)
Get Polyscope version.