Skip to content
Snippets Groups Projects
init.lua 3.06 KiB
Newer Older
Tuxilio's avatar
Tuxilio committed
-- SPDX-FileCopyrightText: 2024 Tuxilio <mail@tuxil.io>
--
-- SPDX-License-Identifier: GPL-3.0-or-later

Tuxilio's avatar
Tuxilio committed
local modname = core.get_current_modname()
local modpath = core.get_modpath(modname)
Tuxilio's avatar
Tuxilio committed

Tuxilio's avatar
Tuxilio committed
local data = {
Tuxilio's avatar
Tuxilio committed
    url = "mqtt.felix-zauberer.de",
Tuxilio's avatar
Tuxilio committed
    clientId = "ledtree_luanti",
    username = "kalle1",
Tuxilio's avatar
Tuxilio committed
    password = "eZcKn1zBE6KtbeFnZ33lV1EKnr9Bcn8B",
Tuxilio's avatar
Tuxilio committed
}

Tuxilio's avatar
Tuxilio committed
local mqttBaseTopic = core.settings:get("ledtree_base_topic") or "ledtree"

local client = mqtt.connect(data.url, data.clientId, data.username, data.password)

Tuxilio's avatar
Tuxilio committed
mqtt.subscribe(client, "ledtree/led/1", function(topic, payload)
        core.chat_send_all("Received message from topic "..topic..": "..payload)
    end
)

Tuxilio's avatar
Tuxilio committed
local change_color = function(player, led_id)
    local formspec = "size[3,3]" ..
                     "image_button[0,0;1,1;red.png;red;red;false;false;]" ..
Tuxilio's avatar
Tuxilio committed
                     "image_button[1,0;1,1;green.png;green;green;false;false;]" ..
Tuxilio's avatar
Tuxilio committed
                     "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.."]")
Tuxilio's avatar
Tuxilio committed
end

Tuxilio's avatar
Tuxilio committed
for id=1, 10, 1 do
    core.register_node("ledtree:led"..id, {
        description = "LED"..id,
Tuxilio's avatar
Tuxilio committed
        --tiles = "",
Tuxilio's avatar
Tuxilio committed
        on_rightclick = function(pos, node, player, itemstack, pointed_thing)
            change_color(player, id)
        end,
Tuxilio's avatar
Tuxilio committed
    })
end
Tuxilio's avatar
Tuxilio committed

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)