arrays - 如何使用 LUA 中的表来 ping 多个设备并检测变量状态的变化
问题描述
我正在尝试以定义的时间间隔 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
解决方案
您发布的代码是有效的。由于表中的条目更多,导致失败的原因并不多。
os.execute
执行操作系统 shell 命令。这就像 C 的 system() 函数。返回系统相关状态码。
运行os.execute
将启动一个 arping 并返回一个退出代码。然后,您正在比较statuscheckIP == 0
a lastcheckIP == 256
。之前的 if 是多余的。如果为真,您将发送消息并继续。
在完成所有条目后,您将 lastcheckIP 设置为 statusCheckIP,这可能是您的错误。它应该在最后一个 if 之前并在您的循环内。0
但即便如此,如果是唯一正确的返回码也没有任何意义。如果 lastcheckIP 设置为任何其他值,那么您的两个 if 将永远不会再次变为 true。
要么你的最后一行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
如果我对您的理解有误,并且您不想让该程序一直运行,或者不想将所有地址都放在一个表中,那么您应该再次询问或在其他地方询问,因为那将超出主题。
推荐阅读
- windows - Autohotkey:激活隐藏的应用程序
- javascript - 使用 vue-router 会出现“找不到元素#app”和“TypeError:无法读取未定义的属性‘匹配’”错误
- php - Codeigniter 上的自动内存缓存
- c# - 接收字符串作为内存流并编码后,添加更多字符
- c# - System.InvalidCastException 与 Xamarin.Forms 中的 ListView
- java - 使用 Mockito 但在运行测试用例时出现空指针异常
- msbuild - 如何在 CentO 上安装 msbuild
- reactjs - 在 Redux 中 - 使用 store.dispatch() 和 dispatch() 有什么区别?
- python - django rest_framework 自定义异常错误
- google-maps - Cordova 在全新的 Mojave 安装上失败