Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requesthandler: Add Compare and Assert #1199

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/requesthandler/RequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const std::unordered_map<std::string, RequestMethodHandler> RequestHandler::_han
{"TriggerHotkeyByName", &RequestHandler::TriggerHotkeyByName},
{"TriggerHotkeyByKeySequence", &RequestHandler::TriggerHotkeyByKeySequence},
{"Sleep", &RequestHandler::Sleep},
{"Compare", &RequestHandler::Compare},
{"Assert", &RequestHandler::Assert},

// Config
{"GetPersistentData", &RequestHandler::GetPersistentData},
Expand Down
2 changes: 2 additions & 0 deletions src/requesthandler/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class RequestHandler {
RequestResult TriggerHotkeyByName(const Request &);
RequestResult TriggerHotkeyByKeySequence(const Request &);
RequestResult Sleep(const Request &);
RequestResult Compare(const Request &);
RequestResult Assert(const Request &);

// Config
RequestResult GetPersistentData(const Request &);
Expand Down
64 changes: 63 additions & 1 deletion src/requesthandler/RequestHandler_General.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ RequestResult RequestHandler::TriggerHotkeyByKeySequence(const Request &request)
}

/**
* Sleeps for a time duration or number of frames. Only available in request batches with types `SERIAL_REALTIME` or `SERIAL_FRAME`.
* Sleeps for a time duration or number of frames.
*
* Note: Only available in request batches with types `SERIAL_REALTIME` or `SERIAL_FRAME`.
*
* @requestField ?sleepMillis | Number | Number of milliseconds to sleep for (if `SERIAL_REALTIME` mode) | >= 0, <= 50000
* @requestField ?sleepFrames | Number | Number of frames to sleep for (if `SERIAL_FRAME` mode) | >= 0, <= 10000
Expand Down Expand Up @@ -365,3 +367,63 @@ RequestResult RequestHandler::Sleep(const Request &request)
return RequestResult::Error(RequestStatus::UnsupportedRequestBatchExecutionType);
}
}

/**
* Compares the values of the two request fields, `left` and `right`.
*
* Note: Only available in request batches with types `SERIAL_REALTIME` or `SERIAL_FRAME`.
*
* @requestField left | Any | First request batch variable in comparison
* @requestField right | Any | Second request batch variable in comparison
*
* @responseField result | Boolean | Whether the comparison is equal
*
* @requestType Compare
* @complexity 4
* @rpcVersion -1
* @initialVersion 5.4.0
* @category general
* @api requests
*/
RequestResult RequestHandler::Compare(const Request &request)
{
if (!request.RequestData.contains("left") || !request.RequestData.contains("right"))
return RequestResult::Error(RequestStatus::MissingRequestField, "One or more sides of the comparison are missing.");

bool result = request.RequestData["left"] == request.RequestData["right"];

json responseData;
responseData["result"] = result;
return RequestResult::Success(responseData);
}


/**
* Returns an error if the value of `check` is not `true`.
*
* This can be useful to interrupt a request batch from proceeding if an assumed state does not match real-world state.
*
* Note: Only available in request batches with types `SERIAL_REALTIME` or `SERIAL_FRAME`.
*
* @requestField check | Boolean | Value to assert to be true
*
* @requestType Assert
* @complexity 4
* @rpcVersion -1
* @initialVersion 5.4.0
* @category general
* @api requests
*/
RequestResult RequestHandler::Assert(const Request &request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateBoolean("check", statusCode, comment))
return RequestResult::Error(statusCode, comment);

bool check = request.RequestData["check"];
if (!check)
return RequestResult::Error(RequestStatus::AssertFailed, "Assertion failed.");

return RequestResult::Success();
}
11 changes: 11 additions & 0 deletions src/requesthandler/types/RequestStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,5 +415,16 @@ namespace RequestStatus {
* @api enums
*/
CannotAct = 703,
/**
* Assertion failed.
*
* @enumIdentifier AssertFailed
* @enumValue 704
* @enumType RequestStatus
* @rpcVersion -1
* @initialVersion 5.4.0
* @api enums
*/
AssertFailed = 704,
};
}
Loading