Newer
Older
-- SPDX-FileCopyrightText: 2024 Tuxilio <mail@tuxil.io>
--
-- SPDX-License-Identifier: GPL-3.0-or-later
local modname = core.get_current_modname()
local modpath = core.get_modpath(modname)
local mqttBaseTopic = core.settings:get("ledtree_base_topic") or "ledtree"
local client = mqtt.connect(data.url, data.clientId, data.username, data.password)
mqtt.subscribe(client, "ledtree/led/1", function(topic, payload)
core.chat_send_all("Received message from topic "..topic..": "..payload)
end
)
local change_color = function(player, led_id)
local formspec = "size[3,3]" ..
"image_button[0,0;1,1;red.png;red;red;false;false;]" ..
"image_button[2,0;1,1;blue.png;blue;blue;false;false;]" ..
"image_button[0,1;1,1;yellow.png;yellow;yellow;false;false;]" ..
"image_button[1,1;1,1;cyan.png;cyan;cyan;false;false;]" ..
"image_button[2,1;1,1;magenta.png;magenta;magenta;false;false;]" ..
"image_button[0,2;1,1;white.png;white;white;false;false;]" ..
"image_button[1,2;1,1;black.png;black;black;false;false;]" ..
"image_button[2,2;1,1;gray.png;gray;gray;false;false;]"
core.show_formspec(player:get_player_name(), "ledtree:colorpicker", formspec)
end
local function send_mqtt(id, rgb)
mqtt.publish(client, mqttBaseTopic.."/led/"..id, "["..rgb.."]")
for id=1, 10, 1 do
core.register_node("ledtree:led"..id, {
description = "LED"..id,
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
change_color(player, id)
end,
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
core.register_on_player_receive_fields(function(player, formname, fields)
if formname == "ledtree:colorpicker" then
for color, _ in pairs(fields) do
if color then
local rgb = ""
if color == "red" then
rgb = "255,0,0"
elseif color == "green" then
rgb = "0,255,0"
elseif color == "blue" then
rgb = "0,0,255"
elseif color == "yellow" then
rgb = "255,255,0"
elseif color == "cyan" then
rgb = "0,255,255"
elseif color == "magenta" then
rgb = "255,0,255"
elseif color == "white" then
rgb = "255,255,255"
elseif color == "black" then
rgb = "0,0,0"
elseif color == "gray" then
rgb = "128,128,128"
end
if rgb ~= "" then
core.chat_send_player(player:get_player_name(), "RGB: " .. rgb)
send_mqtt(1, rgb)
end
end
end
end
end)