I'm creating a script that retrieves a list of devices attached to my router, and checks them against a local persistent dataset - so I can tell when new devices connect to my network, and also so I can use this for local presence detection.
The routine works fine, but I'm not 100% happy with the code. I'm ok with programming, but not an expert on LUA and how it handles tables/arrays.
What I do is to create a local persistent dataset as part of the script:
Code: Select all
data = {
macs = { initial = {} }
},
I then receive a JSON file which lists all the devices attached to my router. I iterate through that data set, and check each mac address in the JSON file against the macs dataset above.
At the moment, I'm doing this by having two loops:
Code: Select all
for i in pairs(json) do
mac = json[i].mac
for j,regmac in ipairs(domoticz.data.macs) do
if mac == regmac then ...
I'm convinced that there must be a simpler way - at least to remove the inner loop. I was hoping I could do something like:
Code: Select all
if domoticz.data.macs[mac] then...
But that doesn't work.
Any pointers?
Thanks