forked from daneos/lutango
add command and attribute info to DeviceProxy
This commit is contained in:
parent
9bd078b221
commit
3535cfc13b
@ -21,9 +21,9 @@ DeviceProxyWrapper::DeviceProxyWrapper(const char* name)
|
|||||||
Tango::CommandInfoList* cmdlist = dev->command_list_query();
|
Tango::CommandInfoList* cmdlist = dev->command_list_query();
|
||||||
|
|
||||||
for(int i = 0; i < attrlist->size(); i++)
|
for(int i = 0; i < attrlist->size(); i++)
|
||||||
attr_cache[(*attrlist)[i].name] = (*attrlist)[i];
|
attr_info[(*attrlist)[i].name] = (*attrlist)[i];
|
||||||
for(int i = 0; i < cmdlist->size(); i++)
|
for(int i = 0; i < cmdlist->size(); i++)
|
||||||
cmd_cache[(*cmdlist)[i].cmd_name] = (*cmdlist)[i];
|
cmd_info[(*cmdlist)[i].cmd_name] = (*cmdlist)[i];
|
||||||
|
|
||||||
delete attrlist;
|
delete attrlist;
|
||||||
delete cmdlist;
|
delete cmdlist;
|
||||||
@ -34,26 +34,6 @@ DeviceProxyWrapper::~DeviceProxyWrapper()
|
|||||||
delete dev;
|
delete dev;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DeviceProxyWrapper::attr_type(const char* name)
|
|
||||||
{
|
|
||||||
return attr_cache[name].data_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
Tango::AttrDataFormat DeviceProxyWrapper::attr_format(const char* name)
|
|
||||||
{
|
|
||||||
return attr_cache[name].data_format;
|
|
||||||
}
|
|
||||||
|
|
||||||
int DeviceProxyWrapper::cmd_in_type(const char* name)
|
|
||||||
{
|
|
||||||
return cmd_cache[name].in_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
int DeviceProxyWrapper::cmd_out_type(const char* name)
|
|
||||||
{
|
|
||||||
return cmd_cache[name].out_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
// COMMAND WRAPPER ------------------------------------------------------------
|
// COMMAND WRAPPER ------------------------------------------------------------
|
||||||
int __cmd_wrapper(lua_State* L)
|
int __cmd_wrapper(lua_State* L)
|
||||||
{
|
{
|
||||||
@ -64,13 +44,12 @@ int __cmd_wrapper(lua_State* L)
|
|||||||
|
|
||||||
LUT_LOG(TRACE, "Running command wrapper %s", full_name.c_str());
|
LUT_LOG(TRACE, "Running command wrapper %s", full_name.c_str());
|
||||||
|
|
||||||
|
int in_type = udata->cmd_info[cmd_name].in_type;
|
||||||
|
int out_type = udata->cmd_info[cmd_name].out_type;
|
||||||
|
LUT_LOG(TRACE, "Command: %s in:%d out:%d", full_name.c_str(), in_type, out_type);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int in_type = udata->cmd_in_type(cmd_name);
|
|
||||||
int out_type = udata->cmd_out_type(cmd_name);
|
|
||||||
|
|
||||||
LUT_LOG(TRACE, "Command: %s in:%d out:%d", full_name.c_str(), in_type, out_type);
|
|
||||||
|
|
||||||
Tango::DeviceData in = lut_toTangoType<Tango::DeviceData>(L, 1, in_type);
|
Tango::DeviceData in = lut_toTangoType<Tango::DeviceData>(L, 1, in_type);
|
||||||
Tango::DeviceData out = udata->dev->command_inout(cmd_name, in);
|
Tango::DeviceData out = udata->dev->command_inout(cmd_name, in);
|
||||||
lut_fromTangoType(L, out, out_type);
|
lut_fromTangoType(L, out, out_type);
|
||||||
@ -125,6 +104,41 @@ int lut_DeviceProxy_get_attribute_list(lua_State* L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lut_DeviceProxy_get_attribute_config(lua_State* L)
|
||||||
|
{
|
||||||
|
LUT_LOG(TRACE, "TANGO API DeviceProxy:get_attribute_config()");
|
||||||
|
DeviceProxyWrapper* udata = (DeviceProxyWrapper*)lut_getobj(L, 1);
|
||||||
|
const char* name = luaL_checkstring(L, 3); // arg[2] is Lua "self"
|
||||||
|
std::string dev_name = udata->dev->dev_name();
|
||||||
|
std::string full_name = build_name(dev_name, name);
|
||||||
|
|
||||||
|
if(udata->attr_info.find(name) == udata->attr_info.end())
|
||||||
|
{
|
||||||
|
LUT_LOG(ERROR, "Attribute %s not found!", full_name.c_str());
|
||||||
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
lut_AttributeInfo(L, udata->attr_info[name]);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lut_DeviceProxy_attribute_list_query(lua_State* L)
|
||||||
|
{
|
||||||
|
LUT_LOG(TRACE, "TANGO API DeviceProxy:attribute_list_query()");
|
||||||
|
DeviceProxyWrapper* udata = (DeviceProxyWrapper*)lut_getobj(L, 1);
|
||||||
|
|
||||||
|
lua_newtable(L);
|
||||||
|
int top = lua_gettop(L);
|
||||||
|
for(AttrInfoMap::iterator i = udata->attr_info.begin(); i != udata->attr_info.end(); i++)
|
||||||
|
{
|
||||||
|
lut_AttributeInfo(L, i->second);
|
||||||
|
lua_setfield(L, top, i->first.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int lut_DeviceProxy_get_command_list(lua_State* L)
|
int lut_DeviceProxy_get_command_list(lua_State* L)
|
||||||
{
|
{
|
||||||
LUT_LOG(TRACE, "TANGO API DeviceProxy:get_command_list()");
|
LUT_LOG(TRACE, "TANGO API DeviceProxy:get_command_list()");
|
||||||
@ -150,6 +164,41 @@ int lut_DeviceProxy_get_command_list(lua_State* L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lut_DeviceProxy_get_command_config(lua_State* L)
|
||||||
|
{
|
||||||
|
LUT_LOG(TRACE, "TANGO API DeviceProxy:get_command_config()");
|
||||||
|
DeviceProxyWrapper* udata = (DeviceProxyWrapper*)lut_getobj(L, 1);
|
||||||
|
const char* name = luaL_checkstring(L, 3); // arg[2] is Lua "self"
|
||||||
|
std::string dev_name = udata->dev->dev_name();
|
||||||
|
std::string full_name = build_name(dev_name, name);
|
||||||
|
|
||||||
|
if(udata->cmd_info.find(name) == udata->cmd_info.end())
|
||||||
|
{
|
||||||
|
LUT_LOG(ERROR, "Command %s not found!", full_name.c_str());
|
||||||
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
lut_CommandInfo(L, udata->cmd_info[name]);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lut_DeviceProxy_command_list_query(lua_State* L)
|
||||||
|
{
|
||||||
|
LUT_LOG(TRACE, "TANGO API DeviceProxy:command_list_query()");
|
||||||
|
DeviceProxyWrapper* udata = (DeviceProxyWrapper*)lut_getobj(L, 1);
|
||||||
|
|
||||||
|
lua_newtable(L);
|
||||||
|
int top = lua_gettop(L);
|
||||||
|
for(CmdInfoMap::iterator i = udata->cmd_info.begin(); i != udata->cmd_info.end(); i++)
|
||||||
|
{
|
||||||
|
lut_CommandInfo(L, i->second);
|
||||||
|
lua_setfield(L, top, i->first.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// LUA API --------------------------------------------------------------------
|
// LUA API --------------------------------------------------------------------
|
||||||
void lut_lua_register_DeviceProxy(lua_State* L)
|
void lut_lua_register_DeviceProxy(lua_State* L)
|
||||||
{
|
{
|
||||||
@ -204,33 +253,35 @@ int lut_DeviceProxy_index(lua_State* L)
|
|||||||
|
|
||||||
LUT_LOG(TRACE, "LUA API DeviceProxy(%s):index(%s)", dev_name.c_str(), name);
|
LUT_LOG(TRACE, "LUA API DeviceProxy(%s):index(%s)", dev_name.c_str(), name);
|
||||||
|
|
||||||
try
|
if(udata->cmd_info.find(name) != udata->cmd_info.end())
|
||||||
{
|
{
|
||||||
std::vector<std::string>* cmdlist = udata->dev->get_command_list();
|
// index is a command, push wrapper closure
|
||||||
|
// commands are supposed to be called, to allow this
|
||||||
if(std::count(cmdlist->begin(), cmdlist->end(), name))
|
// command "read" returns a function closure
|
||||||
|
lua_pushlightuserdata(L, udata);
|
||||||
|
lua_pushstring(L, name);
|
||||||
|
lua_pushcclosure(L, __cmd_wrapper, 2);
|
||||||
|
}
|
||||||
|
else if(udata->attr_info.find(name) != udata->attr_info.end())
|
||||||
|
{
|
||||||
|
// index is an attribute
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// index is a command, push wrapper closure
|
|
||||||
// commands are supposed to be called, to allow this
|
|
||||||
// command "read" returns a function closure
|
|
||||||
lua_pushlightuserdata(L, udata);
|
|
||||||
lua_pushstring(L, name);
|
|
||||||
lua_pushcclosure(L, __cmd_wrapper, 2);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// index is an attribute
|
|
||||||
LUT_LOG(TRACE, "Reading attribute %s", full_name.c_str());
|
LUT_LOG(TRACE, "Reading attribute %s", full_name.c_str());
|
||||||
Tango::DeviceAttribute attr = udata->dev->read_attribute(name);
|
Tango::DeviceAttribute attr = udata->dev->read_attribute(name);
|
||||||
lut_fromTangoType(L, attr, attr.get_type(), attr.get_data_format());
|
lut_fromTangoType(L, attr, attr.get_type(), attr.get_data_format());
|
||||||
}
|
}
|
||||||
delete cmdlist;
|
catch(Tango::DevFailed e)
|
||||||
|
{
|
||||||
|
lut_DevFailed(L, e, full_name.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(Tango::DevFailed e)
|
else
|
||||||
{
|
{
|
||||||
lut_DevFailed(L, e, full_name.c_str());
|
LUT_LOG(ERROR, "%s is neither command nor attribute!", full_name.c_str());
|
||||||
|
lua_pushnil(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,24 +295,20 @@ int lut_DeviceProxy_newindex(lua_State* L)
|
|||||||
|
|
||||||
LUT_LOG(TRACE, "LUA API DeviceProxy(%s):newindex(%s)", dev_name.c_str(), attr_name);
|
LUT_LOG(TRACE, "LUA API DeviceProxy(%s):newindex(%s)", dev_name.c_str(), attr_name);
|
||||||
|
|
||||||
try
|
if(udata->attr_info.find(attr_name) == udata->attr_info.end())
|
||||||
|
LUT_LOG(ERROR, "Not an attribute: %s!", full_name.c_str());
|
||||||
|
else
|
||||||
{
|
{
|
||||||
std::vector<std::string>* attrlist = udata->dev->get_attribute_list();
|
try
|
||||||
|
|
||||||
if(!std::count(attrlist->begin(), attrlist->end(), attr_name))
|
|
||||||
{
|
{
|
||||||
// name is not an attribute
|
Tango::DeviceAttribute v = lut_toTangoType<Tango::DeviceAttribute>(L, 3, udata->attr_info[attr_name].data_type, udata->attr_info[attr_name].data_format);
|
||||||
LUT_LOG(ERROR, "Not an attribute: %s", full_name.c_str());
|
v.set_name(attr_name);
|
||||||
return 0;
|
udata->dev->write_attribute(v);
|
||||||
|
}
|
||||||
|
catch(Tango::DevFailed e)
|
||||||
|
{
|
||||||
|
lut_DevFailed(L, e, full_name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
Tango::DeviceAttribute v = lut_toTangoType<Tango::DeviceAttribute>(L, 3, udata->attr_type(attr_name), udata->attr_format(attr_name));
|
|
||||||
v.set_name(attr_name);
|
|
||||||
udata->dev->write_attribute(v);
|
|
||||||
}
|
|
||||||
catch(Tango::DevFailed e)
|
|
||||||
{
|
|
||||||
lut_DevFailed(L, e, full_name.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -26,6 +26,7 @@
|
|||||||
#include <core/logging/log.h>
|
#include <core/logging/log.h>
|
||||||
|
|
||||||
#include <core/tango/types.h>
|
#include <core/tango/types.h>
|
||||||
|
#include <core/tango/AttrCmdInfo/lut_AttrCmdInfo.h>
|
||||||
#include <core/tango/DevFailed/lut_DevFailed.h>
|
#include <core/tango/DevFailed/lut_DevFailed.h>
|
||||||
|
|
||||||
#define LUT_DEVICEPROXY "lut_DeviceProxy"
|
#define LUT_DEVICEPROXY "lut_DeviceProxy"
|
||||||
@ -35,18 +36,11 @@ class DeviceProxyWrapper
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Tango::DeviceProxy* dev;
|
Tango::DeviceProxy* dev;
|
||||||
|
AttrInfoMap attr_info;
|
||||||
|
CmdInfoMap cmd_info;
|
||||||
|
|
||||||
DeviceProxyWrapper(const char* name);
|
DeviceProxyWrapper(const char* name);
|
||||||
~DeviceProxyWrapper();
|
~DeviceProxyWrapper();
|
||||||
|
|
||||||
int attr_type(const char* name);
|
|
||||||
Tango::AttrDataFormat attr_format(const char* name);
|
|
||||||
int cmd_in_type(const char* name);
|
|
||||||
int cmd_out_type(const char* name);
|
|
||||||
|
|
||||||
private:
|
|
||||||
AttrInfoMap attr_cache;
|
|
||||||
CmdInfoMap cmd_cache;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Command wrapper
|
// Command wrapper
|
||||||
@ -55,7 +49,11 @@ int __cmd_wrapper(lua_State* L);
|
|||||||
// Tango API
|
// Tango API
|
||||||
int lut_DeviceProxy_status(lua_State* L);
|
int lut_DeviceProxy_status(lua_State* L);
|
||||||
int lut_DeviceProxy_get_attribute_list(lua_State* L);
|
int lut_DeviceProxy_get_attribute_list(lua_State* L);
|
||||||
|
int lut_DeviceProxy_get_attribute_config(lua_State* L);
|
||||||
|
int lut_DeviceProxy_attribute_list_query(lua_State* L);
|
||||||
int lut_DeviceProxy_get_command_list(lua_State* L);
|
int lut_DeviceProxy_get_command_list(lua_State* L);
|
||||||
|
int lut_DeviceProxy_get_command_config(lua_State* L);
|
||||||
|
int lut_DeviceProxy_command_list_query(lua_State* L);
|
||||||
|
|
||||||
// Lua API
|
// Lua API
|
||||||
void lut_lua_register_DeviceProxy(lua_State* L);
|
void lut_lua_register_DeviceProxy(lua_State* L);
|
||||||
@ -74,7 +72,11 @@ static const luaL_Reg lut_DeviceProxy[] =
|
|||||||
// Tango API
|
// Tango API
|
||||||
{ "status", lut_DeviceProxy_status },
|
{ "status", lut_DeviceProxy_status },
|
||||||
{ "get_attribute_list", lut_DeviceProxy_get_attribute_list },
|
{ "get_attribute_list", lut_DeviceProxy_get_attribute_list },
|
||||||
|
{ "get_attribute_config", lut_DeviceProxy_get_attribute_config },
|
||||||
|
{ "attribute_list_query", lut_DeviceProxy_attribute_list_query },
|
||||||
{ "get_command_list", lut_DeviceProxy_get_command_list },
|
{ "get_command_list", lut_DeviceProxy_get_command_list },
|
||||||
|
{ "get_command_config", lut_DeviceProxy_get_command_config },
|
||||||
|
{ "command_list_query", lut_DeviceProxy_command_list_query },
|
||||||
|
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|||||||
27
tests/_print_kv.lua
Normal file
27
tests/_print_kv.lua
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
-------------------------------------------------------------------------------
|
||||||
|
--
|
||||||
|
-- luTango - Lua binding for Tango
|
||||||
|
--
|
||||||
|
-- Copyright (C) 2023 Grzegorz Kowalski
|
||||||
|
-- See LICENSE for legal information
|
||||||
|
--
|
||||||
|
-- file: _print_kv.lua
|
||||||
|
--
|
||||||
|
-- Nested table printing utility
|
||||||
|
--
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
local function print_kv(k, v, prefix)
|
||||||
|
prefix = prefix or ""
|
||||||
|
print(prefix..tostring(k)..": "..tostring(v))
|
||||||
|
if type(v) == "table" then
|
||||||
|
for kk,vv in ipairs(v) do
|
||||||
|
print_kv(kk, vv, prefix.."\t")
|
||||||
|
end
|
||||||
|
for kk,vv in pairs(v) do
|
||||||
|
print_kv(kk, vv, prefix.."\t")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return print_kv
|
||||||
@ -12,21 +12,9 @@
|
|||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
local lut = require "lutango"
|
local lut = require "lutango"
|
||||||
|
local print_kv = require "_print_kv"
|
||||||
lut.log:set_log_level(lut.log.level.WARNING)
|
lut.log:set_log_level(lut.log.level.WARNING)
|
||||||
|
|
||||||
local function print_kv(k, v, prefix)
|
|
||||||
prefix = prefix or ""
|
|
||||||
print(prefix..k..": "..tostring(v))
|
|
||||||
if type(v) == "table" then
|
|
||||||
for kk,vv in ipairs(v) do
|
|
||||||
print_kv(kk, vv, prefix.."\t")
|
|
||||||
end
|
|
||||||
for kk,vv in pairs(v) do
|
|
||||||
print_kv(kk, vv, prefix.."\t")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local ap = lut.AttributeProxy("sys/tg_test/1/ampli")
|
local ap = lut.AttributeProxy("sys/tg_test/1/ampli")
|
||||||
|
|
||||||
print("Reading state and status")
|
print("Reading state and status")
|
||||||
|
|||||||
49
tests/dp_info.lua
Normal file
49
tests/dp_info.lua
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
-------------------------------------------------------------------------------
|
||||||
|
--
|
||||||
|
-- luTango - Lua binding for Tango
|
||||||
|
--
|
||||||
|
-- Copyright (C) 2023 Grzegorz Kowalski
|
||||||
|
-- See LICENSE for legal information
|
||||||
|
--
|
||||||
|
-- file: dp_info.lua
|
||||||
|
--
|
||||||
|
-- Test script for attribute and command info
|
||||||
|
--
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
local lut = require "lutango"
|
||||||
|
local print_kv = require "_print_kv"
|
||||||
|
lut.log:set_log_level(lut.log.level.WARNING)
|
||||||
|
|
||||||
|
local dp = lut.DeviceProxy(arg[1] or "sys/tg_test/1")
|
||||||
|
|
||||||
|
print("Get full lists:")
|
||||||
|
local ai_list = dp:attribute_list_query()
|
||||||
|
for k,v in pairs(ai_list) do
|
||||||
|
print_kv(k, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
local ci_list = dp:command_list_query()
|
||||||
|
for k,v in pairs(ci_list) do
|
||||||
|
print_kv(k, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
print("\nGet single attr:")
|
||||||
|
local ai = dp:get_attribute_config("double_spectrum_ro")
|
||||||
|
for k,v in pairs(ai) do
|
||||||
|
print_kv(k, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
print("\nGet single command:")
|
||||||
|
local ci = dp:get_command_config("DevLong64")
|
||||||
|
for k,v in pairs(ci) do
|
||||||
|
print_kv(k, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
print("\nGet non-existent attr:")
|
||||||
|
local non_ai = dp:get_attribute_config("non_existent_attr")
|
||||||
|
print(tostring(non_ai))
|
||||||
|
|
||||||
|
print("\nGet non-existent command:")
|
||||||
|
local non_ci = dp:get_command_config("NonExistentCommand")
|
||||||
|
print(tostring(non_ci))
|
||||||
@ -30,3 +30,7 @@ for _,v in ipairs(attrlist) do
|
|||||||
end
|
end
|
||||||
print()
|
print()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
print("\nReading non existent attribute")
|
||||||
|
local ne = dp.non_existent_attr
|
||||||
|
print(tostring(ne))
|
||||||
|
|||||||
@ -74,3 +74,7 @@ run_array_cmd("DevVarStringArray", {"hello", "world", "from", "Lua"})
|
|||||||
run_array_cmd("DevVarULong64Array", {11111111111, 22222222222, 33333333333})
|
run_array_cmd("DevVarULong64Array", {11111111111, 22222222222, 33333333333})
|
||||||
run_array_cmd("DevVarULongArray", {11111111111, 22222222222, 33333333333})
|
run_array_cmd("DevVarULongArray", {11111111111, 22222222222, 33333333333})
|
||||||
run_array_cmd("DevVarUShortArray", {1111, 2222, 3333})
|
run_array_cmd("DevVarUShortArray", {1111, 2222, 3333})
|
||||||
|
|
||||||
|
print("\nRunning non-existent command:")
|
||||||
|
local ok, ret = pcall(dp.NonExistentCmd)
|
||||||
|
print("ok="..tostring(ok).." ret="..tostring(ret))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user