Skip to content

Commit f08f52c

Browse files
committed
Add Lua script editor to Domoticz web UI
1 parent 3494b9e commit f08f52c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+787
-118
lines changed

main/EventSystem.cpp

Lines changed: 154 additions & 41 deletions
Large diffs are not rendered by default.

main/EventSystem.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class CEventSystem
2323
{
2424
unsigned long long ID;
2525
std::string Name;
26+
std::string Interpreter;
27+
std::string Type;
2628
std::string Conditions;
2729
std::string Actions;
2830
int SequenceNo;
@@ -99,9 +101,9 @@ class CEventSystem
99101
void EvaluatePython(const std::string &reason, const std::string &filename);
100102
void EvaluatePython(const std::string &reason, const std::string &filename, const unsigned long long DeviceID, const std::string &devname, const int nValue, const char* sValue, std::string nValueWording, const unsigned long long varId);
101103
#endif
102-
void EvaluateLua(const std::string &reason, const std::string &filename, const unsigned long long varId);
103-
void EvaluateLua(const std::string &reason, const std::string &filename);
104-
void EvaluateLua(const std::string &reason, const std::string &filename, const unsigned long long DeviceID, const std::string &devname, const int nValue, const char* sValue, std::string nValueWording, const unsigned long long varId);
104+
void EvaluateLua(const std::string &reason, const std::string &filename, const std::string &LuaString, const unsigned long long varId);
105+
void EvaluateLua(const std::string &reason, const std::string &filename, const std::string &LuaString);
106+
void EvaluateLua(const std::string &reason, const std::string &filename, const std::string &LuaString, const unsigned long long DeviceID, const std::string &devname, const int nValue, const char* sValue, std::string nValueWording, const unsigned long long varId);
105107
void luaThread(lua_State *lua_state, const std::string &filename);
106108
static void luaStop(lua_State *L, lua_Debug *ar);
107109
std::string nValueToWording(const unsigned char dType, const unsigned char dSubType, const _eSwitchType switchtype, const unsigned char nValue, const std::string &sValue, const std::map<std::string, std::string> & options);

main/SQLHelper.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "../msbuild/WindowsHelper.h"
3232
#endif
3333

34-
#define DB_VERSION 89
34+
#define DB_VERSION 90
3535

3636
extern http::server::CWebServerHelper m_webservers;
3737
extern std::string szWWWFolder;
@@ -404,6 +404,8 @@ const char *sqlCreateEventMaster =
404404
"CREATE TABLE IF NOT EXISTS [EventMaster] ("
405405
"[ID] INTEGER PRIMARY KEY, "
406406
"[Name] VARCHAR(200) NOT NULL, "
407+
"[Interpreter] VARCHAR(10) DEFAULT 'Blockly', "
408+
"[Type] VARCHAR(10) DEFAULT 'All', "
407409
"[XMLStatement] TEXT NOT NULL, "
408410
"[Status] INTEGER DEFAULT 0);";
409411

@@ -1603,7 +1605,17 @@ bool CSQLHelper::OpenDatabase()
16031605
szQuery << "UPDATE DeviceStatus SET [DeviceID]='0' || DeviceID WHERE ([Type]=" << pTypeGeneralSwitch << ") AND (SubType=" << sSwitchTypeSelector << ") AND length(DeviceID) = 7";
16041606
query(szQuery.str());
16051607
}
1606-
1608+
if (dbversion < 90)
1609+
{
1610+
if (!DoesColumnExistsInTable("Interpreter", "EventMaster"))
1611+
{
1612+
query("ALTER TABLE EventMaster ADD COLUMN [Interpreter] VARCHAR(10) DEFAULT 'Blockly'");
1613+
}
1614+
if (!DoesColumnExistsInTable("Type", "EventMaster"))
1615+
{
1616+
query("ALTER TABLE EventMaster ADD COLUMN [Type] VARCHAR(10) DEFAULT 'All'");
1617+
}
1618+
}
16071619
}
16081620
else if (bNewInstall)
16091621
{

scripts/templates/All.Lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--
2+
-- Domoticz passes information to scripts through a number of global tables
3+
--
4+
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices:
5+
-- otherdevices['yourotherdevicename'] = "On"
6+
-- otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
7+
-- otherdevices_svalues['yourotherthermometer'] = string of svalues
8+
--
9+
-- uservariables and uservariables_lastupdate are arrays for all user variables:
10+
-- uservariables['yourvariablename'] = 'Test Value'
11+
-- uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
12+
--
13+
-- other useful details are contained in the timeofday table
14+
-- timeofday['Nighttime'] = true or false
15+
-- timeofday['SunriseInMinutes'] = number
16+
-- timeofday['Daytime'] = true or false
17+
-- timeofday['SunsetInMinutes'] = number
18+
-- globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
19+
--
20+
-- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
21+
-- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
22+
--
23+
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.
24+
--
25+
commandArray = {}
26+
27+
print ("All based event fired");
28+
-- loop through all the devices
29+
for deviceName,deviceValue in pairs(otherdevices) do
30+
-- if (deviceName=='myDevice') then
31+
-- if deviceValue == "On" then
32+
-- print("Device is On")
33+
-- elseif deviceValue == "Off" then
34+
-- commandArray['a device name'] = "On"
35+
-- commandArray['Scene:MyScene'] = "Off"
36+
-- end
37+
-- end
38+
end
39+
40+
-- loop through all the variables
41+
for variableName,variableValue in pairs(uservariables) do
42+
-- if (variableName=='myVariable') then
43+
-- if variableValue == 1 then
44+
-- commandArray['a device name'] = "On"
45+
-- commandArray['Group:My Group'] = "Off AFTER 30"
46+
-- end
47+
-- end
48+
end
49+
50+
return commandArray

scripts/templates/Device.Lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--
2+
-- Domoticz passes information to scripts through a number of global tables
3+
--
4+
-- device changed contains state and svalues for the device that changed.
5+
-- devicechanged['yourdevicename'] = state
6+
-- devicechanged['svalues'] = svalues string
7+
--
8+
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices:
9+
-- otherdevices['yourotherdevicename'] = "On"
10+
-- otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
11+
-- otherdevices_svalues['yourotherthermometer'] = string of svalues
12+
--
13+
-- uservariables and uservariables_lastupdate are arrays for all user variables:
14+
-- uservariables['yourvariablename'] = 'Test Value'
15+
-- uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
16+
--
17+
-- other useful details are contained in the timeofday table
18+
-- timeofday['Nighttime'] = true or false
19+
-- timeofday['SunriseInMinutes'] = number
20+
-- timeofday['Daytime'] = true or false
21+
-- timeofday['SunsetInMinutes'] = number
22+
-- globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
23+
--
24+
-- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
25+
-- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
26+
--
27+
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.
28+
--
29+
commandArray = {}
30+
31+
-- loop through all the changed devices
32+
for deviceName,deviceValue in pairs(devicechanged) do
33+
print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
34+
-- if (deviceName=='myDevice') then
35+
-- if deviceValue == "On" then
36+
-- print("Device is On")
37+
-- elseif deviceValue == "Off" then
38+
-- commandArray['a device name'] = "On"
39+
-- commandArray['another device name'] = "Off AFTER 10"
40+
-- commandArray['Scene:MyScene'] = "Off"
41+
-- commandArray['Group:My Group'] = "Off AFTER 30"
42+
-- end
43+
-- end
44+
end
45+
46+
return commandArray

scripts/templates/Security.Lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--
2+
-- Domoticz passes information to scripts through a number of global tables
3+
--
4+
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices:
5+
-- otherdevices['yourotherdevicename'] = "On"
6+
-- otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
7+
-- otherdevices_svalues['yourotherthermometer'] = string of svalues
8+
--
9+
-- uservariables and uservariables_lastupdate are arrays for all user variables:
10+
-- uservariables['yourvariablename'] = 'Test Value'
11+
-- uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
12+
--
13+
-- other useful details are contained in the timeofday table
14+
-- timeofday['Nighttime'] = true or false
15+
-- timeofday['SunriseInMinutes'] = number
16+
-- timeofday['Daytime'] = true or false
17+
-- timeofday['SunsetInMinutes'] = number
18+
-- globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
19+
--
20+
-- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
21+
-- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
22+
--
23+
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.
24+
--
25+
commandArray = {}
26+
27+
print ("Security based event fired");
28+
-- loop through all the devices
29+
for deviceName,deviceValue in pairs(otherdevices) do
30+
-- if (deviceName=='myDevice') then
31+
-- if deviceValue == "On" then
32+
-- print("Device is On")
33+
-- elseif deviceValue == "Off" then
34+
-- commandArray['a device name'] = "On"
35+
-- commandArray['Scene:MyScene'] = "Off"
36+
-- end
37+
-- end
38+
end
39+
40+
-- loop through all the variables
41+
for variableName,variableValue in pairs(uservariables) do
42+
-- if (variableName=='myVariable') then
43+
-- if variableValue == 1 then
44+
-- commandArray['a device name'] = "On"
45+
-- commandArray['Group:My Group'] = "Off AFTER 30"
46+
-- end
47+
-- end
48+
end
49+
50+
return commandArray

scripts/templates/Time.Lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--
2+
-- Domoticz passes information to scripts through a number of global tables
3+
--
4+
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices:
5+
-- otherdevices['yourotherdevicename'] = "On"
6+
-- otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
7+
-- otherdevices_svalues['yourotherthermometer'] = string of svalues
8+
--
9+
-- uservariables and uservariables_lastupdate are arrays for all user variables:
10+
-- uservariables['yourvariablename'] = 'Test Value'
11+
-- uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
12+
--
13+
-- other useful details are contained in the timeofday table
14+
-- timeofday['Nighttime'] = true or false
15+
-- timeofday['SunriseInMinutes'] = number
16+
-- timeofday['Daytime'] = true or false
17+
-- timeofday['SunsetInMinutes'] = number
18+
-- globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
19+
--
20+
-- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
21+
-- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
22+
--
23+
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.
24+
--
25+
26+
commandArray = {}
27+
28+
print ("Time based event fired");
29+
-- loop through all the devices
30+
for deviceName,deviceValue in pairs(otherdevices) do
31+
-- if (deviceName=='myDevice') then
32+
-- if deviceValue == "On" then
33+
-- print("Device is On")
34+
-- elseif deviceValue == "Off" then
35+
-- commandArray['a device name'] = "On"
36+
-- commandArray['Scene:MyScene'] = "Off"
37+
-- end
38+
-- end
39+
end
40+
41+
-- loop through all the variables
42+
for variableName,variableValue in pairs(uservariables) do
43+
-- if (variableName=='myVariable') then
44+
-- if variableValue == 1 then
45+
-- commandArray['a device name'] = "On"
46+
-- commandArray['Group:My Group'] = "Off AFTER 30"
47+
-- end
48+
-- end
49+
end
50+
51+
return commandArray

scripts/templates/UserVariable.Lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--
2+
-- Domoticz passes information to scripts through a number of global tables
3+
--
4+
-- variable changed contains state and svalues for the variable that changed.
5+
-- uservariablechanged['yourvariablename'] = value
6+
--
7+
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices:
8+
-- otherdevices['yourotherdevicename'] = "On"
9+
-- otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
10+
-- otherdevices_svalues['yourotherthermometer'] = string of svalues
11+
--
12+
-- uservariables and uservariables_lastupdate are arrays for all user variables:
13+
-- uservariables['yourvariablename'] = 'Test Value'
14+
-- uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
15+
--
16+
-- other useful details are contained in the timeofday table
17+
-- timeofday['Nighttime'] = true or false
18+
-- timeofday['SunriseInMinutes'] = number
19+
-- timeofday['Daytime'] = true or false
20+
-- timeofday['SunsetInMinutes'] = number
21+
-- globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
22+
--
23+
-- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
24+
-- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
25+
--
26+
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.
27+
--
28+
commandArray = {}
29+
30+
-- loop through all the changed variables
31+
for variableName,variableValue in pairs(uservariablechanged) do
32+
print ("Variable based event fired on '"..variableName.."', value '"..tostring(variableValue).."'");
33+
-- if (variableName=='myVariable') then
34+
-- if variableValue == 1 then
35+
-- commandArray['a device name'] = "On"
36+
-- commandArray['another device name'] = "Off AFTER 10"
37+
-- commandArray['Variable:myVariable'] = 'new value'
38+
-- end
39+
-- end
40+
end
41+
42+
return commandArray

0 commit comments

Comments
 (0)