Improve my script - array handling...

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
doh
Posts: 82
Joined: Monday 01 December 2014 13:28
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: London, UK
Contact:

Improve my script - array handling...

Post by doh »

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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Improve my script - array handling...

Post by waaren »

doh wrote: Wednesday 29 July 2020 11:57 Any pointers?
Can you please share the complete script. It will make it easier to help you.

If you use the script I posted in the other thread as a base it could look like below

Code: Select all

return 
{
    on =
    {  
        customEvents =
        { 
            'JSONFileReady',
        },
    },

    logging =
    {  
        level = domoticz.LOG_DEBUG,
        marker = 'Read JSON file',
    },
    
    data = 
    {
		macs = 
        { 
            initial = 
            {},
        },
	},


    execute = function(dz, item)

        local function readFile(f)
            local file = io.open (f, "r")
            local data =  file:read("*a")  -- read all
            file:close()
            return data
        end    
        
        --main
        local convertedJSON = dz.utils.fromJSON(readFile(item.data))
	--  dz.utils.dumpTable(convertedJSON)
        for i, record in ipairs(convertedJSON) do
            if dz.data.macs[record.mac] == nil then
                dz.log(record.mac .. ' does not exist as Key in persistent data',dz.LOG_DEBUG)
                dz.data.macs[record.mac] = record
            else
                dz.log(record.mac .. ' does already exist as Key in persistent data',dz.LOG_DEBUG)
            end
        end
        dz.utils.dumpTable(dz.data.macs)
        
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
doh
Posts: 82
Joined: Monday 01 December 2014 13:28
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: London, UK
Contact:

Re: Improve my script - array handling...

Post by doh »

Thanks - I've sorted it. I'll tidy up my script a bit and then share in case it's of use to others...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest