Need some help in understanding code of a plugin

Hi,

I installed the WPSswitch plugin as a base of a plugin im trying to build.
And i have a question about the following part: (partial question are mention as comments in the code)

    -- get outlet status
    -- hier wat aanpassen voor xmp parse relays.cgi?relay=7 = relay1 relays.cgi?relay=0 = relay8
    local function getStatus()
        local command = "status.xml" --original this is index.htm but i need status.xml
        local data = ""
        data = httpRequest(command)

        if (data ~= nil) then
            for state in string.gmatch(data, "state=(%w.)") do -- 
 --               local stateBinary = Hex2Bin(state)
                debug(string.format("getStatus: state: %s", stateBinary))
-- output of status.xml is like:
-- <response>
-- <relay0>0</relay0>
-- <relay1>1</relay1>
-- <volts>13</volts>
-- </response>
				
	-- part below is letting my eye role over here			
                local o = OUTLETS_MAX
                for status in string.gmatch(stateBinary, "(%d)") do
                    debug(string.format("getStatus: Outlet-%d=%s", o, status))
                
                    local device = findChild(PARENT_DEVICE, string.format("Outlet-%d", o))
                    if (device ~= nil) then
                        local lastStatus = luup.variable_get(SWITCH_SERVICEID, "Status", device)
    -- below i understand                    
                        -- check if new status is different from last status
                        if (lastStatus ~= status) then
                            local time = os.time(os.date('*t'))
                            
                            luup.variable_set(SWITCH_SERVICEID, "Status", status, device)
 -- no need for HADevice   luup.variable_set(HADEVICE_SID, "LastUpdate", time, device)
                        end
                    end
                    o = o - 1
                end
            end
        end
    end

    --
    -- below i put some questions behind the code line
    --
    local function outletControl(outlet, state)
        debug(string.format("outletControl: %d state: %s", outlet, state))
-- url to switch status = "http://ip/relays.cgi?relay=7" for relais 1 and relays.cgi?relay=0 switches relais 8 so some convertion is needed here
        local command = "relay?" .. outlet .. "=" .. state -- think i need to do some text format to get the word relayX here
        httpRequest(command)
        getStatus()
    end

Hopefully somebody can help me with this.

If I understand your question correctly, try this:

local command = string.format("relay%d=%d",outlet,state)

If outlet = 5 and state = 1, command will be relay5=1

This code looks awfully familiar… Wait that is my code.

The part you do not understand:

-- part below is letting my eye role over here			
                local o = OUTLETS_MAX
                for status in string.gmatch(stateBinary, "(%d)") do
                    debug(string.format("getStatus: Outlet-%d=%s", o, status))
                
                    local device = findChild(PARENT_DEVICE, string.format("Outlet-%d", o))
                    if (device ~= nil) then
                        local lastStatus = luup.variable_get(SWITCH_SERVICEID, "Status", device)

The state that the web power switch reports it’s status is in hex. So for example it would report 00. I check 00 against a hex to binary table and the binary form for 00 would be 00000000. This means that all of the 8 outlets are off. A 10 would look like this in binary form: 00010000. Where outlet 4 is on and the rest of the outlets are off. I am looping through each binary number and setting the status based on that binary number. The code “string.gmatch(stateBinary, “(%d)”) do” pretty much means for each digit in 00000000.

Hopefully I did not confuse you.

  • Garrett

p.s. if you are going to publish the plugin, some credit would be nice.