Untitled

 avatar
unknown
plain_text
a year ago
1.9 kB
5
Indexable
-- Define initial configuration
local ipAddress = "192.168.1.100"  -- Default IP address
local port = 23  -- Default port
local telnetSocket
local timer

-- Function to establish a telnet connection
function connectTelnet()
    telnetSocket = TcpSocket.New()
    telnetSocket:Connect(ipAddress, port)
    telnetSocket.Data = function(socket, data) print(data) end  -- Optional: Handle incoming data
    telnetSocket.Connected = function(socket) sendAuthentication() end
end

-- Function to send authentication string in ASCII
function sendAuthentication()
    local authCmd = "login netio netio" .. "\r\n"
    telnetSocket:Write(authCmd)
end

-- Function to send command in ASCII
function sendCommand(cmd)
    if telnetSocket and telnetSocket:IsConnected() then
        local fullCmd = cmd .. "\r\n"
        telnetSocket:Write(fullCmd)
    end
end

-- ScreenUp and ScreenDown button handlers
function ScreenUp()
    sendCommand("port 1 1")
    if timer then timer:Cancel() end
    timer = Timer.New()
    timer.Timeout = function()
        sendCommand("port 1 0")
        Timer.CallAfter(function() sendCommand("port 2 0") end, 2)  -- Send "port 2 0" after 2 seconds
    end
    timer:Start(30)  -- Set for 30 seconds
end

function ScreenDown()
    sendCommand("port 2 1")
    if timer then timer:Cancel() end
    timer = Timer.New()
    timer.Timeout = function()
        sendCommand("port 1 0")
        Timer.CallAfter(function() sendCommand("port 2 0") end, 2)  -- Send "port 2 0" after 2 seconds
    end
    timer:Start(30)  -- Set for 30 seconds
end

-- Function to update IP address and port from UI
function updateConnectionParameters(ip, portNum)
    ipAddress = ip
    port = portNum
    if telnetSocket then
        telnetSocket:Disconnect()
    end
    connectTelnet()
end

-- Initial connection
connectTelnet()
Editor is loading...
Leave a Comment