79 lines
3.4 KiB
Text
79 lines
3.4 KiB
Text
local timer_box = { type = "fixed", fixed = {{-0.5, -0.5, 0.4, 0.5, 0.5, 0.5}} }
|
|
|
|
minetest.register_node("smart_light:timer", {
|
|
description = "Smart Light Zeitschaltuhr",
|
|
tiles = {
|
|
"smart_light_panel_sides.png", "smart_light_panel_sides.png",
|
|
"smart_light_panel_sides.png", "smart_light_panel_sides.png",
|
|
"smart_light_panel_sides.png", "smart_light_panel_front_timer.png"
|
|
},
|
|
drawtype = "nodebox", paramtype = "light", paramtype2 = "facedir",
|
|
groups = {cracky = 2, oddly_breakable_by_hand = 1},
|
|
node_box = timer_box,
|
|
|
|
on_construct = function(pos)
|
|
local meta = minetest.get_meta(pos)
|
|
meta:set_string("infotext", "Zeitschaltuhr (nicht konfiguriert)")
|
|
end,
|
|
|
|
on_rightclick = function(pos, node, clicker)
|
|
local meta = minetest.get_meta(pos)
|
|
local area = meta:get_string("area_id")
|
|
local name = clicker:get_player_name()
|
|
|
|
if area == "" then
|
|
minetest.chat_send_player(name, "Bitte zuerst Area mit dem Programmer zuweisen!")
|
|
return
|
|
end
|
|
|
|
local fs = "size[11,9]label[0.5,0.2;Zeitschaltuhr: " .. area:upper() .. "]"
|
|
local y, x, count, channels = 1.2, 0.5, 0, {}
|
|
|
|
if smart_light.registry.areas[area] and smart_light.registry.areas[area].channels then
|
|
for c_id, _ in pairs(smart_light.registry.areas[area].channels) do table.insert(channels, c_id) end
|
|
table.sort(channels)
|
|
for _, c_id in ipairs(channels) do
|
|
local c_data = smart_light.registry.areas[area].channels[c_id]
|
|
local t_status = (c_data.timer and c_data.timer.enabled) and "AN" or "AUS"
|
|
-- WICHTIG: Neues Kürzel tmr_m:
|
|
fs = fs .. "button["..x..","..y..";2.5,0.8;tmr_ch_"..c_id..";" .. c_id .. "\n(TIMER: "..t_status..")]"
|
|
x, count = x + 2.6, count + 1
|
|
if count % 4 == 0 then x, y = 0.5, y + 1 end
|
|
end
|
|
else
|
|
fs = fs .. "label[0.5,2;Keine Kanäle in dieser Area gefunden.]"
|
|
end
|
|
minetest.show_formspec(name, "tmr_m:" .. minetest.pos_to_string(pos), fs)
|
|
end,
|
|
|
|
on_timer = function(pos, elapsed)
|
|
local meta = minetest.get_meta(pos)
|
|
local area_id = meta:get_string("area_id")
|
|
if area_id == "" or not smart_light.registry.areas[area_id] then return true end
|
|
|
|
local time = minetest.get_timeofday() * 24
|
|
|
|
for c_id, c_data in pairs(smart_light.registry.areas[area_id].channels) do
|
|
if c_data.timer and c_data.timer.enabled then
|
|
local t = c_data.timer
|
|
local should_be_on = false
|
|
if t.start_h < t.end_h then
|
|
if time >= t.start_h and time < t.end_h then should_be_on = true end
|
|
else
|
|
if time >= t.start_h or time < t.end_h then should_be_on = true end
|
|
end
|
|
|
|
local current_status = smart_light.get_status_text(area_id, c_id)
|
|
if should_be_on and current_status == "AUS" then
|
|
smart_light.switch_channel(area_id, c_id, "on")
|
|
elseif not should_be_on and current_status == "AN" then
|
|
local now = minetest.get_gametime()
|
|
if not c_data.ms_deadline or now >= c_data.ms_deadline then
|
|
smart_light.switch_channel(area_id, c_id, "off")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return true
|
|
end,
|
|
})
|