如何在LUA中使用表格来ping通多个设备并检测变量状态的变化

时间:2019-01-14 16:53:42

标签: arrays lua lua-table

我试图按定义的时间间隔ping通本地网络上的多个IP地址,然后仅在设备连接时发送消息。我设法使它可以在单个设备上工作,但是当我向表中添加其他设备时,代码将失败。 提前谢谢了。

一个没有表而只有一个IP地址的早期版本可以很好地工作。但是,只有在表中有单个条目时,才可以添加表和“ for key,value循环”。

local tble = {
    ["device name"] = "192.168.1.204"
}
for key,value in pairs(tble) do

statuscheckIP=os.execute("arping -f -w 3 " .. value)


if statuscheckIP  ~= lastcheckIP then
if statuscheckIP == 0 and lastcheckIP == 256 then
subject ="" ..key.. " , ( IP Address " ..value.. ") Connected"
message = "Connection Alert\nThe device named " ..key.. ", with the IP address " ..value.. " has just connected to the WiFi network"
  --send email notification
  luup.call_action("urn:upnp-org:serviceId:SmtpNotification1", "SendEmail", { Recipient_Name="SomeOne", Recipient_eMail="someone@somewhere.com", Subject= subject, Message=message }, 24)luup.call_action("urn:upnporg:serviceId:SmtpNotification1","ResetCount",{}, 24)
  else
  end
 end
end
lastcheckIP = statuscheckIP

1 个答案:

答案 0 :(得分:1)

您发布的代码有效。由于表中的条目过多而导致失败的原因没有很多。

  

os.execute执行操作系统外壳程序命令。就像C system()函数一样。返回系统相关的状态代码。

运行os.execute将开始arping,并返回退出代码。然后,您正在比较statuscheckIP == 0lastcheckIP == 256。如果以前是多余的。如果为true,则表示您正在发送消息并继续。

在完成所有条目后,您将lastcheckIP设置为statusCheckIP,这很可能是您的错误。它应该在最后一个if和循环内部。但是即使0是唯一正确的返回码也没有意义。如果lastcheckIP设置为任何其他值,则两个if都将不再为真。

最后一行lastcheckIP = statuscheckIP的放置错误,lastcheckIP从未初始化为256,或者您应该重新考虑整个程序。

编辑:

了解所提供程序的用途之后,我创建了一个可能有效的示例。这应该向您展示如何轻松地将Lua中的表用作结构。我无法测试以下代码。

local WAIT_TIME = 10

local STATUS_CODE_CONNECTED = 0
local STATUS_CODE_NOT_CONNECT = 256 -- not sure about this (return code from arping for failure)

local device_table = 
{
    ["device_name1"] =
    {
        ip = "<ip address>",
        status_code = STATUS_CODE_NOT_CONNECT
    },
    ["device_name1"] =
    {
        ip = "<ip address>",
        status_code = STATUS_CODE_NOT_CONNECT
    } 
    -- , ...
}

while true do
    -- check for changed return codes
    for device_name, device in pairs(device_table) do
        local temp_status_code = os.execute("arping -f -w 3 " .. device.ip)

        -- status code changed
        if temp_status_code ~= device.status_code then

            -- device connected
            if temp_status_code == STATUS_CODE_CONNECTED then
                local subject = "" .. device_name .. " , ( IP Address " .. device.ip .. ") Connected"
                local message = "Connection Alert\nThe device named " .. device_name .. ", with the IP address " .. device.ip .. " has just connected to the WiFi network"

                --send email notification

                luup.call_action(
                    "urn:upnp-org:serviceId:SmtpNotification1", 
                    "SendEmail", 
                    { 
                        Recipient_Name = "SomeOne", 
                        Recipient_eMail = "someone@somewhere.com", 
                        Subject = subject, 
                        Message = message 
                    }, 24)
                luup.call_action(
                    "urn:upnporg:serviceId:SmtpNotification1", 
                    "ResetCount",
                    { }, 24)
            end

            -- update last device status_code if changed
            device.status_code = temp_status_code
        end
    end

    os.execute("sleep " .. tonumber(WAIT_TIME)) -- wait some time for next check
end

如果我对您的理解有误,或者您不想一直运行该程序,或者不想在表中拥有所有地址,那么您应该再次询问或在其他地方询问,因为这可能超出主题