2020-03-06 01:00:51 +03:00
|
|
|
--[[
|
|
|
|
|
2020-03-09 00:36:23 +03:00
|
|
|
Techage
|
|
|
|
=======
|
2020-03-06 01:00:51 +03:00
|
|
|
|
2020-03-09 00:36:23 +03:00
|
|
|
Copyright (C) 2020 Joachim Stolberg
|
2020-03-06 01:00:51 +03:00
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2020-03-06 01:00:51 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
commands.lua:
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-03-06 01:00:51 +03:00
|
|
|
Register all basic controller commands
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- store protection data locally
|
|
|
|
local LocalRef = {}
|
|
|
|
local function not_protected(owner, numbers)
|
2020-03-15 01:02:13 +03:00
|
|
|
if owner and numbers then
|
|
|
|
LocalRef[owner] = LocalRef[owner] or {}
|
|
|
|
if LocalRef[owner][numbers] == nil then
|
|
|
|
LocalRef[owner][numbers] = techage.check_numbers(numbers, owner)
|
|
|
|
end
|
|
|
|
return LocalRef[owner][numbers]
|
2020-03-06 01:00:51 +03:00
|
|
|
end
|
2020-03-15 01:02:13 +03:00
|
|
|
return false
|
2020-03-06 01:00:51 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_function("get_input", {
|
|
|
|
cmnd = function(self, num)
|
|
|
|
num = tostring(num or "")
|
|
|
|
return techage.lua_ctlr.get_input(self.meta.number, num)
|
|
|
|
end,
|
|
|
|
help = ' $get_input(num) --> "on", "off", or nil\n'..
|
|
|
|
' Read local input value from device with number "num".\n'..
|
|
|
|
' example: inp = $get_input("1234")\n'..
|
|
|
|
" The device has to be connected with the controller."
|
|
|
|
})
|
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
techage.lua_ctlr.register_function("get_next_input", {
|
|
|
|
cmnd = function(self)
|
|
|
|
return techage.lua_ctlr.get_next_input(self.meta.number)
|
|
|
|
end,
|
|
|
|
help = ' $get_next_input() --> number and state\n'..
|
|
|
|
' Similar to $get_input(), but provides the\n'..
|
|
|
|
' input node number in addition.\n'..
|
|
|
|
' example: num, state = $get_next_input()\n'..
|
|
|
|
' This function deletes the input and returns\n'..
|
|
|
|
' nil if no further input value is available.'
|
|
|
|
})
|
|
|
|
|
2020-03-15 01:02:13 +03:00
|
|
|
techage.lua_ctlr.register_function("read_data", {
|
2020-07-23 14:43:10 +03:00
|
|
|
cmnd = function(self, num, cmnd, data)
|
2020-03-06 01:00:51 +03:00
|
|
|
num = tostring(num or "")
|
2020-07-23 14:43:10 +03:00
|
|
|
cmnd = tostring(cmnd or "")
|
|
|
|
if not_protected(self.meta.owner, num) then
|
|
|
|
return techage.send_single(self.meta.number, num, cmnd, data)
|
|
|
|
end
|
2020-03-06 01:00:51 +03:00
|
|
|
end,
|
2020-07-23 14:43:10 +03:00
|
|
|
help = " $read_data(num, cmnd, add_data)\n"..
|
|
|
|
" This function is deprecated.\n"..
|
|
|
|
" It will be removed in future releases.\n"..
|
|
|
|
" Use $send_cmnd(num, cmnd, add_data) instead."
|
2020-03-06 01:00:51 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_function("time_as_str", {
|
2022-01-03 23:40:31 +03:00
|
|
|
cmnd = function(self)
|
2020-03-06 01:00:51 +03:00
|
|
|
local t = minetest.get_timeofday()
|
|
|
|
local h = math.floor(t*24) % 24
|
|
|
|
local m = math.floor(t*1440) % 60
|
|
|
|
return string.format("%02d:%02d", h, m)
|
|
|
|
end,
|
|
|
|
help = " $time_as_str() --> e.g. '18:45'\n"..
|
|
|
|
" Read time of day as string (24h).\n"..
|
|
|
|
' example: time = $time_as_str()'
|
|
|
|
})
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_function("time_as_num", {
|
2022-01-03 23:40:31 +03:00
|
|
|
cmnd = function(self, num)
|
2020-03-06 01:00:51 +03:00
|
|
|
local t = minetest.get_timeofday()
|
|
|
|
local h = math.floor(t*24) % 24
|
|
|
|
local m = math.floor(t*1440) % 60
|
|
|
|
return h * 100 + m
|
|
|
|
end,
|
|
|
|
help = " $time_as_num() --> e.g.: 1845\n"..
|
|
|
|
" Read time of day as number (24h).\n"..
|
|
|
|
' example: time = $time_as_num()'
|
|
|
|
})
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_action("send_cmnd", {
|
2022-01-03 23:40:31 +03:00
|
|
|
cmnd = function(self, num, cmnd, data)
|
2020-03-06 01:00:51 +03:00
|
|
|
num = tostring(num or "")
|
2020-03-15 01:02:13 +03:00
|
|
|
cmnd = tostring(cmnd or "")
|
2020-03-06 01:00:51 +03:00
|
|
|
if not_protected(self.meta.owner, num) then
|
2020-03-22 13:01:44 +03:00
|
|
|
return techage.send_single(self.meta.number, num, cmnd, data)
|
2020-03-06 01:00:51 +03:00
|
|
|
end
|
|
|
|
end,
|
2020-03-22 13:01:44 +03:00
|
|
|
help = " $send_cmnd(num, cmnd, add_data)\n"..
|
2020-03-06 01:00:51 +03:00
|
|
|
' Send a command to the device with number "num".\n'..
|
2020-03-22 13:01:44 +03:00
|
|
|
' "cmnd" is the command as text string\n'..
|
|
|
|
' "add_data" is additional data (optional)\n'..
|
2020-03-06 01:00:51 +03:00
|
|
|
' example: $send_cmnd("1234", "on")'
|
|
|
|
})
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_action("set_filter", {
|
2022-01-03 23:40:31 +03:00
|
|
|
cmnd = function(self, num, slot, val)
|
2020-03-06 01:00:51 +03:00
|
|
|
num = tostring(num or "")
|
|
|
|
slot = tostring(slot or "red")
|
|
|
|
val = tostring(val or "on")
|
|
|
|
if not_protected(self.meta.owner, num) then
|
2020-07-26 16:43:44 +03:00
|
|
|
techage.send_single(self.meta.number, num, "port", slot.."="..val)
|
2020-03-06 01:00:51 +03:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
help = " $set_filter(num, slot, val)\n"..
|
|
|
|
' Turn on/off a Distributor filter slot.\n'..
|
|
|
|
' example: $set_filter("1234", "red", "off")'
|
|
|
|
})
|
|
|
|
|
2021-01-31 21:10:14 +03:00
|
|
|
techage.lua_ctlr.register_action("get_filter", {
|
|
|
|
cmnd = function(self, num, slot)
|
|
|
|
num = tostring(num or "")
|
|
|
|
slot = tostring(slot or "red")
|
|
|
|
if not_protected(self.meta.owner, num) then
|
|
|
|
return techage.send_single(self.meta.number, num, "port", slot)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
help = " $get_filter(num, slot)\n"..
|
|
|
|
' Read state of a Distributor filter slot.\n'..
|
|
|
|
' Return value is "on" or "off".\n'..
|
|
|
|
' example: state = $get_filter("1234", "red")'
|
|
|
|
})
|
2020-03-06 01:00:51 +03:00
|
|
|
|
|
|
|
techage.lua_ctlr.register_action("display", {
|
2020-03-15 01:02:13 +03:00
|
|
|
cmnd = function(self, num, row, text)
|
2020-03-06 01:00:51 +03:00
|
|
|
num = tostring(num or "")
|
2020-03-15 01:02:13 +03:00
|
|
|
row = tonumber(row or 1) or 1
|
|
|
|
text = tostring(text or "")
|
2020-03-06 01:00:51 +03:00
|
|
|
if not_protected(self.meta.owner, num) then
|
2020-03-15 01:02:13 +03:00
|
|
|
if text:byte(1) ~= 32 then -- left aligned?
|
|
|
|
text = "<"..text -- use the '<' lcdlib control char for left-aligned
|
|
|
|
else
|
|
|
|
text = text:sub(2) -- delete blank for centered
|
|
|
|
end
|
|
|
|
if row == 0 then -- add line?
|
|
|
|
techage.send_single(self.meta.number, num, "add", text)
|
|
|
|
else
|
2020-07-26 16:43:44 +03:00
|
|
|
local payload = safer_lua.Store()
|
|
|
|
payload.set("row", row)
|
|
|
|
payload.set("str", text)
|
|
|
|
techage.send_single(self.meta.number, num, "set", payload)
|
2020-03-15 01:02:13 +03:00
|
|
|
end
|
2020-03-06 01:00:51 +03:00
|
|
|
end
|
|
|
|
end,
|
2020-03-15 01:02:13 +03:00
|
|
|
help = " $display(num, row, text)\n"..
|
2020-03-06 01:00:51 +03:00
|
|
|
' Send a text line to the display with number "num".\n'..
|
2020-03-15 01:02:13 +03:00
|
|
|
" 'row' is a value from 1..5, or 0 for scroll screen\n"..
|
|
|
|
" and add a new line. If the first char of the string\n"..
|
|
|
|
" is a blank, the text will be horizontally centered.\n"..
|
|
|
|
' example: $display("123", 1, "Hello "..name)'
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-03-06 01:00:51 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_action("clear_screen", {
|
2022-01-03 23:40:31 +03:00
|
|
|
cmnd = function(self, num)
|
2020-03-06 01:00:51 +03:00
|
|
|
num = tostring(num or "")
|
|
|
|
if not_protected(self.meta.owner, num) then
|
|
|
|
techage.send_single(self.meta.number, num, "clear", nil)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
help = " $clear_screen(num)\n"..
|
|
|
|
' Clear the screen of the display\n'..
|
|
|
|
' with number "num".\n'..
|
|
|
|
' example: $clear_screen("1234")'
|
|
|
|
})
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_action("chat", {
|
2022-01-03 23:40:31 +03:00
|
|
|
cmnd = function(self, text)
|
2020-03-16 01:50:15 +03:00
|
|
|
text = tostring(text or "")
|
|
|
|
minetest.chat_send_player(self.meta.owner, "[TA4 Lua Controller] "..text)
|
2020-03-06 01:00:51 +03:00
|
|
|
end,
|
|
|
|
help = " $chat(text,...)\n"..
|
|
|
|
" Send yourself a chat message.\n"..
|
2020-03-16 01:50:15 +03:00
|
|
|
' example: $chat("Hello "..name)'
|
2020-03-06 01:00:51 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_action("door", {
|
2022-01-03 23:40:31 +03:00
|
|
|
cmnd = function(self, pos, text)
|
2020-03-06 01:00:51 +03:00
|
|
|
pos = tostring(pos or "")
|
|
|
|
text = tostring(text or "")
|
|
|
|
pos = minetest.string_to_pos("("..pos..")")
|
|
|
|
if pos then
|
|
|
|
local door = doors.get(pos)
|
|
|
|
if door then
|
|
|
|
local player = {
|
|
|
|
get_player_name = function() return self.meta.owner end,
|
|
|
|
is_player = function() return true end,
|
|
|
|
}
|
|
|
|
if text == "open" then
|
|
|
|
door:open(player)
|
|
|
|
elseif text == "close" then
|
|
|
|
door:close(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
help = " $door(pos, text)\n"..
|
|
|
|
' Open/Close a door at position "pos"\n'..
|
|
|
|
' example: $door("123,7,-1200", "close")\n'..
|
|
|
|
" Hint: Use the Techage Programmer to\ndetermine the door position."
|
|
|
|
})
|
2020-03-09 00:36:23 +03:00
|
|
|
|
2020-07-27 22:52:52 +03:00
|
|
|
techage.lua_ctlr.register_function("item_description", {
|
|
|
|
cmnd = function(self, itemstring)
|
|
|
|
local item_def = minetest.registered_items[itemstring]
|
|
|
|
if item_def and item_def.description then
|
2020-08-03 20:35:33 +03:00
|
|
|
return minetest.get_translated_string("en", item_def.description)
|
2020-07-27 22:52:52 +03:00
|
|
|
end
|
|
|
|
return ""
|
|
|
|
end,
|
|
|
|
help = " $item_description(itemstring)\n"..
|
|
|
|
" Get the description for a specified itemstring.\n"..
|
2020-08-03 20:35:33 +03:00
|
|
|
' example: desc = $item_description("default:apple")'
|
2020-07-27 22:52:52 +03:00
|
|
|
})
|
|
|
|
|
2020-03-22 13:01:44 +03:00
|
|
|
|
2020-03-09 00:36:23 +03:00
|
|
|
-- function not_protected(owner, number(s))
|
2022-01-04 21:40:27 +03:00
|
|
|
techage.lua_ctlr.not_protected = not_protected
|