Skip to content

Commit

Permalink
Add logging for result of Lua script ID Lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
charlestytler committed Apr 13, 2020
1 parent 73a78a7 commit da64ee3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
Binary file modified Release/com.ctytler.dcs.streamDeckPlugin
Binary file not shown.
15 changes: 10 additions & 5 deletions Sources/DcsInterface/DcsIdLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ json get_installed_modules(const std::string &dcs_install_path, const std::strin
json get_clickabledata(const std::string &dcs_install_path,
const std::string &module_name,
const std::string &lua_script) {
json clickabledata_items;
json clickabledata_and_result;
clickabledata_and_result["clickabledata_items"] = json::array();
clickabledata_and_result["result"] = "";

// create new Lua state
lua_State *lua_state;
Expand All @@ -47,22 +49,25 @@ json get_clickabledata(const std::string &dcs_install_path,
const int file_status = luaL_loadfile(lua_state, lua_script.c_str());
if (file_status != 0) {
lua_close(lua_state);
return clickabledata_items;
clickabledata_and_result["result"] = "Lua file load error: " + std::to_string(file_status);
return clickabledata_and_result;
}
const int script_status = lua_pcall(lua_state, 0, LUA_MULTRET, 0);
if (script_status != 0) {
lua_close(lua_state);
return clickabledata_items;
clickabledata_and_result["result"] = "Lua script runtime error: " + std::to_string(script_status);
return clickabledata_and_result;
}

// Handle each of the return values one at a time.
while ((lua_gettop(lua_state) - lua_stack_size) > 0) {
clickabledata_items.push_back(lua_tostring(lua_state, lua_gettop(lua_state)));
clickabledata_and_result["result"] = "success";
clickabledata_and_result["clickabledata_items"].push_back(lua_tostring(lua_state, lua_gettop(lua_state)));
lua_pop(lua_state, 1);
}

// close the Lua state
lua_close(lua_state);

return clickabledata_items;
return clickabledata_and_result;
}
12 changes: 9 additions & 3 deletions Sources/MyStreamDeckPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void MyStreamDeckPlugin::DidReceiveGlobalSettings(const json &inPayload) {
try {
dcs_interface_ = new DcsInterface(connection_settings);
} catch (const std::exception &e) {
std::cerr << "Exception: " << e.what() << std::endl;
mConnectionManager->LogMessage("Caught Exception While Opening Connection: " + std::string(e.what()));
}
}
}
Expand Down Expand Up @@ -246,8 +246,14 @@ void MyStreamDeckPlugin::SendToPlugin(const std::string &inAction,
if (event == "RequestIdLookup") {
const std::string dcs_install_path = EPLJSONUtils::GetStringByName(inPayload, "dcs_install_path");
const std::string module = EPLJSONUtils::GetStringByName(inPayload, "module");
json clickabledata = get_clickabledata(dcs_install_path, module, "extract_clickabledata.lua");
json clickabledata_and_result = get_clickabledata(dcs_install_path, module, "extract_clickabledata.lua");
const std::string lua_result = EPLJSONUtils::GetStringByName(clickabledata_and_result, "result");
if (lua_result != "success") {
mConnectionManager->LogMessage(module + " Clickabledata Result: " + lua_result);
}
mConnectionManager->SendToPropertyInspector(
inAction, inContext, json({{"event", "Clickabledata"}, {"clickabledata", clickabledata}}));
inAction,
inContext,
json({{"event", "Clickabledata"}, {"clickabledata", clickabledata_and_result["clickabledata_items"]}}));
}
}
4 changes: 3 additions & 1 deletion Sources/Test/DcsIdLookupTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "gtest/gtest.h"

#include "../Common/EPLJSONUtils.h"
#include "../DcsInterface/DcsIdLookup.cpp"

namespace test {
Expand All @@ -17,7 +18,8 @@ TEST(DcsIdLookupTest, nonexistant_lua_file) {
// Test that a bad path will return an empty json container.
const std::string lua_script = "non-existant-file.lua";
json returned_values = get_clickabledata("path", "module", lua_script);
EXPECT_TRUE(returned_values.empty());
EXPECT_EQ("Lua file load error: 6", EPLJSONUtils::GetStringByName(returned_values, "result"));
EXPECT_EQ(0, returned_values["clickabledata_items"].size());
}

} // namespace test
Binary file modified Sources/com.ctytler.dcs.sdPlugin/dcs_interface.exe
Binary file not shown.

0 comments on commit da64ee3

Please sign in to comment.