2018-08-28 22:24:43 +02:00
|
|
|
--[[
|
|
|
|
|
|
|
|
ICTA Controller
|
|
|
|
===============
|
|
|
|
|
|
|
|
Part of the SmartLine mod
|
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
Copyright (C) 2017-2020 Joachim Stolberg
|
2018-08-28 22:24:43 +02:00
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
AGPL v3
|
2018-08-28 22:24:43 +02:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
balancer.lua
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local MAX_DIFF = 10
|
|
|
|
|
|
|
|
function smartline.balancer_condition(number1, number2, ratio1, ratio2, owner)
|
|
|
|
local cnt1 = tubelib.send_request(number1, "counter", nil) / ratio1
|
|
|
|
local cnt2 = tubelib.send_request(number2, "counter", nil) / ratio2
|
|
|
|
if cnt1 > cnt2 + MAX_DIFF then
|
|
|
|
tubelib.send_message(number1, owner, nil, "off", nil)
|
|
|
|
return number1
|
|
|
|
elseif cnt2 > cnt1 + MAX_DIFF then
|
|
|
|
tubelib.send_message(number2, owner, nil, "off", nil)
|
|
|
|
return number2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
smartline.icta_register_condition("ratio", {
|
2018-09-08 18:17:43 +02:00
|
|
|
title = "balancer ratio",
|
2018-08-28 22:24:43 +02:00
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "numbers",
|
|
|
|
name = "number1",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Pusher1 number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "digits",
|
|
|
|
name = "ratio1",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Ratio1 value",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "numbers",
|
|
|
|
name = "number2",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Pusher2 number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "digits",
|
|
|
|
name = "ratio2",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Ratio1 value",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Hint: Pusher1:Pusher2 shall have an\nitem counter ratio of Ratio1:Ratio2.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
-- Return two chunks of executable Lua code for the controller, according:
|
|
|
|
-- return <read condition>, <expected result>
|
|
|
|
code = function(data, environ)
|
|
|
|
local s = 'smartline.balancer_condition("%s", "%s", %s, %s, "%s")'
|
|
|
|
return s:format(data.number1, data.number2, data.ratio1, data.ratio2, environ.owner), '~= nil'
|
|
|
|
end,
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return "ratio("..smartline.fmt_number(data.number1)..","..
|
|
|
|
smartline.fmt_number(data.number2)..","..data.ratio1..":"..data.ratio2..')'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_action("balancer", {
|
2018-09-08 18:17:43 +02:00
|
|
|
title = "balancer action",
|
2018-08-28 22:24:43 +02:00
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Hint: Action part of the balancer rule.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
button = function(data, environ)
|
|
|
|
return 'balancer()'
|
|
|
|
end,
|
|
|
|
code = function(data, environ)
|
|
|
|
local s = 'tubelib.send_message(env.result[#], "%s", nil, "on", nil)'
|
|
|
|
return string.format(s, data.number, environ.owner)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_action("clearcounter", {
|
2018-09-08 18:17:43 +02:00
|
|
|
title = "balancer clear counter",
|
2018-08-28 22:24:43 +02:00
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "numbers",
|
|
|
|
name = "number1",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Pusher1 number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "numbers",
|
|
|
|
name = "number2",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Pusher2 number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Hint: Clear both Pusher counters\ne.g. after controller start.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'clear cnt('..smartline.fmt_number(data.number1)..","..
|
|
|
|
smartline.fmt_number(data.number2)..')'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
code = function(data, environ)
|
|
|
|
local s = [[tubelib.send_message("%s", "%s", nil, "clear_counter", nil)
|
|
|
|
tubelib.send_message("%s", "%s", nil, "clear_counter", nil)]]
|
|
|
|
return string.format(s, data.number1, environ.owner, data.number2, environ.owner)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|