-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuaSensorScript.cpp
172 lines (141 loc) · 7.57 KB
/
LuaSensorScript.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @file OssLuaSensorScript.cpp
*
* @author thomas
*
* @date 19. September 2013
* @copyright GNU General Public License (GPL) 3.0
*/
#include <iostream>
#include <string>
#include <sstream>
#include <tr1/memory>
#include <tinyxml2.h>
extern "C" {
#include <lua5.2/lua.h>
#include <lua5.2/lualib.h>
#include <lua5.2/lauxlib.h>
}
#include <luabind/luabind.hpp>
#include "../../include/lua/OssLuaSensorScript.hpp"
#include "../../include/OssGroups.hpp"
#include "../../include/OssConstants.hpp"
#include "../../include/sensor/OssSensor.hpp"
#include "../../include/OssTree.hpp"
#include "../../include/OssNodeType.hpp"
#include "../../include/i2c/OssI2cSlave.hpp"
namespace oss {
namespace lua {
LuaSensorScript::LuaSensorScript() {
this->LuaScriptEnabled = false;
// Create a new lua state
this->LuaState = luaL_newstate();
luabind::open(this->LuaState);
luaL_openlibs(this->LuaState);
}
LuaSensorScript::LuaSensorScript(const LuaSensorScript& orig) {
}
LuaSensorScript::~LuaSensorScript() {
// Close a new lua state
lua_close(this->LuaState);
}
void LuaSensorScript::InitChild() {
// Function is now called from parseXml
// this->InitLua();
}
void LuaSensorScript::InitLua() {
// Loading file
#ifdef DEBUG
std::clog << "DEBUG: Loading Lua-File: " << this->GetVariable(oss::constants::variableNames::FileName) << std::endl;
#endif
if (luaL_dofile(this->LuaState, this->GetVariable(oss::constants::variableNames::FileName).c_str())) {
std::clog << "WARNING: " << lua_tostring(this->LuaState, -1) << std::endl;
}
// Loading main functions
luabind::class_<LuaSensorScript> LuaObject_LuaSensorScript("oss");
this->RegisterInLua(LuaObject_LuaSensorScript);
luabind::module(this->LuaState) [ LuaObject_LuaSensorScript ];
// Bind to this object
luabind::globals(this->LuaState)["oss"] = this;
// Add List of possible objects
// example: NodeTypes.oss_i2c_Host
luabind::class_<oss::NodeType> LuaObject_NodeType("NodeTypes");
oss::NodeType::RegisterInLua(LuaObject_NodeType);
luabind::module(this->LuaState) [ LuaObject_NodeType ];
// Add special I2c-functions if possible
std::tr1::shared_ptr<oss::i2c::Slave> i2cParrentNode = std::tr1::dynamic_pointer_cast<oss::i2c::Slave>(this->GetParrentNode());
if (i2cParrentNode.get()) {
luabind::class_<oss::i2c::Slave> LuaObject_I2c("i2c");
i2cParrentNode->RegisterInLua(LuaObject_I2c);
luabind::module(this->LuaState) [ LuaObject_I2c ];
luabind::globals(this->LuaState)["i2c"] = i2cParrentNode.get();
}
try {
// Call Init Function
if (luabind::call_function<bool>(this->LuaState, "check")) {
this->LuaScriptEnabled = true;
// Init Class
luabind::call_function<void>(this->LuaState, "init");
} else {
this->LuaScriptEnabled = false;
std::cerr << "ERROR: Lua Script is not compatible, file: " << this->GetVariable(oss::constants::variableNames::FileName) << std::endl;
}
} catch (luabind::error e) {
std::cerr << "ERROR: " << e.what() << ", " << luabind::object(luabind::from_stack(e.state(), -1)) << std::endl;
}
}
void LuaSensorScript::RegisterInLua(luabind::class_<LuaSensorScript>& x) {
x.def("GetVariable", &oss::tree::TreeNode::GetVariable);
x.def("SetVariable", &oss::tree::TreeNode::SetVariable);
x.def("GetConstante", &oss::tree::TreeNode::GetConstante);
x.def("SetConstante", &oss::tree::TreeNode::SetConstante);
x.def("GetVariableFromParrent", &oss::lua::LuaSensorScript::GetVariableFromParrent);
x.def("GetNodeTypeFromParrent", &oss::lua::LuaSensorScript::GetNodeTypeFromParrent);
x.def("GetVariableFromChildNode", (const std::string(oss::lua::LuaSensorScript::*)(std::string, const std::string)) & oss::lua::LuaSensorScript::GetVariableFromChildNode);
x.def("GetVariableFromChildNode", (const std::string(oss::lua::LuaSensorScript::*)(std::string, const unsigned int)) & oss::lua::LuaSensorScript::GetVariableFromChildNode);
x.def("GetNodeTypeFromChildNode", (const oss::NodeType::NodeTypes(oss::lua::LuaSensorScript::*)(const std::string)) & oss::lua::LuaSensorScript::GetNodeTypeFromChildNode);
x.def("GetNodeTypeFromChildNode", (const oss::NodeType::NodeTypes(oss::lua::LuaSensorScript::*)(const unsigned int)) & oss::lua::LuaSensorScript::GetNodeTypeFromChildNode);
//x.def("GetChildNode", (std::tr1::shared_ptr<oss::tree::TreeNode>(oss::tree::TreeNode::*)(const unsigned int)) & oss::tree::TreeNode::GetChildNode);
//x.def("GetChildNode", (std::tr1::shared_ptr<oss::tree::TreeNode>(oss::tree::TreeNode::*)(const std::string)) & oss::tree::TreeNode::GetChildNode);
x.def("GetChildSize", &oss::tree::TreeNode::GetChildSize);
//x.def("GetParrentNode", &oss::tree::TreeNode::GetParrentNode);
//x.def("GetRootNode", &oss::tree::TreeNode::GetRootNode);
}
void LuaSensorScript::RunNode() {
if (this->LuaScriptEnabled) {
try {
// Call Init Function
luabind::call_function<void>(this->LuaState, "run");
} catch (luabind::error e) {
std::cerr << "ERROR: " << e.what() << ", " << luabind::object(luabind::from_stack(e.state(), -1)) << std::endl;
}
this->TreeNode::RunNode();
}
}
void LuaSensorScript::parseXml(tinyxml2::XMLNode * const xmlNode) {
if (std::string(xmlNode->ToElement()->Name()) == oss::constants::xmlElementNames::luaElements::LuaScript) {
std::string childName;
for (tinyxml2::XMLElement* helpElement = xmlNode->FirstChildElement()
; helpElement != NULL
; helpElement = helpElement->NextSiblingElement()
) {
childName.clear();
childName = helpElement->Name();
if (childName == oss::constants::xmlElementNames::luaElements::LuaScript) {
std::tr1::shared_ptr<oss::helpGroups::LuaAndSensorGroup> newChildElement(new oss::lua::LuaSensorScript);
this->AddChildNode(newChildElement);
newChildElement->parseXml(helpElement);
} else if (childName == oss::constants::xmlElementNames::sensorElements::GeneralSensor) {
std::tr1::shared_ptr<oss::helpGroups::LuaAndSensorGroup> newChildElement(new oss::sensor::Sensor);
this->AddChildNode(newChildElement);
newChildElement->parseXml(helpElement);
}
}
this->MainTreeGroup::parseMainXmlParameter(xmlNode);
this->InitLua();
} else {
std::clog << "WARNING: <" << oss::constants::xmlElementNames::luaElements::LuaScript << "> isn't parent-node, ignoring child elements and values, node is: <" << std::string(xmlNode->ToElement()->Name()) << ">" << std::endl;
}
}
}
}